check_ircd.pl 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. # 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 lib utils.pm;
  50. use utils qw($TIMEOUT %ERRORS &print_revision &support &usage);
  51. # ----------------------------------------------------[ Function Prototypes ]--
  52. sub print_help ();
  53. sub print_usage ();
  54. sub connection ($$$$);
  55. sub bindRemote ($$);
  56. # -------------------------------------------------------------[ Enviroment ]--
  57. $ENV{PATH} = "";
  58. $ENV{ENV} = "";
  59. $ENV{BASH_ENV} = "";
  60. # -----------------------------------------------------------------[ Global ]--
  61. $PROGNAME = "check_ircd";
  62. my $NICK="ircd$$";
  63. my $USER_INFO="monitor localhost localhost : ";
  64. # -------------------------------------------------------------[ connection ]--
  65. sub connection ($$$$)
  66. {
  67. my ($in_remotehost,$in_users,$in_warn,$in_crit) = @_;
  68. my $state;
  69. my $answer;
  70. print "connection(debug): users = $in_users\n" if $verbose;
  71. $in_users =~ s/\ //g;
  72. if ($in_users >= 0) {
  73. if ($in_users > $in_crit) {
  74. $state = "CRITICAL";
  75. $answer = "Critical Number Of Clients Connected : $in_users (Limit = $in_crit)\n";
  76. } elsif ($in_users > $in_warn) {
  77. $state = "WARNING";
  78. $answer = "Warning Number Of Clients Connected : $in_users (Limit = $in_warn)\n";
  79. } else {
  80. $state = "OK";
  81. $answer = "IRCD ok - Current Local Users: $in_users\n";
  82. }
  83. } else {
  84. $state = "UNKNOWN";
  85. $answer = "Server $in_remotehost has less than 0 users! Something is Really WRONG!\n";
  86. }
  87. print ClientSocket "quit\n";
  88. print $answer;
  89. exit $ERRORS{$state};
  90. }
  91. # ------------------------------------------------------------[ print_usage ]--
  92. sub print_usage () {
  93. print "Usage: $PROGNAME -H <host> [-w <warn>] [-c <crit>] [-p <port>]\n";
  94. }
  95. # -------------------------------------------------------------[ print_help ]--
  96. sub print_help ()
  97. {
  98. print_revision($PROGNAME,'@NP_VERSION@');
  99. print "Copyright (c) 2000 Richard Mayhew/Karl DeBisschop
  100. Perl Check IRCD plugin for Nagios
  101. ";
  102. print_usage();
  103. print "
  104. -H, --hostname=HOST
  105. Name or IP address of host to check
  106. -w, --warning=INTEGER
  107. Number of connected users which generates a warning state (Default: 50)
  108. -c, --critical=INTEGER
  109. Number of connected users which generates a critical state (Default: 100)
  110. -p, --port=INTEGER
  111. Port that the ircd daemon is running on <host> (Default: 6667)
  112. -v, --verbose
  113. Print extra debugging information
  114. ";
  115. }
  116. # -------------------------------------------------------------[ bindRemote ]--
  117. sub bindRemote ($$)
  118. {
  119. my ($in_remotehost, $in_remoteport) = @_;
  120. my $proto = getprotobyname('tcp');
  121. my $sockaddr;
  122. my $that;
  123. my ($name, $aliases,$type,$len,$thataddr) = gethostbyname($in_remotehost);
  124. if (!socket(ClientSocket,AF_INET, SOCK_STREAM, $proto)) {
  125. print "IRCD UNKNOWN: Could not start socket ($!)\n";
  126. exit $ERRORS{"UNKNOWN"};
  127. }
  128. $sockaddr = 'S n a4 x8';
  129. $that = pack($sockaddr, AF_INET, $in_remoteport, $thataddr);
  130. if (!connect(ClientSocket, $that)) {
  131. print "IRCD UNKNOWN: Could not connect socket ($!)\n";
  132. exit $ERRORS{"UNKNOWN"};
  133. }
  134. select(ClientSocket); $| = 1; select(STDOUT);
  135. return \*ClientSocket;
  136. }
  137. # ===================================================================[ MAIN ]==
  138. MAIN:
  139. {
  140. my $hostname;
  141. Getopt::Long::Configure('bundling');
  142. GetOptions
  143. ("V" => \$opt_V, "version" => \$opt_V,
  144. "h" => \$opt_h, "help" => \$opt_h,
  145. "v" => \$verbose,"verbose" => \$verbose,
  146. "t=i" => \$opt_t, "timeout=i" => \$opt_t,
  147. "w=i" => \$opt_w, "warning=i" => \$opt_w,
  148. "c=i" => \$opt_c, "critical=i" => \$opt_c,
  149. "p=i" => \$opt_p, "port=i" => \$opt_p,
  150. "H=s" => \$opt_H, "hostname=s" => \$opt_H);
  151. if ($opt_V) {
  152. print_revision($PROGNAME,'@NP_VERSION@');
  153. exit $ERRORS{'OK'};
  154. }
  155. if ($opt_h) {print_help(); exit $ERRORS{'OK'};}
  156. ($opt_H) || ($opt_H = shift) || usage("Host name/address not specified\n");
  157. my $remotehost = $1 if ($opt_H =~ /([-.A-Za-z0-9]+)/);
  158. ($remotehost) || usage("Invalid host: $opt_H\n");
  159. ($opt_w) || ($opt_w = shift) || ($opt_w = 50);
  160. my $warn = $1 if ($opt_w =~ /^([0-9]+)$/);
  161. ($warn) || usage("Invalid warning threshold: $opt_w\n");
  162. ($opt_c) || ($opt_c = shift) || ($opt_c = 100);
  163. my $crit = $1 if ($opt_c =~ /^([0-9]+)$/);
  164. ($crit) || usage("Invalid critical threshold: $opt_c\n");
  165. ($opt_p) || ($opt_p = shift) || ($opt_p = 6667);
  166. my $remoteport = $1 if ($opt_p =~ /^([0-9]+)$/);
  167. ($remoteport) || usage("Invalid port: $opt_p\n");
  168. if ($opt_t && $opt_t =~ /^([0-9]+)$/) { $TIMEOUT = $1; }
  169. # Just in case of problems, let's not hang Nagios
  170. $SIG{'ALRM'} = sub {
  171. print "Somthing is Taking a Long Time, Increase Your TIMEOUT (Currently Set At $TIMEOUT Seconds)\n";
  172. exit $ERRORS{"UNKNOWN"};
  173. };
  174. alarm($TIMEOUT);
  175. my ($name, $alias, $proto) = getprotobyname('tcp');
  176. print "MAIN(debug): binding to remote host: $remotehost -> $remoteport\n" if $verbose;
  177. my $ClientSocket = &bindRemote($remotehost,$remoteport);
  178. print ClientSocket "NICK $NICK\nUSER $USER_INFO\n";
  179. while (<ClientSocket>) {
  180. print "MAIN(debug): default var = $_\n" if $verbose;
  181. # DALnet,LagNet,UnderNet etc. Require this!
  182. # Replies with a PONG when presented with a PING query.
  183. # If a server doesn't require it, it will be ignored.
  184. if (m/^PING (.*)/) {print ClientSocket "PONG $1\n";}
  185. alarm(0);
  186. # Look for pattern in IRCD Output to gather Client Connections total.
  187. connection($remotehost,$1,$warn,$crit) if (m/:I have\s+(\d+)/);
  188. }
  189. print "IRCD UNKNOWN: Unknown error - maybe could not authenticate\n";
  190. exit $ERRORS{"UNKNOWN"};
  191. }