-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathpingtest.pl
More file actions
executable file
·124 lines (106 loc) · 3.2 KB
/
pingtest.pl
File metadata and controls
executable file
·124 lines (106 loc) · 3.2 KB
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
118
119
120
121
122
123
124
#!/usr/bin/perl -w
# This example scripts shows how to use Pingmachine so that you get an immediate feedback
# about ping results (to react as soon as possible)
#
# Note that it is rather complicated, because of that requirements (immediate
# notification). You shouldn't do this, unless you need to...
use strict;
use AnyEvent;
use EV;
use Linux::Inotify2;
use Digest::MD5 qw(md5_hex);
use YAML::XS qw(LoadFile);
use POSIX qw(strftime);
my $step = 1; # write results every $step interval
my $ping_ip = '8.8.8.8';
my $orders_dir = '/var/lib/pingmachine/orders';
my $output_base = '/var/lib/pingmachine/output';
### write_order: Write the Pingmachine order file
###
my $order_w; # We would use 'state' for this, if we had Perl >= 5.10...
my $order_id;
sub write_order {
# Write order file
my $order = <<END;
user: example
task: $ping_ip
step: $step
pings: 1
probe: fping
fping:
host: $ping_ip
END
$order_id = md5_hex($order);
my $order_file = $orders_dir . "/$order_id";
my $fh;
open($fh, '>', $order_file) or
die "ERROR: can't write $order_file: $!\n";
print $fh $order;
close($fh);
# Schedule a rewrite of the file
# (so that pingmachine doesn't delete it)
$order_w = AnyEvent->timer(
after => 300,
cb => sub { write_order(); }
);
return $output_base . "/$order_id";
}
### remove_order: Remove the order file, when we quit
### (note that Pingmachine removes it for us after one hour that we didn't
### refresh it, but we do it anyway for neatiness)
###
sub remove_order {
unlink $orders_dir . "/$order_id";
}
### watch_output: Watch output directory for new "last_result" file.
### (note that Pingmachine creates a tempory file and then
### renames (moves) it to "last_results")
my $watch_output_w;
sub watch_output {
my ($output_dir) = @_;
my $inotify = Linux::Inotify2->new() or
die "ERROR: Unable to create new inotify object: $!";
$inotify->watch("$output_dir",
IN_MOVED_TO,
sub {
my $e = shift;
$e->name eq 'last_result' or return;
read_result($e->fullname);
}
);
$watch_output_w = AnyEvent->io(
fh => $inotify->fileno,
poll => 'r',
cb => sub { $inotify->poll }
);
}
### read_result: Called when last_result has been updated by Pingmachine
###
sub read_result {
my ($file) = @_;
my $results = LoadFile($file);
printf("%-15s %-15s %s\n",
strftime("%H:%M:%S", localtime($results->{updated})),
strftime("%H:%M:%S", localtime(time)),
$results->{median}
);
}
### main: Main routine
### (because I don't like having things in the root scope)
###
sub main {
my $output_dir = write_order();
# Install signal watchers for SIGINT and SIGTERM
my $quit_cv = AnyEvent->condvar;
my $w1 = AnyEvent->signal(signal => "INT", cb => sub { $quit_cv->send() });
my $w2 = AnyEvent->signal(signal => "TERM", cb => sub { $quit_cv->send() });
# Install output directory watcher
watch_output($output_dir);
# Write header
printf("%-15s %-15s %s\n", "Sample time", "Now", "RTT");
# Start event loop
$quit_cv->recv;
# Quitting -> remove order file
remove_order();
}
main;