check_dlswcircuit.pl 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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
  41. use strict;
  42. use Net::SNMP;
  43. use Getopt::Long;
  44. &Getopt::Long::config('auto_abbrev');
  45. my $status;
  46. my $TIMEOUT = 15;
  47. my %ERRORS = ('UNKNOWN' , '-1',
  48. 'OK' , '0',
  49. 'WARNING', '1',
  50. 'CRITICAL', '2');
  51. my %dlswCircuitStatus = (
  52. '1','disconnected',
  53. '2','circuitStart',
  54. '3','resolvePending',
  55. '4','circuitPending',
  56. '5','circuitEstablished',
  57. '6','connectPending',
  58. '7','contactPending',
  59. '8','connected',
  60. '9','disconnectPending',
  61. '10','haltPending',
  62. '11','haltPendingNoack',
  63. '13','circuitRestart',
  64. '14','restartPending');
  65. my $state = "UNKNOWN";
  66. my $answer = "";
  67. my $smac = "";
  68. my $dmac = "";
  69. my $community = "public";
  70. my $port = 161;
  71. #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
  72. my $enterpriseOid = "1.3.6.1.4.1";
  73. my $ciscoDlswCircuitOid = ".9.10.9.1.5.2.1.17.";
  74. my $unknownOid = "6.";
  75. my $smacOid = "";
  76. my $dmacOid = "";
  77. my $tmpOid = "";
  78. my @tmparg;
  79. my $snmpoid;
  80. my @snmpoids;
  81. my $hostname;
  82. my $session;
  83. my $error;
  84. my $response;
  85. my $p = "";
  86. my $q = "";
  87. sub usage {
  88. printf "\nMissing arguments!\n";
  89. printf "\n";
  90. printf "Perl Check Cisco Dlsw Circuit State plugin for Nagios\n";
  91. printf "checks operational status of specified DLSW Circuit\n";
  92. printf "usage: \n";
  93. printf "check_dlswcircuit.pl -s <SourceMac> -d <DestMac> -c <READCOMMUNITY> -p <PORT> <HOSTNAME>";
  94. printf "\nCopyright (C) 2000 Carsten Foss\n";
  95. printf "check_dlswcircuit.pl comes with ABSOLUTELY NO WARRANTY\n";
  96. printf "This programm is licensed under the terms of the ";
  97. printf "GNU General Public License\n(check source code for details)\n";
  98. printf "\n\n";
  99. exit $ERRORS{"UNKNOWN"};
  100. }
  101. # Just in case of problems, let's not hang Nagios
  102. $SIG{'ALRM'} = sub {
  103. print ("ERROR: No snmp response from $hostname (alarm)\n");
  104. exit $ERRORS{"UNKNOWN"};
  105. };
  106. alarm($TIMEOUT);
  107. $status = GetOptions("sourcemac=s",\$smac,"destmac=s",\$dmac,
  108. "community=s",\$community,
  109. "port=i",\$port);
  110. if ($status == 0)
  111. {
  112. &usage;
  113. }
  114. #
  115. #Convert Source Mac & Sap
  116. #
  117. @tmparg = split(/\./,$smac);
  118. #print "-$smac-\n";
  119. #print "@tmparg\n";
  120. #print "$#tmparg\n";
  121. if($#tmparg != 6)
  122. {
  123. print "SourceMac/Sap format $smac not valid\n";
  124. &usage;
  125. }
  126. while($p = shift @tmparg)
  127. {
  128. $q = hex($p);
  129. $smacOid = $smacOid.$q;
  130. $smacOid = $smacOid.'.';
  131. }
  132. #print "@tmparg1\n";
  133. #print "$smacOid\n";
  134. #
  135. #Convert Dest Mac & Sap
  136. #
  137. @tmparg = split(/\./,$dmac);
  138. #print "-$dmac-\n";
  139. #print "@tmparg\n";
  140. #print "$#tmparg\n";
  141. if($#tmparg != 6)
  142. {
  143. print "DestMac/Sap format $dmac not valid\n";
  144. &usage;
  145. }
  146. while($p = shift @tmparg)
  147. {
  148. $q = hex($p);
  149. $dmacOid = $dmacOid.$q;
  150. $dmacOid = $dmacOid.'.';
  151. }
  152. # Remove Trailing Dot
  153. $dmacOid = substr($dmacOid,0,length($dmacOid)-1);
  154. #print "@tmparg1\n";
  155. #print "$dmacOid\n";
  156. #Build the Dlsw Oic to use
  157. $snmpoid = $enterpriseOid.$ciscoDlswCircuitOid.$unknownOid.$smacOid.$unknownOid.$dmacOid ;
  158. #print "$snmpoid\n";
  159. #shift;
  160. $hostname = shift || &usage;
  161. ($session, $error) = Net::SNMP->session(
  162. -hostname => $hostname,
  163. -community => $community,
  164. -port => $port
  165. );
  166. if (!defined($session)) {
  167. $state='UNKNOWN';
  168. $answer=$error;
  169. print ("$state: $answer");
  170. exit $ERRORS{$state};
  171. }
  172. push(@snmpoids,$snmpoid);
  173. #push(@snmpoids,$snmpLocIfDescr);
  174. if (!defined($response = $session->get_request(@snmpoids))) {
  175. $answer=$session->error;
  176. $session->close;
  177. $state = 'CRITICAL';
  178. print ("$state: $answer,$community,$smac - $dmac");
  179. exit $ERRORS{$state};
  180. }
  181. $answer = sprintf("dlsw circuit %s - %s at host '%s',is %s\n",
  182. $smac,
  183. $dmac,
  184. $hostname,
  185. $dlswCircuitStatus{$response->{$snmpoid}}
  186. );
  187. $session->close;
  188. if ( $response->{$snmpoid} == 8 ) {
  189. $state = 'OK';
  190. }
  191. else {
  192. $state = 'CRITICAL';
  193. }
  194. print ("$state: $answer");
  195. exit $ERRORS{$state};