Code

Fix Debian bug #545940: Failure when run via ePN
[nagiosplug.git] / plugins-scripts / check_ircd.pl
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 #
11 # Description:          This script will check to see if an IRCD is running
12 #                       about how many users it has
13 #
14 # Email:                netsaint@splash.co.za
15 #
16 # -----------------------------------------------------------------------------
17 # Copyright 1999 (c) Richard Mayhew
18 #
19 # Credits go to Ethan Galstad for coding Nagios
20 #
21 # If any changes are made to this script, please mail me a copy of the
22 # changes :)
23 #
24 # Some code taken from Charlie Cook (check_disk.pl)
25 #
26 # License GPL
27 #
28 # -----------------------------------------------------------------------------
29 # Date          Author          Reason
30 # ----          ------          ------
31 #
32 # 1999/09/20    RM              Creation
33 #
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'
38 #
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
41 #
43 # ----------------------------------------------------------------[ Require ]--
45 require 5.004;
47 # -------------------------------------------------------------------[ Uses ]--
49 use Socket;
50 use strict;
51 use Getopt::Long;
52 use vars qw($opt_V $opt_h $opt_t $opt_p $opt_H $opt_w $opt_c $verbose);
53 use vars qw($PROGNAME);
54 use lib utils.pm;
55 use utils qw($TIMEOUT %ERRORS &print_revision &support &usage);
57 # ----------------------------------------------------[ Function Prototypes ]--
59 sub print_help ();
60 sub print_usage ();
61 sub connection ($$$$);
62 sub bindRemote ($$);
64 # -------------------------------------------------------------[ Enviroment ]--
66 $ENV{PATH} = "";
67 $ENV{ENV} = "";
68 $ENV{BASH_ENV} = "";
70 # -----------------------------------------------------------------[ Global ]--
72 $PROGNAME = "check_ircd";
73 my $NICK="ircd$$";
74 my $USER_INFO="monitor localhost localhost : ";
75         
76 # -------------------------------------------------------------[ connection ]--
77 sub connection ($$$$)
78 {
79         my ($in_remotehost,$in_users,$in_warn,$in_crit) = @_;
80         my $state;
81         my $answer;
83         print "connection(debug): users = $in_users\n" if $verbose;
84         $in_users =~ s/\ //g;
85         
86         if ($in_users >= 0) {
88                 if ($in_users > $in_crit) {
89                         $state = "CRITICAL";
90                         $answer = "Critical Number Of Clients Connected : $in_users (Limit = $in_crit)\n";
92                 } elsif ($in_users > $in_warn) {
93                         $state = "WARNING";
94                         $answer = "Warning Number Of Clients Connected : $in_users (Limit = $in_warn)\n";
96                 } else {
97                         $state = "OK";
98                         $answer = "IRCD ok - Current Local Users: $in_users\n";
99                 }
101         } else {
102                 $state = "UNKNOWN";
103                 $answer = "Server $in_remotehost has less than 0 users! Something is Really WRONG!\n";
104         }
105         
106         print ClientSocket "quit\n";
107         print $answer;
108         exit $ERRORS{$state};
111 # ------------------------------------------------------------[ print_usage ]--
113 sub print_usage () {
114         print "Usage: $PROGNAME -H <host> [-w <warn>] [-c <crit>] [-p <port>]\n";
117 # -------------------------------------------------------------[ print_help ]--
119 sub print_help ()
121         print_revision($PROGNAME,'@NP_VERSION@');
122         print "Copyright (c) 2000 Richard Mayhew/Karl DeBisschop
124 Perl Check IRCD plugin for Nagios
126 ";
127         print_usage();
128         print "
129 -H, --hostname=HOST
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)
135 -p, --port=INTEGER
136    Port that the ircd daemon is running on <host> (Default: 6667)
137 -v, --verbose
138    Print extra debugging information
139 ";
142 # -------------------------------------------------------------[ bindRemote ]--
144 sub bindRemote ($$)
146         my ($in_remotehost, $in_remoteport) = @_;
147         my $proto = getprotobyname('tcp');
148         my $sockaddr;
149         my $that;
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"};
155         }
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"};
161         }
162         select(ClientSocket); $| = 1; select(STDOUT);
163         return \*ClientSocket;
166 # ===================================================================[ MAIN ]==
168 MAIN:
170         my $hostname;
172         Getopt::Long::Configure('bundling');
173         GetOptions
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);
183         if ($opt_V) {
184                 print_revision($PROGNAME,'@NP_VERSION@');
185                 exit $ERRORS{'OK'};
186         }
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
209         $SIG{'ALRM'} = sub {
210                 print "Somthing is Taking a Long Time, Increase Your TIMEOUT (Currently Set At $TIMEOUT Seconds)\n";
211                 exit $ERRORS{"UNKNOWN"};
212         };
213         
214         alarm($TIMEOUT);
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);
220         
221         print ClientSocket "NICK $NICK\nUSER $USER_INFO\n";
222         
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.
229         
230                 if (m/^PING (.*)/) {print ClientSocket "PONG $1\n";}
231         
232                 alarm(0);
233         
234                 # Look for pattern in IRCD Output to gather Client Connections total.
235                 connection($remotehost,$1,$warn,$crit) if (m/:I have\s+(\d+)/);
236         }
237         print "IRCD UNKNOWN: Unknown error - maybe could not authenticate\n";
238         exit $ERRORS{"UNKNOWN"};