-
Notifications
You must be signed in to change notification settings - Fork 3
/
advsnmp.discovery
executable file
·184 lines (159 loc) · 5.36 KB
/
advsnmp.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/perl
use strict;
# script: advsnmp.discovery
# version: 2.0
# author: Simon Kowallik <sk simonkowallik.com>
# description: Zabbix low level discovery script for advanced index discovery
# license: GPL2
#USAGE:
#advsnmp.discovery[{HOST.IP},"-v2c -cpublic",.1.2.3.4.5.6,offset.length,offset.length,offset.lentgh
# ^ ^ ^ ^ ^ ^
# | | | | | |
#ARGV 0 1 2 3 4 5
# | | | | | |
# IP/DNS Name | | | | |
# SNMP Settings SNMP OID INDEX_offset.INDEX_length
#
#OID: .0.0.0.0.0.0.1.0.2.1.1.4.4.0
#will be cut to: .1.0.2.1.1.4.4.0
#
# . 1 . 0 . 2 . 1 . 1 . 4 . 4 . 0
#0=>'', 1=>'1', 2=>'0', 3=>'2', 4=>'1', 5=>'1', 6=>'4', 7=>'4', 8=>'0'
#
# $_offset $_len
# \ /
#config: INDEX1 -> 1.1
# INDEX2 -> 3.2
# INDEX3 -> 5.3
#
# INDEX1 INDEX2 INDEX3
# \ | /
# .[1].0.[2.1].[1.4.4].0
#
#
#EXAMPLE:
# OIDs to query:
# .0.0.0.0.0.0.1.0.2.1.1.4.4.0
# .0.0.0.0.0.0.2.0.3.6.1.4.4.0
# .0.0.0.0.0.0.3.0.4.5.1.4.4.0
# .0.0.0.0.0.0.1.0.2.4.5.3.4.0
# .0.0.0.0.0.0.2.0.2.2.5.3.4.0
# .0.0.0.0.0.0.3.0.2.9.5.3.4.0
#
# Discovery Rule with "external check"
# Key: advsnmp.discovery[{HOST.IP},"-v2c -cpublic",.0.0.0.0.0.0,1.1,3.2,5.3]
#
# Idexes will be:
# ADVSNMPINDEX1 ADVSNMPINDEX2 ADVSNMPINDEX3
# 1 2.1 1.4.4
# 2 3.6 1.4.4
# 3 4.5 1.4.4
# 1 2.4 5.3.4
# 2 2.2 5.3.4
# 3 2.9 5.3.4
# global options
my $GLOB_SNMPW = "-OQn";
my $SNMPWALK_BIN = "/usr/bin/snmpwalk";
# never modify these values, as we have to pass them back to Zabbix within the JSON element!
my $OPT_HOST = $ARGV[0];
my $OPT_SNMPW = $ARGV[1];
my $OPT_OID = $ARGV[2];
# build config
my %OPT_CONFIG;
my $OPT_CONFIG_STRING;
for (my $cnt = 3; $cnt < scalar(@ARGV); $cnt++) {
my ($_offset, $_len) = split(/\./, $ARGV[$cnt]);
$OPT_CONFIG{$_offset} = $_len;
$OPT_CONFIG_STRING = $OPT_CONFIG_STRING . ',' . $ARGV[$cnt];
}
#variables
my %INDEXES;
# assign OPT_OID to opt_oid_escape
my $opt_oid_escape = $OPT_OID;
# prepend a dot (.) to opt_oid_escape in case OPT_OID was specified without one
if($opt_oid_escape !~ m/^\./) {
$opt_oid_escape = '.' . $opt_oid_escape;
}
# v1.1: cut off trailing dot, if it exists
if($opt_oid_escape =~ m/\.$/) {
chop($opt_oid_escape);
}
# create snmpw_oid from sanitized OPT_OID
my $snmpw_oid = $opt_oid_escape;
# escape all dots in opt_oid_escape for future regexes
$opt_oid_escape =~ s/\./\\./g;
foreach my $snmpw_line (`$SNMPWALK_BIN $GLOB_SNMPW $OPT_SNMPW $OPT_HOST $snmpw_oid`)
{
# remove newline
chomp($snmpw_line);
# split OID = VALUE
my ($oid, $value) = split(/ = /, $snmpw_line);
# sanitize $value, remove start/end quotes
$value =~ s/^"//;
$value =~ s/"$//;
$value =~ s/"/\\"/g;
$value =~ s/\\/\\\\/g;
# remove OPT_OID part from $oid
# and we will get the trailing OID part, where we will extract the INDEX
#
# before: .0.0.0.0.0.0.1.0.2.1.0
$oid =~ s/$opt_oid_escape//;
# after: .1.0.2.1.0
# split rest of oid into array
# index N ($OPT_INDEX) is ARRAY[N] -> ARRAY[$OPT_INDEX]
my @oid_arr = split(/\./, $oid);
#contains: 0=>'', 1=>'1', 2=>'0', 3=>'2', 4=>'1', 5=>'0'
# put indexes into @%indexes
my $line_index;
# fetch offset from OPT_CONFIG
foreach my $_offset (sort keys %OPT_CONFIG) {
# set $index to index (OID part)
my $index = $oid_arr[$_offset];
# when complete $index by looking at len (stored in $OPT_CONFIG{$_offset})
for (my $i = 1; $i < $OPT_CONFIG{$_offset}; $i++) {
#v1.1: next if oid_arr element is empty. this can happen for dymanic length indexes
next if($oid_arr[$_offset+$i] =~ m/^$/);
# attach next OID part for len of index
$index = $index .'.'. $oid_arr[$_offset+$i];
}
$line_index = $line_index . ";$index";
}
# assign value to INDEXES Hash. INDEX => VALUE
$INDEXES{$line_index} = $value;
}
#
# print JSON object
#
print "{\n";
# from Zabbix 2.0.0rc1 the array name has changed to 'data'
print "\t\"data\":[\n";
my $first_line = 1;
#for (my $i = 0; $i < $#INDEXES; $i++) {
foreach my $line_index (sort keys %INDEXES) {
# print if it is not the first line
if($first_line) {
# we are at first line
$first_line = 0;
} else {
# we are not at first line
print "\t,\n";
}
print "\t\t{\n";
# print all INDEXES
my $ctr = 1;
foreach my $index ( split(/;/, $line_index) ) {
next if (!defined($index));
print "\t\t\"{#ADVSNMPINDEX$ctr}\": \"$index\",\n";
$ctr++
}
# print value if there are indexes (ctr would be 2 for one index)
if ($ctr > 1) {
print "\t\t\"{#ADVSNMPVALUE}\":\"$INDEXES{$line_index}\"\n";
print "\t\t}";
} else {
# if there is no index, close JSON
print "\t\t}";
}
} #for
print "\n\t]\n";
print "}\n";