4
0

check_ircd.pl 6.7 KB

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