3
# -----------------------------------------------------------------------------
4
# File Name: check_ircd.pl
6
# Author: Richard Mayhew - South Africa
10
# $Id: check_ircd.pl,v 1.3 2002/05/07 05:35:49 sghosh Exp $
12
# Description: This script will check to see if an IRCD is running
13
# about how many users it has
15
# Email: netsaint@splash.co.za
17
# -----------------------------------------------------------------------------
18
# Copyright 1999 (c) Richard Mayhew
20
# Credits go to Ethan Galstad for coding Nagios
22
# If any changes are made to this script, please mail me a copy of the
25
# Some code taken from Charlie Cook (check_disk.pl)
29
# -----------------------------------------------------------------------------
33
# 1999/09/20 RM Creation
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'
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
44
# ----------------------------------------------------------------[ Require ]--
48
# -------------------------------------------------------------------[ Uses ]--
53
use vars qw($opt_V $opt_h $opt_t $opt_p $opt_H $opt_w $opt_c $verbose);
54
use vars qw($PROGNAME);
56
use utils qw($TIMEOUT %ERRORS &print_revision &support &usage);
58
# ----------------------------------------------------[ Function Prototypes ]--
62
sub connection ($$$$);
65
# -------------------------------------------------------------[ Enviroment ]--
71
# -----------------------------------------------------------------[ Global ]--
73
$PROGNAME = "check_ircd";
75
my $USER_INFO="monitor localhost localhost : ";
77
# -------------------------------------------------------------[ connection ]--
80
my ($in_remotehost,$in_users,$in_warn,$in_crit) = @_;
84
print "connection(debug): users = $in_users\n" if $verbose;
89
if ($in_users > $in_crit) {
91
$answer = "Critical Number Of Clients Connected : $in_users (Limit = $in_crit)\n";
93
} elsif ($in_users > $in_warn) {
95
$answer = "Warning Number Of Clients Connected : $in_users (Limit = $in_warn)\n";
99
$answer = "IRCD ok - Current Local Users: $in_users\n";
104
$answer = "Server $in_remotehost has less than 0 users! Something is Really WRONG!\n";
107
print ClientSocket "quit\n";
109
exit $ERRORS{$state};
112
# ------------------------------------------------------------[ print_usage ]--
115
print "Usage: $PROGNAME -H <host> [-w <warn>] [-c <crit>] [-p <port>]\n";
118
# -------------------------------------------------------------[ print_help ]--
122
print_revision($PROGNAME,'$Revision: 1.3 $ ');
123
print "Copyright (c) 2000 Richard Mayhew/Karl DeBisschop
125
Perl Check IRCD plugin for Nagios
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)
137
Port that the ircd daemon is running on <host> (Default: 6667)
139
Print extra debugging information
143
# -------------------------------------------------------------[ bindRemote ]--
147
my ($in_remotehost, $in_remoteport, $in_hostname) = @_;
148
my $proto = getprotobyname('tcp');
151
my $thisaddr = gethostbyname($in_hostname);
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"};
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"};
167
if (!connect(ClientSocket, $that)) {
168
print "IRCD UNKNOWN: Could not connect socket ($!)\n";
169
exit $ERRORS{"UNKNOWN"};
171
select(ClientSocket); $| = 1; select(STDOUT);
172
return \*ClientSocket;
175
# ===================================================================[ MAIN ]==
181
Getopt::Long::Configure('bundling');
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);
193
print_revision($PROGNAME,'$Revision: 1.3 $ ');
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
219
print "Somthing is Taking a Long Time, Increase Your TIMEOUT (Currently Set At $TIMEOUT Seconds)\n";
220
exit $ERRORS{"UNKNOWN"};
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";}
246
# Look for pattern in IRCD Output to gather Client Connections total.
247
connection($remotehost,$1,$warn,$crit) if (m/:I have\s+(\d+)/);
249
print "IRCD UNKNOWN: Unknown error - maybe could not authenticate\n";
250
exit $ERRORS{"UNKNOWN"};