check_sockets.pl 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #! /usr/bin/perl
  2. # ------------------------------------------------------------------------------
  3. # File Name: check_sockets.pl
  4. # Author: Richard Mayhew - South Africa
  5. # Date: 2000/07/11
  6. # Version: 1.0
  7. # Description: This script will check to see how may open sockets
  8. # a server has and waron respectivly
  9. # Email: netsaint@splash.co.za
  10. # ------------------------------------------------------------------------------
  11. # Copyright 1999 (c) Richard Mayhew
  12. # Credits go to Ethan Galstad for coding Nagios
  13. # If any changes are made to this script, please mail me a copy of the
  14. # changes :)
  15. # Some code taken from Charlie Cook (check_disk.pl)
  16. # License GPL
  17. #
  18. # ------------------------------------------------------------------------------
  19. # Date Author Reason
  20. # ---- ------ ------
  21. # 1999/09/20 RM Creation
  22. # 1999/09/20 TP Changed script to use strict, more secure by
  23. # specifying $ENV variables. The bind command is
  24. # still insecure through. Did most of my work
  25. # with perl -wT and 'use strict'
  26. #
  27. # ------------------------------------------------------------------------------
  28. # -----------------------------------------------------------------[ Require ]--
  29. require 5.004;
  30. # --------------------------------------------------------------------[ Uses ]--
  31. use Socket;
  32. use strict;
  33. # --------------------------------------------------------------[ Enviroment ]--
  34. $ENV{'PATH'}='/bin:/sbin:/usr/bin:/usr/sbin';
  35. $ENV{BASH_ENV} = "";
  36. # ------------------------------------------------------------------[ Global ]--
  37. my $TIMEOUT = 20;
  38. my %ERRORS = (
  39. 'UNKNOWN', '-1',
  40. 'OK', '0',
  41. 'WARNING', '1',
  42. 'CRITICAL', '2');
  43. # --------------------------------------------------------------[ connection ]--
  44. sub connection
  45. {
  46. my ($in_total,$in_warn,$in_crit,$in_high) = @_;
  47. my $state;
  48. my $answer;
  49. $in_total =~ s/\ //g;
  50. if ($in_total >= 0) {
  51. if ($in_total > $in_crit) {
  52. $state = "CRITICAL";
  53. $answer = "Critical Number Of Sockets Connected : $in_total (Limit = $in_crit)\n";
  54. } elsif ($in_total > $in_warn) {
  55. $state = "WARNING";
  56. $answer = "Warning Number Of Sockets Connected : $in_total (Limit = $in_warn)\n";
  57. } else {
  58. if ($in_high ne "") {
  59. $answer = "Sockets OK - Current Sockets: $in_total : $in_high\n";
  60. }
  61. if ($in_high eq "") {
  62. $answer = "Sockets OK - Current Sockets: $in_total\n";
  63. }
  64. $state = "OK";
  65. }
  66. } else {
  67. $state = "UNKNOWN";
  68. $answer = "Something is Really WRONG! Sockets Is A Negative Figure!\n";
  69. }
  70. print $answer;
  71. exit $ERRORS{$state};
  72. }
  73. # -------------------------------------------------------------------[ usage ]--
  74. sub usage
  75. {
  76. print "Minimum arguments not supplied!\n";
  77. print "\n";
  78. print "Perl Check Sockets plugin for Nagios\n";
  79. print "Copyright (c) 2000 Richard Mayhew\n";
  80. print "\n";
  81. print "Usage: check_sockets.pl <type> <warn> <crit>\n";
  82. print "\n";
  83. print "<type> = TOTAL, TCP, UDP, RAW.\n";
  84. print "<warn> = Number of sockets connected at which a warning message will be generated.[Default = 256]\n";
  85. print "<crit> = Number of sockets connected at which a critical message will be generated.[Default = 512]\n";
  86. exit $ERRORS{"UNKNOWN"};
  87. }
  88. # ====================================================================[ MAIN ]==
  89. MAIN:
  90. {
  91. my $type = shift || &usage;
  92. my $warn = shift || 256;
  93. my $crit = shift || 512;
  94. my $data;
  95. my @data;
  96. my $line;
  97. my $data1;
  98. my $data2;
  99. my $data3;
  100. my $junk;
  101. my $total1;
  102. my $total2;
  103. $type = uc $type;
  104. if ($type eq "TOTAL") {
  105. $type = "sockets";
  106. }
  107. # Just in case of problems, let's not hang Nagios
  108. $SIG{'ALRM'} = sub {
  109. print "Somthing is Taking a Long Time, Increase Your TIMEOUT (Currently Set At $TIMEOUT Seconds)\n";
  110. exit $ERRORS{"UNKNOWN"};
  111. };
  112. $data = `/bin/cat /proc/net/sockstat`;
  113. @data = split("\n",$data);
  114. alarm($TIMEOUT);
  115. my $output = "";
  116. my $high;
  117. foreach $line (@data) {
  118. if ($line =~ /$type/) {
  119. ($data1,$data2,$data3) = split(" ",$line,3);
  120. if ($data3 =~ /highest/){
  121. ($total1,$junk,$total2) = split(" ",$data3,3);
  122. $output = $total1;
  123. $high = $total2;
  124. }
  125. else {$output = $data3;}
  126. alarm(0);
  127. connection($output,$warn,$crit,$high);
  128. }
  129. }
  130. }