summaryrefslogtreecommitdiffstats
path: root/contrib/check_ifstatus.pl
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/check_ifstatus.pl')
-rw-r--r--contrib/check_ifstatus.pl178
1 files changed, 178 insertions, 0 deletions
diff --git a/contrib/check_ifstatus.pl b/contrib/check_ifstatus.pl
new file mode 100644
index 0000000..a7ab39e
--- /dev/null
+++ b/contrib/check_ifstatus.pl
@@ -0,0 +1,178 @@
1#!/usr/bin/perl -w
2#
3# check_ifstatus.pl - nagios plugin
4#
5#
6# Copyright (C) 2000 Christoph Kron
7#
8# This program is free software; you can redistribute it and/or
9# modify it under the terms of the GNU General Public License
10# as published by the Free Software Foundation; either version 2
11# of the License, or (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with this program; if not, write to the Free Software
20# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21#
22#
23# Report bugs to: ck@zet.net
24#
25# 11.01.2000 Version 1.0
26
27use strict;
28
29use Net::SNMP;
30use Getopt::Long;
31&Getopt::Long::config('auto_abbrev');
32
33
34my $status;
35my $TIMEOUT = 1500;
36
37my %ERRORS = ('UNKNOWN' , '-1',
38 'OK' , '0',
39 'WARNING', '1',
40 'CRITICAL', '2');
41
42my %ifOperStatus = ('1','up',
43 '2','down',
44 '3','testing',
45 '4','unknown',
46 '5','dormant',
47 '6','notPresent');
48
49my $state = "UNKNOWN";
50my $answer = "";
51my $snmpkey;
52my $snmpoid;
53my $key;
54my $community = "public";
55my $port = 161;
56my @snmpoids;
57my $snmpIfAdminStatus = '1.3.6.1.2.1.2.2.1.7';
58my $snmpIfDescr = '1.3.6.1.2.1.2.2.1.2';
59my $snmpIfOperStatus = '1.3.6.1.2.1.2.2.1.8';
60my $snmpLocIfDescr = '1.3.6.1.4.1.9.2.2.1.1.28';
61my $hostname;
62my $session;
63my $error;
64my $response;
65my %ifStatus;
66my $ifup =0 ;
67my $ifdown =0;
68my $ifdormant = 0;
69my $ifmessage;
70
71sub usage {
72 printf "\nMissing arguments!\n";
73 printf "\n";
74 printf "Perl Check IfStatus plugin for Nagios\n";
75 printf "monitors operational status of each interface\n";
76 printf "usage: \n";
77 printf "check_ifstatus.pl -c <READCOMMUNITY> -p <PORT> <HOSTNAME>\n";
78 printf "Copyright (C) 2000 Christoph Kron\n";
79 printf "check_ifstatus.pl comes with ABSOLUTELY NO WARRANTY\n";
80 printf "This programm is licensed under the terms of the ";
81 printf "GNU General Public License\n(check source code for details)\n";
82 printf "\n\n";
83 exit $ERRORS{"UNKNOWN"};
84}
85
86# Just in case of problems, let's not hang Nagios
87$SIG{'ALRM'} = sub {
88 print ("ERROR: No snmp response from $hostname (alarm)\n");
89 exit $ERRORS{"UNKNOWN"};
90};
91alarm($TIMEOUT);
92
93
94$status = GetOptions("community=s",\$community,
95 "port=i",\$port);
96if ($status == 0)
97{
98 &usage;
99}
100
101 #shift;
102 $hostname = shift || &usage;
103
104
105
106 push(@snmpoids,$snmpIfOperStatus);
107 push(@snmpoids,$snmpLocIfDescr);
108 push(@snmpoids,$snmpIfAdminStatus);
109 push(@snmpoids,$snmpIfDescr);
110
111foreach $snmpoid (@snmpoids) {
112
113 ($session, $error) = Net::SNMP->session(
114 -hostname => $hostname,
115 -community => $community,
116 -port => $port
117 );
118
119 if (!defined($session)) {
120 $state='UNKNOWN';
121 $answer=$error;
122 print ("$state: $answer");
123 exit $ERRORS{$state};
124 }
125
126 if (!defined($response = $session->get_table($snmpoid))) {
127 $answer=$session->error;
128 $session->close;
129 $state = 'CRITICAL';
130 print ("$state: $answer,$community,$snmpkey");
131 exit $ERRORS{$state};
132 }
133
134 foreach $snmpkey (keys %{$response}) {
135 $snmpkey =~ /.*\.(\d+)$/;
136 $key = $1;
137 $ifStatus{$key}{$snmpoid} = $response->{$snmpkey};
138 }
139 $session->close;
140}
141
142 foreach $key (keys %ifStatus) {
143 # check only if interface is administratively up
144 if ($ifStatus{$key}{$snmpIfAdminStatus} == 1 ) {
145 if ($ifStatus{$key}{$snmpIfOperStatus} == 1 ) { $ifup++ ;}
146 if ($ifStatus{$key}{$snmpIfOperStatus} == 2 ) {
147 $ifdown++ ;
148 $ifmessage .= sprintf("%s: down -> %s<BR>",
149 $ifStatus{$key}{$snmpIfDescr},
150 $ifStatus{$key}{$snmpLocIfDescr});
151
152 }
153 if ($ifStatus{$key}{$snmpIfOperStatus} == 5 ) { $ifdormant++ ;}
154 }
155 }
156
157
158 if ($ifdown > 0) {
159 $state = 'CRITICAL';
160 $answer = sprintf("host '%s', interfaces up: %d, down: %d, dormant: %d<BR>",
161 $hostname,
162 $ifup,
163 $ifdown,
164 $ifdormant);
165 $answer = $answer . $ifmessage . "\n";
166 }
167 else {
168 $state = 'OK';
169 $answer = sprintf("host '%s', interfaces up: %d, down: %d, dormant: %d\n",
170 $hostname,
171 $ifup,
172 $ifdown,
173 $ifdormant);
174 }
175
176print ("$state: $answer");
177exit $ERRORS{$state};
178