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, $in_hostname) = @_;
147 my $proto = getprotobyname('tcp');
150 my $thisaddr = gethostbyname($in_hostname);
152 my ($name, $aliases,$type,$len,$thataddr) = gethostbyname($in_remotehost);
153 # ($name,$aliases,$type,$len,$thisaddr) = gethostbyname($in_hostname);
155 if (!socket(ClientSocket,AF_INET, SOCK_STREAM, $proto)) {
156 print "IRCD UNKNOWN: Could not start socket ($!)\n";
157 exit $ERRORS{"UNKNOWN"};
159 $sockaddr = 'S n a4 x8';
160 $this = pack($sockaddr, AF_INET, 0, $thisaddr);
161 $that = pack($sockaddr, AF_INET, $in_remoteport, $thataddr);
162 if (!bind(ClientSocket, $this)) {
163 print "IRCD UNKNOWN: Could not bind socket ($!)\n";
164 exit $ERRORS{"UNKNOWN"};
166 if (!connect(ClientSocket, $that)) {
167 print "IRCD UNKNOWN: Could not connect socket ($!)\n";
168 exit $ERRORS{"UNKNOWN"};
170 select(ClientSocket); $| = 1; select(STDOUT);
171 return \*ClientSocket;
174 # ===================================================================[ MAIN ]==
180 Getopt::Long::Configure('bundling');
182 ("V" => \$opt_V, "version" => \$opt_V,
183 "h" => \$opt_h, "help" => \$opt_h,
184 "v" => \$verbose,"verbose" => \$verbose,
185 "t=i" => \$opt_t, "timeout=i" => \$opt_t,
186 "w=i" => \$opt_w, "warning=i" => \$opt_w,
187 "c=i" => \$opt_c, "critical=i" => \$opt_c,
188 "p=i" => \$opt_p, "port=i" => \$opt_p,
189 "H=s" => \$opt_H, "hostname=s" => \$opt_H);
192 print_revision($PROGNAME,'@NP_VERSION@');
196 if ($opt_h) {print_help(); exit $ERRORS{'OK'};}
198 ($opt_H) || ($opt_H = shift) || usage("Host name/address not specified\n");
199 my $remotehost = $1 if ($opt_H =~ /([-.A-Za-z0-9]+)/);
200 ($remotehost) || usage("Invalid host: $opt_H\n");
202 ($opt_w) || ($opt_w = shift) || ($opt_w = 50);
203 my $warn = $1 if ($opt_w =~ /^([0-9]+)$/);
204 ($warn) || usage("Invalid warning threshold: $opt_w\n");
206 ($opt_c) || ($opt_c = shift) || ($opt_c = 100);
207 my $crit = $1 if ($opt_c =~ /^([0-9]+)$/);
208 ($crit) || usage("Invalid critical threshold: $opt_c\n");
210 ($opt_p) || ($opt_p = shift) || ($opt_p = 6667);
211 my $remoteport = $1 if ($opt_p =~ /^([0-9]+)$/);
212 ($remoteport) || usage("Invalid port: $opt_p\n");
214 if ($opt_t && $opt_t =~ /^([0-9]+)$/) { $TIMEOUT = $1; }
216 # Just in case of problems, let's not hang Nagios
218 print "Somthing is Taking a Long Time, Increase Your TIMEOUT (Currently Set At $TIMEOUT Seconds)\n";
219 exit $ERRORS{"UNKNOWN"};
224 chomp($hostname = `/bin/hostname`);
225 $hostname = $1 if ($hostname =~ /([-.a-zA-Z0-9]+)/);
226 my ($name, $alias, $proto) = getprotobyname('tcp');
227 print "MAIN(debug): hostname = $hostname\n" if $verbose;
229 print "MAIN(debug): binding to remote host: $remotehost -> $remoteport -> $hostname\n" if $verbose;
230 my $ClientSocket = &bindRemote($remotehost,$remoteport,$hostname);
232 print ClientSocket "NICK $NICK\nUSER $USER_INFO\n";
234 while (<ClientSocket>) {
235 print "MAIN(debug): default var = $_\n" if $verbose;
237 # DALnet,LagNet,UnderNet etc. Require this!
238 # Replies with a PONG when presented with a PING query.
239 # If a server doesn't require it, it will be ignored.
241 if (m/^PING (.*)/) {print ClientSocket "PONG $1\n";}
245 # Look for pattern in IRCD Output to gather Client Connections total.
246 connection($remotehost,$1,$warn,$crit) if (m/:I have\s+(\d+)/);
248 print "IRCD UNKNOWN: Unknown error - maybe could not authenticate\n";
249 exit $ERRORS{"UNKNOWN"};