Ad Widget

Collapse

Enhanced SNMP Low Level Discovery

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • CraftyHunter
    Junior Member
    • Dec 2015
    • 2

    #16
    I rewrote the script

    I rewrote the script: added the exclude option, changed the polling mechanism of the device.
    Did for their own needs. May be suitable?

    PHP Code:
    #!/usr/bin/php
    <?PHP
    /* Version 2.3
     * Rewrited by: Anatoly Nuraev 
     * Changed the mechanism of SNMP requests. 
     * Now first retrieves all the necessary SNMP data (index, value, filter, exclude), then from the results exclude unnecessary values.
     *
     * Version 2.2
     * Author: Tim Koopman
     * Added SNMPv3 support by Tauame Pacce:
     *
     * Request timeouts raised to 1s, from 10ms.
     * There is a new -v option to inform the protocol version to be used.
     * Also several SNMPv3 only options were added.
     *
     * Version 2.1:
     *
     * For doing SNMP Low Level Discoveries in ZABBIX
     * Inbuilt only allows filter macros on SNMPINDEX and SNMPVALUE
     * While you can change what SNMPVALUE would represent by changing the OID
     * you sometimes what more values, one for filtering and one for item prototype names.
     *
     * This script allows you to add as many other values as you like
     * I use with the following to allow me to filter for interfaces that Admin Status is Up
     * while still having the interface name on each of the items names.
     *
     * Edit discovery rule "Network Interfaces" on template "Template SNMP Interfaces"
     * Type: External check
     * Key: SNMPDiscovery.php["-h", {HOST.CONN}, "-c", {$SNMP_COMMUNITY}, "--index", "IF-MIB::ifDescr", "--value", "IF-MIB::ifAdminStatus,ifAdminStatus"]
     * Filter:
     *    Macro: {#ifAdminStatus}
     *    Regexp: 1
     *
     * When used without the --value option the result would be identical to the inbuilt SNMP LLD.
     * This means all existing item prototypes will continue to work as is.
     * Can add as many --value options as you need.
     *
     * v2.0 added --filter as I wanted to seperate SNMP storage devices between memory and disks but Zabbix
     * won't allow you to have the same key with just different filters. So doing it this way
     * means the keys will be different and you can leave the Zabbix filter options blank
     *
     * Usage
     * SNMPDiscovery.php -h <host> -c <community> [-v 1|2c|3] --index <OID>
        [-n <CONTEXTNAME> -u <SECURITYNAME> -l noAuthNoPriv|authNoPriv|authPriv [-a MD5|SHA -A <AUTHPASS>] [-x AES|DES -X <PRIVPASS>] ]
        [--value <OID>,<MACRONAME>]... [--filter <OID>,<REGEX>]... [--exclude <OID>,<REGEX>]...

     *   -h        : SNMP Host to query. When used in Zabbix this would normally be {HOST.CONN}
     *   -c        : SNMP Community. When used in Zabbix this would normally be {$SNMP_COMMUNITY}
     *   -v        : SNMP version to be used, possible options are "1", "2c" or "3". Defaults to "1".
     *   --index   : OID to walk. Is used to populate {#SNMPINDEX} and {#SNMPVALUE} macros.
     *   --value   : Comma seperated OID and macro name to also return. Can reference in zabbix using macro {#<MACRONAME>}
     *               You can use --value multiple times if you need more macros returned.
     *   --filter  : Comma seperated OID and regular expression. Any index that does not matches the regex will be excluded from the results.
     *               You can use --filter multiple times if you need.
     *   --exclude : Comma seperated OID and regular expression. Any index that matches the regex will be excluded from the results.
     *               You can use --exclude multiple times if you need.
     *
     *   SNMPv3 exclusive options (enabled with "-v 3"):
     *   -u        : Security Name.
     *   -a        : Authentication Protocol, usually "MD5" or "SHA". Defaults to "SHA".
     *   -x        : Privacy Protocol, usually "AES" or "DES". Defaults to "DES".
     *   -A        : Authentication passphrase (Only enabled with "-a" option).
     *   -X        : Privacy passphrase (Only enabled with "-x" option).
     *   -l        : Security level, possible options are "noAuthNoPriv", "authNoPriv" or "authPriv". Defaults to "authPriv".
     *
     * Returns JSON text which Zabbix LLD uses.
     *
     * Examples
     *   SNMPDiscovery.php -h 127.0.0.1 -c public --index IF-MIB::ifDescr --value IF-MIB::ifAdminStatus,ifAdminStatus
     *   SNMPDiscovery.php -h 127.0.0.1 -c public --index HOST-RESOURCES-MIB::hrStorageDescr --filter HOST-RESOURCES-MIB::hrStorageType,hrStorageFixedDisk$
     *   SNMPDiscovery.php -h 127.0.0.1 -c public --index HOST-RESOURCES-MIB::hrStorageDescr --filter 'HOST-RESOURCES-MIB::hrStorageType,(hrStorageRam|hrStorageVirtualMemory)$'
     *   SNMPDiscovery.php -h 127.0.0.1 -v 3 -u bob -a AES -A mypasswd -l authNoPriv --index IF-MIB::ifDescr --value IF-MIB::ifAdminStatus,ifAdminStatus
    */

    error_reporting(E_ALLE_WARNING);
    $delay 1000000;
    $attempts 3;

    function 
    SNMPData($data)
    {
        if (
    strPos($data"STRING: ")===0)
        {
            
    preg_match_all('!^STRING: \"?(.*?)\"?$!'$data$matches);
            return 
    $matches[1][0];
        } elseif (
    strPos($data"INTEGER: ")===0) {
            
    preg_match_all('!\d+!'$data$matches);
            return (int) 
    $matches[0][0];
        } else {
            return 
    $data;
        }
    }

    function 
    usage()
    {
        print 
    "Usage: SNMPDiscovery.php -h <host> -c <community> --index <OID>
        [-v 3 -u <SECURITYNAME> -l <SECURITYLEVEL> [-a <AUTHPROTOCOL> -A <AUTHPASS>] [-x <PRIVPROTOCOL> -X <PRIVPASS>] ]
        [--value <OID>,<MACRONAME>]... [--filter <OID>,<REGEX>]... [--exclude <OID>,<REGEX>]...\n"
    ;
        exit(
    1);
    }

    function 
    getId(&$arrString) {
        
    $tmp = array();
        foreach (
    $arrString as $key=>$val) {
            
    $newKey substr($keystrripos($key,'.')+1);
            
    $tmp[$newKey] = $val;
        }
        
    $arrString $tmp;
    }

    $options getopt("c:h:v:u:a:x:A:X:l:",array("index:","value:","filter:","exclude:"));

    if ( (
    count($options) == 0) || (!array_key_exists("index"$options)) )
    {
        
    usage();
    }

    //Set default key state

    if (!array_key_exists("a"$options))
    {
        
    $options["a"]="SHA";
    }

    if (!
    array_key_exists("A"$options))
    {
        
    $options["A"]="";
    }


    if (!
    array_key_exists("x"$options))
    {
        
    $options["x"]="DES";
    }

    if (!
    array_key_exists("X"$options))
    {
        
    $options["X"]="";
    }

    if (!
    array_key_exists("l"$options))
    {
        
    $options["l"]="authPriv";
    }

    //Split options

    $uniopts = Array();

    if (
    array_key_exists("index"$options))
    {
        
    $uniopts['index'][] = $options["index"];
    }

    if (
    array_key_exists("value"$options))
    {
        if (
    is_array($options["value"]))
        {
            foreach(
    $options["value"] as $value)
            {
                
    $explode explode(","$value);
                
    $uniopts['value_path'][] = $explode[0];
                
    $uniopts['value_string'][] = $explode[1];
            }
        } 
        else 
        {
            
    $explode explode(","$options["value"]);
            
    $uniopts['value_path'][] = $explode[0];
            
    $uniopts['value_string'][] = $explode[1];
        }
    }

    if (
    array_key_exists("filter"$options))
    {
        if (
    is_array($options["filter"]))
        {
            foreach(
    $options["filter"] as $value)
            {
                
    $explode explode(","$value);
                
    $uniopts['filter_path'][] = $explode[0];
                
    $uniopts['filter_string'][] = $explode[1];
            }
        } else {
            
    $explode explode(","$options["filter"]);
            
    $uniopts['filter_path'][] = $explode[0];
            
    $uniopts['filter_string'][] = $explode[1];
        }
    }

    if (
    array_key_exists("exclude"$options))
    {
        if (
    is_array($options["exclude"]))
        {
            foreach(
    $options["exclude"] as $value)
            {
                
    $explode explode(","$value);
                
    $uniopts['exclude_path'][] = $explode[0];
                
    $uniopts['exclude_string'][] = $explode[1];
            }
        } else {
            
    $explode explode(","$options["exclude"]);
            
    $uniopts['exclude_path'][] = $explode[0];
            
    $uniopts['exclude_string'][] = $explode[1];
        }
    }

    //Getting SNMP data

    $data = Array();

    if (
    array_key_exists("v"$options))
    {
        switch(
    $options["v"])
        {
            case 
    "1":
                
    $res['KEYS'] = snmprealwalk($options["h"], $options["c"] , $options["index"], $delay$attempts);
                break;
            case 
    "2c":
                
    $res['KEYS'] = snmp2_real_walk($options["h"], $options["c"] , $options["index"], $delay$attempts);
                break;
            case 
    "3":
                
    $res['KEYS'] = snmp3_real_walk($options["h"], $options["u"] , $options["l"], $options["a"], $options["A"], $options["x"], $options["X"], $options["index"], $delay$attempts);
                break;
            default:
                
    $res['KEYS'] = snmprealwalk($options["h"], $options["c"] , $options["index"], $delay$attempts);
        }
    }else{
        
    $res['KEYS'] = snmprealwalk($options["h"], $options["c"] , $options["index"], $delay$attempts);
    }

    if (!empty(
    $uniopts['value_path']))
        foreach(
    $uniopts['value_path'] as $key => $value)
        {
            if (
    array_key_exists("v"$options))
            {
                switch(
    $options["v"])
                {
                    case 
    "1":
                        
    $res['VALS'][] = snmprealwalk($options["h"], $options["c"] , $uniopts['value_path'][$key], $delay$attempts);
                        break;
                    case 
    "2c":
                        
    $res['VALS'][] = snmp2_real_walk($options["h"], $options["c"] , $uniopts['value_path'][$key], $delay$attempts);
                        break;
                    case 
    "3":
                        
    $res['VALS'][] = snmp3_real_walk($options["h"], $options["u"] , $options["l"], $options["a"], $options["A"], $options["x"], $options["X"], $uniopts['value_path'][$key], $delay$attempts);
                        break;
                    default:
                        
    $res['VALS'][] = snmprealwalk($options["h"], $options["c"] , $uniopts['value_path'][$key], $delay$attempts);
                }
            }else{
                
    $res['VALS'][] = snmprealwalk($options["h"], $options["c"] , $uniopts['value_path'][$key], $delay$attempts);
            }

        }

    if (!empty(
    $uniopts['filter_path']))
        foreach(
    $uniopts['filter_path'] as $key => $value)
        {
            if (
    array_key_exists("v"$options))
            {
                switch(
    $options["v"])
                {
                    case 
    "1":
                        
    $res['FILS'][] = snmprealwalk($options["h"], $options["c"] , $uniopts['filter_path'][$key], $delay$attempts);
                        break;
                    case 
    "2c":
                        
    $res['FILS'][] = snmp2_real_walk($options["h"], $options["c"] , $uniopts['filter_path'][$key], $delay$attempts);
                        break;
                    case 
    "3":
                        
    $res['FILS'][] = snmp3_real_walk($options["h"], $options["u"] , $options["l"], $options["a"], $options["A"], $options["x"], $options["X"], $uniopts['filter_path'][$key], $delay$attempts);
                        break;
                    default:
                        
    $res['FILS'][] = snmprealwalk($options["h"], $options["c"] , $uniopts['filter_path'][$key], $delay$attempts);
                }
            }else{
                
    $res['FILS'][] = snmprealwalk($options["h"], $options["c"] , $uniopts['filter_path'][$key], $delay$attempts);
            }
        }

    if (!empty(
    $uniopts['exclude_path']))
        foreach(
    $uniopts['exclude_path'] as $key => $value)
        {
            if (
    array_key_exists("v"$options))
            {
                switch(
    $options["v"])
                {
                    case 
    "1":
                        
    $res['EXCLS'][] = snmprealwalk($options["h"], $options["c"] , $uniopts['exclude_path'][$key], $delay$attempts); 
                        break;
                    case 
    "2c":
                        
    $res['EXCLS'][] = snmp2_real_walk($options["h"], $options["c"] , $uniopts['exclude_path'][$key], $delay$attempts); 
                        break;
                    case 
    "3":
                        
    $res['EXCLS'][] = snmp3_real_walk($options["h"], $options["u"] , $options["l"], $options["a"], $options["A"], $options["x"], $options["X"], $uniopts['exclude_path'][$key], $delay$attempts); 
                        break;
                    default:
                        
    $res['EXCLS'][] = snmprealwalk($options["h"], $options["c"] , $uniopts['exclude_path'][$key], $delay$attempts); 
                }   
            }else{
                
    $res['EXCLS'][] = snmprealwalk($options["h"], $options["c"] , $uniopts['exclude_path'][$key], $delay$attempts); 
            }   
        }

    //Clearing index from path

    $arrTmp    $res;

    if (!empty(
    $uniopts['exclude_path']))
        foreach (
    $res['EXCLS'] as $key=>$arrStr) {
        
    getId($arrTmp['EXCLS'][$key]);    
        }

    if (!empty(
    $uniopts['filter_path']))
        foreach (
    $res['FILS'] as $key=>$arrStr) {
        
    getId($arrTmp['FILS'][$key]);    
        }

    if (!empty(
    $uniopts['value_path']))
        foreach (
    $res['VALS'] as $key=>$arrStr) {
        
    getId($arrTmp['VALS'][$key]);    
        }

    getId($arrTmp['KEYS']);    

    $res $arrTmp;

    //SNMP data processing and conclusion

    foreach($res['KEYS'] as $key => $value)
    {
        
    $isFiltered True;
        
        if (!empty(
    $uniopts['filter_path']))
        {
            foreach(
    $uniopts['filter_path'] as $s_key => $s_value)
            {   
                if (
    $uniopts['filter_string'][$s_key] != SNMPData($res['FILS'][$s_key][$key]))
                {
                    
    $isFiltered False;
                }
            } 
        }

        
    $isExcluded False;

        if (
    $isFiltered && !empty($uniopts['exclude_path']))
        {
            foreach(
    $uniopts['exclude_path'] as $s_key => $s_value)
            {
                if (
    $uniopts['exclude_string'][$s_key] == SNMPData($res['EXCLS'][$s_key][$key]))
                {
                    
    $isExcluded True;
                }
            }
        }
        
        if (
    $isFiltered && !$isExcluded)
        {
            
    $dataItem = Array();
            
    $dataItem["{#SNMPINDEX}"] = $key;
            
    $dataItem["{#SNMPVALUE}"] = SNMPData($value);
            
            if (!empty(
    $uniopts['value_path']))
                {
                    foreach(
    $uniopts['value_path'] as $s_key => $s_value)
                    {
                        
    $dataItem["{#".$uniopts['value_string'][$s_key]."}"] = SNMPData($res['VALS'][$s_key][$key]);
                    }
                }
            
    array_push($data$dataItem);
        }
    }

    $jsonData = Array();
    $jsonData["data"] = $data;

    echo 
    json_encode($jsonData);
    echo 
    "\n";   
    ?>

    Comment

    • gjaekel
      Junior Member
      • Nov 2014
      • 14

      #17
      And I did some fixes

      Originally posted by CraftyHunter
      I rewrote the script: added the exclude option, changed the polling mechanism of the device.
      Did for their own needs. May be suitable?
      Dear CraftyHunter,
      thank you very much for your work. With this algorithm, it will take e.g. less than 0.5s instead of 3s to query a big CISCO 6500 and filter out some groups of interfaces.

      But first I fixed a serious bug, because you had used string compares instead of regex matches for the filter and exclude features. I change the default for '-v' to be '2c' to maintain backward compatibility and brushed-up the code up a little bit. And I made '-c' optional to give the chance to use the value defined for snmp in it's own configuration.

      Now, the algorithm may be further enhanced at the filter/exclude fetch loop to use (copy from $res['VALS'] to $res['FILS'/'EXCLS']) an already retrieved value in case of the OID for it is also an OID for a value.

      PHP Code:
      #!/usr/bin/php
      <?PHP
      /*
       * Version 2.3.1        20160112
       * Author: Guido Jäkel
       * 
       * Make -c optional to use external configured snmp default 
       * Set a default for the snmp protocol version (-v)  to simplify the code
       *   and make '2c' the default to maintain backward compatibility
       * Fix the filter and exclude feature to realy use regexp instead of string compare.
       * 
       *
       * Version 2.3
       *
       * Rewrited by: Anatoly Nuraev 
       * Changed the mechanism of SNMP requests. 
       * Now first retrieves all the necessary SNMP data (index, value, filter, exclude), then from the results exclude unnecessary values.
       *
       *
       * Version 2.2
       * Author: Tim Koopman
       * Added SNMPv3 support by Tauame Pacce:
       *
       * Request timeouts raised to 1s, from 10ms.
       * There is a new -v option to inform the protocol version to be used.
       * Also several SNMPv3 only options were added.
       *
       *
       * Version 2.1:
       *
       * For doing SNMP Low Level Discoveries in ZABBIX
       * Inbuilt only allows filter macros on SNMPINDEX and SNMPVALUE
       * While you can change what SNMPVALUE would represent by changing the OID
       * you sometimes what more values, one for filtering and one for item prototype names.
       *
       * This script allows you to add as many other values as you like
       * I use with the following to allow me to filter for interfaces that Admin Status is Up
       * while still having the interface name on each of the items names.
       *
       * Edit discovery rule "Network Interfaces" on template "Template SNMP Interfaces"
       * Type: External check
       * Key: SNMPDiscovery.php["-h", {HOST.CONN}, "-c", {$SNMP_COMMUNITY}, "--index", "IF-MIB::ifDescr", "--value", "IF-MIB::ifAdminStatus,ifAdminStatus"]
       * Filter:
       *    Macro: {#ifAdminStatus}
       *    Regexp: 1
       *
       * When used without the --value option the result would be identical to the inbuilt SNMP LLD.
       * This means all existing item prototypes will continue to work as is.
       * Can add as many --value options as you need.
       *
       * v2.0 added --filter as I wanted to seperate SNMP storage devices between memory and disks but Zabbix
       * won't allow you to have the same key with just different filters. So doing it this way
       * means the keys will be different and you can leave the Zabbix filter options blank
       *
       * Usage
       * SNMPDiscovery.php -h <host> [-c <community>] [-v 1|2c|3] --index <OID>
          [-n <CONTEXTNAME> -u <SECURITYNAME> -l noAuthNoPriv|authNoPriv|authPriv [-a MD5|SHA -A <AUTHPASS>] [-x AES|DES -X <PRIVPASS>] ]
          [--value <OID>,<MACRONAME>]... [--filter <OID>,<REGEX>]... [--exclude <OID>,<REGEX>]...

       *   -h        : SNMP Host to query. When used in Zabbix this would normally be {HOST.CONN}
       *   -c        : SNMP Community. When used in Zabbix this would normally be {$SNMP_COMMUNITY}. Defaults to value configured for smnp
       *   -v        : SNMP version to be used, possible options are "1", "2c" or "3". Defaults to "2c".
       *   --index   : OID to walk. Is used to populate {#SNMPINDEX} and {#SNMPVALUE} macros.
       *   --value   : Comma seperated OID and macro name to also return. Can reference in zabbix using macro {#<MACRONAME>}
       *               You can use --value multiple times if you need more macros returned.
       *   --filter  : Comma seperated OID and regular expression. Any index that does not matches the regex will be excluded from the results.
       *               You can use --filter multiple times if you need.
       *   --exclude : Comma seperated OID and regular expression. Any index that matches the regex will be excluded from the results.
       *               You can use --exclude multiple times if you need.
       *
       *   SNMPv3 exclusive options (enabled with "-v 3"):
       *   -u        : Security Name.
       *   -a        : Authentication Protocol, usually "MD5" or "SHA". Defaults to "SHA".
       *   -x        : Privacy Protocol, usually "AES" or "DES". Defaults to "DES".
       *   -A        : Authentication passphrase (Only enabled with "-a" option).
       *   -X        : Privacy passphrase (Only enabled with "-x" option).
       *   -l        : Security level, possible options are "noAuthNoPriv", "authNoPriv" or "authPriv". Defaults to "authPriv".
       *
       * Returns JSON text which Zabbix LLD uses.
       *
       * Examples
       *   SNMPDiscovery.php -h 127.0.0.1 -c public --index IF-MIB::ifDescr --value IF-MIB::ifAdminStatus,ifAdminStatus
       *   SNMPDiscovery.php -h 127.0.0.1 -c public --index HOST-RESOURCES-MIB::hrStorageDescr --filter HOST-RESOURCES-MIB::hrStorageType,hrStorageFixedDisk$
       *   SNMPDiscovery.php -h 127.0.0.1 -c public --index HOST-RESOURCES-MIB::hrStorageDescr --filter 'HOST-RESOURCES-MIB::hrStorageType,(hrStorageRam|hrStorageVirtualMemory)$'
       *   SNMPDiscovery.php -h 127.0.0.1 -v 3 -u bob -a AES -A mypasswd -l authNoPriv --index IF-MIB::ifDescr --value IF-MIB::ifAdminStatus,ifAdminStatus
      */

      error_reporting(E_ALLE_WARNING);
      $delay 1000000;
      $attempts 3;

      function 
      SNMPData($data)
      {
          if (
      strPos($data"STRING: ")===0)
          {
              
      preg_match_all('!^STRING: \"?(.*?)\"?$!'$data$matches);
              return 
      $matches[1][0];
          } elseif (
      strPos($data"INTEGER: ")===0) {
              
      preg_match_all('!\d+!'$data$matches);
              return (int) 
      $matches[0][0];
          } else {
              return 
      $data;
          }
      }

      function 
      usage()
      {
          print 
      "Usage: SNMPDiscovery.php -h <host> -c <community> --index <OID>
          [-v 3 -u <SECURITYNAME> -l <SECURITYLEVEL> [-a <AUTHPROTOCOL> -A <AUTHPASS>] [-x <PRIVPROTOCOL> -X <PRIVPASS>] ]
          [--value <OID>,<MACRONAME>]... [--filter <OID>,<REGEX>]... [--exclude <OID>,<REGEX>]...\n"
      ;
          exit(
      1);
      }

      function 
      getId(&$arrString) {
          
      $tmp = array();
          foreach (
      $arrString as $key=>$val) {
              
      $newKey substr($keystrripos($key,'.')+1);
              
      $tmp[$newKey] = $val;
          }
          
      $arrString $tmp;
      }

      $options getopt("c:h:v:u:a:x:A:X:l:",array("index:","value:","filter:","exclude:"));

      if ( (
      count($options) == 0) || (!array_key_exists("index"$options)) )
      {
          
      usage();
      }

      //Set default key state

      if (!array_key_exists("a"$options))
      {
          
      $options["a"]="SHA";
      }

      if (!
      array_key_exists("A"$options))
      {
          
      $options["A"]="";
      }


      if (!
      array_key_exists("x"$options))
      {
          
      $options["x"]="DES";
      }

      if (!
      array_key_exists("X"$options))
      {
          
      $options["X"]="";
      }

      if (!
      array_key_exists("l"$options))
      {
          
      $options["l"]="authPriv";
      }

      if (!
      array_key_exists("c"$options))
      {
          
      $options["c"]=""# use value configured externaly for snmp lib
      }

      if (!
      array_key_exists("v"$options))
      {
          
      $options["v"]="2c";
      }

      //Split options

      $uniopts = Array();

      if (
      array_key_exists("index"$options))
      {
          
      $uniopts['index'][] = $options["index"];
      }

      if (
      array_key_exists("value"$options))
      {
          if (
      is_array($options["value"]))
          {
              foreach(
      $options["value"] as $value)
              {
                  
      $explode explode(","$value);
                  
      $uniopts['value_path'][] = $explode[0];
                  
      $uniopts['value_string'][] = $explode[1];
              }
          } 
          else 
          {
              
      $explode explode(","$options["value"]);
              
      $uniopts['value_path'][] = $explode[0];
              
      $uniopts['value_string'][] = $explode[1];
          }
      }

      if (
      array_key_exists("filter"$options))
      {
          if (
      is_array($options["filter"]))
          {
              foreach(
      $options["filter"] as $value)
              {
                  
      $explode explode(","$value);
                  
      $uniopts['filter_path'][] = $explode[0];
                  
      $uniopts['filter_string'][] = $explode[1];
              }
          } else {
              
      $explode explode(","$options["filter"]);
              
      $uniopts['filter_path'][] = $explode[0];
              
      $uniopts['filter_string'][] = $explode[1];
          }
      }

      if (
      array_key_exists("exclude"$options))
      {
          if (
      is_array($options["exclude"]))
          {
              foreach(
      $options["exclude"] as $value)
              {
                  
      $explode explode(","$value);
                  
      $uniopts['exclude_path'][] = $explode[0];
                  
      $uniopts['exclude_string'][] = $explode[1];
              }
          } else {
              
      $explode explode(","$options["exclude"]);
              
      $uniopts['exclude_path'][] = $explode[0];
              
      $uniopts['exclude_string'][] = $explode[1];
          }
      }


      //Getting SNMP data

      $data = Array();

      switch(
      $options["v"])
      {
          case 
      "1":
              
      $res['KEYS'] = snmprealwalk($options["h"], $options["c"] , $options["index"], $delay$attempts);
              break;
          case 
      "2c":
              
      $res['KEYS'] = snmp2_real_walk($options["h"], $options["c"] , $options["index"], $delay$attempts);
              break;
          case 
      "3":
              
      $res['KEYS'] = snmp3_real_walk($options["h"], $options["u"] , $options["l"], $options["a"], $options["A"], $options["x"], $options["X"], $options["index"], $delay$attempts);
              break;
      }

      if (!empty(
      $uniopts['value_path']))
          foreach(
      $uniopts['value_path'] as $key => $value)
          {
              switch(
      $options["v"])
              {
                  case 
      "1":
                      
      $res['VALS'][] = snmprealwalk($options["h"], $options["c"] , $uniopts['value_path'][$key], $delay$attempts);
                      break;
                  case 
      "2c":
                      
      $res['VALS'][] = snmp2_real_walk($options["h"], $options["c"] , $uniopts['value_path'][$key], $delay$attempts);
                      break;
                  case 
      "3":
                      
      $res['VALS'][] = snmp3_real_walk($options["h"], $options["u"] , $options["l"], $options["a"], $options["A"], $options["x"], $options["X"], $uniopts['value_path'][$key], $delay$attempts);
                      break;
              }
          }

      if (!empty(
      $uniopts['filter_path']))
          foreach(
      $uniopts['filter_path'] as $key => $value)
          {
              switch(
      $options["v"])
              {
                  case 
      "1":
                      
      $res['FILS'][] = snmprealwalk($options["h"], $options["c"] , $uniopts['filter_path'][$key], $delay$attempts);
                      break;
                  case 
      "2c":
                      
      $res['FILS'][] = snmp2_real_walk($options["h"], $options["c"] , $uniopts['filter_path'][$key], $delay$attempts);
                      break;
                  case 
      "3":
                      
      $res['FILS'][] = snmp3_real_walk($options["h"], $options["u"] , $options["l"], $options["a"], $options["A"], $options["x"], $options["X"], $uniopts['filter_path'][$key], $delay$attempts);
                      break;
              }
          }

      if (!empty(
      $uniopts['exclude_path']))
          foreach(
      $uniopts['exclude_path'] as $key => $value)
          {
              switch(
      $options["v"])
              {
                  case 
      "1":
                      
      $res['EXCLS'][] = snmprealwalk($options["h"], $options["c"] , $uniopts['exclude_path'][$key], $delay$attempts); 
                      break;
                  case 
      "2c":
                      
      $res['EXCLS'][] = snmp2_real_walk($options["h"], $options["c"] , $uniopts['exclude_path'][$key], $delay$attempts); 
                      break;
                  case 
      "3":
                      
      $res['EXCLS'][] = snmp3_real_walk($options["h"], $options["u"] , $options["l"], $options["a"], $options["A"], $options["x"], $options["X"], $uniopts['exclude_path'][$key], $delay$attempts); 
                      break;
              }   
          }


      //Clearing index from path

      $arrTmp    $res;

      if (!empty(
      $uniopts['exclude_path']))
          foreach (
      $res['EXCLS'] as $key=>$arrStr) {
          
      getId($arrTmp['EXCLS'][$key]);    
          }

      if (!empty(
      $uniopts['filter_path']))
          foreach (
      $res['FILS'] as $key=>$arrStr) {
          
      getId($arrTmp['FILS'][$key]);    
          }

      if (!empty(
      $uniopts['value_path']))
          foreach (
      $res['VALS'] as $key=>$arrStr) {
          
      getId($arrTmp['VALS'][$key]);    
          }

      getId($arrTmp['KEYS']);    

      $res $arrTmp;


      //SNMP data processing and conclusion

      foreach($res['KEYS'] as $key => $value)
      {
          
      $isFiltered True;
          if (!empty(
      $uniopts['filter_path']))
          {
              foreach(
      $uniopts['filter_path'] as $s_key => $s_value)
              {   
                  if  (! 
      preg_match("/".$uniopts['filter_string'][$s_key]."/",SNMPData($res['FILS'][$s_key][$key])))
                  {
                      
      $isFiltered False;
                  }
              } 
          }

          
      $isExcluded False;
          if (
      $isFiltered && !empty($uniopts['exclude_path']))
          {
              foreach(
      $uniopts['exclude_path'] as $s_key => $s_value)
              {
                  if (
      preg_match("/".$uniopts['exclude_string'][$s_key]."/",SNMPData($res['EXCLS'][$s_key][$key])))
                  {
                      
      $isExcluded True;
                  }
              }
          }
          
          if (
      $isFiltered && !$isExcluded)
          {
              
      $dataItem = Array();
              
      $dataItem["{#SNMPINDEX}"] = $key;
              
      $dataItem["{#SNMPVALUE}"] = SNMPData($value);
              
              if (!empty(
      $uniopts['value_path']))
                  {
                      foreach(
      $uniopts['value_path'] as $s_key => $s_value)
                      {
                          
      $dataItem["{#".$uniopts['value_string'][$s_key]."}"] = SNMPData($res['VALS'][$s_key][$key]);
                      }
                  }
              
      array_push($data$dataItem);
          }
      }

      $jsonData = Array();
      $jsonData["data"] = $data;

      echo 
      json_encode($jsonData);
      echo 
      "\n";   
      ?>
      Last edited by gjaekel; 12-01-2016, 12:16. Reason: typo

      Comment

      • CraftyHunter
        Junior Member
        • Dec 2015
        • 2

        #18
        Dear, gjaekel,
        thank you very much. I missed the comparison by the regular expression because they are not used yet. Although the original script supported them, when I rewrite the script I have missed this opportunity.
        The purpose of rewriting the script by selecting add exception. Along the way, it was decided to completely rewrite the script to reduce the load on network devices and CPU zabbix server. In some situations, the increase amounted to 800%.
        I queried more than 200 devices. The load on zabbix significantly decreased. Plans to rewrite this script in Python.

        Comment

        • Cenzoooo
          Member
          • Jul 2015
          • 37

          #19
          Originally posted by gjaekel
          Dear CraftyHunter,
          thank you very much for your work. With this algorithm, it will take e.g. less than 0.5s instead of 3s to query a big CISCO 6500 and filter out some groups of interfaces.

          But first I fixed a serious bug, because you had used string compares instead of regex matches for the filter and exclude features. I change the default for '-v' to be '2c' to maintain backward compatibility and brushed-up the code up a little bit. And I made '-c' optional to give the chance to use the value defined for snmp in it's own configuration.

          Now, the algorithm may be further enhanced at the filter/exclude fetch loop to use (copy from $res['VALS'] to $res['FILS'/'EXCLS']) an already retrieved value in case of the OID for it is also an OID for a value.

          PHP Code:
          #!/usr/bin/php
          <?PHP
          /*
           * Version 2.3.1        20160112
           * Author: Guido Jäkel
           * 
           * Make -c optional to use external configured snmp default 
           * Set a default for the snmp protocol version (-v)  to simplify the code
           *   and make '2c' the default to maintain backward compatibility
           * Fix the filter and exclude feature to realy use regexp instead of string compare.
           * 
           *
           * Version 2.3
           *
           * Rewrited by: Anatoly Nuraev 
           * Changed the mechanism of SNMP requests. 
           * Now first retrieves all the necessary SNMP data (index, value, filter, exclude), then from the results exclude unnecessary values.
           *
           *
           * Version 2.2
           * Author: Tim Koopman
           * Added SNMPv3 support by Tauame Pacce:
           *
           * Request timeouts raised to 1s, from 10ms.
           * There is a new -v option to inform the protocol version to be used.
           * Also several SNMPv3 only options were added.
           *
           *
           * Version 2.1:
           *
           * For doing SNMP Low Level Discoveries in ZABBIX
           * Inbuilt only allows filter macros on SNMPINDEX and SNMPVALUE
           * While you can change what SNMPVALUE would represent by changing the OID
           * you sometimes what more values, one for filtering and one for item prototype names.
           *
           * This script allows you to add as many other values as you like
           * I use with the following to allow me to filter for interfaces that Admin Status is Up
           * while still having the interface name on each of the items names.
           *
           * Edit discovery rule "Network Interfaces" on template "Template SNMP Interfaces"
           * Type: External check
           * Key: SNMPDiscovery.php["-h", {HOST.CONN}, "-c", {$SNMP_COMMUNITY}, "--index", "IF-MIB::ifDescr", "--value", "IF-MIB::ifAdminStatus,ifAdminStatus"]
           * Filter:
           *    Macro: {#ifAdminStatus}
           *    Regexp: 1
           *
           * When used without the --value option the result would be identical to the inbuilt SNMP LLD.
           * This means all existing item prototypes will continue to work as is.
           * Can add as many --value options as you need.
           *
           * v2.0 added --filter as I wanted to seperate SNMP storage devices between memory and disks but Zabbix
           * won't allow you to have the same key with just different filters. So doing it this way
           * means the keys will be different and you can leave the Zabbix filter options blank
           *
           * Usage
           * SNMPDiscovery.php -h <host> [-c <community>] [-v 1|2c|3] --index <OID>
              [-n <CONTEXTNAME> -u <SECURITYNAME> -l noAuthNoPriv|authNoPriv|authPriv [-a MD5|SHA -A <AUTHPASS>] [-x AES|DES -X <PRIVPASS>] ]
              [--value <OID>,<MACRONAME>]... [--filter <OID>,<REGEX>]... [--exclude <OID>,<REGEX>]...

           *   -h        : SNMP Host to query. When used in Zabbix this would normally be {HOST.CONN}
           *   -c        : SNMP Community. When used in Zabbix this would normally be {$SNMP_COMMUNITY}. Defaults to value configured for smnp
           *   -v        : SNMP version to be used, possible options are "1", "2c" or "3". Defaults to "2c".
           *   --index   : OID to walk. Is used to populate {#SNMPINDEX} and {#SNMPVALUE} macros.
           *   --value   : Comma seperated OID and macro name to also return. Can reference in zabbix using macro {#<MACRONAME>}
           *               You can use --value multiple times if you need more macros returned.
           *   --filter  : Comma seperated OID and regular expression. Any index that does not matches the regex will be excluded from the results.
           *               You can use --filter multiple times if you need.
           *   --exclude : Comma seperated OID and regular expression. Any index that matches the regex will be excluded from the results.
           *               You can use --exclude multiple times if you need.
           *
           *   SNMPv3 exclusive options (enabled with "-v 3"):
           *   -u        : Security Name.
           *   -a        : Authentication Protocol, usually "MD5" or "SHA". Defaults to "SHA".
           *   -x        : Privacy Protocol, usually "AES" or "DES". Defaults to "DES".
           *   -A        : Authentication passphrase (Only enabled with "-a" option).
           *   -X        : Privacy passphrase (Only enabled with "-x" option).
           *   -l        : Security level, possible options are "noAuthNoPriv", "authNoPriv" or "authPriv". Defaults to "authPriv".
           *
           * Returns JSON text which Zabbix LLD uses.
           *
           * Examples
           *   SNMPDiscovery.php -h 127.0.0.1 -c public --index IF-MIB::ifDescr --value IF-MIB::ifAdminStatus,ifAdminStatus
           *   SNMPDiscovery.php -h 127.0.0.1 -c public --index HOST-RESOURCES-MIB::hrStorageDescr --filter HOST-RESOURCES-MIB::hrStorageType,hrStorageFixedDisk$
           *   SNMPDiscovery.php -h 127.0.0.1 -c public --index HOST-RESOURCES-MIB::hrStorageDescr --filter 'HOST-RESOURCES-MIB::hrStorageType,(hrStorageRam|hrStorageVirtualMemory)$'
           *   SNMPDiscovery.php -h 127.0.0.1 -v 3 -u bob -a AES -A mypasswd -l authNoPriv --index IF-MIB::ifDescr --value IF-MIB::ifAdminStatus,ifAdminStatus
          */

          error_reporting(E_ALLE_WARNING);
          $delay 1000000;
          $attempts 3;

          function 
          SNMPData($data)
          {
              if (
          strPos($data"STRING: ")===0)
              {
                  
          preg_match_all('!^STRING: \"?(.*?)\"?$!'$data$matches);
                  return 
          $matches[1][0];
              } elseif (
          strPos($data"INTEGER: ")===0) {
                  
          preg_match_all('!\d+!'$data$matches);
                  return (int) 
          $matches[0][0];
              } else {
                  return 
          $data;
              }
          }

          function 
          usage()
          {
              print 
          "Usage: SNMPDiscovery.php -h <host> -c <community> --index <OID>
              [-v 3 -u <SECURITYNAME> -l <SECURITYLEVEL> [-a <AUTHPROTOCOL> -A <AUTHPASS>] [-x <PRIVPROTOCOL> -X <PRIVPASS>] ]
              [--value <OID>,<MACRONAME>]... [--filter <OID>,<REGEX>]... [--exclude <OID>,<REGEX>]...\n"
          ;
              exit(
          1);
          }

          function 
          getId(&$arrString) {
              
          $tmp = array();
              foreach (
          $arrString as $key=>$val) {
                  
          $newKey substr($keystrripos($key,'.')+1);
                  
          $tmp[$newKey] = $val;
              }
              
          $arrString $tmp;
          }

          $options getopt("c:h:v:u:a:x:A:X:l:",array("index:","value:","filter:","exclude:"));

          if ( (
          count($options) == 0) || (!array_key_exists("index"$options)) )
          {
              
          usage();
          }

          //Set default key state

          if (!array_key_exists("a"$options))
          {
              
          $options["a"]="SHA";
          }

          if (!
          array_key_exists("A"$options))
          {
              
          $options["A"]="";
          }


          if (!
          array_key_exists("x"$options))
          {
              
          $options["x"]="DES";
          }

          if (!
          array_key_exists("X"$options))
          {
              
          $options["X"]="";
          }

          if (!
          array_key_exists("l"$options))
          {
              
          $options["l"]="authPriv";
          }

          if (!
          array_key_exists("c"$options))
          {
              
          $options["c"]=""# use value configured externaly for snmp lib
          }

          if (!
          array_key_exists("v"$options))
          {
              
          $options["v"]="2c";
          }

          //Split options

          $uniopts = Array();

          if (
          array_key_exists("index"$options))
          {
              
          $uniopts['index'][] = $options["index"];
          }

          if (
          array_key_exists("value"$options))
          {
              if (
          is_array($options["value"]))
              {
                  foreach(
          $options["value"] as $value)
                  {
                      
          $explode explode(","$value);
                      
          $uniopts['value_path'][] = $explode[0];
                      
          $uniopts['value_string'][] = $explode[1];
                  }
              } 
              else 
              {
                  
          $explode explode(","$options["value"]);
                  
          $uniopts['value_path'][] = $explode[0];
                  
          $uniopts['value_string'][] = $explode[1];
              }
          }

          if (
          array_key_exists("filter"$options))
          {
              if (
          is_array($options["filter"]))
              {
                  foreach(
          $options["filter"] as $value)
                  {
                      
          $explode explode(","$value);
                      
          $uniopts['filter_path'][] = $explode[0];
                      
          $uniopts['filter_string'][] = $explode[1];
                  }
              } else {
                  
          $explode explode(","$options["filter"]);
                  
          $uniopts['filter_path'][] = $explode[0];
                  
          $uniopts['filter_string'][] = $explode[1];
              }
          }

          if (
          array_key_exists("exclude"$options))
          {
              if (
          is_array($options["exclude"]))
              {
                  foreach(
          $options["exclude"] as $value)
                  {
                      
          $explode explode(","$value);
                      
          $uniopts['exclude_path'][] = $explode[0];
                      
          $uniopts['exclude_string'][] = $explode[1];
                  }
              } else {
                  
          $explode explode(","$options["exclude"]);
                  
          $uniopts['exclude_path'][] = $explode[0];
                  
          $uniopts['exclude_string'][] = $explode[1];
              }
          }


          //Getting SNMP data

          $data = Array();

          switch(
          $options["v"])
          {
              case 
          "1":
                  
          $res['KEYS'] = snmprealwalk($options["h"], $options["c"] , $options["index"], $delay$attempts);
                  break;
              case 
          "2c":
                  
          $res['KEYS'] = snmp2_real_walk($options["h"], $options["c"] , $options["index"], $delay$attempts);
                  break;
              case 
          "3":
                  
          $res['KEYS'] = snmp3_real_walk($options["h"], $options["u"] , $options["l"], $options["a"], $options["A"], $options["x"], $options["X"], $options["index"], $delay$attempts);
                  break;
          }

          if (!empty(
          $uniopts['value_path']))
              foreach(
          $uniopts['value_path'] as $key => $value)
              {
                  switch(
          $options["v"])
                  {
                      case 
          "1":
                          
          $res['VALS'][] = snmprealwalk($options["h"], $options["c"] , $uniopts['value_path'][$key], $delay$attempts);
                          break;
                      case 
          "2c":
                          
          $res['VALS'][] = snmp2_real_walk($options["h"], $options["c"] , $uniopts['value_path'][$key], $delay$attempts);
                          break;
                      case 
          "3":
                          
          $res['VALS'][] = snmp3_real_walk($options["h"], $options["u"] , $options["l"], $options["a"], $options["A"], $options["x"], $options["X"], $uniopts['value_path'][$key], $delay$attempts);
                          break;
                  }
              }

          if (!empty(
          $uniopts['filter_path']))
              foreach(
          $uniopts['filter_path'] as $key => $value)
              {
                  switch(
          $options["v"])
                  {
                      case 
          "1":
                          
          $res['FILS'][] = snmprealwalk($options["h"], $options["c"] , $uniopts['filter_path'][$key], $delay$attempts);
                          break;
                      case 
          "2c":
                          
          $res['FILS'][] = snmp2_real_walk($options["h"], $options["c"] , $uniopts['filter_path'][$key], $delay$attempts);
                          break;
                      case 
          "3":
                          
          $res['FILS'][] = snmp3_real_walk($options["h"], $options["u"] , $options["l"], $options["a"], $options["A"], $options["x"], $options["X"], $uniopts['filter_path'][$key], $delay$attempts);
                          break;
                  }
              }

          if (!empty(
          $uniopts['exclude_path']))
              foreach(
          $uniopts['exclude_path'] as $key => $value)
              {
                  switch(
          $options["v"])
                  {
                      case 
          "1":
                          
          $res['EXCLS'][] = snmprealwalk($options["h"], $options["c"] , $uniopts['exclude_path'][$key], $delay$attempts); 
                          break;
                      case 
          "2c":
                          
          $res['EXCLS'][] = snmp2_real_walk($options["h"], $options["c"] , $uniopts['exclude_path'][$key], $delay$attempts); 
                          break;
                      case 
          "3":
                          
          $res['EXCLS'][] = snmp3_real_walk($options["h"], $options["u"] , $options["l"], $options["a"], $options["A"], $options["x"], $options["X"], $uniopts['exclude_path'][$key], $delay$attempts); 
                          break;
                  }   
              }


          //Clearing index from path

          $arrTmp    $res;

          if (!empty(
          $uniopts['exclude_path']))
              foreach (
          $res['EXCLS'] as $key=>$arrStr) {
              
          getId($arrTmp['EXCLS'][$key]);    
              }

          if (!empty(
          $uniopts['filter_path']))
              foreach (
          $res['FILS'] as $key=>$arrStr) {
              
          getId($arrTmp['FILS'][$key]);    
              }

          if (!empty(
          $uniopts['value_path']))
              foreach (
          $res['VALS'] as $key=>$arrStr) {
              
          getId($arrTmp['VALS'][$key]);    
              }

          getId($arrTmp['KEYS']);    

          $res $arrTmp;


          //SNMP data processing and conclusion

          foreach($res['KEYS'] as $key => $value)
          {
              
          $isFiltered True;
              if (!empty(
          $uniopts['filter_path']))
              {
                  foreach(
          $uniopts['filter_path'] as $s_key => $s_value)
                  {   
                      if  (! 
          preg_match("/".$uniopts['filter_string'][$s_key]."/",SNMPData($res['FILS'][$s_key][$key])))
                      {
                          
          $isFiltered False;
                      }
                  } 
              }

              
          $isExcluded False;
              if (
          $isFiltered && !empty($uniopts['exclude_path']))
              {
                  foreach(
          $uniopts['exclude_path'] as $s_key => $s_value)
                  {
                      if (
          preg_match("/".$uniopts['exclude_string'][$s_key]."/",SNMPData($res['EXCLS'][$s_key][$key])))
                      {
                          
          $isExcluded True;
                      }
                  }
              }
              
              if (
          $isFiltered && !$isExcluded)
              {
                  
          $dataItem = Array();
                  
          $dataItem["{#SNMPINDEX}"] = $key;
                  
          $dataItem["{#SNMPVALUE}"] = SNMPData($value);
                  
                  if (!empty(
          $uniopts['value_path']))
                      {
                          foreach(
          $uniopts['value_path'] as $s_key => $s_value)
                          {
                              
          $dataItem["{#".$uniopts['value_string'][$s_key]."}"] = SNMPData($res['VALS'][$s_key][$key]);
                          }
                      }
                  
          array_push($data$dataItem);
              }
          }

          $jsonData = Array();
          $jsonData["data"] = $data;

          echo 
          json_encode($jsonData);
          echo 
          "\n";   
          ?>


          Your script works for me in CLI, but i can't get it right in ZABBIX.
          Discovery rule returns "Value should be JSON object". I believe i should correct my key, but not sure how.

          This is my output in CLI which works:

          php5 SNMP.discovery35 -h xxxhostxxx -c public --index IF-MIB::ifDescr --filter 'IF-MIB::ifOperStatus,^(1|up)' --filter 'IF-MIB::ifAlias,^([A-Z][A-Z][3-5]_|[A-Z][A-Z][A-Z][3-5]_)'

          I created item referencing your explanation
          Key: SNMPDiscovery.php["-h", {HOST.CONN}, "-c", {$SNMP_COMMUNITY}, "--index", "IF-MIB::ifDescr", "--value", "IF-MIB::ifAdminStatus,ifAdminStatus"]

          So my key looks like:
          SNMP.discovery35["-h", {HOST.CONN}, "-c", {$SNMP_COMMUNITY}, "--index", "IF-MIB::ifDescr", "--filter", "'IF-MIB::ifOperStatus,^(1|up)'", "--filter", "'IF-MIB::ifAlias,^([A-Z][A-Z][3-5]_|[A-Z][A-Z][A-Z][3-5]_)'"]

          What is wrong?
          And btw, you did great work with that script! All of you 3 guys did, thank you very much!



          NWM i solved problem! I removed comma's in filter statements and it worked! Thank you very much once again!¨
          Last edited by Cenzoooo; 15-01-2016, 16:32. Reason: i solved problem

          Comment

          • sysadm01
            Junior Member
            • Sep 2015
            • 19

            #20
            IS not work on zabbix2.4.7

            I add this script on zabbix2.4.7 but not work.
            value should be json object. error accourd.

            Key: snmpdiscovery.php["-h Switch_IP","-c public --index",.1.3.6.1.4.1.1588.2.1.1.1.6.2.1.37]

            below JSON out
            {"data":[{"{#SNMPINDEX}":"7.1","{#SNMPVALUE}":"0"},{"{#SNMP INDEX}":"7.2","{#SNMPVALUE}":"1"},{"{#SNMPINDEX}": "7.3","{#SNMPVALUE}":"2"},{"{#SNMPINDEX}":"7.4","{ #SNMPVALUE}":"3"},{"{#SNMPINDEX}":"7.5","{#SNMPVAL UE}":"4"},{"{#SNMPINDEX}":"7.6","{#SNMPVALUE}":"5" },{"{#SNMPINDEX}":"7.7","{#SNMPVALUE}":"6"},{"{#SN MPINDEX}":"7.8","{#SNMPVALUE}":"7"},{"{#SNMPINDEX} ":"7.9","{#SNMPVALUE}":"8"},{"{#SNMPINDEX}":"7.10" ,"{#SNMPVALUE}":"9"},{"{#SNMPINDEX}":"7.11","{#SNM PVALUE}":"10"},{"{#SNMPINDEX}":"7.12","{#SNMPVALUE }":"11"},{"{#SNMPINDEX}":"7.13","{#SNMPVALUE}":"12 "},{"{#SNMPINDEX}":"7.14","{#SNMPVALUE}":"13"},{"{ #SNMPINDEX}":"7.15","{#SNMPVALUE}":"14"},{"{#SNMPI NDEX}":"7.16","{#SNMPVALUE}":"15"},{"{#SNMPINDEX}" :"7.17","{#SNMPVALUE}":"16"},{"{#SNMPINDEX}":"7.18 ","{#SNMPVALUE}":"17"},{"{#SNMPINDEX}":"7.19","{#S NMPVALUE}":"18"},{"{#SNMPINDEX}":"7.20","{#SNMPVAL UE}":"19"},{"{#SNMPINDEX}":"7.21","{#SNMPVALUE}":" 20"},{"{#SNMPINDEX}":"7.22","{#SNMPVALUE}":"21"},{ "{#SNMPINDEX}":"7.23","{#SNMPVALUE}":"22"},{"{#SNM PINDEX}":"7.24","{#SNMPVALUE}":"23"}]}

            Comment

            • sysadm01
              Junior Member
              • Sep 2015
              • 19

              #21
              IS not work on zabbix2.4.7

              I add this script on zabbix2.4.7 but not work.
              value should be json object. error accourd.

              Key: snmpdiscovery.php["-h Switch_IP","-c public --index",.1.3.6.1.4.1.1588.2.1.1.1.6.2.1.37]

              below JSON out
              {"data":[{"{#SNMPINDEX}":"7.1","{#SNMPVALUE}":"0"},{"{#S NMP INDEX}":"7.2","{#SNMPVALUE}":"1"},{"{#SNMPINDEX}": "7.3","{#SNMPVALUE}":"2"},{"{#SNMPINDEX}":"7.4 ","{ #SNMPVALUE}":"3"},{"{#SNMPINDEX}":"7.5","{#SNMPVAL UE}":"4"},{"{#SNMPINDEX}":"7.6","{#SNMPVALUE}":"5 " },{"{#SNMPINDEX}":"7.7","{#SNMPVALUE}":"6"},{"{#S N MPINDEX}":"7.8","{#SNMPVALUE}":"7"},{"{#SNMPINDEX} ":"7.9","{#SNMPVALUE}":"8"},{"{#SNMPINDEX}":"7 .10" ,"{#SNMPVALUE}":"9"},{"{#SNMPINDEX}":"7.11","{# SNM PVALUE}":"10"},{"{#SNMPINDEX}":"7.12","{#SNMPVALUE }":"11"},{"{#SNMPINDEX}":"7.13","{#SNMPVALUE}": "12 "},{"{#SNMPINDEX}":"7.14","{#SNMPVALUE}":"13"} ,{"{ #SNMPINDEX}":"7.15","{#SNMPVALUE}":"14"},{"{#SNMPI NDEX}":"7.16","{#SNMPVALUE}":"15"},{"{#SNMPINDEX}" :"7.17","{#SNMPVALUE}":"16"},{"{#SNMPINDEX}":"7 .18 ","{#SNMPVALUE}":"17"},{"{#SNMPINDEX}":"7.19", "{#S NMPVALUE}":"18"},{"{#SNMPINDEX}":"7.20","{#SNMPVAL UE}":"19"},{"{#SNMPINDEX}":"7.21","{#SNMPVALUE}": " 20"},{"{#SNMPINDEX}":"7.22","{#SNMPVALUE}":"21"} ,{ "{#SNMPINDEX}":"7.23","{#SNMPVALUE}":"22"},{"{ #SNM PINDEX}":"7.24","{#SNMPVALUE}":"23"}]}

              Comment

              • sysadm01
                Junior Member
                • Sep 2015
                • 19

                #22
                IS not work on zabbix2.4.7

                Originally posted by Skrator
                Hello, I needed this script to work with SNMPv3 as well. So I included the option to choose which SNMP version you want with the '-v' option, just like the net-snmp tools. The script is retrocompatible, so you can update it and have the discovery with the old parameters still working.

                In general, it basically uses the same parameters you would use with the net-snmp tools (snmpget, snmpwalk, etc.).

                Here is the result:

                PHP Code:
                #!/usr/bin/php
                <?PHP
                /* Version 2.2
                 * Author: Tim Koopman
                 * Added SNMPv3 support by Tauame Pacce:
                 *
                 * Request timeouts raised to 1s, from 10ms.
                 * There is a new -v option to inform the protocol version to be used.
                 * Also several SNMPv3 only options were added.
                 *    
                 * Version 2.1:
                 *
                 * For doing SNMP Low Level Discoveries in ZABBIX
                 * Inbuilt only allows filter macros on SNMPINDEX and SNMPVALUE
                 * While you can change what SNMPVALUE would represent by changing the OID
                 * you sometimes what more values, one for filtering and one for item prototype names.
                 *
                 * This script allows you to add as many other values as you like
                 * I use with the following to allow me to filter for interfaces that Admin Status is Up
                 * while still having the interface name on each of the items names.
                 *
                 * Edit discovery rule "Network Interfaces" on template "Template SNMP Interfaces"
                 * Type: External check
                 * Key: SNMPDiscovery.php["-h", {HOST.CONN}, "-c", {$SNMP_COMMUNITY}, "--index", "IF-MIB::ifDescr", "--value", "IF-MIB::ifAdminStatus,ifAdminStatus"]
                 * Filter:
                 *    Macro: {#ifAdminStatus}
                 *    Regexp: 1
                 *
                 * When used without the --value option the result would be identical to the inbuilt SNMP LLD.
                 * This means all existing item prototypes will continue to work as is.
                 * Can add as many --value options as you need.
                 *
                 * v2.0 added --filter as I wanted to seperate SNMP storage devices between memory and disks but Zabbix
                 * won't allow you to have the same key with just different filters. So doing it this way
                 * means the keys will be different and you can leave the Zabbix filter options blank
                 *
                 * Usage
                 * SNMPDiscovery.php -h <host> -c <community> [-v 1|2c|3] --index <OID>
                    [-n <CONTEXTNAME> -u <SECURITYNAME> -l noAuthNoPriv|authNoPriv|authPriv [-a MD5|SHA -A <AUTHPASS>] [-x AES|DES -X <PRIVPASS>] ]
                    [--value <OID>,<MACRONAME>]... [--filter <OID>,<REGEX>]...
                    
                 *   -h        : SNMP Host to query. When used in Zabbix this would normally be {HOST.CONN}
                 *   -c        : SNMP Community. When used in Zabbix this would normally be {$SNMP_COMMUNITY}
                 *   -v        : SNMP version to be used, possible options are "1", "2c" or "3". Defaults to "1".
                 *   --index   : OID to walk. Is used to populate {#SNMPINDEX} and {#SNMPVALUE} macros.
                 *   --value   : Comma seperated OID and macro name to also return. Can reference in zabbix using macro {#<MACRONAME>}
                 *               You can use --value multiple times if you need more macros returned.
                 *   --filter  : Comma seperated OID and regular expression. Any index that does not matches the regex will be excluded from the results.
                 *               You can use --filter multiple times if you need.
                 *
                 *   SNMPv3 exclusive options (enabled with "-v 3"):
                 *   -u        : Security Name.
                 *   -a        : Authentication Protocol, usually "MD5" or "SHA". Defaults to "SHA".
                 *   -x        : Privacy Protocol, usually "AES" or "DES". Defaults to "DES".
                 *   -A        : Authentication passphrase (Only enabled with "-a" option).
                 *   -X        : Privacy passphrase (Only enabled with "-x" option).
                 *   -l        : Security level, possible options are "noAuthNoPriv", "authNoPriv" or "authPriv". Defaults to "authPriv".
                 *
                 * Returns JSON text which Zabbix LLD uses.
                 *
                 * Examples
                 *   SNMPDiscovery.php -h 127.0.0.1 -c public --index IF-MIB::ifDescr --value IF-MIB::ifAdminStatus,ifAdminStatus
                 *   SNMPDiscovery.php -h 127.0.0.1 -c public --index HOST-RESOURCES-MIB::hrStorageDescr --filter HOST-RESOURCES-MIB::hrStorageType,hrStorageFixedDisk$
                 *   SNMPDiscovery.php -h 127.0.0.1 -c public --index HOST-RESOURCES-MIB::hrStorageDescr --filter 'HOST-RESOURCES-MIB::hrStorageType,(hrStorageRam|hrStorageVirtualMemory)$'
                 *   SNMPDiscovery.php -h 127.0.0.1 -v 3 -u bob -a AES -A mypasswd -l authNoPriv --index IF-MIB::ifDescr --value IF-MIB::ifAdminStatus,ifAdminStatus
                */
                error_reporting(E_ALLE_WARNING);
                 
                function 
                SNMPData($data)
                {
                    if (
                strPos($data"STRING: ")===0)
                    {
                        
                preg_match_all('!^STRING: \"?(.*?)\"?$!'$data$matches);
                        return 
                $matches[1][0];
                    } elseif (
                strPos($data"INTEGER: ")===0) {
                        
                preg_match_all('!\d+!'$data$matches);
                        return (int) 
                $matches[0][0];
                    } else {
                        return 
                $data;
                    }
                }

                function 
                usage()
                {
                    print 
                "Usage: SNMPDiscovery.php -h <host> -c <community> --index <OID>
                    [-v 3 -u <SECURITYNAME> -l <SECURITYLEVEL> [-a <AUTHPROTOCOL> -A <AUTHPASS>] [-x <PRIVPROTOCOL> -X <PRIVPASS>] ]
                    [--value <OID>,<MACRONAME>]... [--filter <OID>,<REGEX>]...\n"
                ;
                    exit(
                1);
                }
                 

                $options getopt("c:h:v:u:a:x:A:X:l:",array("index:","value:","filter:"));

                if (
                count($options) == 0)
                {
                    
                usage();
                }

                if (!
                array_key_exists("a"$options))
                {
                    
                $options["a"]="SHA";
                }

                if (!
                array_key_exists("A"$options))
                {
                    
                $options["A"]="";
                }


                if (!
                array_key_exists("x"$options))
                {
                    
                $options["x"]="DES";
                }

                if (!
                array_key_exists("X"$options))
                {
                    
                $options["X"]="";
                }


                if (!
                array_key_exists("l"$options))
                {
                    
                $options["l"]="authPriv";
                }

                $values = Array();
                if (
                array_key_exists("value"$options))
                {
                    if (
                is_array($options["value"]))
                    {
                        foreach(
                $options["value"] as $value)
                        {
                            
                $explode explode(","$value);
                            
                $values[$explode[0]] = $explode[1];
                        }
                    } else {
                        
                $explode explode(","$options["value"]);
                        
                $values[$explode[0]] = $explode[1];
                    }
                }
                 
                $filters = Array();
                $defaultFilter false;
                if (
                array_key_exists("filter"$options))
                {
                    if (
                is_array($options["filter"]))
                    {
                        foreach(
                $options["filter"] as $value)
                        {
                            
                $explode explode(","$value);
                            
                $filters[$explode[0]] = $explode[1];
                        }
                    } else {
                        
                $explode explode(","$options["filter"]);
                        
                $filters[$explode[0]] = $explode[1];
                    }
                } else {
                    
                $defaultFilter true;
                }
                 
                $data = Array();
                 
                if (
                array_key_exists("v"$options))
                {
                    switch(
                $options["v"])
                    {
                        case 
                "1":
                            
                $keys snmprealwalk($options["h"], $options["c"] , $options["index"], 1000000 5);
                            break;
                        case 
                "2c":
                            
                $keys snmp2_real_walk($options["h"], $options["c"] , $options["index"], 1000000 5);
                            break;
                        case 
                "3":
                            
                $keys snmp3_real_walk($options["h"], $options["u"] , $options["l"], $options["a"], $options["A"], $options["x"], $options["X"], $options["index"], 1000000 5);
                            break;
                        default:
                            
                $keys snmprealwalk($options["h"], $options["c"] , $options["index"], 1000000 5);
                    }
                }else{
                    
                $keys snmprealwalk($options["h"], $options["c"] , $options["index"], 1000000 5);
                }

                foreach(
                $keys as $key => $value)
                {
                    
                $key substr($keystrlen($options["index"])+1);
                    
                $value SNMPData($value);
                    
                $dataItem = Array();
                    
                $dataItem["{#SNMPINDEX}"] = $key;
                    
                $dataItem["{#SNMPVALUE}"] = $value;
                 
                    
                $filtered $defaultFilter;
                    foreach(
                $filters as $oid => $regex)
                    {
                        if (
                array_key_exists("v"$options))
                        {
                            switch(
                $options["v"])
                            {
                                case 
                "1":
                                    
                $oidvalue snmpget($options["h"], $options["c"], "{$oid}.{$key}"10000005);
                                    break;
                                case 
                "2c":
                                    
                $oidvalue snmp2_get($options["h"], $options["c"], "{$oid}.{$key}"10000005);
                                    break;
                                case 
                "3":
                                    
                $oidvalue snmp3_get($options["h"], $options["u"] , $options["l"], $options["a"], $options["A"], $options["x"], $options["X"], "{$oid}.{$key}"1000000 5);
                                    break;
                                default:
                                    
                $oidvalue snmpget($options["h"], $options["c"], "{$oid}.{$key}"10000005);
                            }
                        }else{
                            
                $oidvalue snmpget($options["h"], $options["c"], "{$oid}.{$key}"10000005);
                        }
                        
                $oidvalue SNMPData($oidvalue);
                        if (
                preg_match("/".$regex."/"$oidvalue)) $filtered true;
                    }
                 
                    if (
                $filtered)
                    {
                        foreach(
                $values as $oid => $name)
                        {
                            if (
                array_key_exists("v"$options))
                            {
                                switch(
                $options["v"])
                                {
                                    case 
                "1":
                                        
                $oidvalue snmpget($options["h"], $options["c"], "{$oid}.{$key}"10000005);
                                        break;
                                    case 
                "2c":
                                        
                $oidvalue snmp2_get($options["h"], $options["c"], "{$oid}.{$key}"10000005);
                                        break;
                                    case 
                "3":
                                        
                $oidvalue snmp3_get($options["h"], $options["u"] , $options["l"], $options["a"], $options["A"], $options["x"], $options["X"], "{$oid}.{$key}"1000000 5);
                                        break;
                                    default:
                                        
                $oidvalue snmpget($options["h"], $options["c"], "{$oid}.{$key}"10000005);
                                }
                            }else{
                                
                $oidvalue snmpget($options["h"], $options["c"], "{$oid}.{$key}"10000005);
                            }
                            
                $oidvalue SNMPData($oidvalue);
                            
                $dataItem["{#{$name}}"] = $oidvalue;
                        }
                 
                        
                array_push($data$dataItem);
                    }
                }
                 
                $jsonData = Array();
                $jsonData["data"] = $data;
                 
                echo 
                json_encode($jsonData);
                echo 
                "\n";
                ?>
                Hope it helps.
                I add this script on zabbix2.4.7 but not work.
                value should be json object. error accourd.

                Key: snmpdiscovery.php["-h Switch_IP","-c public --index",.1.3.6.1.4.1.1588.2.1.1.1.6.2.1.37]

                below JSON out
                {"data":[{"{#SNMPINDEX}":"7.1","{#SNMPVALUE}":"0"},{"{#S NMP INDEX}":"7.2","{#SNMPVALUE}":"1"},{"{#SNMPINDEX}": "7.3","{#SNMPVALUE}":"2"},{"{#SNMPINDEX}":"7.4 ","{ #SNMPVALUE}":"3"},{"{#SNMPINDEX}":"7.5","{#SNMPVAL UE}":"4"},{"{#SNMPINDEX}":"7.6","{#SNMPVALUE}":"5 " },{"{#SNMPINDEX}":"7.7","{#SNMPVALUE}":"6"},{"{#S N MPINDEX}":"7.8","{#SNMPVALUE}":"7"},{"{#SNMPINDEX} ":"7.9","{#SNMPVALUE}":"8"},{"{#SNMPINDEX}":"7 .10" ,"{#SNMPVALUE}":"9"},{"{#SNMPINDEX}":"7.11","{# SNM PVALUE}":"10"},{"{#SNMPINDEX}":"7.12","{#SNMPVALUE }":"11"},{"{#SNMPINDEX}":"7.13","{#SNMPVALUE}": "12 "},{"{#SNMPINDEX}":"7.14","{#SNMPVALUE}":"13"} ,{"{ #SNMPINDEX}":"7.15","{#SNMPVALUE}":"14"},{"{#SNMPI NDEX}":"7.16","{#SNMPVALUE}":"15"},{"{#SNMPINDEX}" :"7.17","{#SNMPVALUE}":"16"},{"{#SNMPINDEX}":"7 .18 ","{#SNMPVALUE}":"17"},{"{#SNMPINDEX}":"7.19", "{#S NMPVALUE}":"18"},{"{#SNMPINDEX}":"7.20","{#SNMPVAL UE}":"19"},{"{#SNMPINDEX}":"7.21","{#SNMPVALUE}": " 20"},{"{#SNMPINDEX}":"7.22","{#SNMPVALUE}":"21"} ,{ "{#SNMPINDEX}":"7.23","{#SNMPVALUE}":"22"},{"{ #SNM PINDEX}":"7.24","{#SNMPVALUE}":"23"}]}

                Comment

                • Skrator
                  Junior Member
                  Zabbix Certified SpecialistZabbix Certified Professional
                  • Apr 2015
                  • 8

                  #23
                  Does the out put has these blank spaces inside the macro's names? i.e. "{#S NMP INDEX}".
                  The output looks fine if it doesn't have the blank spaces.

                  Comment

                  • sysadm01
                    Junior Member
                    • Sep 2015
                    • 19

                    #24
                    I type was wrong

                    Originally posted by Skrator
                    Does the out put has these blank spaces inside the macro's names? i.e. "{#S NMP INDEX}".
                    The output looks fine if it doesn't have the blank spaces.
                    outpus is json vaild.
                    i check on https://jsonformatter.curiousconcept.com/

                    Output is

                    {"data":[{"{#SNMPINDEX}":"7.1","{#SNMPVALUE}":"0"},{"{#SNMP INDEX}":"7.2","{#SNMPVALUE}":"1"},{"{#SNMPINDEX}": "7.3","{#SNMPVALUE}":"2"},{"{#SNMPINDEX}":"7.4","{ #SNMPVALUE}":"3"},{"{#SNMPINDEX}":"7.5","{#SNMPVAL UE}":"4"},{"{#SNMPINDEX}":"7.6","{#SNMPVALUE}":"5" },{"{#SNMPINDEX}":"7.7","{#SNMPVALUE}":"6"},{"{#SN MPINDEX}":"7.8","{#SNMPVALUE}":"7"},{"{#SNMPINDEX} ":"7.9","{#SNMPVALUE}":"8"},{"{#SNMPINDEX}":"7.10" ,"{#SNMPVALUE}":"9"},{"{#SNMPINDEX}":"7.11","{#SNM PVALUE}":"10"},{"{#SNMPINDEX}":"7.12","{#SNMPVALUE }":"11"},{"{#SNMPINDEX}":"7.13","{#SNMPVALUE}":"12 "},{"{#SNMPINDEX}":"7.14","{#SNMPVALUE}":"13"},{"{ #SNMPINDEX}":"7.15","{#SNMPVALUE}":"14"},{"{#SNMPI NDEX}":"7.16","{#SNMPVALUE}":"15"},{"{#SNMPINDEX}" :"7.17","{#SNMPVALUE}":"16"},{"{#SNMPINDEX}":"7.18 ","{#SNMPVALUE}":"17"},{"{#SNMPINDEX}":"7.19","{#S NMPVALUE}":"18"},{"{#SNMPINDEX}":"7.20","{#SNMPVAL UE}":"19"},{"{#SNMPINDEX}":"7.21","{#SNMPVALUE}":" 20"},{"{#SNMPINDEX}":"7.22","{#SNMPVALUE}":"21"},{ "{#SNMPINDEX}":"7.23","{#SNMPVALUE}":"22"},{"{#SNM PINDEX}":"7.24","{#SNMPVALUE}":"23"},{"{#SNMPINDEX }":"7.25","{#SNMPVALUE}":"24"},{"{#SNMPINDEX}":"7. 26","{#SNMPVALUE}":"25"},{"{#SNMPINDEX}":"7.27","{ #SNMPVALUE}":"26"},{"{#SNMPINDEX}":"7.28","{#SNMPV ALUE}":"27"},{"{#SNMPINDEX}":"7.29","{#SNMPVALUE}" :"28"},{"{#SNMPINDEX}":"7.30","{#SNMPVALUE}":"29"} ,{"{#SNMPINDEX}":"7.31","{#SNMPVALUE}":"30"},{"{#S NMPINDEX}":"7.32","{#SNMPVALUE}":"31"},{"{#SNMPIND EX}":"7.33","{#SNMPVALUE}":"32"},{"{#SNMPINDEX}":" 7.34","{#SNMPVALUE}":"33"},{"{#SNMPINDEX}":"7.35", "{#SNMPVALUE}":"34"},{"{#SNMPINDEX}":"7.36","{#SNM PVALUE}":"35"},{"{#SNMPINDEX}":"7.37","{#SNMPVALUE }":"36"},{"{#SNMPINDEX}":"7.38","{#SNMPVALUE}":"37 "},{"{#SNMPINDEX}":"7.39","{#SNMPVALUE}":"38"},{"{ #SNMPINDEX}":"7.40","{#SNMPVALUE}":"39"},{"{#SNMPI NDEX}":"7.41","{#SNMPVALUE}":"40"},{"{#SNMPINDEX}" :"7.42","{#SNMPVALUE}":"41"},{"{#SNMPINDEX}":"7.43 ","{#SNMPVALUE}":"42"},{"{#SNMPINDEX}":"7.44","{#S NMPVALUE}":"43"},{"{#SNMPINDEX}":"7.45","{#SNMPVAL UE}":"44"},{"{#SNMPINDEX}":"7.46","{#SNMPVALUE}":" 45"},{"{#SNMPINDEX}":"7.47","{#SNMPVALUE}":"46"},{ "{#SNMPINDEX}":"7.48","{#SNMPVALUE}":"47"},{"{#SNM PINDEX}":"7.49","{#SNMPVALUE}":"48"},{"{#SNMPINDEX }":"7.50","{#SNMPVALUE}":"49"},{"{#SNMPINDEX}":"7. 51","{#SNMPVALUE}":"50"},{"{#SNMPINDEX}":"7.52","{ #SNMPVALUE}":"51"},{"{#SNMPINDEX}":"7.53","{#SNMPV ALUE}":"52"},{"{#SNMPINDEX}":"7.54","{#SNMPVALUE}" :"53"},{"{#SNMPINDEX}":"7.55","{#SNMPVALUE}":"54"} ,{"{#SNMPINDEX}":"7.56","{#SNMPVALUE}":"55"},{"{#S NMPINDEX}":"7.57","{#SNMPVALUE}":"56"},{"{#SNMPIND EX}":"7.58","{#SNMPVALUE}":"57"},{"{#SNMPINDEX}":" 7.59","{#SNMPVALUE}":"58"},{"{#SNMPINDEX}":"7.60", "{#SNMPVALUE}":"59"},{"{#SNMPINDEX}":"7.61","{#SNM PVALUE}":"60"},{"{#SNMPINDEX}":"7.62","{#SNMPVALUE }":"61"},{"{#SNMPINDEX}":"7.63","{#SNMPVALUE}":"62 "},{"{#SNMPINDEX}":"7.64","{#SNMPVALUE}":"63"},{"{ #SNMPINDEX}":"7.65","{#SNMPVALUE}":"64"},{"{#SNMPI NDEX}":"7.66","{#SNMPVALUE}":"65"},{"{#SNMPINDEX}" :"7.67","{#SNMPVALUE}":"66"},{"{#SNMPINDEX}":"7.68 ","{#SNMPVALUE}":"67"},{"{#SNMPINDEX}":"7.69","{#S NMPVALUE}":"68"},{"{#SNMPINDEX}":"7.70","{#SNMPVAL UE}":"69"},{"{#SNMPINDEX}":"7.71","{#SNMPVALUE}":" 70"},{"{#SNMPINDEX}":"7.72","{#SNMPVALUE}":"71"},{ "{#SNMPINDEX}":"7.73","{#SNMPVALUE}":"72"},{"{#SNM PINDEX}":"7.74","{#SNMPVALUE}":"73"},{"{#SNMPINDEX }":"7.75","{#SNMPVALUE}":"74"},{"{#SNMPINDEX}":"7. 76","{#SNMPVALUE}":"75"},{"{#SNMPINDEX}":"7.77","{ #SNMPVALUE}":"76"},{"{#SNMPINDEX}":"7.78","{#SNMPV ALUE}":"77"},{"{#SNMPINDEX}":"7.79","{#SNMPVALUE}" :"78"},{"{#SNMPINDEX}":"7.80","{#SNMPVALUE}":"7 9"}]}

                    Comment

                    • Skrator
                      Junior Member
                      Zabbix Certified SpecialistZabbix Certified Professional
                      • Apr 2015
                      • 8

                      #25
                      The problem you are having doesn't look like a problem with the script.

                      BUT, if you only need the macros {#SNMPINDEX} and {#SNMPVALUE}, then there is no need to use this script. You can use Zabbix integrated SNMP Discovery.

                      Just use the item type "SNMPv2 Agent" in your discovery.

                      Comment

                      • Skrator
                        Junior Member
                        Zabbix Certified SpecialistZabbix Certified Professional
                        • Apr 2015
                        • 8

                        #26
                        Zabbix 3.0 and the SNMP Enhanced discovery

                        Since Zabbix 3.0 is now fully released and integrates the Advanced SNMP Discovery, with multiple macros, this is script is no longer needed. It will be useful only for older releases environments.

                        Reference: https://www.zabbix.com/documentation...snmp_discovery

                        Comment

                        • frankwangzc
                          Junior Member
                          • Aug 2016
                          • 2

                          #27
                          Thanks a lot

                          It's very useful. In my environment, each switch has 300+ ports. What a wonderful script!

                          Comment

                          • registration_is_lame
                            Senior Member
                            • Nov 2007
                            • 148

                            #28
                            Any idea how to debug this script? For some host it returns no data at all. But SNMP walk retrieves data.

                            ./SNMPDiscovery.php -h 1.1.1.1 -c public --index IF-MIB::ifDescr --value....
                            {"data":[{"{#SNMPINDEX}":"1","{#SNMPVALUE}":"GigabitEtherne t1\........]}

                            ./SNMPDiscovery.php -h 2.2.2.2 -c public --index IF-MIB::ifDescr
                            {"data":[]}

                            snmpwalk -v 2c -c public 1.1.1.1 IF-MIB::ifDescr
                            .1.3.6.1.2.1.2.2.1.2.2 = STRING:#####
                            .1.3.6.1.2.1.2.2.1.2.3 = STRING:#####
                            .1.3.6.1.2.1.2.2.1.2.4 = STRING:#####

                            snmpwalk -v 2c -c public 2.2.2.2 IF-MIB::ifDescr
                            .1.3.6.1.2.1.2.2.1.2.2 = STRING:#####
                            .1.3.6.1.2.1.2.2.1.2.3 = STRING:#####
                            .1.3.6.1.2.1.2.2.1.2.4 = STRING:#####

                            Comment

                            • jhgrc
                              Member
                              • Jun 2009
                              • 52

                              #29
                              Originally posted by Skrator
                              Since Zabbix 3.0 is now fully released and integrates the Advanced SNMP Discovery, with multiple macros, this is script is no longer needed. It will be useful only for older releases environments.

                              Reference: https://www.zabbix.com/documentation...snmp_discovery
                              Yes these kind of scripts are needed because Zabbix 3 LLD is too bulk.

                              However there is a path how this script should be enhanced and that is parsing SNMPINDEX to pieces. Current script works fantastic to single-oid-index. But if you start collecting NBAR, BGP, with BGP you need to parse IP-address out with index and NBAR interface port number and protocol number.

                              This is solved with tsicadvsnmp.discovery script of mine (found on GIT). But my script needs enhancement for multi-table discovery and filters.

                              Comment

                              • gjaekel
                                Junior Member
                                • Nov 2014
                                • 14

                                #30
                                Originally posted by jhgrc
                                Yes these kind of scripts are needed because Zabbix 3 LLD is too bulk.
                                Thank you for this usefull, but also disappointing information. I have not time yet to take a look about Zabbix 3.0. May you give a reference to your project here? Maybe we can merge it into the current V2.3.1 .
                                Last edited by gjaekel; 02-02-2017, 10:01.

                                Comment

                                Working...