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 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
|
#!/usr/bin/env perl6
#
# Similarly to the Perl 5 version ethstats.pl, I am hereby
# releasing ethstats.p6 into the public domain.
# - Peter Pentchev <roam@ringlet.net>
use v6;
use strict;
use Getopt::Tiny;
use Terminal::ANSIColor;
constant VERSION = '1.2.1';
sub version()
{
say 'ethstats ' ~ VERSION;
}
sub features()
{
say 'Features: ethstats=' ~ VERSION;
}
sub usage(Bool:D $err = True)
{
my $s = q:to/EOUSAGE/;
Usage: ethstats [-t] [-C | -M] [-c count] [-i iface] [-n period]
ethstats -V | --version | -h | --help
-C color the "total" line in the output
-c exit after the specified number of samples
-h display program usage information and exit
-i specify a single network interface to monitor
-M do not color the "total" line in the output
-n specify the polling period in seconds (default: 10)
-t prefix each line with the Unix timestamp
-V display program version information and exit
EOUSAGE
if $err {
$*ERR.print($s);
exit 1;
} else {
print $s;
}
}
class Stats {
has Real $.in = 0;
has Real $.out = 0;
method !set(Real:D $in, Real:D $out)
{
$!in = $in;
$!out = $out;
}
method reset()
{
self!set(0, 0);
}
method update_traffic(Stats:D $old, UInt:D $period)
{
my $diff-in = $!in - $old.in;
$diff-in += 4294967296 if $diff-in < 0;
my $diff-out = $!out - $old.out;
$diff-out += 4294967296 if $diff-out < 0;
$old!set($!in, $!out);
self!set($diff-in / $period, $diff-out / $period);
}
method add(Stats:D $more)
{
$!in += $more.in;
$!out += $more.out;
}
method kb_from(Stats:D $bytes)
{
$!in = $bytes.in / 1000000 * 8;
$!out = $bytes.out / 1000000 * 8;
}
}
class InterfaceStats {
has Str $.name is required;
has Stats $.bytes = Stats.new;
has Stats $.packets = Stats.new;
has Stats $.kb = Stats.new;
method reset()
{
$!bytes.reset();
$!packets.reset();
$!kb.reset();
}
method update_traffic(InterfaceStats:D $odev, UInt:D $period)
{
$!bytes.update_traffic($odev.bytes, $period);
$!packets.update_traffic($odev.packets, $period);
}
method add(InterfaceStats:D $more)
{
$!bytes.add($more.bytes);
$!packets.add($more.packets);
$!kb.add($more.kb);
}
method format()
{
return ($!name eq 'total'
?? 'total: '
!! sprintf(' %6s: ', $!name)) ~
sprintf('%7.2f Mb/s In %7.2f Mb/s Out - ' ~
'%8.1f p/s In %8.1f p/s Out',
$!kb.in, $!kb.out,
$!packets.in, $!packets.out);
}
}
my InterfaceStats %prevstat;
grammar Interfaces {
token TOP {
<headerline>
<headerline>
<ifaceline>*
}
rule headerline { <nonnl>\n }
token nonnl { <- [\n] >* }
rule ifaceline { <iface>':' <in> <out>\n }
rule in {<bytes> <packets> <errs> <drop> <fifo> <frame> <compressed> <multicast>}
rule out {<bytes> <packets> <errs> <drop> <fifo> <colls> <carrier> <compressed>}
token iface { <[ \w . / - ]>+ }
token bytes { <uint> }
token packets { <uint> }
token errs { <uint> }
token drop { <uint> }
token fifo { <uint> }
token frame { <uint> }
token compressed { <uint> }
token multicast { <uint> }
token colls { <uint> }
token carrier { <uint> }
token uint { \d+ }
}
class InterfaceActions {
method TOP($/) { $/.make: $<ifaceline>.map(*.made) }
method ifaceline($/) {
$/.make: InterfaceStats.new(
name => $<iface>.made,
bytes => Stats.new(
in => $<in>.made<bytes>,
out => $<out>.made<bytes>,
),
packets => Stats.new(
in => $<in>.made<packets>,
out => $<out>.made<packets>,
),
)
}
method in($/) { $/.make: { :bytes($<bytes>.made), :packets($<packets>.made) } }
method out($/) { $/.make: { :bytes($<bytes>.made), :packets($<packets>.made) } }
method iface($/) { $/.make: ~$/; }
method bytes($/) { $/.make: $<uint>.made }
method packets($/) { $/.make: $<uint>.made }
method uint($/) { $/.make: $/.UInt }
}
sub convert(Str $iface, UInt:D $period, InterfaceStats:D $total)
{
try my $f = open :r, '/proc/net/dev', :chomp(False), :enc('latin1');
die "Could not open the interface info pseudo-file: $!" if $!;
my Str $contents = '';
for $f.lines -> $line {
# Ah, the joys of reading from pseudo-files...
last if $line eq '';
$contents ~= $line;
}
$f.close;
my InterfaceStats %devs =
Interfaces.parse($contents, :actions(InterfaceActions))
.made.map: { $_.name => $_ };
if defined $iface {
%devs = %devs{$iface}:kv;
}
$total.reset;
%devs<lo>:delete;
for %devs.values -> $dev {
my $name = $dev.name;
%prevstat{$name} //= InterfaceStats.new(:name($name));
$dev.update_traffic(%prevstat{$name}, $period);
$dev.kb.kb_from($dev.bytes);
$total.add($dev);
}
return %devs;
}
{
my Bool %flags;
my Getopt::Tiny $opts .= new;
$opts.bool('h', Nil, -> $v { %flags<h> = $v });
$opts.bool('V', Nil, -> $v { %flags<V> = $v });
$opts.str('-', Nil, -> $v {
given $v {
when 'help' { %flags<h> = True }
when 'version' { %flags<V> = True }
when 'features' { %flags<features> = True }
default {
note "Invalid long option '$v'";
usage;
}
}
});
my Bool $addtime = False;
$opts.bool('t', Nil, -> $v { $addtime = $v; });
my UInt $period = 10;
$opts.int('n', Nil, -> $v {
if $v < 1 {
note 'The period must be a positive integer';
usage;
}
$period = $v;
});
my UInt $count = 0;
$opts.int('c', Nil, -> $v {
if $v < 1 {
note 'The count must be a positive integer';
usage;
}
$count = $v;
});
my Str $iface = Nil;
$opts.str('i', Nil, -> $v { $iface = $v; });
my Bool $use_color = $*OUT.t;
$opts.bool('C', Nil, -> $v {
if %flags<M> {
note 'The -C and -M options are mutually exclusive';
usage;
}
%flags<C> = True;
$use_color = True;
});
$opts.bool('M', Nil, -> $v {
if %flags<C> {
note 'The -C and -M options are mutually exclusive';
usage;
}
%flags<M> = True;
$use_color = False;
});
$opts.parse([@*ARGS]);
if %flags<V> {
version;
}
if %flags<features> {
features;
}
if %flags<h> {
usage False;
}
if %flags<V> || %flags<features> || %flags<h> {
exit 0;
}
my Str %c = <yellow reset>.map: { $_ => $use_color?? color($_)!! '' };
my InterfaceStats $total .= new(:name('total'));
convert $iface, 1, $total;
sleep 1;
loop {
my InterfaceStats %devs = convert $iface, $period, $total;
print time ~ ' ' if $addtime;
if %devs.elems > 1 {
say %c<yellow> ~ $total.format ~ %c<reset>;
}
.format.say for %devs.values.sort: *.name;
if $count > 0 {
$count--;
exit 0 if $count < 1;
}
sleep $period;
}
}
|