sub sched_5m_ifstats { my $filename = "$HOME_TMP/ifstats-last5m.csv"; my %stat; my $OUT; for my $line (adb_result ("mcalc avg time last5m ifutil * * /^IF-MIB.if.*Util/")) { my ($device, $interface, $attr, undef, $val) = split (" ", $line, 5); $attr =~ s/IF-MIB\.if//m; next if ($val eq "n/a"); $stat{$device}{$interface}{$attr} = $val; } for my $line (adb_result ("mcalc avg time last5m ifrate * * /^IF-MIB/")) { my ($device, $interface, $attr, undef, $val) = split (" ", $line, 5); $attr =~ s/IF-MIB\.if//m; next if ($val eq "n/a"); $stat{$device}{$interface}{$attr} = $val; } for my $line (adb_result ("mcalc total time last5m counter * * /^IF-MIB/")) { my ($device, $interface, $attr, undef, $val) = split (" ", $line, 5); $attr =~ s/IF-MIB\.if//m; $attr =~ s/^HC//m; next if ($val eq "n/a"); $stat{$device}{$interface}{$attr} = $val; } open ($OUT, ">", "$filename.tmp") or EXIT_FATAL ("Can't open $filename.tmp: $!"); for my $device (sort keys %stat) { for my $interface (sort keys %{ $stat{$device} }) { my $InPkts; my $OutPkts; my $InUtil = ""; my $OutUtil = ""; my $InErrorsPct = 0; my $OutErrorsPct = 0; my $InDiscardsPct = 0; my $OutDiscardsPct = 0; my $ref = $stat{$device}{$interface}; next if (not defined $ref->{InUtil}); next if (not defined $ref->{OutUtil}); if ($ref->{InUtil} ne "") { $InUtil = sprintf ("%.2f", $ref->{InUtil} / 100); } if ($ref->{OutUtil} ne "") { $OutUtil = sprintf ("%.2f", $ref->{OutUtil} / 100); } $InPkts = ($ref->{InUcastPkts} || 0) + ($ref->{InBroadcastPkts} || 0) + ($ref->{InMulticastPkts} || 0) + ($ref->{InErrors} || 0) + ($ref->{InDiscards} || 0); $OutPkts = ($ref->{OutUcastPkts} || 0) + ($ref->{OutBroadcastPkts} || 0) + ($ref->{OutMulticastPkts} || 0); if ($InPkts > 0) { $InErrorsPct = ($ref->{InErrors} || 0) * 100 / $InPkts; $InDiscardsPct = ($ref->{InDiscards} || 0) * 100 / $InPkts; } if ($OutPkts > 0) { $OutErrorsPct = ($ref->{OutErrors} || 0) * 100 / $OutPkts; $OutDiscardsPct = ($ref->{OutDiscards} || 0) * 100 / $OutPkts; } printf $OUT "%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%.2f,%.2f,%.2f,%.2f\n", $device, $interface, $ref->{InOctets} || 0, $ref->{OutOctets} || 0, $ref->{InBitRate} || 0, $ref->{OutBitRate} || 0, $InUtil, $OutUtil, $InPkts, $OutPkts, $ref->{InUcastPkts} || 0, $ref->{OutUcastPkts} || 0, $ref->{InBroadcastPkts} || 0, $ref->{OutBroadcastPkts} || 0, $ref->{InMulticastPkts} || 0, $ref->{OutMulticastPkts} || 0, $ref->{InErrors} || 0, $ref->{OutErrors} || 0, $ref->{InDiscards} || 0, $ref->{OutDiscards} || 0, $InErrorsPct, $OutErrorsPct, $InDiscardsPct, $OutDiscardsPct; } } close $OUT; rename ("$filename.tmp", $filename); }