Code

Fix for regex input of '|', being output causing problems with Nagios' parsing of
[nagiosplug.git] / contrib / check_sockets.pl
1 #! /usr/bin/perl
2 # ------------------------------------------------------------------------------
3 # File Name:            check_sockets.pl
4 # Author:               Richard Mayhew - South Africa
5 # Date:                 2000/07/11
6 # Version:              1.0
7 # Description:          This script will check to see how may open sockets
8 #                       a server has and waron respectivly
9 # Email:                netsaint@splash.co.za
10 # ------------------------------------------------------------------------------
11 # Copyright 1999 (c) Richard Mayhew
12 # Credits go to Ethan Galstad for coding Nagios
13 # If any changes are made to this script, please mail me a copy of the
14 # changes :)
15 # Some code taken from Charlie Cook (check_disk.pl)
16 # License GPL
17 #
18 # ------------------------------------------------------------------------------
19 # Date          Author          Reason
20 # ----          ------          ------
21 # 1999/09/20    RM              Creation
22 # 1999/09/20    TP              Changed script to use strict, more secure by
23 #                               specifying $ENV variables. The bind command is
24 #                               still insecure through.  Did most of my work
25 #                               with perl -wT and 'use strict'
26 #
27 # ------------------------------------------------------------------------------
29 # -----------------------------------------------------------------[ Require ]--
30 require 5.004;
31 # --------------------------------------------------------------------[ Uses ]--
32 use Socket;
33 use strict;
34 # --------------------------------------------------------------[ Enviroment ]--
35 $ENV{'PATH'}='/bin:/sbin:/usr/bin:/usr/sbin';
36 $ENV{BASH_ENV} = "";
37 # ------------------------------------------------------------------[ Global ]--
38 my $TIMEOUT = 20;
39 my %ERRORS = (
40         'UNKNOWN', '-1',
41         'OK', '0',
42         'WARNING', '1',
43         'CRITICAL', '2');
44 # --------------------------------------------------------------[ connection ]--
45 sub connection
46 {
47         my ($in_total,$in_warn,$in_crit,$in_high) = @_;
48         my $state;
49         my $answer;
51         $in_total =~ s/\ //g;
52         if ($in_total >= 0) {
54                 if ($in_total > $in_crit) {
55                         $state = "CRITICAL";
56                         $answer = "Critical Number Of Sockets Connected : $in_total (Limit = $in_crit)\n";
58                 } elsif ($in_total > $in_warn) {
59                         $state = "WARNING";
60                         $answer = "Warning Number Of Sockets Connected : $in_total (Limit = $in_warn)\n";
62                 } else {
63                         if ($in_high ne "") {
64                         $answer = "Sockets OK - Current Sockets: $in_total : $in_high\n";
65                         }
66                         if ($in_high eq "") {
67                         $answer = "Sockets OK - Current Sockets: $in_total\n";
68                         }
69                         $state = "OK";
70                         }
72         } else {
73                 $state = "UNKNOWN";
74                 $answer = "Something is Really WRONG! Sockets Is A Negative Figure!\n";
75         }
76         
77         print $answer;
78         exit $ERRORS{$state};
79 }
81 # -------------------------------------------------------------------[ usage ]--
82 sub usage
83 {
84         print "Minimum arguments not supplied!\n";
85         print "\n";
86         print "Perl Check Sockets plugin for Nagios\n";
87         print "Copyright (c) 2000 Richard Mayhew\n";
88         print "\n";
89         print "Usage: check_sockets.pl <type> <warn> <crit>\n";
90         print "\n";
91         print "<type> = TOTAL, TCP, UDP, RAW.\n";
92         print "<warn> = Number of sockets connected at which a warning message will be generated.[Default = 256]\n";
93         print "<crit> = Number of sockets connected at which a critical message will be generated.[Default = 512]\n";
94         exit $ERRORS{"UNKNOWN"};
96 }
98 # ====================================================================[ MAIN ]==
99 MAIN:
101         my $type = shift || &usage;
102         my $warn = shift || 256;
103         my $crit = shift || 512;
104         my $data;       
105         my @data;
106         my $line;
107         my $data1;
108         my $data2;
109         my $data3;
110         my $junk;
111         my $total1;
112         my $total2;
113         $type = uc $type;
114         if ($type eq "TOTAL") {
115         $type = "sockets";
116         }
118         # Just in case of problems, let's not hang Nagios
119         $SIG{'ALRM'} = sub {
120                 print "Somthing is Taking a Long Time, Increase Your TIMEOUT (Currently Set At $TIMEOUT Seconds)\n";
121                 exit $ERRORS{"UNKNOWN"};
122         };
124         $data = `/bin/cat /proc/net/sockstat`;
125         @data = split("\n",$data);
126         alarm($TIMEOUT);
127         my $output = "";
128         my $high;
131         foreach $line (@data) {
132                 if ($line =~ /$type/) {
133                 ($data1,$data2,$data3) = split(" ",$line,3);    
135                         if ($data3 =~ /highest/){
136                         ($total1,$junk,$total2) = split(" ",$data3,3);  
137                         $output = $total1;
138                         $high = $total2;
139                         }
140                         else {$output = $data3;}
141         alarm(0);
142         connection($output,$warn,$crit,$high);
143                 }
144         }