Merge pull request #2006 from monitoring-plugins/check_curl_features
[monitoring-plugins.git] / plugins-scripts / check_ircd.pl
blob15a708021c2073a5a91d0511e3898b14fc7fd8cc
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.14.0;
45 # -------------------------------------------------------------------[ Uses ]--
47 use strict;
48 use IO::Socket::IP;
49 use Getopt::Long;
50 use vars qw($opt_V $opt_h $opt_t $opt_p $opt_H $opt_w $opt_c $opt_4 $opt_6 $verbose);
51 use vars qw($PROGNAME);
52 use vars qw($ClientSocket);
53 use FindBin;
54 use lib "$FindBin::Bin";
55 use utils qw($TIMEOUT %ERRORS &print_revision &support &usage);
57 # ----------------------------------------------------[ Function Prototypes ]--
59 sub print_help ();
60 sub print_usage ();
61 sub connection ($$$$);
63 # -------------------------------------------------------------[ Environment ]--
65 $ENV{'PATH'}='@TRUSTED_PATH@';
66 $ENV{'BASH_ENV'}='';
67 $ENV{'ENV'}='';
69 # -----------------------------------------------------------------[ Global ]--
71 $PROGNAME = "check_ircd";
72 # nickname shouldn't be longer than 9 chars, this might happen with large PIDs
73 # To prevent this, we cut of the part over 10000
74 my $NICK="ircd" . $$ % 10000;
75 my $USER_INFO="monitor localhost localhost : ";
77 # -------------------------------------------------------------[ connection ]--
78 sub connection ($$$$)
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";
102 } else {
103 $state = "UNKNOWN";
104 $answer = "Server $in_remotehost has less than 0 users! Something is Really WRONG!\n";
107 print $ClientSocket "quit\n";
108 print $answer;
109 exit $ERRORS{$state};
112 # ------------------------------------------------------------[ print_usage ]--
114 sub print_usage () {
115 print "Usage: $PROGNAME -H <host> [-w <warn>] [-c <crit>] [-p <port>] [ -4|-6 ]\n";
118 # -------------------------------------------------------------[ print_help ]--
120 sub print_help ()
122 print_revision($PROGNAME,'@NP_VERSION@');
123 print "Copyright (c) 2000 Richard Mayhew/Karl DeBisschop
125 Perl Check IRCD plugin for monitoring
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 -4, --use-ipv4
139 Use IPv4 connection
140 -6, --use-ipv6
141 Use IPv6 connection
142 -v, --verbose
143 Print extra debugging information
147 # ===================================================================[ MAIN ]==
149 MAIN:
151 my $hostname;
153 Getopt::Long::Configure('bundling');
154 GetOptions
155 ("V" => \$opt_V, "version" => \$opt_V,
156 "h" => \$opt_h, "help" => \$opt_h,
157 "v" => \$verbose,"verbose" => \$verbose,
158 "t=i" => \$opt_t, "timeout=i" => \$opt_t,
159 "w=i" => \$opt_w, "warning=i" => \$opt_w,
160 "c=i" => \$opt_c, "critical=i" => \$opt_c,
161 "p=i" => \$opt_p, "port=i" => \$opt_p,
162 "4" => \$opt_4, "use-ipv4" => \$opt_4,
163 "6" => \$opt_6, "use-ipv6" => \$opt_6,
164 "H=s" => \$opt_H, "hostname=s" => \$opt_H);
166 if ($opt_V) {
167 print_revision($PROGNAME,'@NP_VERSION@');
168 exit $ERRORS{'UNKNOWN'};
171 if ($opt_h) {print_help(); exit $ERRORS{'UNKNOWN'};}
173 ($opt_H) || ($opt_H = shift @ARGV) || usage("Host name/address not specified\n");
174 my $remotehost = $1 if ($opt_H =~ /([-.:%A-Za-z0-9]+)/);
175 ($remotehost) || usage("Invalid host: $opt_H\n");
177 ($opt_w) || ($opt_w = shift @ARGV) || ($opt_w = 50);
178 my $warn = $1 if ($opt_w =~ /^([0-9]+)$/);
179 ($warn) || usage("Invalid warning threshold: $opt_w\n");
181 ($opt_c) || ($opt_c = shift @ARGV) || ($opt_c = 100);
182 my $crit = $1 if ($opt_c =~ /^([0-9]+)$/);
183 ($crit) || usage("Invalid critical threshold: $opt_c\n");
185 ($opt_p) || ($opt_p = shift @ARGV) || ($opt_p = 6667);
186 my $remoteport = $1 if ($opt_p =~ /^([0-9]+)$/);
187 ($remoteport) || usage("Invalid port: $opt_p\n");
189 if ($opt_t && $opt_t =~ /^([0-9]+)$/) { $TIMEOUT = $1; }
191 # Just in case of problems, let's not hang the monitoring system
192 $SIG{'ALRM'} = sub {
193 print "Something is Taking a Long Time, Increase Your TIMEOUT (Currently Set At $TIMEOUT Seconds)\n";
194 exit $ERRORS{"UNKNOWN"};
197 alarm($TIMEOUT);
199 print "MAIN(debug): binding to remote host: $remotehost -> $remoteport\n" if $verbose;
200 $ClientSocket = IO::Socket::IP->new(
201 PeerHost => $remotehost,
202 PeerService => $remoteport,
203 Family => $opt_4 ? AF_INET : $opt_6 ? AF_INET6 : undef,
204 Type => SOCK_STREAM,
206 if (!$ClientSocket) {
207 print "IRCD UNKNOWN: Could not start socket ($!)\n";
208 exit $ERRORS{"UNKNOWN"};
211 print $ClientSocket "NICK $NICK\nUSER $USER_INFO\n";
213 while (<$ClientSocket>) {
214 print "MAIN(debug): default var = $_\n" if $verbose;
216 # DALnet,LagNet,UnderNet etc. Require this!
217 # Replies with a PONG when presented with a PING query.
218 # If a server doesn't require it, it will be ignored.
220 if (m/^PING (.*)/) {print $ClientSocket "PONG $1\n";}
222 alarm(0);
224 # Look for pattern in IRCD Output to gather Client Connections total.
225 connection($remotehost,$1,$warn,$crit) if (m/:I have\s+(\d+)/);
227 print "IRCD UNKNOWN: Unknown error - maybe could not authenticate\n";
228 exit $ERRORS{"UNKNOWN"};