1 #!/usr/bin/perl -w
2 #
3 # check_dlswcircuit.pl - nagios plugin
4 #
5 # Checks if a Cisco Dlsw circuit is connected.
6 #
7 #
8 # Copyright (C) 2000 Carsten Foss & Christoph Kron
9 #
10 # Basically this is an adapted version of Christoph Kron's (ck@zet.net) check_ifoperstatus.pl plugin.
11 # most of the thanks should go to him.
12 #
13 # This program is free software; you can redistribute it and/or
14 # modify it under the terms of the GNU General Public License
15 # as published by the Free Software Foundation; either version 2
16 # of the License, or (at your option) any later version.
17 #
18 # This program is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 # GNU General Public License for more details.
22 #
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 #
27 # Arguments : -s <SourceMac> -d <DestMac> -c <READCOMMUNITY> -p <PORT> <HOSTNAME or IP-Addr>
28 # -
29 # Source & Dest Mac/Sap arguments must be given in Hex as this example : 40.00.01.37.45.01.ss (Where ss is the sap)
30 #
31 # Sample command line : check_dlswcircuit.pl -s 40.00.01.37.45.01.04 -d 40.00.02.37.45.02.04 -c secret 1.2.3.4
32 #
33 # Sample host.cfg entry :
34 #service[Dlsw-xx]=NCP1-NCP2;0;24x7;3;5;1;router-admins;240;24x7;1;1;0;;check_dlswcircuit!-s 40.00.01.37.45.01.04!-d 40.00..01.37.45.02.04!-c secret!1.2.3.4
35 # remember to add the service to commands.cfg , something like this:
36 # command[check_dlswcircuit]=$USER1$/check_dlswcircuit.pl $ARG1$ $ARG2$ $ARG3$ $ARG4$ $ARG5$
37 #
38 # Report bugs to: cfo@dmdata.dk
39 #
40 # 11.03.2000 Version 1.0
42 use strict;
44 use Net::SNMP;
45 use Getopt::Long;
46 &Getopt::Long::config('auto_abbrev');
49 my $status;
50 my $TIMEOUT = 15;
52 my %ERRORS = ('UNKNOWN' , '-1',
53 'OK' , '0',
54 'WARNING', '1',
55 'CRITICAL', '2');
57 my %dlswCircuitStatus = (
58 '1','disconnected',
59 '2','circuitStart',
60 '3','resolvePending',
61 '4','circuitPending',
62 '5','circuitEstablished',
63 '6','connectPending',
64 '7','contactPending',
65 '8','connected',
66 '9','disconnectPending',
67 '10','haltPending',
68 '11','haltPendingNoack',
69 '13','circuitRestart',
70 '14','restartPending');
72 my $state = "UNKNOWN";
73 my $answer = "";
74 my $smac = "";
75 my $dmac = "";
76 my $community = "public";
77 my $port = 161;
78 #Dlsw Circuit Oid enterprises.9.10.9.1.5.2.1.17.6.0.96.148.47.230.166.4.6.64.0.1.55.69.2.4 = 8
79 my $enterpriseOid = "1.3.6.1.4.1";
80 my $ciscoDlswCircuitOid = ".9.10.9.1.5.2.1.17.";
81 my $unknownOid = "6.";
82 my $smacOid = "";
83 my $dmacOid = "";
84 my $tmpOid = "";
85 my @tmparg;
86 my $snmpoid;
87 my @snmpoids;
88 my $hostname;
89 my $session;
90 my $error;
91 my $response;
92 my $p = "";
93 my $q = "";
95 sub usage {
96 printf "\nMissing arguments!\n";
97 printf "\n";
98 printf "Perl Check Cisco Dlsw Circuit State plugin for Nagios\n";
99 printf "checks operational status of specified DLSW Circuit\n";
100 printf "usage: \n";
101 printf "check_dlswcircuit.pl -s <SourceMac> -d <DestMac> -c <READCOMMUNITY> -p <PORT> <HOSTNAME>";
102 printf "\nCopyright (C) 2000 Carsten Foss\n";
103 printf "check_dlswcircuit.pl comes with ABSOLUTELY NO WARRANTY\n";
104 printf "This programm is licensed under the terms of the ";
105 printf "GNU General Public License\n(check source code for details)\n";
106 printf "\n\n";
107 exit $ERRORS{"UNKNOWN"};
108 }
110 # Just in case of problems, let's not hang Nagios
111 $SIG{'ALRM'} = sub {
112 print ("ERROR: No snmp response from $hostname (alarm)\n");
113 exit $ERRORS{"UNKNOWN"};
114 };
115 alarm($TIMEOUT);
118 $status = GetOptions("sourcemac=s",\$smac,"destmac=s",\$dmac,
119 "community=s",\$community,
120 "port=i",\$port);
121 if ($status == 0)
122 {
123 &usage;
124 }
126 #
127 #Convert Source Mac & Sap
128 #
129 @tmparg = split(/\./,$smac);
130 #print "-$smac-\n";
131 #print "@tmparg\n";
132 #print "$#tmparg\n";
133 if($#tmparg != 6)
134 {
135 print "SourceMac/Sap format $smac not valid\n";
136 &usage;
137 }
138 while($p = shift @tmparg)
139 {
140 $q = hex($p);
141 $smacOid = $smacOid.$q;
142 $smacOid = $smacOid.'.';
143 }
145 #print "@tmparg1\n";
146 #print "$smacOid\n";
148 #
149 #Convert Dest Mac & Sap
150 #
151 @tmparg = split(/\./,$dmac);
152 #print "-$dmac-\n";
153 #print "@tmparg\n";
154 #print "$#tmparg\n";
155 if($#tmparg != 6)
156 {
157 print "DestMac/Sap format $dmac not valid\n";
158 &usage;
159 }
161 while($p = shift @tmparg)
162 {
163 $q = hex($p);
164 $dmacOid = $dmacOid.$q;
165 $dmacOid = $dmacOid.'.';
166 }
167 # Remove Trailing Dot
168 $dmacOid = substr($dmacOid,0,length($dmacOid)-1);
171 #print "@tmparg1\n";
172 #print "$dmacOid\n";
173 #Build the Dlsw Oic to use
174 $snmpoid = $enterpriseOid.$ciscoDlswCircuitOid.$unknownOid.$smacOid.$unknownOid.$dmacOid ;
175 #print "$snmpoid\n";
177 #shift;
178 $hostname = shift || &usage;
180 ($session, $error) = Net::SNMP->session(
181 -hostname => $hostname,
182 -community => $community,
183 -port => $port
184 );
186 if (!defined($session)) {
187 $state='UNKNOWN';
188 $answer=$error;
189 print ("$state: $answer");
190 exit $ERRORS{$state};
191 }
193 push(@snmpoids,$snmpoid);
194 #push(@snmpoids,$snmpLocIfDescr);
196 if (!defined($response = $session->get_request(@snmpoids))) {
197 $answer=$session->error;
198 $session->close;
199 $state = 'CRITICAL';
200 print ("$state: $answer,$community,$smac - $dmac");
201 exit $ERRORS{$state};
202 }
204 $answer = sprintf("dlsw circuit %s - %s at host '%s',is %s\n",
205 $smac,
206 $dmac,
207 $hostname,
208 $dlswCircuitStatus{$response->{$snmpoid}}
209 );
211 $session->close;
213 if ( $response->{$snmpoid} == 8 ) {
214 $state = 'OK';
215 }
216 else {
217 $state = 'CRITICAL';
218 }
220 print ("$state: $answer");
221 exit $ERRORS{$state};