aboutsummaryrefslogtreecommitdiff
path: root/contrib/aix/check_ping
blob: aaa8c84e9b336457efc6bfb53ac3435442f580af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/perl -w

#================================================================
#
# This perl script will accept an argument and simply pass it
# to ping.  It works by sending 2 ping to the specified host
# and evaluating on the average delta time of those 2 pings.
#
#    Author: SpEnTBoY 
#    Email: lonny@abyss.za.org
#    April 5,2000
#
#================================================================

#============================
# State predefined stuff and
#       requirements
#============================

require 5.004;
use POSIX;
use strict;

sub usage;

my $ipaddr = $ARGV[0];

my $TIMEOUT = 15;

my %ERRORS = ('UNKNOWN' , '-1',
              'OK' , '0',
              'WARNING', '1',
              'CRITICAL', '2');

my $remote = shift || &usage(%ERRORS);
my $warning = shift || 750;
my $critical = shift || 1000;

my $state = "OK";
my $answer = undef;
my $offset = undef;
my $line = undef;

#============================================================
# If theres no response we can exit the bloody thing cleanly
# last thing I want to do is hang an AIX system ;-)
#============================================================

$SIG{'ALRM'} = sub {
     print ("ERROR: No response from PING! (alarm)\n");
     exit $ERRORS{"UNKNOWN"};
};
alarm($TIMEOUT);

#================================================
# Pass stddn from $ARGV to the command and parse
# the info we need (namely the value for "max"
#================================================



open(PING,"/usr/sbin/ping -c 2 '$ipaddr' >&1|");
while (<PING>) {
        $line = $_;
	if (/round-trip min\/avg\/max = (.+)\/(.+)\/(.+) ms/) {
        $offset = $3;
        last;
    }
}

#==================================================
# Do some error checking on the output of the file
# and implement values for <crit> and <warn> 
# deffinitions if they were specified by the user
# or sub in the predefined ones
#==================================================

if (defined $offset) {
    if (abs($offset) > $warning) {
        if (abs($offset) > $critical) {
            $state = "CRITICAL";
            $answer = ": Ping Time $offset MS greater than +/- $critical MS\n";
        } else {
            $state = "WARNING";
            $answer = ": Ping Time $offset MS greater than +/- $warning MS\n";
        }
    } else {
        $state = "OK";
        $answer = ": Ping Time $offset MS\n";
    }
} else {
    $state = "UNKNOWN";
    $answer = ": $line\n";
}
print ("$state$answer");
exit $ERRORS{$state};

sub usage {
	print "\n";
        print "#=========================================\n";
        print "Check_Ping 0.02 script by Lonny Selinger\n";
        print "Made with AIX in mind ;-)\n";
        print "#=========================================\n";
        print "\n";
        print "#================================================\n";
	print " I'm going to need a few more arguments from you\n";
        print "#================================================\n";
	print "\n";
        print "#================================================\n";
        print "Usage: check_ping <host> [<warn> [<crit>]\n";
        print "#================================================\n";
        print "\n";
        print "<warn> = Ping in MS at which a warning message will be generated.\n      Defaults to 750.\n";
        print "<crit> = Ping in MS at which a critical message will be generated.\n     Defaults to 1000.\n\n";
        exit $ERRORS{"UNKNOWN"};
}