aboutsummaryrefslogtreecommitdiff
path: root/contrib/check_dlswcircuit.pl
blob: f6ef9311518fd8baf7fb1bbd510e004f61182158 (plain)
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
#!/usr/bin/perl -w
#
# check_dlswcircuit.pl - nagios plugin 
# 
# Checks if a Cisco Dlsw circuit is connected.
#
#
# Copyright (C) 2000 Carsten Foss & Christoph Kron
# 
# Basically this is an adapted version of Christoph Kron's (ck@zet.net) check_ifoperstatus.pl plugin.
# most of the thanks should go to him.
#
# This program 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.
#
# This program 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 this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
# Arguments : -s <SourceMac> -d <DestMac> -c <READCOMMUNITY> -p <PORT> <HOSTNAME or IP-Addr>
#		-
# 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) 
#
# 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
#
# Sample host.cfg entry :
#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
# remember to add the service to commands.cfg , something like this:
# command[check_dlswcircuit]=$USER1$/check_dlswcircuit.pl $ARG1$ $ARG2$ $ARG3$ $ARG4$ $ARG5$
#
# Report bugs to: cfo@dmdata.dk
#
# 11.03.2000 Version 1.0

use strict;

use Net::SNMP;
use Getopt::Long;
&Getopt::Long::config('auto_abbrev');


my $status;
my $TIMEOUT = 15;

my %ERRORS = ('UNKNOWN' , '-1',
              'OK' , '0',
              'WARNING', '1',
              'CRITICAL', '2');

my %dlswCircuitStatus = (
	  '1','disconnected',
        '2','circuitStart',
        '3','resolvePending',
        '4','circuitPending',
        '5','circuitEstablished',
        '6','connectPending',
        '7','contactPending',
        '8','connected',
        '9','disconnectPending',
        '10','haltPending',
        '11','haltPendingNoack',
        '13','circuitRestart',
        '14','restartPending');

my $state = "UNKNOWN";
my $answer = "";
my $smac = "";
my $dmac = "";
my $community = "public";
my $port = 161;
#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
my $enterpriseOid = "1.3.6.1.4.1";
my $ciscoDlswCircuitOid = ".9.10.9.1.5.2.1.17.";
my $unknownOid = "6.";
my $smacOid = "";
my $dmacOid = "";
my $tmpOid = "";
my @tmparg;
my $snmpoid;
my @snmpoids;
my $hostname;
my $session;
my $error;
my $response;
my $p = "";
my $q = "";

sub usage {
  printf "\nMissing arguments!\n";
  printf "\n";
  printf "Perl Check Cisco Dlsw Circuit State plugin for Nagios\n";
  printf  "checks operational status of specified DLSW Circuit\n";
  printf "usage: \n";
  printf "check_dlswcircuit.pl -s <SourceMac> -d <DestMac> -c <READCOMMUNITY> -p <PORT> <HOSTNAME>";
  printf "\nCopyright (C) 2000 Carsten Foss\n";
  printf "check_dlswcircuit.pl comes with ABSOLUTELY NO WARRANTY\n";
  printf "This programm is licensed under the terms of the ";
  printf "GNU General Public License\n(check source code for details)\n";
  printf "\n\n";
  exit $ERRORS{"UNKNOWN"};
}

# Just in case of problems, let's not hang Nagios
$SIG{'ALRM'} = sub {
     print ("ERROR: No snmp response from $hostname (alarm)\n");
     exit $ERRORS{"UNKNOWN"};
};
alarm($TIMEOUT);


$status = GetOptions("sourcemac=s",\$smac,"destmac=s",\$dmac,
                     "community=s",\$community,
                     "port=i",\$port);
if ($status == 0)
{
        &usage;
}

#
#Convert Source Mac & Sap
#  
   @tmparg = split(/\./,$smac);
   #print "-$smac-\n";
   #print "@tmparg\n";
   #print "$#tmparg\n";
   if($#tmparg != 6)
   {
     print "SourceMac/Sap format $smac not valid\n";
     &usage;
   }
   while($p = shift @tmparg)
   {
     $q = hex($p); 
     $smacOid = $smacOid.$q;
     $smacOid = $smacOid.'.';
   }

   #print "@tmparg1\n";
   #print "$smacOid\n";

#
#Convert Dest Mac & Sap
#  
   @tmparg = split(/\./,$dmac);
   #print "-$dmac-\n";
   #print "@tmparg\n";
   #print "$#tmparg\n";
   if($#tmparg != 6)
   {
     print "DestMac/Sap format $dmac not valid\n";
     &usage;
   }

   while($p = shift @tmparg)
   {
     $q = hex($p); 
     $dmacOid = $dmacOid.$q;
     $dmacOid = $dmacOid.'.';
   }
#  Remove Trailing Dot
   $dmacOid = substr($dmacOid,0,length($dmacOid)-1);
   

   #print "@tmparg1\n";
   #print "$dmacOid\n";
#Build the Dlsw Oic to use
   $snmpoid = $enterpriseOid.$ciscoDlswCircuitOid.$unknownOid.$smacOid.$unknownOid.$dmacOid ;
   #print "$snmpoid\n";

   #shift;
   $hostname  = shift || &usage;

   ($session, $error) = Net::SNMP->session(
      -hostname  => $hostname,
      -community => $community,
      -port      => $port
   );

   if (!defined($session)) {
      $state='UNKNOWN';
      $answer=$error;
      print ("$state: $answer");
      exit $ERRORS{$state};
   }

   push(@snmpoids,$snmpoid);
   #push(@snmpoids,$snmpLocIfDescr);

   if (!defined($response = $session->get_request(@snmpoids))) {
      $answer=$session->error;
      $session->close;
      $state = 'CRITICAL';
      print ("$state: $answer,$community,$smac - $dmac");
      exit $ERRORS{$state};
   }

   $answer = sprintf("dlsw circuit %s - %s at host '%s',is %s\n", 
      $smac,
	$dmac,
      $hostname, 
      $dlswCircuitStatus{$response->{$snmpoid}}
   );

   $session->close;

   if ( $response->{$snmpoid} == 8 ) {
      $state = 'OK';
   }
   else {
	$state = 'CRITICAL';
   }

print ("$state: $answer");
exit $ERRORS{$state};