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