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?
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_ALL^ E_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($key, strripos($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