diff options
author | Subhendu Ghosh <sghosh@users.sourceforge.net> | 2002-05-08 04:58:22 +0000 |
---|---|---|
committer | Subhendu Ghosh <sghosh@users.sourceforge.net> | 2002-05-08 04:58:22 +0000 |
commit | c2be722c8592d416570c1a2aff4778042ebb5422 (patch) | |
tree | 819d3d89e017de5a94b3a95bb076083d3a9cd974 /plugins-scripts/check_ifstatus.pl | |
parent | d95644521ccd544cbb9a8bfeda927a6b9f6fb01a (diff) | |
download | monitoring-plugins-c2be722c8592d416570c1a2aff4778042ebb5422.tar.gz |
moved updated check_ifstatus to standard plugin
git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@23 f882894a-f735-0410-b71e-b25c423dba1c
Diffstat (limited to 'plugins-scripts/check_ifstatus.pl')
-rwxr-xr-x | plugins-scripts/check_ifstatus.pl | 250 |
1 files changed, 250 insertions, 0 deletions
diff --git a/plugins-scripts/check_ifstatus.pl b/plugins-scripts/check_ifstatus.pl new file mode 100755 index 00000000..f54008f2 --- /dev/null +++ b/plugins-scripts/check_ifstatus.pl | |||
@@ -0,0 +1,250 @@ | |||
1 | #!/usr/local/bin/perl -w | ||
2 | # | ||
3 | # check_ifstatus.pl - nagios plugin | ||
4 | # | ||
5 | # | ||
6 | # Copyright (C) 2000 Christoph Kron | ||
7 | # Modified 5/2002 to conform to updated Nagios Plugin Guidelines (S. Ghosh) | ||
8 | # | ||
9 | # This program is free software; you can redistribute it and/or | ||
10 | # modify it under the terms of the GNU General Public License | ||
11 | # as published by the Free Software Foundation; either version 2 | ||
12 | # of the License, or (at your option) any later version. | ||
13 | # | ||
14 | # This program is distributed in the hope that it will be useful, | ||
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
17 | # GNU General Public License for more details. | ||
18 | # | ||
19 | # You should have received a copy of the GNU General Public License | ||
20 | # along with this program; if not, write to the Free Software | ||
21 | # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | ||
22 | # | ||
23 | # | ||
24 | # Report bugs to: ck@zet.net, nagiosplug-help@lists.sf.net | ||
25 | # | ||
26 | # 11.01.2000 Version 1.0 | ||
27 | # | ||
28 | # $Id$ | ||
29 | |||
30 | use POSIX; | ||
31 | use strict; | ||
32 | use lib utils.pm ; | ||
33 | use utils qw($TIMEOUT %ERRORS &print_revision &support); | ||
34 | |||
35 | use Net::SNMP; | ||
36 | use Getopt::Long; | ||
37 | Getopt::Long::Configure('bundling'); | ||
38 | |||
39 | my $PROGNAME = "check_ifstatus"; | ||
40 | |||
41 | |||
42 | my $status; | ||
43 | my %ifOperStatus = ('1','up', | ||
44 | '2','down', | ||
45 | '3','testing', | ||
46 | '4','unknown', | ||
47 | '5','dormant', | ||
48 | '6','notPresent'); | ||
49 | |||
50 | my $state = "UNKNOWN"; | ||
51 | my $answer = ""; | ||
52 | my $snmpkey=0; | ||
53 | my $snmpoid=0; | ||
54 | my $key=0; | ||
55 | my $community = "public"; | ||
56 | my $port = 161; | ||
57 | my @snmpoids; | ||
58 | my $snmpIfAdminStatus = '1.3.6.1.2.1.2.2.1.7'; | ||
59 | my $snmpIfDescr = '1.3.6.1.2.1.2.2.1.2'; | ||
60 | my $snmpIfOperStatus = '1.3.6.1.2.1.2.2.1.8'; | ||
61 | my $snmpIfName = '1.3.6.1.2.1.31.1.1.1.1'; | ||
62 | my $snmpIfAlias = '1.3.6.1.2.1.31.1.1.1.18'; | ||
63 | my $snmpLocIfDescr = '1.3.6.1.4.1.9.2.2.1.1.28'; | ||
64 | my $hostname; | ||
65 | my $session; | ||
66 | my $error; | ||
67 | my $response; | ||
68 | my %ifStatus; | ||
69 | my $ifup =0 ; | ||
70 | my $ifdown =0; | ||
71 | my $ifdormant = 0; | ||
72 | my $ifmessage = ""; | ||
73 | my $snmp_version = 1; | ||
74 | my $ifXTable; | ||
75 | my $opt_h ; | ||
76 | my $opt_V ; | ||
77 | |||
78 | |||
79 | |||
80 | |||
81 | |||
82 | # Just in case of problems, let's not hang Nagios | ||
83 | $SIG{'ALRM'} = sub { | ||
84 | print ("ERROR: No snmp response from $hostname (alarm timeout)\n"); | ||
85 | exit $ERRORS{"UNKNOWN"}; | ||
86 | }; | ||
87 | alarm($TIMEOUT); | ||
88 | |||
89 | |||
90 | |||
91 | #Option checking | ||
92 | $status = GetOptions( | ||
93 | "V" => \$opt_V, "version" => \$opt_V, | ||
94 | "h" => \$opt_h, "help" => \$opt_h, | ||
95 | "v=i" => \$snmp_version, "snmp_version=i" => \$snmp_version, | ||
96 | "C=s" =>\$community,"community=s" => \$community, | ||
97 | "p=i" =>\$port, "port=i" => \$port, | ||
98 | "H=s" => \$hostname, "hostname=s" => \$hostname, | ||
99 | "I" => \$ifXTable, "ifmib" => \$ifXTable ); | ||
100 | |||
101 | if ($status == 0) | ||
102 | { | ||
103 | print_help() ; | ||
104 | exit $ERRORS{'OK'}; | ||
105 | } | ||
106 | |||
107 | |||
108 | if ($opt_V) { | ||
109 | print_revision($PROGNAME,'$Revision$ '); | ||
110 | exit $ERRORS{'OK'}; | ||
111 | } | ||
112 | |||
113 | if ($opt_h) { | ||
114 | print_help(); | ||
115 | exit $ERRORS{'OK'}; | ||
116 | } | ||
117 | |||
118 | if (! utils::is_hostname($hostname)){ | ||
119 | usage(); | ||
120 | exit $ERRORS{"UNKNOWN"}; | ||
121 | } | ||
122 | |||
123 | if ( ! $snmp_version ) { | ||
124 | $snmp_version =1 ; | ||
125 | }else{ | ||
126 | if ( $snmp_version =~ /[12]/ ) { | ||
127 | |||
128 | ($session, $error) = Net::SNMP->session( | ||
129 | -hostname => $hostname, | ||
130 | -community => $community, | ||
131 | -port => $port, | ||
132 | -version => $snmp_version | ||
133 | ); | ||
134 | |||
135 | if (!defined($session)) { | ||
136 | $state='UNKNOWN'; | ||
137 | $answer=$error; | ||
138 | print ("$state: $answer"); | ||
139 | exit $ERRORS{$state}; | ||
140 | } | ||
141 | |||
142 | |||
143 | }elsif ( $snmp_version =~ /3/ ) { | ||
144 | $state='UNKNOWN'; | ||
145 | print ("$state: No support for SNMP v3 yet\n"); | ||
146 | exit $ERRORS{$state}; | ||
147 | }else{ | ||
148 | $state='UNKNOWN'; | ||
149 | print ("$state: No support for SNMP v$snmp_version yet\n"); | ||
150 | exit $ERRORS{$state}; | ||
151 | } | ||
152 | } | ||
153 | |||
154 | |||
155 | |||
156 | push(@snmpoids,$snmpIfOperStatus); | ||
157 | push(@snmpoids,$snmpIfAdminStatus); | ||
158 | push(@snmpoids,$snmpIfDescr); | ||
159 | push(@snmpoids,$snmpIfName) if ( defined $ifXTable); | ||
160 | |||
161 | |||
162 | |||
163 | foreach $snmpoid (@snmpoids) { | ||
164 | |||
165 | if (!defined($response = $session->get_table($snmpoid))) { | ||
166 | $answer=$session->error; | ||
167 | $session->close; | ||
168 | $state = 'CRITICAL'; | ||
169 | print ("$state: $answer for $snmpoid with snmp version $snmp_version\n"); | ||
170 | exit $ERRORS{$state}; | ||
171 | } | ||
172 | |||
173 | foreach $snmpkey (keys %{$response}) { | ||
174 | $snmpkey =~ /.*\.(\d+)$/; | ||
175 | $key = $1; | ||
176 | $ifStatus{$key}{$snmpoid} = $response->{$snmpkey}; | ||
177 | } | ||
178 | } | ||
179 | |||
180 | |||
181 | $session->close; | ||
182 | |||
183 | foreach $key (keys %ifStatus) { | ||
184 | |||
185 | # check only if interface is administratively up | ||
186 | if ($ifStatus{$key}{$snmpIfAdminStatus} == 1 ) { | ||
187 | if ($ifStatus{$key}{$snmpIfOperStatus} == 1 ) { $ifup++ ;} | ||
188 | if ($ifStatus{$key}{$snmpIfOperStatus} == 2 ) { | ||
189 | $ifdown++ ; | ||
190 | $ifmessage .= sprintf("%s: down -> %s<BR>", | ||
191 | $ifStatus{$key}{$snmpIfDescr}, | ||
192 | $ifStatus{$key}{$snmpIfName}); | ||
193 | |||
194 | } | ||
195 | if ($ifStatus{$key}{$snmpIfOperStatus} == 5 ) { $ifdormant++ ;} | ||
196 | } | ||
197 | } | ||
198 | |||
199 | |||
200 | if ($ifdown > 0) { | ||
201 | $state = 'CRITICAL'; | ||
202 | $answer = sprintf("host '%s', interfaces up: %d, down: %d, dormant: %d<BR>", | ||
203 | $hostname, | ||
204 | $ifup, | ||
205 | $ifdown, | ||
206 | $ifdormant); | ||
207 | $answer = $answer . $ifmessage . "\n"; | ||
208 | } | ||
209 | else { | ||
210 | $state = 'OK'; | ||
211 | $answer = sprintf("host '%s', interfaces up: %d, down: %d, dormant: %d\n", | ||
212 | $hostname, | ||
213 | $ifup, | ||
214 | $ifdown, | ||
215 | $ifdormant); | ||
216 | } | ||
217 | |||
218 | print ("$state: $answer"); | ||
219 | exit $ERRORS{$state}; | ||
220 | |||
221 | |||
222 | sub usage { | ||
223 | printf "\nMissing arguments!\n"; | ||
224 | printf "\n"; | ||
225 | printf "check_ifstatus -C <READCOMMUNITY> -p <PORT> -H <HOSTNAME>\n"; | ||
226 | printf "Copyright (C) 2000 Christoph Kron\n"; | ||
227 | printf "Updates 5/2002 Subhendu Ghosh\n"; | ||
228 | printf "\n\n"; | ||
229 | support(); | ||
230 | exit $ERRORS{"UNKNOWN"}; | ||
231 | } | ||
232 | |||
233 | sub print_help { | ||
234 | printf "check_ifstatus plugin for Nagios monitors operational \n"; | ||
235 | printf "status of each network interface on the target host\n"; | ||
236 | printf "\nUsage:\n"; | ||
237 | printf " -H (--hostname) Hostname to query - (required)\n"; | ||
238 | printf " -C (--community) SNMP read community (defaults to public,\n"; | ||
239 | printf " used with SNMP v1 and v2c\n"; | ||
240 | printf " -v (--snmp_version) 1 for SNMP v1 (default)\n"; | ||
241 | printf " 2 for SNMP v2c\n"; | ||
242 | printf " SNMP v2c will use get_bulk for less overhead\n"; | ||
243 | printf " -p (--port) SNMP port (default 161)\n"; | ||
244 | printf " -I (--ifmib) Agent supports IFMIB ifXTable. Do not use if\n"; | ||
245 | printf " you don't know what this is.\n"; | ||
246 | printf " -V (--version) Plugin version\n"; | ||
247 | printf " -h (--help) usage help \n\n"; | ||
248 | print_revision($PROGNAME, '$Revision$'); | ||
249 | |||
250 | } | ||