check_ircd.pl 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. # 2002/05/02 SG Fixed for Embedded Perl
  41. #
  42. # ----------------------------------------------------------------[ Require ]--
  43. require 5.004;
  44. # -------------------------------------------------------------------[ Uses ]--
  45. use Socket;
  46. use strict;
  47. use Getopt::Long;
  48. use vars qw($opt_V $opt_h $opt_t $opt_p $opt_H $opt_w $opt_c $verbose);
  49. use vars qw($PROGNAME);
  50. use lib utils.pm;
  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} = "";
  59. $ENV{ENV} = "";
  60. $ENV{BASH_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,'$Revision$ ');
  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, $in_hostname) = @_;
  121. my $proto = getprotobyname('tcp');
  122. my $sockaddr;
  123. my $this;
  124. my $thisaddr = gethostbyname($in_hostname);
  125. my $that;
  126. my ($name, $aliases,$type,$len,$thataddr) = gethostbyname($in_remotehost);
  127. # ($name,$aliases,$type,$len,$thisaddr) = gethostbyname($in_hostname);
  128. if (!socket(ClientSocket,AF_INET, SOCK_STREAM, $proto)) {
  129. print "IRCD UNKNOWN: Could not start socket ($!)\n";
  130. exit $ERRORS{"UNKNOWN"};
  131. }
  132. $sockaddr = 'S n a4 x8';
  133. $this = pack($sockaddr, AF_INET, 0, $thisaddr);
  134. $that = pack($sockaddr, AF_INET, $in_remoteport, $thataddr);
  135. if (!bind(ClientSocket, $this)) {
  136. print "IRCD UNKNOWN: Could not bind socket ($!)\n";
  137. exit $ERRORS{"UNKNOWN"};
  138. }
  139. if (!connect(ClientSocket, $that)) {
  140. print "IRCD UNKNOWN: Could not connect socket ($!)\n";
  141. exit $ERRORS{"UNKNOWN"};
  142. }
  143. select(ClientSocket); $| = 1; select(STDOUT);
  144. return \*ClientSocket;
  145. }
  146. # ===================================================================[ MAIN ]==
  147. MAIN:
  148. {
  149. my $hostname;
  150. Getopt::Long::Configure('bundling');
  151. GetOptions
  152. ("V" => \$opt_V, "version" => \$opt_V,
  153. "h" => \$opt_h, "help" => \$opt_h,
  154. "v" => \$verbose,"verbose" => \$verbose,
  155. "t=i" => \$opt_t, "timeout=i" => \$opt_t,
  156. "w=i" => \$opt_w, "warning=i" => \$opt_w,
  157. "c=i" => \$opt_c, "critical=i" => \$opt_c,
  158. "p=i" => \$opt_p, "port=i" => \$opt_p,
  159. "H=s" => \$opt_H, "hostname=s" => \$opt_H);
  160. if ($opt_V) {
  161. print_revision($PROGNAME,'$Revision$ ');
  162. exit $ERRORS{'OK'};
  163. }
  164. if ($opt_h) {print_help(); exit $ERRORS{'OK'};}
  165. ($opt_H) || ($opt_H = shift) || usage("Host name/address not specified\n");
  166. my $remotehost = $1 if ($opt_H =~ /([-.A-Za-z0-9]+)/);
  167. ($remotehost) || usage("Invalid host: $opt_H\n");
  168. ($opt_w) || ($opt_w = shift) || ($opt_w = 50);
  169. my $warn = $1 if ($opt_w =~ /^([0-9]+)$/);
  170. ($warn) || usage("Invalid warning threshold: $opt_w\n");
  171. ($opt_c) || ($opt_c = shift) || ($opt_c = 100);
  172. my $crit = $1 if ($opt_c =~ /^([0-9]+)$/);
  173. ($crit) || usage("Invalid critical threshold: $opt_c\n");
  174. ($opt_p) || ($opt_p = shift) || ($opt_p = 6667);
  175. my $remoteport = $1 if ($opt_p =~ /^([0-9]+)$/);
  176. ($remoteport) || usage("Invalid port: $opt_p\n");
  177. if ($opt_t && $opt_t =~ /^([0-9]+)$/) { $TIMEOUT = $1; }
  178. # Just in case of problems, let's not hang Nagios
  179. $SIG{'ALRM'} = sub {
  180. print "Somthing is Taking a Long Time, Increase Your TIMEOUT (Currently Set At $TIMEOUT Seconds)\n";
  181. exit $ERRORS{"UNKNOWN"};
  182. };
  183. alarm($TIMEOUT);
  184. chomp($hostname = `/bin/hostname`);
  185. $hostname = $1 if ($hostname =~ /([-.a-zA-Z0-9]+)/);
  186. my ($name, $alias, $proto) = getprotobyname('tcp');
  187. print "MAIN(debug): hostname = $hostname\n" if $verbose;
  188. print "MAIN(debug): binding to remote host: $remotehost -> $remoteport -> $hostname\n" if $verbose;
  189. my $ClientSocket = &bindRemote($remotehost,$remoteport,$hostname);
  190. print ClientSocket "NICK $NICK\nUSER $USER_INFO\n";
  191. while (<ClientSocket>) {
  192. print "MAIN(debug): default var = $_\n" if $verbose;
  193. # DALnet,LagNet,UnderNet etc. Require this!
  194. # Replies with a PONG when presented with a PING query.
  195. # If a server doesn't require it, it will be ignored.
  196. if (m/^PING (.*)/) {print ClientSocket "PONG $1\n";}
  197. alarm(0);
  198. # Look for pattern in IRCD Output to gather Client Connections total.
  199. connection($remotehost,$1,$warn,$crit) if (m/:I have\s+(\d+)/);
  200. }
  201. print "IRCD UNKNOWN: Unknown error - maybe could not authenticate\n";
  202. exit $ERRORS{"UNKNOWN"};
  203. }