-
Notifications
You must be signed in to change notification settings - Fork 2
/
onlinemon
executable file
·71 lines (62 loc) · 1.45 KB
/
onlinemon
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
#!/usr/bin/env perl
use strict;
use warnings;
use FindBin;
use lib $FindBin::Bin;
use Net::Ping::External qw(ping);
use POSIX qw(strftime);
use X::Osd;
# --------------------------
my $server = "nix.cz";
my $interval = 3;
my $osd_interval = 0;
my $color_on = "Gray";
my $color_off = "Red";
# --------------------------
# --------------------------
# --------------------------
my $osd = X::Osd->new(1);
$osd->set_font("-*-terminal-*-r-*-*-*-140-*-*-*-*-*-*");
$osd->set_timeout($osd_interval) if $osd_interval > 0;
$osd->set_pos(XOSD_top);
$osd->set_align(XOSD_left);
sub my_ping {
my $host = shift;
my $count = shift;
my $command = "ping -n -c $count $host";
my $devnull = "/dev/null";
$command .= " 1>$devnull 2>$devnull";
my $exit_status = system($command) >> 8;
return 1 if $exit_status == 0;
return 0;
}
sub pingtest {
my $host = shift;
return my_ping($host, 2);
# return Net::Ping::External::ping(host => $host, count => 2);
}
sub display_msg {
my $online = shift;
my $ms;
if($online){
$ms = "Online";
$osd->set_colour($color_on);
} else {
$ms = "OFFLINE";
$osd->set_colour($color_off);
}
print strftime("%Y%m%d-%H%M%S",localtime());
print ": ", $ms, "\n";
$osd->string(0, $ms);
}
my $last = -1;
my $ip = join(".", unpack("C4", (gethostbyname($server))[4]));
print "Pinging ", $ip, " [", $server, "] each $interval sec.\n";
while(1) {
my $res = pingtest($ip);
if($res != $last) {
$last = $res;
display_msg($res);
}
sleep($interval);
}