-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpingmachine-status
executable file
·206 lines (188 loc) · 6.33 KB
/
pingmachine-status
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/usr/bin/perl
###############################################################################
#
# Pingmachine - Smokeping-like Latency Measurement
#
# Written by David Schweikert <[email protected]>, June 2011
# Copyright (c) 2011-2014 Open Systems AG, Switzerland
# All Rights Reserved.
#
# See LICENSE file for the software licensing conditions.
#
###############################################################################
use strict;
use warnings;
use feature ':5.10';
use YAML::XS qw(LoadFile Dump);
use Term::ANSIColor;
use Try::Tiny;
use List::Util qw(max);
my $ORDERS_DIR = '/var/lib/pingmachine/orders';
my $OUTPUT_DIR = '/var/lib/pingmachine/output';
my %pretty_state_map = (
'up' => my_colored(' UP ', 'green'),
'down' => my_colored(' DOWN ', 'red'),
'unknown' => ' unkn ',
);
sub my_colored {
if(-t STDOUT) {
return colored(@_);
}
else {
return $_[0];
}
}
sub pretty_rtt {
my ($rtt) = @_;
if(not defined $rtt) {
return '-';
}
return sprintf('%.0f ms', $rtt*1000);
}
sub pretty_loss {
my ($loss, $pings) = @_;
if(not defined $loss) {
return '-';
}
my $loss_pct = sprintf('%3.0f%%', $loss*100/$pings);
if($loss == 0 or $pings > 2 and $loss == 1) {
return my_colored($loss_pct, 'green');
}
elsif($loss < $pings) {
return my_colored($loss_pct, 'yellow');
}
else {
return my_colored($loss_pct, 'red');
}
return
}
sub pretty_time {
my ($time) = @_;
my $delta_t = time - $time;
if($delta_t < 120) {
return sprintf("%3d s ", $delta_t);
}
elsif($delta_t < 7200) {
return sprintf("%3d min", $delta_t / 60); # "120 min" -> 7 chars
}
elsif($delta_t < 48 * 3600) {
return sprintf("%3d h ", $delta_t / 3600);
}
else {
return sprintf("%3d d ", $delta_t / (3600*24));
}
}
sub sortable_ip {
my ($ip) = @_;
defined $ip or return undef;
if($ip =~ /^[:0-9a-f]+$/i) {
my @n = split(/:/, $ip);
return "a.".join(":", map { sprintf "%04x", hex($_) } @n);
}
elsif($ip =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/) {
return sprintf("%03d.%03d.%03d.%03d", $1, $2, $3, $4);
}
else {
return $ip;
}
}
sub main {
my ($orders, $orders_by_user, $any_ipv6);
($orders, $orders_by_user, $any_ipv6) = scan_orders_dir($ORDERS_DIR, '', $orders, $orders_by_user, $any_ipv6);
# read results
my $oid_length = 8;
for my $oid (keys %{$orders}) {
$oid_length = max($oid_length, length($oid));
if(-f "$OUTPUT_DIR/$oid/last_result") {
try { $orders->{$oid}{result} = LoadFile("$OUTPUT_DIR/$oid/last_result"); }
catch { warn "WARNING: can't parse $OUTPUT_DIR/$oid/last_result\n"; }
}
}
# pretty print
state $format = " %-${oid_length}s %7s %5s %-8s %-15s %-7s %7s %4s%s\n";
if($any_ipv6) {
$format = " %-${oid_length}s %7s %5s %-8s %-36s %-7s %7s %4s%s\n";
}
my $first_user = 1;
for my $user (sort keys %{$orders_by_user}) {
$first_user ? $first_user = 0 : print "\n";
say my_colored("- $user", 'bold');
say "";
printf(my_colored($format, 'bold'), "order", "step", "pings", "probe", "host", "updated", "m.rtt", "loss", "");
printf($format, '-'x$oid_length, '-'x7, '-'x5, '-'x8, '-'x($any_ipv6 ? 32 : 15), '-'x7, '-'x7, '-'x4, "");
for my $oid (sort { $orders->{$a}{sort_key} cmp $orders->{$b}{sort_key} } keys %{$orders_by_user->{$user}})
{
my $order = $orders->{$oid};
my $additional = additional_info($order);
printf($format,
$oid,
"$order->{step} s",
$order->{pings},
$order->{probe},
$order->{probe_host} // '-',
!defined $order->{result} ? '-' : (
time - $order->{result}{updated} > $order->{step} ?
my_colored(pretty_time($order->{result}{updated}), 'red') :
pretty_time($order->{result}{updated})
),
!defined $order->{result} ? '-' : (
pretty_rtt($order->{result}{median})
),
!defined $order->{result} ? '-' : (
pretty_loss($order->{result}{loss}, $order->{pings})
),
$additional ? " ($additional)" : "",
);
}
}
}
sub scan_orders_dir {
my ($orders_dir, $order_id_prefix, $orders, $orders_by_user, $any_ipv6) = @_;
opendir(my $open_orders_dir, $orders_dir) or die "ERROR: can't open $ORDERS_DIR: $!\n";
while(my $order_file = readdir($open_orders_dir)) {
next if $order_file eq '.' or $order_file eq '..';
my $order_id = $order_id_prefix ? "$order_id_prefix/$order_file" : $order_file;
my $order_path = "$orders_dir/$order_file";
if (-d $order_path) {
($orders, $orders_by_user, $any_ipv6) = scan_orders_dir($order_path, $order_id, $orders, $orders_by_user, $any_ipv6);
}
next unless -f $order_path;
my $order;
try { $order = LoadFile($order_path); }
catch { warn "WARNING: can't parse $order_path\n"; };
next unless $order;
next unless $order->{user};
$orders->{$order_id} = $order;
if(defined $order->{probe} and defined $order->{$order->{probe}}{host}) {
$order->{probe_host} = $order->{$order->{probe}}{host};
$order->{sort_key} = sortable_ip($order->{probe_host}) . ":$order->{probe}:$order->{step}:$order_id";
if($order->{$order->{probe}}{host} =~ /^[:0-9a-f]+$/i) {
$any_ipv6 = 1;
}
}
else {
$order->{sort_key} = "ZZZ:$order->{probe}:$order->{step}:$order_id";
}
$orders_by_user->{$order->{user}}{$order_id} = $order;
}
return $orders, $orders_by_user, $any_ipv6;
}
sub additional_info {
my ($order) = @_;
my @additional;
if($order->{probe} eq 'fping') {
if($order->{fping}{interface}) {
push @additional, $order->{fping}{interface};
}
if($order->{fping}{source_ip}) {
push @additional, "source=".$order->{fping}{source_ip};
}
}
if(scalar @additional) {
return join(', ', @additional);
}
else {
return undef;
}
}
main;