check_ping 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #!/usr/bin/perl -w
  2. #================================================================
  3. #
  4. # This perl script will accept an argument and simply pass it
  5. # to ping. It works by sending 2 ping to the specified host
  6. # and evaluating on the average delta time of those 2 pings.
  7. #
  8. # Author: SpEnTBoY
  9. # Email: lonny@abyss.za.org
  10. # April 5,2000
  11. #
  12. #================================================================
  13. #============================
  14. # State predefined stuff and
  15. # requirements
  16. #============================
  17. require 5.004;
  18. use POSIX;
  19. use strict;
  20. sub usage;
  21. my $ipaddr = $ARGV[0];
  22. my $TIMEOUT = 15;
  23. my %ERRORS = ('UNKNOWN' , '-1',
  24. 'OK' , '0',
  25. 'WARNING', '1',
  26. 'CRITICAL', '2');
  27. my $remote = shift || &usage(%ERRORS);
  28. my $warning = shift || 750;
  29. my $critical = shift || 1000;
  30. my $state = "OK";
  31. my $answer = undef;
  32. my $offset = undef;
  33. my $line = undef;
  34. #============================================================
  35. # If theres no response we can exit the bloody thing cleanly
  36. # last thing I want to do is hang an AIX system ;-)
  37. #============================================================
  38. $SIG{'ALRM'} = sub {
  39. print ("ERROR: No response from PING! (alarm)\n");
  40. exit $ERRORS{"UNKNOWN"};
  41. };
  42. alarm($TIMEOUT);
  43. #================================================
  44. # Pass stddn from $ARGV to the command and parse
  45. # the info we need (namely the value for "max"
  46. #================================================
  47. open(PING,"/usr/sbin/ping -c 2 '$ipaddr' >&1|");
  48. while (<PING>) {
  49. $line = $_;
  50. if (/round-trip min\/avg\/max = (.+)\/(.+)\/(.+) ms/) {
  51. $offset = $3;
  52. last;
  53. }
  54. }
  55. #==================================================
  56. # Do some error checking on the output of the file
  57. # and implement values for <crit> and <warn>
  58. # deffinitions if they were specified by the user
  59. # or sub in the predefined ones
  60. #==================================================
  61. if (defined $offset) {
  62. if (abs($offset) > $warning) {
  63. if (abs($offset) > $critical) {
  64. $state = "CRITICAL";
  65. $answer = ": Ping Time $offset MS greater than +/- $critical MS\n";
  66. } else {
  67. $state = "WARNING";
  68. $answer = ": Ping Time $offset MS greater than +/- $warning MS\n";
  69. }
  70. } else {
  71. $state = "OK";
  72. $answer = ": Ping Time $offset MS\n";
  73. }
  74. } else {
  75. $state = "UNKNOWN";
  76. $answer = ": $line\n";
  77. }
  78. print ("$state$answer");
  79. exit $ERRORS{$state};
  80. sub usage {
  81. print "\n";
  82. print "#=========================================\n";
  83. print "Check_Ping 0.02 script by Lonny Selinger\n";
  84. print "Made with AIX in mind ;-)\n";
  85. print "#=========================================\n";
  86. print "\n";
  87. print "#================================================\n";
  88. print " I'm going to need a few more arguments from you\n";
  89. print "#================================================\n";
  90. print "\n";
  91. print "#================================================\n";
  92. print "Usage: check_ping <host> [<warn> [<crit>]\n";
  93. print "#================================================\n";
  94. print "\n";
  95. print "<warn> = Ping in MS at which a warning message will be generated.\n Defaults to 750.\n";
  96. print "<crit> = Ping in MS at which a critical message will be generated.\n Defaults to 1000.\n\n";
  97. exit $ERRORS{"UNKNOWN"};
  98. }