|
| 1 | +#!/opt/local/bin/perl -w |
| 2 | + |
| 3 | +use strict; |
| 4 | +use warnings; |
| 5 | + |
| 6 | +use Storable; |
| 7 | + |
| 8 | +my $TMP_FILE = 'liveplot.tmp'; |
| 9 | +my $DAT_FILE = 'liveplot.dat'; |
| 10 | +my $TIME_STEP = 5; |
| 11 | + |
| 12 | + |
| 13 | +#Retrieve the byte hash from last invocation |
| 14 | +my %oldBytes; |
| 15 | +%oldBytes = %{retrieve($TMP_FILE)} if -e $TMP_FILE; |
| 16 | + |
| 17 | +while(<>) |
| 18 | +{ |
| 19 | + last if $_ =~ /statList/; # don't do anything until we get to the statList array |
| 20 | +} |
| 21 | + |
| 22 | +my %bytes; |
| 23 | + |
| 24 | +while(<>) |
| 25 | +{ |
| 26 | + last if $_ =~ /;/; # finish at the end of the statList array |
| 27 | + #Otherwise, pasrse each row which has content: index, IP, MAC, packets, bytes, etc. |
| 28 | + my @fields = split /, /; |
| 29 | +# print "$fields[1]\t$fields[4]\n"; |
| 30 | + @bytes{substr $fields[1], 1, -1} = $fields[4]; |
| 31 | +} |
| 32 | + |
| 33 | +#print "\nMap\n"; |
| 34 | +#print "$_ $bytes{$_}\n" for (sort keys %bytes); |
| 35 | + |
| 36 | +#store for next invocation |
| 37 | +store \%bytes, $TMP_FILE; |
| 38 | + |
| 39 | +#calculate delta between old bytes and this bytes |
| 40 | +my %deltaBytes; |
| 41 | +for (keys %bytes) |
| 42 | +{ |
| 43 | + if(exists $oldBytes{$_}) |
| 44 | + { |
| 45 | + $deltaBytes{$_} = ($bytes{$_} - $oldBytes{$_}) / (1024 * $TIME_STEP); |
| 46 | + } |
| 47 | + else |
| 48 | + { |
| 49 | + $deltaBytes{$_} = $bytes{$_} / (1024 * $TIME_STEP); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +#print "\nDelta\n"; |
| 54 | +#print "$_ $deltaBytes{$_}\n" for (sort keys %deltaBytes); |
| 55 | + |
| 56 | +# Write or update the gnuplot dat file |
| 57 | +my $datf; # file handle |
| 58 | +my @dat; # file contents |
| 59 | + |
| 60 | +if(-e $DAT_FILE) |
| 61 | +{ |
| 62 | + open($datf, '<', $DAT_FILE); |
| 63 | + $dat[0] = [ split " ", <$datf> ]; # header contains all the ips |
| 64 | + shift @{ $dat[0] }; # get rid of the "time" column |
| 65 | + for(my $i = 1; <$datf>; $i++) |
| 66 | + { |
| 67 | + $dat[$i] = [ split ]; |
| 68 | + shift @{ $dat[$i] }; # get rid of the "time" column |
| 69 | + } |
| 70 | + close($datf); |
| 71 | +} |
| 72 | +else # from existing dat file so start from scratch |
| 73 | +{ |
| 74 | + @dat[0] = [ sort keys %deltaBytes ]; |
| 75 | +} |
| 76 | + |
| 77 | +#insert the new data |
| 78 | +unshift @dat, $dat[0]; #make room at the start but preseve the header row |
| 79 | +$dat[1] = [ () ]; #blank new row |
| 80 | + |
| 81 | +for my $i ( 0 .. $#{$dat[0]} ) |
| 82 | +{ |
| 83 | + #print "pushing value ($deltaBytes{$dat[0][$i]}) for key ($dat[0][$i]).\n"; |
| 84 | + push @{ $dat[1] }, $deltaBytes{$dat[0][$i]}; |
| 85 | +} |
| 86 | + |
| 87 | +pop @dat if $#dat > 21; # limit to last 21 values + header row |
| 88 | + |
| 89 | +open $datf, '>', $DAT_FILE; |
| 90 | + |
| 91 | +for my $i ( 0 .. $#dat ) |
| 92 | +{ |
| 93 | + if($i == 0) |
| 94 | + { |
| 95 | + print { $datf } "time(s) "; |
| 96 | + } |
| 97 | + else |
| 98 | + { |
| 99 | + print { $datf } ($i-1)*$TIME_STEP . " "; |
| 100 | + } |
| 101 | + |
| 102 | + for my $j ( 0 .. $#{$dat[$i]} ) # oh Perl... |
| 103 | + { |
| 104 | + print { $datf } "$dat[$i][$j] "; |
| 105 | + #print "$dat[$i][$j] "; |
| 106 | + } |
| 107 | + print { $datf } "\n"; |
| 108 | + #print "\n"; |
| 109 | +} |
| 110 | + |
| 111 | +close $datf; |
| 112 | + |
0 commit comments