2 # dmon -- DTrace-backed TCP/IP real-time monitoring utility
3 # Copyright (C) 2022 Sergey Matveev <stargrave@stargrave.org>
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, version 3 of the License.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 ########################################################################
23 die "Usage: $0 [tcp|ifname0 ifname1 ...]\n";
25 use File::Temp qw/tempfile/;
26 my ($dfh, $dfn) = tempfile();
29 #pragma D option quiet
30 #pragma D option switchrate=10Hz
36 if ($ARGV[0] eq "tcp") {
38 my ($probe, $dir) = @_;
42 printf(\"$dir [%s]:%d [%s]:%d %d\\n\",
50 tcpprobe "tcp:::send", ">";
51 tcpprobe "tcp:::receive", "<";
54 my ($probe, $dir) = @_;
55 my $conds = join " || ", map { "args[3]->if_name == \"$_\"" } @ARGV;
60 printf(\"$dir %s %s %d %s\\n\",
67 ipprobe "ip:::send", ">";
68 ipprobe "ip:::receive", "<";
71 my $height = (defined $ENV{DMON_HEIGHT}) ? $ENV{DMON_HEIGHT} : 40;
74 ########################################################################
76 my @sizePrefix = ("E", "P", "T", "G", "M", "K");
77 my @countBound = (1e18, 1e15, 1e12, 1e9, 1e6, 1e3);
78 my @sizeBound = (1<<60, 1<<50, 1<<40, 1<<30, 1<<20, 1<<10);
81 my ($s, $suffix, $bounds) = @_;
82 foreach my $i (0 .. $#{$bounds}) {
83 next if $s < @{$bounds}[$i];
84 return sprintf "%.2f %s%s", $s / @{$bounds}[$i], $sizePrefix[$i], $suffix;
89 sub humanCount { return human shift, "", \@countBound }
91 sub humanSize { return human shift, "iB", \@sizeBound }
93 ########################################################################
96 my ($pktsRx, $pktsTx, $bytesRx, $bytesTx) = (0, 0, 0, 0);
98 @>>>>>>>>>> / @<<<<<<<<<< @>>>>>>>>>>>> / @<<<<<<<<<<<<
99 humanCount($pktsRx), humanCount($pktsTx), humanSize($bytesRx), humanSize($bytesTx)
100 ------------------------------------------------------------------------
103 my ($left, $dir, $right, $size);
105 @<<<<<<<<<<<<<<<<<<<<<<<<<< @| @>>>>>>>>>>>>>>>>>>>>>>>>>> @>>>>>>>>>>>>
106 $left, $dir, $right, humanSize($size)
109 ########################################################################
112 open $dexec, "-|", "dtrace -s $dfn" or die "$!";
118 next unless /^[<>T]/;
120 if ($cols[0] eq "T") {
122 foreach my $src (keys %rx) {
123 foreach my $dst (keys %{$rx{$src}}) {
124 push @res, [$dst, "<", $src, $rx{$src}{$dst}];
127 foreach my $src (keys %tx) {
128 foreach my $dst (keys %{$tx{$src}}) {
129 push @res, [$src, ">", $dst, $tx{$src}{$dst}];
132 @res = sort { @{$b}[-1] <=> @{$a}[-1] } @res;
133 printf "\033[H\033[J";
135 foreach (@res[0..$height]) {
137 ($left, $dir, $right, $size) = @{$_};
143 if ($cols[0] eq "<") {
146 $bytesRx += $cols[3];
150 $bytesTx += $cols[3];
152 if (defined ${$xx}{$cols[1]}) {
153 ${$xx}{$cols[1]}{$cols[2]} += $cols[3];
155 ${$xx}{$cols[1]} = {$cols[2] => $cols[3]};