[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
1 #! /usr/bin/perl -wT 2 3 # ----------------------------------------------------------------------------- 4 # File Name: check_ircd.pl 5 # 6 # Author: Richard Mayhew - South Africa 7 # 8 # Date: 1999/09/20 9 # 10 # $Id: check_ircd.pl,v 1.3 2002/05/07 05:35:49 sghosh Exp $ 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 # 43 44 # ----------------------------------------------------------------[ Require ]-- 45 46 require 5.004; 47 48 # -------------------------------------------------------------------[ Uses ]-- 49 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 "/usr/lib/nagios/plugins"; 56 use utils qw($TIMEOUT %ERRORS &print_revision &support &usage); 57 58 # ----------------------------------------------------[ Function Prototypes ]-- 59 60 sub print_help (); 61 sub print_usage (); 62 sub connection ($$$$); 63 sub bindRemote ($$$); 64 65 # -------------------------------------------------------------[ Enviroment ]-- 66 67 $ENV{PATH} = ""; 68 $ENV{ENV} = ""; 69 $ENV{BASH_ENV} = ""; 70 71 # -----------------------------------------------------------------[ Global ]-- 72 73 $PROGNAME = "check_ircd"; 74 my $NICK="ircd$$"; 75 my $USER_INFO="monitor localhost localhost : "; 76 77 # -------------------------------------------------------------[ connection ]-- 78 sub connection ($$$$) 79 { 80 my ($in_remotehost,$in_users,$in_warn,$in_crit) = @_; 81 my $state; 82 my $answer; 83 84 print "connection(debug): users = $in_users\n" if $verbose; 85 $in_users =~ s/\ //g; 86 87 if ($in_users >= 0) { 88 89 if ($in_users > $in_crit) { 90 $state = "CRITICAL"; 91 $answer = "Critical Number Of Clients Connected : $in_users (Limit = $in_crit)\n"; 92 93 } elsif ($in_users > $in_warn) { 94 $state = "WARNING"; 95 $answer = "Warning Number Of Clients Connected : $in_users (Limit = $in_warn)\n"; 96 97 } else { 98 $state = "OK"; 99 $answer = "IRCD ok - Current Local Users: $in_users\n"; 100 } 101 102 } else { 103 $state = "UNKNOWN"; 104 $answer = "Server $in_remotehost has less than 0 users! Something is Really WRONG!\n"; 105 } 106 107 print ClientSocket "quit\n"; 108 print $answer; 109 exit $ERRORS{$state}; 110 } 111 112 # ------------------------------------------------------------[ print_usage ]-- 113 114 sub print_usage () { 115 print "Usage: $PROGNAME -H <host> [-w <warn>] [-c <crit>] [-p <port>]\n"; 116 } 117 118 # -------------------------------------------------------------[ print_help ]-- 119 120 sub print_help () 121 { 122 print_revision($PROGNAME,'$Revision: 1.3 $ '); 123 print "Copyright (c) 2000 Richard Mayhew/Karl DeBisschop 124 125 Perl Check IRCD plugin for Nagios 126 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 } 142 143 # -------------------------------------------------------------[ bindRemote ]-- 144 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 $that; 152 my ($name, $aliases,$type,$len,$thataddr) = gethostbyname($in_remotehost); 153 # ($name,$aliases,$type,$len,$thisaddr) = gethostbyname($in_hostname); 154 155 if (!socket(ClientSocket,AF_INET, SOCK_STREAM, $proto)) { 156 print "IRCD UNKNOWN: Could not start socket ($!)\n"; 157 exit $ERRORS{"UNKNOWN"}; 158 } 159 $sockaddr = 'S n a4 x8'; 160 $this = pack($sockaddr, AF_INET, 0, INADDR_ANY); 161 $that = pack($sockaddr, AF_INET, $in_remoteport, $thataddr); 162 if (!bind(ClientSocket, $this)) { 163 print "IRCD UNKNOWN: Could not bind socket ($!)\n"; 164 exit $ERRORS{"UNKNOWN"}; 165 } 166 if (!connect(ClientSocket, $that)) { 167 print "IRCD UNKNOWN: Could not connect socket ($!)\n"; 168 exit $ERRORS{"UNKNOWN"}; 169 } 170 select(ClientSocket); $| = 1; select(STDOUT); 171 return \*ClientSocket; 172 } 173 174 # ===================================================================[ MAIN ]== 175 176 MAIN: 177 { 178 my $hostname; 179 180 Getopt::Long::Configure('bundling'); 181 GetOptions 182 ("V" => \$opt_V, "version" => \$opt_V, 183 "h" => \$opt_h, "help" => \$opt_h, 184 "v" => \$verbose,"verbose" => \$verbose, 185 "t=i" => \$opt_t, "timeout=i" => \$opt_t, 186 "w=i" => \$opt_w, "warning=i" => \$opt_w, 187 "c=i" => \$opt_c, "critical=i" => \$opt_c, 188 "p=i" => \$opt_p, "port=i" => \$opt_p, 189 "H=s" => \$opt_H, "hostname=s" => \$opt_H); 190 191 if ($opt_V) { 192 print_revision($PROGNAME,'$Revision: 1.3 $ '); 193 exit $ERRORS{'OK'}; 194 } 195 196 if ($opt_h) {print_help(); exit $ERRORS{'OK'};} 197 198 ($opt_H) || ($opt_H = shift) || usage("Host name/address not specified\n"); 199 my $remotehost = $1 if ($opt_H =~ /([-.A-Za-z0-9]+)/); 200 ($remotehost) || usage("Invalid host: $opt_H\n"); 201 202 ($opt_w) || ($opt_w = shift) || ($opt_w = 50); 203 my $warn = $1 if ($opt_w =~ /^([0-9]+)$/); 204 ($warn) || usage("Invalid warning threshold: $opt_w\n"); 205 206 ($opt_c) || ($opt_c = shift) || ($opt_c = 100); 207 my $crit = $1 if ($opt_c =~ /^([0-9]+)$/); 208 ($crit) || usage("Invalid critical threshold: $opt_c\n"); 209 210 ($opt_p) || ($opt_p = shift) || ($opt_p = 6667); 211 my $remoteport = $1 if ($opt_p =~ /^([0-9]+)$/); 212 ($remoteport) || usage("Invalid port: $opt_p\n"); 213 214 if ($opt_t && $opt_t =~ /^([0-9]+)$/) { $TIMEOUT = $1; } 215 216 # Just in case of problems, let's not hang Nagios 217 $SIG{'ALRM'} = sub { 218 print "Somthing is Taking a Long Time, Increase Your TIMEOUT (Currently Set At $TIMEOUT Seconds)\n"; 219 exit $ERRORS{"UNKNOWN"}; 220 }; 221 222 alarm($TIMEOUT); 223 224 chomp($hostname = `/bin/hostname`); 225 $hostname = $1 if ($hostname =~ /([-.a-zA-Z0-9]+)/); 226 my ($name, $alias, $proto) = getprotobyname('tcp'); 227 print "MAIN(debug): hostname = $hostname\n" if $verbose; 228 229 print "MAIN(debug): binding to remote host: $remotehost -> $remoteport -> $hostname\n" if $verbose; 230 my $ClientSocket = &bindRemote($remotehost,$remoteport,$hostname); 231 232 print ClientSocket "NICK $NICK\nUSER $USER_INFO\n"; 233 234 while (<ClientSocket>) { 235 print "MAIN(debug): default var = $_\n" if $verbose; 236 237 # DALnet,LagNet,UnderNet etc. Require this! 238 # Replies with a PONG when presented with a PING query. 239 # If a server doesn't require it, it will be ignored. 240 241 if (m/^PING (.*)/) {print ClientSocket "PONG $1\n";} 242 243 alarm(0); 244 245 # Look for pattern in IRCD Output to gather Client Connections total. 246 connection($remotehost,$1,$warn,$crit) if (m/:I have\s+(\d+)/); 247 } 248 print "IRCD UNKNOWN: Unknown error - maybe could not authenticate\n"; 249 exit $ERRORS{"UNKNOWN"}; 250 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Tue Mar 17 22:47:18 2015 | Cross-referenced by PHPXref 0.7.1 |