check_ircd.pl 7.3 KB

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