Ad Widget

Collapse

Setting Discovered Hosts Host name from snmp

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Farsider
    Junior Member
    • May 2014
    • 2

    #1

    Setting Discovered Hosts Host name from snmp

    Hi i'm trying to understand if there is anyway of automatically assigning the hostname of a device discovered via snmp oid such as SNMPv2-MIB::sysName.0

    The issue i have is the network range is an RFC1918 block and we do not maintain reverse records from ip back to host and even if we do there may be times where this isnt a 1to1 mapping for example. The unique hostname is giving via the above snmp oid though and this is one of discovery criteria.

    At the moment all the devices are added by ip only which isnt ideal. Assuming that isnt possible is it possible to use actions to modify existing devices. So when the sysName is found discovered it updates the host. This would be useful when a system name is changed as monitoring would be updated dynamically as well if wanted.
  • aib
    Senior Member
    • Jan 2014
    • 1615

    #2
    Did you try to modify Auto-Discovery Rule?

    For example, in the field Checks add one more line:
    Code:
    Check Type: SNMPv2 agent
    Port range: 161
    SNMP community: (your default community)
    SNMP OID:  1.3.6.1.2.1.1.5.0
    SNMP OID 1.3.6.1.2.1.1.5.0 contains string with Device.sysName.

    Then, switch the radio box Device uniqueness criteria to SNMPv2 agent "1.3.6.1.2.1.1.5.0"

    After creating that rule all freshly discovered devices which have SNMP, has to be created in Zabbix DB with Device.sysName as a hostname.
    Sincerely yours,
    Aleksey

    Comment

    • Farsider
      Junior Member
      • May 2014
      • 2

      #3
      Discovery Rule Attached but already set as suggested.

      But when host created they are added as 10.250.x.x not as Device.sysName
      Attached Files

      Comment

      • aib
        Senior Member
        • Jan 2014
        • 1615

        #4
        That's weird..
        Sincerely yours,
        Aleksey

        Comment

        • harmonica
          Senior Member
          • Jan 2009
          • 251

          #5
          Originally posted by Farsider
          Discovery Rule Attached but already set as suggested.

          But when host created they are added as 10.250.x.x not as Device.sysName
          Hi,

          We have the same problem in Zabbix 2.4.1. The hosts are added with IP address.

          Comment

          • harmonica
            Senior Member
            • Jan 2009
            • 251

            #6
            Hi,

            This behavior happens to some else? I need to add 3000 snmp devices but its not working Device uniqueness criteria to SNMPv2 agent "1.3.6.1.2.1.1.5.0".

            Regards

            Comment

            • evertonco
              Member
              • Aug 2014
              • 75

              #7
              According documentation (Network discovery):

              Host creation

              If the Add host operation is chosen, a host will be added. If any other operation that would result in an action on that host (like linking template or adding to a group) is chosen, a host will be added even if the Add host operation is missing.

              When adding hosts, a host name is the result of reverse lookup or IP address if reverse lookup fails. Lookup is performed from the Zabbix server or Zabbix proxy, depending on which is doing the discovery. If lookup fails on the proxy, it is not retried on the server. If the host with such a name already exists, the next host would get _2 appended to the name, then _3 and so on.

              Comment

              • RaulSilva
                Junior Member
                • Jun 2015
                • 2

                #8
                is there some development ... ??

                Comment

                • Mechanix
                  Member
                  • Jan 2017
                  • 92

                  #9
                  I know this is an old thread but is there any fix for this?

                  Its quite cumbersome to discover 200 hosts automatically just to modify the IPs as hostnames afterwards

                  Comment

                  • vchrizz
                    Junior Member
                    • Jul 2017
                    • 18

                    #10
                    i've got the same "issue" (on zabbix 3.2) having all hosts added by ip instead by their hostname and am also looking for a "fix" if possible/applicable.

                    meanwhile as a workaround i created a bash script which writes the hostnames into zabbix-server's hosts file so zabbix can resolve the ip adresses with a running dnsmasq as proxy reading local hostsfile.

                    it might not be perfect but it does the job for me. hope it helps someone.

                    edit: my installation is postgresql based, so i added a call to local postgres db to get ipranges from the discovery rule.

                    NOTE: because my hostnames are in format "node.host" and i need them in format "host.node" for dns, there is some customization added.
                    NOTE: if you dont want that, just remove the customizing parts.

                    Code:
                    #!/bin/bash
                    
                    #
                    # README: this script (created 2017 by chil.at, gpl licensed)
                    # 1.> is intended to run on your zabbix-server.
                    # 2.> requires a local (caching-)dns server which uses hosts file like dnsmasq
                    # 3.> thus requires "nameserver 127.0.0.1" as first line in /etc/resolv.conf
                    # 4.> tries to read 'IP range' from your discovery rule (of your pgsql zabbix db)
                    # 5.> thus requires a discovery rule with an snmp check and oid "sysName.0"
                    # 6.> gets the hostname via snmp from your hosts
                    # 7.> writes the ip and hostname to your hosts file (creates a backup first)
                    # 8.> can be run with cron regularly to update hosts file dynamically:
                    # just copy this script to cron, e.g: cp update_hostsfile.sh /etc/cron.daily/
                    #
                    # INSTALLATION:
                    # copy the contents of 'IP range' from your zabbix discovery rule to a file called
                    # "zabbix_ipranges.txt" (filename defined in settings below) in path of this script
                    # then just run this script and it should put your discovered-hosts in hosts file
                    # https://www.zabbix.com/documentation/3.0/manual/discovery/network_discovery/rule
                    #
                    
                    #
                    # SETTINGS
                    #
                    ipranges="zabbix_ipranges.txt"  # 'IP range' from your zabbix discovery rule
                    snmp_community="public"         # snmp community string set on your devices
                    snmp_oid="sysName.0"            # snmp oid of your devices where hostname is set
                    hostsfile="/etc/hosts"          # your system hosts file (typically /etc/hosts)
                    logfile="hosts.log"             # logfile generated each run of this script
                    tmpfile="/tmp/hosts"
                    #
                    # !!! DO NOT EDIT BELOW HERE !!!
                    #
                    
                    # snmp command to run for each host
                    # (note: snmpget parameter "-t" does not seem work, so command "timeout" is used)
                    #snmp_cmd="snmpget -t1 -O vq -v1 -c $snmp_community"
                    snmp_cmd="timeout 1s snmpget -O vq -v1 -c $snmp_community"
                    
                    # remove any existing previously generated logfile and tmpfile
                    if [ -e $logfile ]; then rm -f $logfile $tmpfile; fi
                    
                    # check if given $ipranges file exists and if not, try to get ipranges from database
                    if [ ! -e $ipranges ] || [ ! -s $ipranges ] || test `find "$ipranges" -mmin +60`; then
                      echo -n "# file $ipranges does not exist, is zero size or just older than 60min, trying to read ip ranges from pgsql db ... " | tee $logfile
                      touch $ipranges
                      # if pgsql db reachable and ip range is fetched, write contents to $ipranges file
                      ruleid=$(sudo su - postgres -c "psql zabbix -R, -Atc \"SELECT druleid FROM dchecks WHERE key_='sysName.0';\"")
                      ranges=$(sudo su - postgres -c "psql zabbix -R, -Atc \"SELECT iprange FROM drules WHERE druleid=${ruleid};\"")
                      echo "$ranges" | dos2unix > $ipranges
                      if [ ! -s $ipranges ]; then
                        echo -e "failed.\n# please copy 'IP range' from zabbix discovery rule into it!" | tee $logfile
                        exit
                      else
                        echo "ok."
                      fi
                    fi
                    
                    # process given $ipranges file and add entries to hosts file
                    echo "# update_hosts.sh additions" | tee -a $tmpfile $logfile
                    for i in $(cat $ipranges | sed "s/,/ /g"); do #if [[ ! $i == *$'\n'* ]]; then
                      if [[ $i == *"-"* ]]; then
                        pfx=$(echo $i | cut -d. -f1-3)
                        range=$(echo $i | cut -d. -f4)
                        range_begin=$(echo $range | cut -d- -f1)
                        range_end=$(echo $range | cut -d- -f2)
                        for (( j=$range_begin; j<=$range_end; j++ )); do
                          hostname=$($snmp_cmd $pfx.$j $snmp_oid)
                          #
                          # begin customizing (set hostname "node-host" to "host.node" for dns in hostsfile)
                          #
                          if [ ! -z $hostname ]; then
                            dnsname_node=$(echo $hostname | cut -d. -f1)
                            dnsname_host=$(echo $hostname | cut -d. -f2)
                            echo -e "$pfx.$j\t$dnsname_host.$dnsname_node" >> $tmpfile
                            echo -e "ip: $pfx.$j \thostname: $hostname\t(hostsfile: $dnsname_host.$dnsname_node)" | tee -a $logfile
                          fi
                          #
                          # end customizing
                          #
                        done
                      elif [[ $i == *"."* ]]; then
                        hostname=$($snmp_cmd $i $snmp_oid)
                        #
                        # begin customizing (set hostname "node.host" to "host.node" for dns in hostsfile)
                        #
                        if [ ! -z $hostname ]; then
                          dnsname_node=$(echo $hostname | cut -d- -f1)
                          dnsname_host=$(echo $hostname | cut -d- -f2)
                          echo -e "$i\t$dnsname_host.$dnsname_node" >> $tmpfile
                          echo -e "ip: $i \thostname: $hostname\t(hostsfile: $dnsname_host.$dnsname_node)" | tee -a $logfile
                        fi
                        #
                        # end customizing
                        #
                      fi
                    done
                    # clear all previous additions from this script in hosts file
                    sed -i.bak "/# update_hosts.sh/,$ d" $hostsfile
                    # add content of tmpfile to end of hostsfile
                    cat $tmpfile >> $hostsfile
                    # completed!
                    Last edited by vchrizz; 22-07-2017, 22:42.

                    Comment

                    Working...