-
Notifications
You must be signed in to change notification settings - Fork 18
/
check_url.pl
executable file
·91 lines (73 loc) · 1.61 KB
/
check_url.pl
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
#!/usr/bin/perl
use strict;
my $wget = '/usr/bin/wget --output-document=/tmp/tmp_html --no-check-certificate -S';
my ($url) = @ARGV;
my @OK = ("200");
my @WARN = ("400", "401", "403", "404", "408");
my @CRITICAL = ("500", "501", "502", "503", "504");
my $TIMEOUT = 20;
my %ERRORS = ('UNKNOWN' , '-1',
'OK' , '0',
'WARNING', '1',
'CRITICAL', '2');
my $state = "UNKNOWN";
my $answer = "";
$SIG{'ALRM'} = sub {
print ("ERROR: check_url Time-Out $TIMEOUT s \n");
exit $ERRORS{"UNKNOWN"};
};
alarm($TIMEOUT);
system ("$wget $url 2>/tmp/tmp_res1");
for (1..1000){
}
if (! open STAT1, "/tmp/tmp_res1") {
print ("$state: $wget returns no result!");
exit $ERRORS{$state};
}
close STAT1;
`cat /tmp/tmp_res1|grep 'HTTP/1'|tail -n 1 >/tmp/tmp_res`;
open (STAT, "/tmp/tmp_res");
my @lines = <STAT>;
close STAT;
if ($lines[0]=~/HTTP\/1\.\d+ (\d+)( .*)/) {
my $errcode = $1;
my $errmesg = $2;
$answer = $answer . "$errcode $errmesg";
if ('1' eq &chkerrwarn($errcode) ) {
$state = 'WARNING';
} elsif ('2' eq &chkerrcritical($errcode)) {
$state = 'CRITICAL';
} elsif ('0' eq &chkerrok($errcode)) {
$state = 'OK';
}
}
sub chkerrcritical {
my $err = $1;
foreach (@CRITICAL){
if ($_ eq $err) {
return 2;
}
}
return -1;
}
sub chkerrwarn {
my $err = $1;
foreach (@WARN){
if ($_ eq $err) {
return 1;
}
}
return -1;
}
sub chkerrok {
my $err = $1;
foreach (@OK){
if ($_ eq $err) {
return 0;
}
}
return -1;
}
`rm /tmp/tmp_html /tmp/tmp_res /tmp/tmp_res1`;
print ("$url $state: $answer\n");
exit $ERRORS{$state};