Created
January 20, 2011 09:59
-
-
Save saillinux/787677 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use AnyEvent::Socket; | |
use constant THROTTLE => 3; # Number of count alert will be sent out and die | |
use constant INTERVAL => 60; # Interval for polling health check | |
use constant COOLTIME => 600; # once host down is detected cooldown for amount of time | |
my ($host, $port) = @ARGV; | |
die "No host name is given" unless defined $host; | |
die "No port numb is given" unless defined $port; | |
my $cnt = 1; # for throttling number of alerting | |
my $cv = AnyEvent->condvar; | |
my $once_per_second; | |
$once_per_second = AnyEvent->timer(interval => INTERVAL, | |
cb => \&port_check, | |
); | |
$cv->wait; | |
sub port_check { | |
my $done = AnyEvent->condvar; | |
tcp_connect $host, $port, sub { | |
my ($fh) = @_ or do { | |
my $time = localtime; | |
warn "The host or service is down with [$!] at [$time]\n"; # print 'timeout' at most every second | |
exit if $cnt++ == THROTTLE; | |
undef $once_per_second; | |
$once_per_second = AnyEvent->timer(after => COOLTIME, | |
interval => INTERVAL, | |
cb => \&port_check, | |
); | |
return; | |
}; | |
my $time = localtime; | |
warn "Host($host) is alive at [$time]"; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment