check_snmp_crc_simple.pl 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #!/usr/bin/perl
  2. use lib "/etc/nagios4/scripts/plugins";
  3. use snmp;
  4. use Net::SNMP;
  5. use Config::Tiny;
  6. use File::Path qw( mkpath );
  7. use strict;
  8. my $TIMEOUT = 30;
  9. $SIG{ALRM} = sub { print "ERROR: No response\n"; exit 3; };
  10. alarm($TIMEOUT);
  11. ### return codes
  12. my $RET_OK=0;
  13. my $RET_WARNING=1;
  14. my $RET_UNKNOWN=3;
  15. my $RET_CRITICAL=2;
  16. my $time_cache = 1800;
  17. my $err_step = 100;
  18. if (scalar @ARGV <= 2) {
  19. print "Usage: $0 <host> <snmp_string> <port> [warning] [critical]\n";
  20. print "<snmp_string> => community::version::user::auth::priv\n";
  21. print "for version 3: community = password\n";
  22. exit $RET_OK;
  23. }
  24. my $host = shift @ARGV;
  25. my $snmp_str = shift @ARGV;
  26. my ($community,$version,$user,$auth,$priv) = split(/\:\:/,$snmp_str);
  27. my $snmp;
  28. $snmp->{version} = $version || '2';
  29. $snmp->{timeout} = 30;
  30. $snmp->{community} = $community || 'public';
  31. $snmp->{user} = $user || 'public';
  32. $snmp->{auth} = $auth || 'sha1';
  33. $snmp->{priv} = $priv || 'aes';
  34. my $port = shift @ARGV;
  35. sub get_snmp_crc {
  36. my $port = shift;
  37. my $CRC_OID = ".1.3.6.1.2.1.2.2.1.14.".$port;
  38. my $session = init_snmp($host,$snmp);
  39. my $result = $session->get_request( -varbindlist => [$CRC_OID]);
  40. $session->close;
  41. my %port_crc;
  42. $port_crc{$port} = $result->{$CRC_OID};
  43. return \%port_crc;
  44. }
  45. my $time_cache_min = int($time_cache/60);
  46. my $start_time = time();
  47. my $host_spool_dir = "/var/spool/nagios4/plugins/crc/".$host;
  48. if (!-e "$host_spool_dir") { mkpath( $host_spool_dir, 0, 0770 ); }
  49. my $host_data = $host_spool_dir.'/'.$host."-port-".$port;
  50. my $old_crc_info;
  51. my $cur_crc_info;
  52. my $need_rescan = 0;
  53. if (-e "$host_data") {
  54. my $host_spool = Config::Tiny->new;
  55. $host_spool = Config::Tiny->read($host_data, 'utf8' );
  56. my $old_time=$host_spool->{_}->{timestamp};
  57. foreach my $port (keys %{$host_spool->{crc_data}}) { $old_crc_info->{$port} = $host_spool->{crc_data}->{$port}; }
  58. if (($start_time - $old_time) >=$time_cache) { $need_rescan = 1; } else { $need_rescan = 0; }
  59. } else { $need_rescan = 1; }
  60. if (!$old_crc_info->{$port}) { $old_crc_info->{$port}=0; }
  61. if ($need_rescan) {
  62. $cur_crc_info=get_snmp_crc($port);
  63. my $host_spool = Config::Tiny->new;
  64. $host_spool->{_}->{timestamp}=$start_time;
  65. $host_spool->{crc_data} = $cur_crc_info;
  66. $host_spool->write($host_data);
  67. } else { $cur_crc_info = $old_crc_info; }
  68. my $diff_crc = $cur_crc_info->{$port} - $old_crc_info->{$port};
  69. my $perf="ErrSpeed=%s;0;0;0;10000000;";
  70. if ($diff_crc >0 and $diff_crc >= $err_step) {
  71. my $speed_crc = int($diff_crc/$time_cache_min);
  72. printf("CRIT: CRC error found! Speedup $speed_crc by minute!|".$perf."\n",$speed_crc);
  73. exit $RET_CRITICAL;
  74. }
  75. printf("OK: Errors not found. |".$perf."\n",0);
  76. exit $RET_OK;