-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplay-pcapper.pl
executable file
·194 lines (171 loc) · 4.61 KB
/
replay-pcapper.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
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
#!/usr/bin/perl -w
# Fortian Utilization Monitor, aka MWMG or the Multi-Window Multi-Grapher
# Copyright (c) 2019, Fortian Inc.
# All rights reserved.
# See the file LICENSE which should have accompanied this software.
use strict;
use Data::Dumper;
use POSIX;
use Time::HiRes;
use Compress::Zlib;
use constant INTERVAL => 10;
use constant DEBUG => 0;
use constant VERSION => '1.6';
use constant MARGIN => 26;
use constant HEIGHT => 530;
my %flow = (
1 => 'C',
2 => 'M',
9999 => '1',
7636 => '2',
15001 => '3',
1200 => '4',
15023 => '5',
15029 => '8',
1012 => '7',
);
sub roundup($) {
my $x = shift;
my $rv = 1;
foreach (1 .. 12) {
$rv *= 10;
foreach (1 .. 9) {
if ($x < ($_ * $rv)) {
return $_ * $rv;
}
}
}
return 9 * $rv; # well, return it anyway
}
sub pretty($) {
my $x = shift;
if ($x < 1000) {
return sprintf("%4g", $x);
} elsif ($x < 1000000) {
return sprintf("%3gK", $x / 1000);
} elsif ($x < 1000000000) {
return sprintf("%3gM", $x / 1000000);
} else {
return sprintf("%3gG", $x / 1000000000);
}
}
my %data = ();
my $sip;
my $tndir;
my %aggs = ();
select STDERR;
$| = 1;
select STDOUT;
if ($#ARGV == -1) {
die "You must supply the prefix to the TN-XX directories.\n";
} elsif (not -d "$ARGV[0]") {
die "The supplied directory ($ARGV[0]) does not exist.\n";
} elsif (not -d "$ARGV[0]/TN-01") {
die "The supplied directory ($ARGV[0]) does not hold TN-XX directories.\n";
} else {
$tndir = shift;
}
print "tndir: $tndir\n" if DEBUG;
my @logs;
opendir DIR, "$tndir" or die "Could not open $tndir: $!\n";
@logs = sort grep { /^TN-/ and (-f "$tndir/$_/runtime.log" or -f "$tndir/$_/runtime.log.gz") } readdir DIR;
closedir DIR;
if (not scalar @logs) {
die "Couldn't find any log files - giving up.\n";
}
my $c;
my $delta = 0;
my $lastn;
foreach (@logs) {
my $fname = $_;
my $id;
my $comp;
my $fh;
if ($fname =~ /TN-(\d+)/) {
$id = $1;
} else {
warn "Could not extract ID from $fname, skipping.\n";
next;
}
if (DEBUG) {
my @ticks = POSIX::times();
print "$fname: " . join(', ', @ticks);
my $sum = 0;
foreach (@ticks) { $sum += $_; }
print ': ' . ($sum - $delta) . "\n";
$delta = $sum;
} else {
print STDERR sprintf("\rNow loading %s (%3.2f%% complete)... ",
$fname, $delta / scalar @logs);
$delta += 100;
}
$comp = -f "$tndir/$fname/runtime.log.gz";
if ($comp) {
unless ($fh = gzopen("$tndir/$fname/runtime.log.gz", "rb")) {
warn "Could not open $tndir/$fname.gz: $!\n";
next;
}
} else {
unless (open($fh, "<$tndir/$fname/runtime.log")) {
warn "Could not open $tndir/$fname: $!\n";
next;
}
}
my $thr = 0;
my @scr = (qw(/ - \\ |));
my $line;
while (($comp ? ($fh->gzreadline($line) > 0) : ($line = <$fh>))) {
$thr++;
$thr %= 5000 * scalar @scr;
print STDERR "\010" . $scr[$thr / 5000] if (not $thr % 5000);
my ($when, $holdsip, $dport, $len, $proto);
($when, $holdsip, undef, undef, $dport, $len, $proto) = split /:/,
$line;
if ($proto != 61) {
$len *= 8;
} elsif ($dport == 2) {
$len *= 1024 * 256;
}
next if $proto == 61; # Don't send statistical reports right now
if (defined $flow{$dport}) {
$c = $flow{$dport};
} else {
$c = 'X';
}
if (defined $data{$when}) {
$data{$when} .= "\n";
} else {
$data{$when} = '';
}
$data{$when} .= sprintf('%d.%06d:', Time::HiRes::gettimeofday);
$id =~ s/^0//;
if ($c =~ /^[0-9]*$/) {
$data{$when} .= "$id:6:$len:0\n";
$data{$when} .= "$id:$c:" . int($len * 0.95) . ":1\n";
}
$data{$when} .= "$id:$c:$len:0";
}
if ($comp) {
$fh->gzclose();
} else {
close $fh;
}
$lastn = $id;
}
print STDERR "\rAll logfiles loaded, now sorting...\n";
my @keys = sort keys %data;
print STDERR "\rNow beginning replay... \n";
$| = 1;
my @foo;
my $st = 0;
foreach my $when (sort keys %data) {
if ($st) {
@foo = split /:/, $data{$when};
print STDERR "\rSleeping for " . ($when - $st) . " seconds to send " .
"data for $foo[0] at $when..." if $when - $st > 1;
Time::HiRes::sleep($when - $st);
}
$st = $when;
print "$data{$when}\n";
}
print STDERR "\nDone!\n";