check_ircd.pl 7.2 KB

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