check_ircd.pl 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. #!/usr/bin/perl -wT
  2. # -----------------------------------------------------------------------------
  3. # File Name: check_ircd.pl
  4. #
  5. # Author: Richard Mayhew - South Africa
  6. #
  7. # Date: 1999/09/20
  8. #
  9. #
  10. # Description: This script will check to see if an IRCD is running
  11. # about how many users it has
  12. #
  13. # Email: netsaint@splash.co.za
  14. #
  15. # -----------------------------------------------------------------------------
  16. # Copyright 1999 (c) Richard Mayhew
  17. #
  18. # If any changes are made to this script, please mail me a copy of the
  19. # changes :)
  20. #
  21. # Some code taken from Charlie Cook (check_disk.pl)
  22. #
  23. # License GPL
  24. #
  25. # -----------------------------------------------------------------------------
  26. # Date Author Reason
  27. # ---- ------ ------
  28. #
  29. # 1999/09/20 RM Creation
  30. #
  31. # 1999/09/20 TP Changed script to use strict, more secure by
  32. # specifying $ENV variables. The bind command is
  33. # still insecure through. Did most of my work
  34. # with perl -wT and 'use strict'
  35. #
  36. # test using check_ircd.pl (irc-2.mit.edu|irc.erols.com|irc.core.com)
  37. # 2002/05/02 SG Fixed for Embedded Perl
  38. #
  39. # ----------------------------------------------------------------[ Require ]--
  40. require 5.004;
  41. # -------------------------------------------------------------------[ Uses ]--
  42. use Socket;
  43. use strict;
  44. use Getopt::Long;
  45. use vars qw($opt_V $opt_h $opt_t $opt_p $opt_H $opt_w $opt_c $verbose);
  46. use vars qw($PROGNAME);
  47. use lib utils.pm;
  48. use utils qw($TIMEOUT %ERRORS &print_revision &support &usage);
  49. # ----------------------------------------------------[ Function Prototypes ]--
  50. sub print_help ();
  51. sub print_usage ();
  52. sub connection ($$$$);
  53. sub bindRemote ($$);
  54. # -------------------------------------------------------------[ Enviroment ]--
  55. $ENV{PATH} = "";
  56. $ENV{ENV} = "";
  57. $ENV{BASH_ENV} = "";
  58. # -----------------------------------------------------------------[ Global ]--
  59. $PROGNAME = "check_ircd";
  60. my $NICK="ircd$$";
  61. my $USER_INFO="monitor localhost localhost : ";
  62. # -------------------------------------------------------------[ connection ]--
  63. sub connection ($$$$)
  64. {
  65. my ($in_remotehost,$in_users,$in_warn,$in_crit) = @_;
  66. my $state;
  67. my $answer;
  68. print "connection(debug): users = $in_users\n" if $verbose;
  69. $in_users =~ s/\ //g;
  70. if ($in_users >= 0) {
  71. if ($in_users > $in_crit) {
  72. $state = "CRITICAL";
  73. $answer = "Critical Number Of Clients Connected : $in_users (Limit = $in_crit)\n";
  74. } elsif ($in_users > $in_warn) {
  75. $state = "WARNING";
  76. $answer = "Warning Number Of Clients Connected : $in_users (Limit = $in_warn)\n";
  77. } else {
  78. $state = "OK";
  79. $answer = "IRCD ok - Current Local Users: $in_users\n";
  80. }
  81. } else {
  82. $state = "UNKNOWN";
  83. $answer = "Server $in_remotehost has less than 0 users! Something is Really WRONG!\n";
  84. }
  85. print ClientSocket "quit\n";
  86. print $answer;
  87. exit $ERRORS{$state};
  88. }
  89. # ------------------------------------------------------------[ print_usage ]--
  90. sub print_usage () {
  91. print "Usage: $PROGNAME -H <host> [-w <warn>] [-c <crit>] [-p <port>]\n";
  92. }
  93. # -------------------------------------------------------------[ print_help ]--
  94. sub print_help ()
  95. {
  96. print_revision($PROGNAME,'@NP_VERSION@');
  97. print "Copyright (c) 2000 Richard Mayhew/Karl DeBisschop
  98. Perl Check IRCD plugin for monitoring
  99. ";
  100. print_usage();
  101. print "
  102. -H, --hostname=HOST
  103. Name or IP address of host to check
  104. -w, --warning=INTEGER
  105. Number of connected users which generates a warning state (Default: 50)
  106. -c, --critical=INTEGER
  107. Number of connected users which generates a critical state (Default: 100)
  108. -p, --port=INTEGER
  109. Port that the ircd daemon is running on <host> (Default: 6667)
  110. -v, --verbose
  111. Print extra debugging information
  112. ";
  113. }
  114. # -------------------------------------------------------------[ bindRemote ]--
  115. sub bindRemote ($$)
  116. {
  117. my ($in_remotehost, $in_remoteport) = @_;
  118. my $proto = getprotobyname('tcp');
  119. my $sockaddr;
  120. my $that;
  121. my ($name, $aliases,$type,$len,$thataddr) = gethostbyname($in_remotehost);
  122. if (!socket(ClientSocket,AF_INET, SOCK_STREAM, $proto)) {
  123. print "IRCD UNKNOWN: Could not start socket ($!)\n";
  124. exit $ERRORS{"UNKNOWN"};
  125. }
  126. $sockaddr = 'S n a4 x8';
  127. $that = pack($sockaddr, AF_INET, $in_remoteport, $thataddr);
  128. if (!connect(ClientSocket, $that)) {
  129. print "IRCD UNKNOWN: Could not connect socket ($!)\n";
  130. exit $ERRORS{"UNKNOWN"};
  131. }
  132. select(ClientSocket); $| = 1; select(STDOUT);
  133. return \*ClientSocket;
  134. }
  135. # ===================================================================[ MAIN ]==
  136. MAIN:
  137. {
  138. my $hostname;
  139. Getopt::Long::Configure('bundling');
  140. GetOptions
  141. ("V" => \$opt_V, "version" => \$opt_V,
  142. "h" => \$opt_h, "help" => \$opt_h,
  143. "v" => \$verbose,"verbose" => \$verbose,
  144. "t=i" => \$opt_t, "timeout=i" => \$opt_t,
  145. "w=i" => \$opt_w, "warning=i" => \$opt_w,
  146. "c=i" => \$opt_c, "critical=i" => \$opt_c,
  147. "p=i" => \$opt_p, "port=i" => \$opt_p,
  148. "H=s" => \$opt_H, "hostname=s" => \$opt_H);
  149. if ($opt_V) {
  150. print_revision($PROGNAME,'@NP_VERSION@');
  151. exit $ERRORS{'OK'};
  152. }
  153. if ($opt_h) {print_help(); exit $ERRORS{'OK'};}
  154. ($opt_H) || ($opt_H = shift @ARGV) || usage("Host name/address not specified\n");
  155. my $remotehost = $1 if ($opt_H =~ /([-.A-Za-z0-9]+)/);
  156. ($remotehost) || usage("Invalid host: $opt_H\n");
  157. ($opt_w) || ($opt_w = shift @ARGV) || ($opt_w = 50);
  158. my $warn = $1 if ($opt_w =~ /^([0-9]+)$/);
  159. ($warn) || usage("Invalid warning threshold: $opt_w\n");
  160. ($opt_c) || ($opt_c = shift @ARGV) || ($opt_c = 100);
  161. my $crit = $1 if ($opt_c =~ /^([0-9]+)$/);
  162. ($crit) || usage("Invalid critical threshold: $opt_c\n");
  163. ($opt_p) || ($opt_p = shift @ARGV) || ($opt_p = 6667);
  164. my $remoteport = $1 if ($opt_p =~ /^([0-9]+)$/);
  165. ($remoteport) || usage("Invalid port: $opt_p\n");
  166. if ($opt_t && $opt_t =~ /^([0-9]+)$/) { $TIMEOUT = $1; }
  167. # Just in case of problems, let's not hang the monitoring system
  168. $SIG{'ALRM'} = sub {
  169. print "Somthing is Taking a Long Time, Increase Your TIMEOUT (Currently Set At $TIMEOUT Seconds)\n";
  170. exit $ERRORS{"UNKNOWN"};
  171. };
  172. alarm($TIMEOUT);
  173. my ($name, $alias, $proto) = getprotobyname('tcp');
  174. print "MAIN(debug): binding to remote host: $remotehost -> $remoteport\n" if $verbose;
  175. my $ClientSocket = &bindRemote($remotehost,$remoteport);
  176. print ClientSocket "NICK $NICK\nUSER $USER_INFO\n";
  177. while (<ClientSocket>) {
  178. print "MAIN(debug): default var = $_\n" if $verbose;
  179. # DALnet,LagNet,UnderNet etc. Require this!
  180. # Replies with a PONG when presented with a PING query.
  181. # If a server doesn't require it, it will be ignored.
  182. if (m/^PING (.*)/) {print ClientSocket "PONG $1\n";}
  183. alarm(0);
  184. # Look for pattern in IRCD Output to gather Client Connections total.
  185. connection($remotehost,$1,$warn,$crit) if (m/:I have\s+(\d+)/);
  186. }
  187. print "IRCD UNKNOWN: Unknown error - maybe could not authenticate\n";
  188. exit $ERRORS{"UNKNOWN"};
  189. }