Thread Ausgabe fügt immer 0& am Anfang an (5 answers)
Opened by MULLE123 at 2011-05-20 14:34

Gast MULLE123
 2011-05-20 16:28
#148916 #148916
Hi,

hier das ganze Skript. Ist noch was verbastelt ^^

Code: (dl )
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#!/usr/bin/perl
#
# AUTHORS:
# Copyright (C) 2003-2011 Opsera Limited. All rights reserved
#
# This file is part of Opsview
#
# Opsview is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# Opsview is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Opsview; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# Policy-Plugin for ScreenOS
use lib qw ( /usr/local/nagios/perl/lib );
use Net::SNMP;
use Getopt::Long;

$script = $0;
$script_version = "1.0";
$script_description = "Active sessions on Netscreen device";
$inter_table= '.1.3.6.1.2.1.2.2.1';
$index_table = '1.3.6.1.2.1.2.2.1.1';
$descr_table = '1.3.6.1.2.1.2.2.1.2';
$oper_table = '1.3.6.1.2.1.2.2.1.8.';
$admin_table = '1.3.6.1.2.1.2.2.1.7.';
$speed_table = '1.3.6.1.2.1.2.2.1.5.';
#In packet/error ratio
$in_ucast_table = '1.3.6.1.2.1.2.2.1.11.';
$in_nucast_table = '1.3.6.1.2.1.2.2.1.12.';
$in_error_table = '1.3.6.1.2.1.2.2.1.14.';
#out packet/error ratio
$out_ucast_table = '1.3.6.1.2.1.2.2.1.17.';
$out_nucast_table = '1.3.6.1.2.1.2.2.1.18.';
$out_error_table = '1.3.6.1.2.1.2.2.1.20.';




$label = "Sessions"; # Label used by plugin output
$performance_label = "sessions"; # Label used by plugin performance output

$ipaddress = ""; # Default IP address
$version = "1"; # SNMP version
$o_hostname ="";
$community = "public"; # Default community string
$timeout = 10; # Response timeout (seconds)
$warning = 180; # Default warning threshold
$critical = 190; # Default critical threshold
$status = 0;
$o_help = undef;
$o_version2 = undef;;
$counter = "";
$resultw="";
$resultc="";
$highperf= undef; # Use 64 bits counters
$o_descr = ""; # description filter
$o_octetlength=undef;
$o_warn_opt;
$o_crit_opt;
@o_crit=(5,5,5);
@o_warn=(5,5,5);
$port = 161;
$res="";
$res2="lalala";

# Do we have enough information?
if ( @ARGV < 1 ) {
print "Too few arguments\n";
usage();
}

#Options

Getopt::Long::Configure ("bundling");
GetOptions(
'H:s' => \$o_hostname, 'hostname:s' => \$o_hostname,
'p:i' => \$port, 'port:i' => \$port,
'n:s' => \$o_descr, 'name:s' => \$o_descr,
'C:s' => \$community, 'community:s' => \$community,
'2' => \$o_version2, 'v2c' => \$o_version2,
'w:s' => \$o_warn_opt, 'warning:s' => \$o_warn_opt,
'c:s' => \$o_crit_opt, 'critical:s' => \$o_crit_opt,
'g' => \$highperf, '64bits' => \$highperf
);

@o_warn=split(/,/,$opt_w);
@o_crit=split(/,/,$opt_c);


# Create the SNMP session
my ( $session, $error ) = Net::SNMP->session(
-community => $community,
-hostname => $o_hostname,
-version => '2',
-timeout => $timeout,
-port => $port,
);

main();

# Close the session
$session->close();

#Status-checks
if($status == 0){
print $res;
#for (my $i=0;$i < $num_int; $i++){
# print $descr[$i] . " UP;";
#}
exit 0;
}
if($status == 3){
print "Status is CRITICAL: " . $res;
exit 3
}

####################################################################
# This is where we gather data via SNMP and return results #
####################################################################

sub main {
my @tindex = undef;
my @oids = undef;
@descr = undef;
my (@oid_decrpt,@oid_perf_inucast,@oid_perf_innucast,@oid_perf_inerr,@oid_perf_outucast,@oid_perf_outnucast,@oid_perf_outerr,, @oid_speed)=
(undef,undef,undef,undef,undef,undef,undef,undef);
#my (@oid_decrpt,@oid_perf,@oid_perf_outoct,@oid_perf_inoct,@oid_perf_inerr,@oid_perf_outerr,@oid_perf_indisc,@oid_perf_outdisc)=
#(undef,undef,undef,undef,undef,undef,undef,undef);
$num_int = 0;
# Get desctiption table
$resultat = $session->get_table(-baseoid => $descr_table);

if (!defined($resultat)) {
printf("ERROR: Description table : %s.\n", $session->error);
$session->close;
exit $ERRORS{"UNKNOWN"};
}

# Change to 64 bit counters if option is set :
#if (defined($o_highperf)) {
# $out_octet_table=$out_octet_table_64;
# $in_octet_table=$in_octet_table_64;
#}

foreach my $key ( keys %$resultat) {

#regexp
if($$resultat{$key} =~ /$o_descr/){
# get the index number of the interface
my @oid_list = split (/\./,$key);
$tindex[$num_int] = pop (@oid_list);
# get the full description
$descr[$num_int]=$$resultat{$key};
# Get rid of special caracters (specially for Windows)
$descr[$num_int] =~ s/[[:cntrl:]]//g;
# put the admin or oper oid in an array
$oids[$num_int]= $oper_table . $tindex[$num_int] ;
# Put the performance oid
#description
$oid_decrpt[$num_int]= $descr_table . $tindex[$num_int];
#in
$oid_perf_inucast[$num_int]= $in_ucast_table . $tindex[$num_int];
$oid_perf_innucast[$num_int]= $in_nucast_table . $tindex[$num_int];
$oid_perf_inerr[$num_int]= $in_error_table . $tindex[$num_int];
#out
$oid_perf_outucast[$num_int]= $out_ucast_table . $tindex[$num_int];
$oid_perf_outnucast[$num_int]= $out_nucast_table . $tindex[$num_int];
$oid_perf_outerr[$num_int]= $out_error_table . $tindex[$num_int];
#speed
$oid_speed[$num_int]=$speed_table . $tindex[$num_int];
$num_int++;
}
}

# No interface found -> error
if ( $num_int == 0 ) { print "ERROR : Unknown interface $o_descr\n" ; exit $ERRORS{"UNKNOWN"};}

($result,$resultf)=(undef,undef);
# Get the requested oid values
$result = $session->get_request(
Varbindlist => \@oids
);
# Get the requested performance data
@oid_perf=(@oid_perf_inucast,@oid_perf_innucast,@oid_perf_inerr,@oid_perf_outucast,@oid_perf_outnucast,@oid_perf_outerr,@oid_speed);
$resultf = $session->get_request(
Varbindlist => \@oid_perf
);
if (!defined($resultf)) { printf("ERROR: Statistics table 1: %s.\n", $session->error); $session->close;
exit $ERRORS{"UNKNOWN"};
}

if (!defined($result)) { printf("ERROR: Status table 2: %s.\n", $session->error); $session->close;
exit $ERRORS{"UNKNOWN"};
}

@fin_res = undef;
$ipkts = undef;
$opkts = undef;
$in_err_rat = undef;
$out_err_rat = undef;
for (my $i=0;$i < $num_int; $i++) {
@temp_res = undef;
$ipkts = int($$resultf{$oid_perf_inucast[$i]}+$$resultf{$oid_perf_innucast[$i]});
$opkts = int($$resultf{$oid_perf_outucast[$i]}+$$resultf{$oid_perf_innucast[$i]});
if($$result{$oids[$i]}==1){
$stat="UP";
}
elsif($$result{$oids[$i]}==2){
$stat="down";
}
else{
$stat="testing";
}

if($ipkts != 0 || $opkts != 0){
$in_err_rat = $$resultf{$oid_perf_inerr[$i]}/$ipkts*100;
$out_err_rat = $$resultf{$oid_perf_outerr[$i]}/$opkts*100;
}
else{
$in_err_rat=0;
$out_err_rat=0;
}

if($$result{$oids[$i]}!= 1){
$status=3;
append($descr[$i] . ": " . "speed (" . $$resultf{$oid_speed[$i]} .
"); operational status (" . $stat .
"); received/sent packets/error ratio (" . $in_err_rat . "%/" . $out_err_rat . "%)!!!");
}
else{
append($descr[$i] . ": " . "speed (" . $$resultf{$oid_speed[$i]} .
"); operational status (" . $stat .
"); received/sent packets/error ratio (" . $in_err_rat . "%/" . $out_err_rat . "%)!!!");
}

}
}

sub test{
my %bla = %{$resultf};
my $key;

foreach $key (keys %bla){
print ($resultat{$key}, "==>", $key," : ",$bla{$key},"\n");
}
}

sub append{
my $appendstring = @_[0];
$res= "$res$appendstring";
}

####################################################################
# help and usage information #
####################################################################

sub usage {
print << "USAGE";
--------------------------------------------------------------------
$script v$script_version

$script_description

Usage: $script -H <hostname> -c <community> [...]
Options: -H Hostname or IP address
-p Port (default: 161)
-C Community (default is public)
-w Warning threshold (as %)
-c Critical threshold (as %)

--------------------------------------------------------------------
Copyright (C) 2003-2011 Opsera Limited. All rights reserved

This Nagios plugin comes with absolutely NO WARRANTY either implied
or explicit. This program is licensed under the terms of the GNU
General Public License (check source code for details)
--------------------------------------------------------------------

USAGE
exit 1;
}

Last edited: 2011-05-20 16:31:45 +0200 (CEST)

View full thread Ausgabe fügt immer 0& am Anfang an