Merge pull request #1311 from waja/check_ntp_remove_unused_variables
[monitoring-plugins.git] / plugins-scripts / check_ircd.pl
blob22d21c2e7452e56d11751e2bda3069085a094811
1 #!@PERL@ -w
3 # -----------------------------------------------------------------------------
4 # File Name: check_ircd.pl
6 # Author: Richard Mayhew - South Africa
8 # Date: 1999/09/20
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 # If any changes are made to this script, please mail me a copy of the
20 # changes :)
22 # Some code taken from Charlie Cook (check_disk.pl)
24 # License GPL
26 # -----------------------------------------------------------------------------
27 # Date Author Reason
28 # ---- ------ ------
30 # 1999/09/20 RM Creation
32 # 1999/09/20 TP Changed script to use strict, more secure by
33 # specifying $ENV variables. The bind command is
34 # still insecure through. Did most of my work
35 # with perl -wT and 'use strict'
37 # test using check_ircd.pl (irc-2.mit.edu|irc.erols.com|irc.core.com)
38 # 2002/05/02 SG Fixed for Embedded Perl
41 # ----------------------------------------------------------------[ Require ]--
43 require 5.004;
45 # -------------------------------------------------------------------[ Uses ]--
47 use Socket;
48 use strict;
49 use Getopt::Long;
50 use vars qw($opt_V $opt_h $opt_t $opt_p $opt_H $opt_w $opt_c $verbose);
51 use vars qw($PROGNAME);
52 use FindBin;
53 use lib "$FindBin::Bin";
54 use utils qw($TIMEOUT %ERRORS &print_revision &support &usage);
56 # ----------------------------------------------------[ Function Prototypes ]--
58 sub print_help ();
59 sub print_usage ();
60 sub connection ($$$$);
61 sub bindRemote ($$);
63 # -------------------------------------------------------------[ Enviroment ]--
65 $ENV{'PATH'}='@TRUSTED_PATH@';
66 $ENV{'BASH_ENV'}='';
67 $ENV{'ENV'}='';
69 # -----------------------------------------------------------------[ Global ]--
71 $PROGNAME = "check_ircd";
72 my $NICK="ircd$$";
73 my $USER_INFO="monitor localhost localhost : ";
75 # -------------------------------------------------------------[ connection ]--
76 sub connection ($$$$)
78 my ($in_remotehost,$in_users,$in_warn,$in_crit) = @_;
79 my $state;
80 my $answer;
82 print "connection(debug): users = $in_users\n" if $verbose;
83 $in_users =~ s/\ //g;
85 if ($in_users >= 0) {
87 if ($in_users > $in_crit) {
88 $state = "CRITICAL";
89 $answer = "Critical Number Of Clients Connected : $in_users (Limit = $in_crit)\n";
91 } elsif ($in_users > $in_warn) {
92 $state = "WARNING";
93 $answer = "Warning Number Of Clients Connected : $in_users (Limit = $in_warn)\n";
95 } else {
96 $state = "OK";
97 $answer = "IRCD ok - Current Local Users: $in_users\n";
100 } else {
101 $state = "UNKNOWN";
102 $answer = "Server $in_remotehost has less than 0 users! Something is Really WRONG!\n";
105 print ClientSocket "quit\n";
106 print $answer;
107 exit $ERRORS{$state};
110 # ------------------------------------------------------------[ print_usage ]--
112 sub print_usage () {
113 print "Usage: $PROGNAME -H <host> [-w <warn>] [-c <crit>] [-p <port>]\n";
116 # -------------------------------------------------------------[ print_help ]--
118 sub print_help ()
120 print_revision($PROGNAME,'@NP_VERSION@');
121 print "Copyright (c) 2000 Richard Mayhew/Karl DeBisschop
123 Perl Check IRCD plugin for monitoring
126 print_usage();
127 print "
128 -H, --hostname=HOST
129 Name or IP address of host to check
130 -w, --warning=INTEGER
131 Number of connected users which generates a warning state (Default: 50)
132 -c, --critical=INTEGER
133 Number of connected users which generates a critical state (Default: 100)
134 -p, --port=INTEGER
135 Port that the ircd daemon is running on <host> (Default: 6667)
136 -v, --verbose
137 Print extra debugging information
141 # -------------------------------------------------------------[ bindRemote ]--
143 sub bindRemote ($$)
145 my ($in_remotehost, $in_remoteport) = @_;
146 my $proto = getprotobyname('tcp');
147 my $sockaddr;
148 my $that;
149 my ($name, $aliases,$type,$len,$thataddr) = gethostbyname($in_remotehost);
151 if (!socket(ClientSocket,AF_INET, SOCK_STREAM, $proto)) {
152 print "IRCD UNKNOWN: Could not start socket ($!)\n";
153 exit $ERRORS{"UNKNOWN"};
155 $sockaddr = 'S n a4 x8';
156 $that = pack($sockaddr, AF_INET, $in_remoteport, $thataddr);
157 if (!connect(ClientSocket, $that)) {
158 print "IRCD UNKNOWN: Could not connect socket ($!)\n";
159 exit $ERRORS{"UNKNOWN"};
161 select(ClientSocket); $| = 1; select(STDOUT);
162 return \*ClientSocket;
165 # ===================================================================[ MAIN ]==
167 MAIN:
169 my $hostname;
171 Getopt::Long::Configure('bundling');
172 GetOptions
173 ("V" => \$opt_V, "version" => \$opt_V,
174 "h" => \$opt_h, "help" => \$opt_h,
175 "v" => \$verbose,"verbose" => \$verbose,
176 "t=i" => \$opt_t, "timeout=i" => \$opt_t,
177 "w=i" => \$opt_w, "warning=i" => \$opt_w,
178 "c=i" => \$opt_c, "critical=i" => \$opt_c,
179 "p=i" => \$opt_p, "port=i" => \$opt_p,
180 "H=s" => \$opt_H, "hostname=s" => \$opt_H);
182 if ($opt_V) {
183 print_revision($PROGNAME,'@NP_VERSION@');
184 exit $ERRORS{'UNKNOWN'};
187 if ($opt_h) {print_help(); exit $ERRORS{'UNKNOWN'};}
189 ($opt_H) || ($opt_H = shift @ARGV) || usage("Host name/address not specified\n");
190 my $remotehost = $1 if ($opt_H =~ /([-.A-Za-z0-9]+)/);
191 ($remotehost) || usage("Invalid host: $opt_H\n");
193 ($opt_w) || ($opt_w = shift @ARGV) || ($opt_w = 50);
194 my $warn = $1 if ($opt_w =~ /^([0-9]+)$/);
195 ($warn) || usage("Invalid warning threshold: $opt_w\n");
197 ($opt_c) || ($opt_c = shift @ARGV) || ($opt_c = 100);
198 my $crit = $1 if ($opt_c =~ /^([0-9]+)$/);
199 ($crit) || usage("Invalid critical threshold: $opt_c\n");
201 ($opt_p) || ($opt_p = shift @ARGV) || ($opt_p = 6667);
202 my $remoteport = $1 if ($opt_p =~ /^([0-9]+)$/);
203 ($remoteport) || usage("Invalid port: $opt_p\n");
205 if ($opt_t && $opt_t =~ /^([0-9]+)$/) { $TIMEOUT = $1; }
207 # Just in case of problems, let's not hang the monitoring system
208 $SIG{'ALRM'} = sub {
209 print "Somthing is Taking a Long Time, Increase Your TIMEOUT (Currently Set At $TIMEOUT Seconds)\n";
210 exit $ERRORS{"UNKNOWN"};
213 alarm($TIMEOUT);
215 my ($name, $alias, $proto) = getprotobyname('tcp');
217 print "MAIN(debug): binding to remote host: $remotehost -> $remoteport\n" if $verbose;
218 my $ClientSocket = &bindRemote($remotehost,$remoteport);
220 print ClientSocket "NICK $NICK\nUSER $USER_INFO\n";
222 while (<ClientSocket>) {
223 print "MAIN(debug): default var = $_\n" if $verbose;
225 # DALnet,LagNet,UnderNet etc. Require this!
226 # Replies with a PONG when presented with a PING query.
227 # If a server doesn't require it, it will be ignored.
229 if (m/^PING (.*)/) {print ClientSocket "PONG $1\n";}
231 alarm(0);
233 # Look for pattern in IRCD Output to gather Client Connections total.
234 connection($remotehost,$1,$warn,$crit) if (m/:I have\s+(\d+)/);
236 print "IRCD UNKNOWN: Unknown error - maybe could not authenticate\n";
237 exit $ERRORS{"UNKNOWN"};