-
Notifications
You must be signed in to change notification settings - Fork 0
/
dnet2dog.follower
executable file
·121 lines (100 loc) · 2.68 KB
/
dnet2dog.follower
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
#!/usr/bin/perl
use strict;
use warnings;
use DataDog::DogStatsd;
use File::Tail;
#TODO: handle options
my $opt_proxy = 0;
my $debug = 0;
my $dogstatsd = DataDog::DogStatsd->new;
$dogstatsd->increment( 'dnet.script_runs') unless $debug;
my ($logfile) = @ARGV;
follow_file($logfile,$opt_proxy);
sub follow_file {
my($filename,$proxy) = @_;
if ($proxy) {
die "unimplemented: following proxy logs";
}
unless (-f $filename) {
die "no such file: $filename";
}
my $start_lines = 0;
$start_lines = 20 if $debug;
my $tail = File::Tail->new(name=>$filename, tail=>$start_lines);
my ($freshline,$bufferline);
while (defined( $freshline = $tail->read )) {
chomp($freshline);
#print "TOP $freshline\n";
if ($freshline =~ /^\[/) {
# new
if ($proxy) {
# ??
} else {
agent_line($bufferline) if defined $bufferline;
}
$bufferline = undef;
}
# add fresh to buffer after cleanups
$freshline =~ s/^\[.*UTC] //; # strip timestamps
$freshline =~ s/^ +/ /; # shrink whitespace
$bufferline .= $freshline;
}
}
sub agent_line {
my ($line) = @_;
if ($line =~ /^RC5-72: /) {
$line =~ s/^RC5-72: //;
#print "RC5 $line\n";
if ($line =~ /in buff/) {
my $packet_count;
if ($line =~ /(\d+) packet[s ]/) {
$packet_count = $1;
} else {
warn "no packet count in $line";
return;
}
my $buff_name;
if ($line =~ /in (buff[^ ]*)/) {
$buff_name = $1;
} else {
warn "no file name in $line";
return;
}
my $buff_name_encoded = $buff_name;
$buff_name_encoded =~ s/[.]/_/g;
$dogstatsd->gauge( "dnet.rc5-72.buffer.${buff_name_encoded}",$packet_count) unless $debug;
print "count=$packet_count name=$buff_name\n";
} elsif ($line =~ /^Summary/) {
my $packet_count;
if ($line =~ /(\d+) packet[s]? /) {
$packet_count = $1;
} else {
warn "no packet count in $line";
return;
}
my $keyrate;
if ($line =~ /\[(.*)keys\/s]/) {
$keyrate = $1;
} else {
warn "no keyrate in $line";
return;
}
$keyrate =~ s/,//g; # strip commas
#TODO: translate keyrate
$dogstatsd->gauge( "dnet.rc5-72.packets_processed",$packet_count) unless $debug;
print "count=$packet_count rate=$keyrate\n";
} elsif ($line =~ /(from|to) server\./) {
my $packet_count;
if ($line =~ /to server\./) {
$dogstatsd->gauge( "dnet.rc5-72.buffer.buff_out_r72",0) unless $debug;
print "count=0 for dnet.rc5-72.buffer.buff_out_r72\n";
} elsif ($line =~ /(\d+) packets .*from server\./) {
$packet_count = $1;
$dogstatsd->gauge( "dnet.rc5-72.buffer.buff_in_r72",$packet_count) unless $debug;
print "count=$packet_count for dnet.rc5-72.buffer.buff_in_r72\n";
}
} else {
warn "UNHANDLED $line\n";
}
}
}