-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgp_status.pl
executable file
·156 lines (128 loc) · 3.9 KB
/
gp_status.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
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
#!/usr/bin/perl
###############################################################################
#
# gp_status.pl
#
# Written by Lennart Elsen [email protected], December 2015
# Copyright (c) 2015 Open Systems AG, Switzerland
# All Rights Reserved.
#
# Helper script to correctly format goprobe's status output coming from the
# control socket.
#
################################################################################
use strict;
use warnings;
my $MAX_WIDTH = 8;
my $SL_INDENT = 54;
my $SL_NOCOLORS=0;
my $SL_RED = '';
my $SL_GREEN = '';
my $SL_YELLOW = '';
my $SL_NORMAL = '';
if(!$ENV{'NOCOLORS'}) {
$SL_RED =sprintf("%c[1;31m",27);
$SL_GREEN =sprintf("%c[1;32m",27);
$SL_YELLOW=sprintf("%c[1;33m",27);
$SL_NORMAL=sprintf("%c[0;39m",27);
}
my $_status_open = 0;
sub statusline {
my $string = shift;
my $dots = $SL_INDENT - length($string);
$dots=1 if ($dots<1);
printf("- %-s%-s",$string,"."x$dots);
$_status_open = 1;
}
sub statusok {
my $resultcode = shift;
my $shortmessage = shift || "";
if($resultcode =~ /^(ok|success)$/) {
printf("[ %sOK%s ] %s\n",$SL_GREEN,$SL_NORMAL,$shortmessage);
} elsif($resultcode =~ /^(warn|warning)$/) {
printf("[ %sATTN%s ] %s\n",$SL_YELLOW,$SL_NORMAL,$shortmessage);
} else {
printf("[%sFAILED%s] %s\n",$SL_RED,$SL_NORMAL,$shortmessage);
}
$_status_open = 0;
}
sub statusline_is_open {
return $_status_open;
}
sub humanize {
my $precision = shift; my $div = shift; my $count = 0;
my %units = (
1024 => ["B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"],
1000 => ["", "K", "M", "G", "T", "P", "E", "Z", "Y"],
);
my @ret;
foreach my $item ( @_ ){
$count=0;
while ($item > $div) {
$item /= $div;
$count++;
}
my $fmt_item = ( $count == 0 ? $item : sprintf("%${precision}f %s", $item, @{$units{$div}}[$count]) );
my $spaces = $MAX_WIDTH - length($fmt_item);
$fmt_item = sprintf("%-s%-s"," "x$spaces, $fmt_item);
push(@ret, $fmt_item);
}
return @ret;
}
my $lnum=0;
my ($detailed, $time_elapsed);
my ($t_rcv_gp, $t_rcv_pcap, $t_drop_pcap, $t_ifdrop);
my $iface_states;
# this script reads from STDIN
while(<>) {
chomp($_);
if ($lnum == 0) {
($detailed, $time_elapsed) = split(" ", $_);
$lnum++; next;
}
my ($iface, $state, $rcv_gp, $rcv_pcap, $drop_pcap, $ifdrop) = split(" ", $_);
$iface_states->{$iface} = {
state => $state,
rcv_gp => $rcv_gp, rcv_pcap => $rcv_pcap,
drop_pcap => $drop_pcap, ifdrop => $ifdrop,
};
$t_rcv_gp += $rcv_gp;
$t_rcv_pcap += $rcv_pcap;
$t_drop_pcap += $drop_pcap;
$t_ifdrop += $ifdrop;
$lnum++;
}
my $last_write = "${time_elapsed}s ago";
print "Interface Capture Statistics:\n
last writeout: ", sprintf("%-s%-s", " "x($MAX_WIDTH-length($last_write)), $last_write),"
packets received: ", humanize(".2", "1000", $t_rcv_pcap),"
dropped by pcap: ", humanize(".2", "1000", $t_drop_pcap),"
dropped by iface: ", humanize(".2", "1000", $t_ifdrop),"\n\n";
# print detailed statistics
if ($detailed) {
# prepare header assumes that SL_INDENT = 54 from statusline
my $spaces=66;
my $header="PKTS RCV DROP IF DROP";
$header = sprintf("%-s%-s"," "x$spaces, $header);
print "$header\n";
# prepare individual iface stats
foreach my $iface (sort keys %{ $iface_states } ) {
statusline("$iface ");
my $stats = $iface_states->{$iface};
if ($stats->{'state'} eq "active") {
statusok("ok", " " .
join(" ",
humanize(".2", "1000",
$stats->{rcv_gp},
$stats->{drop_pcap},
$stats->{ifdrop},
),
),
);
} else {
statusok("warn", "not capturing");
}
}
print "\n";
}
1;