3 # -----------------------------------------------------------------------------
4 # File Name: check_ircd.pl
6 # Author: Richard Mayhew - South Africa
11 # Description: This script will check to see if an IRCD is running
12 # about how many users it has
14 # Email: netsaint@splash.co.za
16 # -----------------------------------------------------------------------------
17 # Copyright 1999 (c) Richard Mayhew
19 # Credits go to Ethan Galstad for coding Nagios
21 # If any changes are made to this script, please mail me a copy of the
24 # Some code taken from Charlie Cook (check_disk.pl)
28 # -----------------------------------------------------------------------------
32 # 1999/09/20 RM Creation
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'
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
43 # ----------------------------------------------------------------[ Require ]--
47 # -------------------------------------------------------------------[ Uses ]--
52 use vars
qw($opt_V $opt_h $opt_t $opt_p $opt_H $opt_w $opt_c $verbose);
53 use vars qw($PROGNAME);
55 use utils qw($TIMEOUT %ERRORS &print_revision &support &usage);
57 # ----------------------------------------------------[ Function Prototypes ]--
61 sub connection ($$$$);
64 # -------------------------------------------------------------[ Enviroment ]--
70 # -----------------------------------------------------------------[ Global ]--
72 $PROGNAME = "check_ircd";
74 my $USER_INFO="monitor localhost localhost : ";
76 # -------------------------------------------------------------[ connection ]--
79 my ($in_remotehost,$in_users,$in_warn,$in_crit) = @_;
83 print "connection(debug): users = $in_users\n" if $verbose;
88 if ($in_users > $in_crit) {
90 $answer = "Critical Number Of Clients Connected : $in_users (Limit = $in_crit)\n";
92 } elsif ($in_users > $in_warn) {
94 $answer = "Warning Number Of Clients Connected : $in_users (Limit = $in_warn)\n";
98 $answer = "IRCD ok - Current Local Users: $in_users\n";
103 $answer = "Server $in_remotehost has less than 0 users! Something is Really WRONG!\n";
106 print ClientSocket "quit\n";
108 exit $ERRORS{$state};
111 # ------------------------------------------------------------[ print_usage ]--
114 print "Usage: $PROGNAME -H <host> [-w <warn>] [-c <crit>] [-p <port>]\n";
117 # -------------------------------------------------------------[ print_help ]--
121 print_revision($PROGNAME,'@NP_VERSION@');
122 print "Copyright (c) 2000 Richard Mayhew/Karl DeBisschop
124 Perl Check IRCD plugin for Nagios
130 Name or IP address of host to check
131 -w, --warning=INTEGER
132 Number of connected users which generates a warning state (Default: 50)
133 -c, --critical=INTEGER
134 Number of connected users which generates a critical state (Default: 100)
136 Port that the ircd daemon is running on <host> (Default: 6667)
138 Print extra debugging information
142 # -------------------------------------------------------------[ bindRemote ]--
146 my ($in_remotehost, $in_remoteport) = @_;
147 my $proto = getprotobyname('tcp');
150 my ($name, $aliases,$type,$len,$thataddr) = gethostbyname($in_remotehost);
152 if (!socket(ClientSocket,AF_INET, SOCK_STREAM, $proto)) {
153 print "IRCD UNKNOWN: Could not start socket ($!)\n";
154 exit $ERRORS{"UNKNOWN"};
156 $sockaddr = 'S n a4 x8';
157 $that = pack($sockaddr, AF_INET, $in_remoteport, $thataddr);
158 if (!connect(ClientSocket, $that)) {
159 print "IRCD UNKNOWN: Could not connect socket ($!)\n";
160 exit $ERRORS{"UNKNOWN"};
162 select(ClientSocket); $| = 1; select(STDOUT);
163 return \*ClientSocket;
166 # ===================================================================[ MAIN ]==
172 Getopt::Long::Configure('bundling');
174 ("V" => \$opt_V, "version" => \$opt_V,
175 "h" => \$opt_h, "help" => \$opt_h,
176 "v" => \$verbose,"verbose" => \$verbose,
177 "t=i" => \$opt_t, "timeout=i" => \$opt_t,
178 "w=i" => \$opt_w, "warning=i" => \$opt_w,
179 "c=i" => \$opt_c, "critical=i" => \$opt_c,
180 "p=i" => \$opt_p, "port=i" => \$opt_p,
181 "H=s" => \$opt_H, "hostname=s" => \$opt_H);
184 print_revision($PROGNAME,'@NP_VERSION@');
188 if ($opt_h) {print_help(); exit $ERRORS{'OK'};}
190 ($opt_H) || ($opt_H = shift @ARGV) || usage("Host name/address not specified\n");
191 my $remotehost = $1 if ($opt_H =~ /([-.A-Za-z0-9]+)/);
192 ($remotehost) || usage("Invalid host: $opt_H\n");
194 ($opt_w) || ($opt_w = shift @ARGV) || ($opt_w = 50);
195 my $warn = $1 if ($opt_w =~ /^([0-9]+)$/);
196 ($warn) || usage("Invalid warning threshold: $opt_w\n");
198 ($opt_c) || ($opt_c = shift @ARGV) || ($opt_c = 100);
199 my $crit = $1 if ($opt_c =~ /^([0-9]+)$/);
200 ($crit) || usage("Invalid critical threshold: $opt_c\n");
202 ($opt_p) || ($opt_p = shift @ARGV) || ($opt_p = 6667);
203 my $remoteport = $1 if ($opt_p =~ /^([0-9]+)$/);
204 ($remoteport) || usage("Invalid port: $opt_p\n");
206 if ($opt_t && $opt_t =~ /^([0-9]+)$/) { $TIMEOUT = $1; }
208 # Just in case of problems, let's not hang Nagios
210 print "Somthing is Taking a Long Time, Increase Your TIMEOUT (Currently Set At $TIMEOUT Seconds)\n";
211 exit $ERRORS{"UNKNOWN"};
216 my ($name, $alias, $proto) = getprotobyname('tcp');
218 print "MAIN(debug): binding to remote host: $remotehost -> $remoteport\n" if $verbose;
219 my $ClientSocket = &bindRemote($remotehost,$remoteport);
221 print ClientSocket "NICK $NICK\nUSER $USER_INFO\n";
223 while (<ClientSocket>) {
224 print "MAIN(debug): default var = $_\n" if $verbose;
226 # DALnet,LagNet,UnderNet etc. Require this!
227 # Replies with a PONG when presented with a PING query.
228 # If a server doesn't require it, it will be ignored.
230 if (m/^PING (.*)/) {print ClientSocket "PONG $1\n";}
234 # Look for pattern in IRCD Output to gather Client Connections total.
235 connection($remotehost,$1,$warn,$crit) if (m/:I have\s+(\d+)/);
237 print "IRCD UNKNOWN: Unknown error - maybe could not authenticate\n";
238 exit $ERRORS{"UNKNOWN"};