-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_syslog.pl
60 lines (51 loc) · 1.38 KB
/
check_syslog.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
#!/usr/bin/perl -w
use Getopt::Long;
use Sys::Syslog qw( :DEFAULT setlogsock);
use Sys::Hostname;
use IO::Socket;
use threads;
use Digest::MD5 qw(md5 md5_base64 md5_hex);
use warnings;
use strict;
my $timeout = 10;
my $syslog_server = "127.0.0.1";
my $local_port = 2000;
my $syslog_facility = "local6";
my $syslog_level = "info";
GetOptions("timeout=i" => \$timeout, "ip=s" => \$syslog_server, "facility=s" => \$syslog_facility, "level=s" => \$syslog_level, "local-port=i" => \$local_port);
my $syslog_ident = "nagios_syslog_check";
my $message = md5_hex("nagios" . hostname . time);
sub udp_socket {
my $server = IO::Socket::INET->new(LocalPort => $local_port, Proto => "udp")
or die "Couldn't be a udp server on port $local_port : $@\n";
my $MAX_TO_READ=1024;
my $datagram = "";
while ($server->recv($datagram, $MAX_TO_READ)) {
return 1 if ($datagram =~ m/$message/);
}
}
sub send_log {
setlogsock("udp", $syslog_server);
openlog($syslog_ident, "nofatal", $syslog_facility);
syslog($syslog_level, $message);
closelog;
}
my $t = threads->new(\&udp_socket);
sleep 1;
send_log;
sleep $timeout;
if ($t->is_joinable()) {
if ($t->join()) {
print "Syslog in $syslog_server OK\n";
exit 0;
}
else {
print "Invalid response from Syslog in $syslog_server\n";
exit 2;
}
}
else {
$t->detach();
print "No response from the Syslog in $syslog_server\n";
exit 2;
}