/[nagios-plugins-perl]/trunk/plugins/check_snmp_nsextend.pl
ViewVC logotype

Contents of /trunk/plugins/check_snmp_nsextend.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 208 - (show annotations) (download)
Thu Apr 11 07:11:34 2019 UTC (19 months, 2 weeks ago) by racvision
File MIME type: text/plain
File size: 4837 byte(s)
add check_snmp_nsextend.pl
1 #!/usr/bin/perl -w
2 #
3 # Copyright (c) 2010 Hervé Donati <herve.donati@ac-caen.fr>
4 # Copyright (c) 2019 Stéphane URBANOVSKI - MEN / CNS / CRT Supervision
5 #
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty
13 # of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # you should have received a copy of the GNU General Public License
17 # along with this program (or with Nagios); if not, write to the
18 # Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 # Boston, MA 02111-1307, USA
20 #
21 # $Id: $
22 #
23 #
24
25 use strict;
26 use warnings;
27 use Net::SNMP;
28 use Nagios::Plugin;
29 use Locale::gettext;
30 use POSIX qw(setlocale);
31 use Data::Dumper;
32
33
34 # 1.3.6.1.4.1.8072.1.3.2.2.1.2 nsExtendCommand
35 # 1.3.6.1.4.1.8072.1.3.2.3.1.1 nsExtendOutput1Line
36
37 my $DEBUG = 0;
38 my $vsnmp = 2;
39 my $timeout = 2;
40 my $base_oid = '1.3.6.1.4.1.8072.1.3.2.3.1';
41 my $version = '1.0';
42
43 my $status = OK;
44 my $message = 'Invalid status';
45
46
47 my %nsExtendOutput1Entry = (
48 1 => 'nsExtendOutput1Line',
49 2 => 'nsExtendOutputFull',
50 3 => 'nsExtendOutNumLines',
51 4 => 'nsExtendResult'
52 );
53
54
55
56 my $np = Nagios::Plugin->new(
57 version => $version,
58 blurb => 'Plugin that use nsExtendObject',
59 usage => "Usage: %s -H|--host <host> [-p|--port=<snmp port>] [-c|--community=<snmp community>]",
60 timeout => 10
61 );
62
63 $np->add_arg (
64 spec => 'host|H=s',
65 help => 'Name of the host to test',
66 required => 1,
67 );
68
69 $np->add_arg (
70 spec => 'port|p=i',
71 help => 'SNMP port',
72 default => 161,
73 );
74
75 $np->add_arg (
76 spec => 'community|C=s',
77 help => 'SNMP community',
78 default => 'public',
79 );
80
81 $np->add_arg (
82 spec => 'w=f',
83 help => _gt('Warning threshold'),
84 required => 0,
85 );
86 $np->add_arg (
87 spec => 'c=f',
88 help => _gt('Critical threshold'),
89 required => 0,
90 );
91
92 $np->add_arg (
93 spec => 'script|s=s@',
94 help => 'Only use those scripts name (use multiple time id needed)',
95 );
96
97 $np->add_arg (
98 spec => 'debug|d',
99 help => _gt('Debug level'),
100 );
101
102 $np->getopts;
103 my $host = $np->opts->get('host');
104 my $port = $np->opts->get('port');
105 my $comm = $np->opts->get('community');
106
107 my $scriptFilters = $np->opts->get('script');
108
109 my $warn = $np->opts->get('w');
110 my $crit = $np->opts->get('c');
111
112 $DEBUG = $np->opts->get('debug');
113
114 my ( $session, $error ) = Net::SNMP->session(
115 -hostname => $host,
116 -community => $comm,
117 -timeout => $timeout,
118 -version => $vsnmp,
119 -port => $port,
120 );
121
122 if ( !defined $session ) {
123 $np->nagios_exit( UNKNOWN, "ERROR: %s." );
124 }
125
126 my $reponse = $session->get_table( $base_oid );
127 if ( !defined $reponse ) {
128 $np->nagios_exit( UNKNOWN, "Unable to get SNMP values" );
129 }
130 if ( !defined($scriptFilters) ) {
131 $scriptFilters = ['*'];
132 }
133 # print Dumper(\$scriptFilters);
134
135 my $base_oid_re = $base_oid;
136 $base_oid_re =~ s/\./\\\./;
137
138 my %allValues = ();
139
140
141 foreach my $oid ( keys %{$reponse} ) {
142
143 my $v = $reponse->{$oid};
144
145 if ( $oid =~ /^$base_oid_re\.(\d+)\.(.*)/ ) {
146
147 my $check = '';
148
149 my $ent = defined($nsExtendOutput1Entry{$1}) ? $nsExtendOutput1Entry{$1}: $1;
150 foreach my $x ( split( /\./,$2) ) {
151 if ( $x >= 65 && $x <= 122 ) {
152 $check .= pack('c',$x);
153 }
154 }
155 logD( " $ent ($check) => $v");
156
157 foreach my $sf (@$scriptFilters) {
158 if ( $sf eq '*' || $check eq $sf ) {
159 $allValues{$check}{$ent} = $v;
160 }
161 }
162 } else {
163 logW( "Unmatched OID found : $oid");
164 }
165 }
166 # print Dumper(\%allValues);
167
168
169 foreach my $check ( keys %allValues ) {
170
171 if ( defined($allValues{$check}{'nsExtendResult'}) ) {
172 if ( $allValues{$check}{'nsExtendResult'} == 0 ) {
173 if ( $allValues{$check}{'nsExtendOutput1Line'} =~ /^(\d+)$/ ) {
174 my $tmpStatus = $np->check_threshold(
175 'check' => $1,
176 'warning' => $warn,
177 'critical' => $crit,
178 );
179 $np->add_perfdata(
180 'label' => $check,
181 'value' => $1,
182 'threshold' => $np->threshold()
183 );
184 $np->add_message($tmpStatus, sprintf(_gt("%s : %s"), $check , $allValues{$check}{'nsExtendOutput1Line'}) );
185 } else {
186 $np->add_message(OK, sprintf(_gt("%s : %s"), $check , $allValues{$check}{'nsExtendOutput1Line'}) );
187 }
188 } else {
189 $np->add_message(CRITICAL, sprintf(_gt("Script (%s) failled (%d) : %s"), $check , $allValues{$check}{'nsExtendResult'}, $allValues{$check}{'nsExtendOutput1Line'}) );
190 }
191 }
192 }
193
194
195 ($status, $message) = $np->check_messages('join' => ' / ');
196
197 $np->nagios_exit( $status, $message );
198
199
200 sub logD {
201 print STDERR 'DEBUG: '.$_[0]."\n" if ($DEBUG);
202 }
203 sub logW {
204 print STDERR 'WARNING: '.$_[0]."\n" if ($DEBUG);
205 }
206 # Gettext wrapper
207 sub _gt {
208 return gettext($_[0]);
209 }

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.8