1
0

check_snmp_bandwidth.pl 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #!/usr/bin/perl
  2. use lib "/etc/nagios4/scripts/plugins";
  3. use Net::SNMP;
  4. use Config::Tiny;
  5. use File::Path qw( mkpath );
  6. use POSIX;
  7. use strict;
  8. use snmp;
  9. my $TIMEOUT = 30;
  10. $SIG{ALRM} = sub { print "ERROR: No response\n"; exit 3; };
  11. alarm($TIMEOUT);
  12. my $ifSpeed = '.1.3.6.1.2.1.2.2.1.5';
  13. my $ifInOctets = '.1.3.6.1.2.1.2.2.1.10';
  14. my $ifOutOctets = '.1.3.6.1.2.1.2.2.1.16';
  15. my $ifHighSpeed = '.1.3.6.1.2.1.31.1.1.1.15';
  16. my $ifHCInOctets = '.1.3.6.1.2.1.31.1.1.1.6';
  17. my $ifHCOutOctets = '.1.3.6.1.2.1.31.1.1.1.10';
  18. ### return codes
  19. my $RET_OK=0;
  20. my $RET_WARNING=1;
  21. my $RET_UNKNOWN=3;
  22. my $RET_CRITICAL=2;
  23. if (scalar @ARGV <= 2) {
  24. print "Usage: $0 <host> <snmp_string> <port> <32|64> [warning] [critical]\n";
  25. print "<snmp_string> => community::version::user::auth::priv\n";
  26. print "for version 3: community = password\n";
  27. exit $RET_OK;
  28. }
  29. my $host = shift @ARGV;
  30. my $snmp_str = shift @ARGV;
  31. my ($community,$version,$user,$auth,$priv) = split(/\:\:/,$snmp_str);
  32. my $snmp;
  33. $snmp->{version} = $version || '2';
  34. $snmp->{timeout} = 30;
  35. $snmp->{community} = $community || 'public';
  36. $snmp->{user} = $user || 'public';
  37. $snmp->{auth} = $auth || 'sha1';
  38. $snmp->{priv} = $priv || 'aes';
  39. my $port = shift @ARGV;
  40. my $counter = shift @ARGV || 64;
  41. my $warning = shift (@ARGV) || 80;
  42. my $critical = shift (@ARGV) || 90;
  43. sub get_snmp_band {
  44. my $port = shift;
  45. my $counter = shift;
  46. my $IN_OID = $ifHCInOctets.".".$port;
  47. my $OUT_OID = $ifHCOutOctets.".".$port;
  48. my $SPEED_OID = $ifHighSpeed.".".$port;
  49. if ($counter eq 32) {
  50. $IN_OID = $ifInOctets.".".$port;
  51. $OUT_OID = $ifOutOctets.".".$port;
  52. $SPEED_OID = $ifSpeed.".".$port;
  53. }
  54. my $session = init_snmp($host,$snmp);
  55. my $result_in = $session->get_request( -varbindlist => [$IN_OID]);
  56. my $result_out = $session->get_request( -varbindlist => [$OUT_OID]);
  57. my $result_speed = $session->get_request( -varbindlist => [$SPEED_OID]);
  58. $session->close;
  59. my $port_speed;
  60. $port_speed->{timestamp}=time();
  61. $port_speed->{counter}=$counter;
  62. $port_speed->{in} = $result_in->{$IN_OID};
  63. $port_speed->{out} = $result_out->{$OUT_OID};
  64. $port_speed->{speed} = $result_speed->{$SPEED_OID};
  65. return $port_speed;
  66. }
  67. my $start_time = time();
  68. my $host_spool_dir = "/var/spool/nagios4/plugins/bandwidth/";
  69. if (!-e "$host_spool_dir") { mkpath( $host_spool_dir, 0, 0770 ); }
  70. my $host_data = $host_spool_dir.'/'.$host;
  71. my $old_info;
  72. my $cur_info;
  73. my $clean_start = 0;
  74. my $host_spool = Config::Tiny->new;
  75. if (-e "$host_data") {
  76. $host_spool = Config::Tiny->read($host_data, 'utf8' );
  77. $old_info=$host_spool->{$port};
  78. } else { $clean_start=1; }
  79. $cur_info=get_snmp_band($port,$counter);
  80. #speed patch for x64 counter
  81. if ($counter eq 64) { $cur_info->{speed}=$cur_info->{speed}*1000; }
  82. #extreme patch
  83. if ($cur_info->{speed} eq 4294967295) { $cur_info->{speed}=10000000; }
  84. $host_spool->{$port} = $cur_info;
  85. $host_spool->write($host_data);
  86. my $perf="IN=%s%%;$warning;$critical OUT=%s%%;$warning;$critical";
  87. if ($clean_start or $cur_info->{speed} eq 0) {
  88. printf("OK: Bandwidth in=%s%% out=%s%% |".$perf."\n",0,0,0,0);
  89. exit $RET_OK;
  90. }
  91. my $deltaIn = $cur_info->{in} - $old_info->{in};
  92. my $deltaOut = $cur_info->{out} - $old_info->{out};
  93. my $deltaTime = $cur_info->{timestamp} - $old_info->{timestamp};
  94. my $counter_div = 10;
  95. if ($counter eq 32) { $counter_div = 1; }
  96. my $band_in = ceil((($deltaIn * 8) / $deltaTime) / $cur_info->{speed} / $counter_div);
  97. my $band_out = ceil((($deltaOut * 8) / $deltaTime) / $cur_info->{speed} / $counter_div);
  98. if ($band_in <$warning and $band_out<$warning) {
  99. printf("OK: Bandwidth in=%s%% out=%s%%|".$perf."\n",$band_in,$band_out,$band_in,$band_out);
  100. exit $RET_OK;
  101. }
  102. if ($band_in >=$critical or $band_out>=$critical) {
  103. printf("CRIT: Bandwidth in=%s%% out=%s%%|".$perf."\n",$band_in,$band_out,$band_in,$band_out);
  104. exit $RET_CRITICAL;
  105. }
  106. if ($band_in >=$warning and $band_in<$critical) {
  107. printf("WARN: Bandwidth in=%s%% out=%s%%|".$perf."\n",$band_in,$band_out,$band_in,$band_out);
  108. exit $RET_WARNING;
  109. }
  110. if ($band_out >=$warning and $band_out<$critical) {
  111. printf("WARN: Bandwidth in=%s%% out=%s%%|".$perf."\n",$band_in,$band_out,$band_in,$band_out);
  112. exit $RET_WARNING;
  113. }
  114. printf("OK: You don't see this! in=%s%% out=%s%% |".$perf."\n",$band_in,$band_out);
  115. exit $RET_OK;