-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathjunmpls.discovery
89 lines (71 loc) · 2.53 KB
/
junmpls.discovery
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/php
<?php
/**
* SNMP discovery script for Zabbix providing multiple SNMP values combined by index
*
* @author Robin Roevens <robin.roevens (at) gmail.com>
*/
$snmpwalk = '/usr/bin/snmpwalk';
$argv[0] = basename($argv[0]);
$usage = <<<EOM
{$argv[0]} is an alternative to the build-in SNMP low level discovery of Zabbix 2.0+
allowing to retreive multiple SNMP OID values by a common index.
It integrates seamless into Zabbix by using "External check" discovery rules.
Usage: {$argv[0]} <ip|host> <snmpwalk args> <OID1> [<OID2> ...]
Example: {$argv[0]} 192.1.1.1 "-v2c -cpublic" 1.3.6.1.4.1.19746.1.15.1.1.1.2 1.3.6.1.4.1.19746.1.15.1.1.1.8
This wil retreive all values for both given OIDs sorted under their common indexes in the form
{#MVSNMPINDEX} = 0
{#MVSNMPVALUE1} = value of first OID.0
{#MVSNMPVALUE2} = value of second OID.0
{#MVSNMPINDEX} = 1
{#MVSNMPVALUE1} = value of first OID.1
{#MVSNMPVALUE2} = value of second OID.1
and so on for all available indexes in both OIDs.
If an index is missing in one OID, then the value of that OID for that index will be empty.
Usage in Zabbx:
Define a discovery rule and select "External Check".
In the "Key"-field, you specify: {$argv[0]}[<ip|host>,<snmpwalk args>,<OID1>[,<OID2>,...]]
Example:
{$argv[0]}[192.1.1.1,"-v2c -c\{\$SNMP_COMMUNITY\}",1.3.6.1.4.1.19746.1.15.1.1.1.2,1.3.6.1.4.1.19746.1.15.1.1.1.8]
EOM;
array_shift($argv);
// Check input
if (!is_array($argv) || !array_key_exists(0, $argv)) {
die($usage);
}
$host = $argv[0];
array_shift($argv);
if (!array_key_exists(0, $argv)) {
die($usage);
}
$snmpArgs = $argv[0];
array_shift($argv);
if (!array_key_exists(0, $argv)) {
die($usage);
}
$oids = $argv;
// Get values
$multivalues = array();
foreach ($oids as $oidIndex => $oid) {
$output = shell_exec("$snmpwalk $snmpArgs -On -Oq $host $oid");
if (preg_match_all('/\.((?<=.{27}).+(\d+){1,4}(\.(\d+){1,4})+) "?([^"\r\n]*)/', $output, $matches)) {
foreach ($matches[1] as $key => $valuesIndex) {
$multivalues[$valuesIndex][$oidIndex] = $matches[2][$key];
}
}
}
if (count($multivalues) == 0) {
die("No values found");
}
// Build data array
$data = array('data' => array());
$i = 0;
foreach ($multivalues as $valuesIndex => $values) {
$data['data'][$i]['{#MVSNMPINDEX}'] = $valuesIndex;
foreach ($values as $oidIndex => $value) {
$data['data'][$i]['{#MVSNMPVALUE'.($oidIndex + 1).'}'] = trim($value);
}
$i++;
}
// Spit it out as JSON
print json_encode($data);