-
Notifications
You must be signed in to change notification settings - Fork 33
/
dnscrypt_stats.pl
144 lines (104 loc) · 3.25 KB
/
dnscrypt_stats.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
#!/usr/bin/perl
# Author: Trizen
# Date: 04 May 2022
# May the 4th Be With You
# https://github.com/trizen
# Show human-readable stats for the dnscrypt-proxy query log.
use 5.020;
use strict;
use warnings;
use List::Util qw(sum uniq);
use experimental qw(signatures);
use Getopt::Long qw(GetOptions);
binmode(STDOUT, ':utf8');
my $top = 10;
my $log_file = '/var/log/dnscrypt-proxy/query.log';
sub help {
print <<"EOT";
usage: $0 [options]
options:
--top=i : display the top results (default: $top)
--file=s : path to the log file
--help : display this message
EOT
exit;
}
GetOptions(
"top=i" => \$top,
"file=s" => \$log_file,
"h|help" => \&help,
)
or die("Error in command line arguments\n");
my %domains;
my %resolvers;
my %cache_misses;
my %cache_hits;
my @durations;
my @recent_domains;
my @recent_resolvers;
open my $fh, '<:utf8', $log_file
or die "Can't open <<$log_file>>: $!";
while (<$fh>) {
if (m{^\[.*?\]\s+\S+\s+(\S+)\s+\S+\s+(\S+)\s+(\S+)\s+(\S+)}) {
my ($host, $status, $time_ms, $resolver) = ($1, $2, $3, $4);
$status eq 'PASS' or next;
$domains{$host}++;
if ($resolver eq '-') {
$resolvers{'--cache--'}++;
$cache_hits{$host}++;
}
else {
$cache_misses{$host}++;
$resolvers{$resolver}++;
push @recent_domains, $host;
push @recent_resolvers, $resolver;
push @durations, ($time_ms =~ /^(\d+)/);
}
}
}
close $fh;
sub make_top ($header, $data) {
my @entries = sort { ($data->{$b} <=> $data->{$a}) || ($a cmp $b) } keys %$data;
my $total = sum(values %$data);
if (scalar(@entries) > $top) {
$#entries = $top - 1;
}
my @rows;
push @rows, sprintf($header, scalar(@entries));
foreach my $entry (@entries) {
push @rows, sprintf("%40s %5d %2.0f%%", $entry, $data->{$entry}, $data->{$entry} / $total * 100);
}
return \@rows;
}
sub make_recent ($msg, $data) {
my @entries = uniq(reverse @$data);
if (scalar(@entries) > $top) {
$#entries = $top - 1;
}
my @rows;
push @rows, sprintf($msg, scalar(@entries));
foreach my $entry (@entries) {
push @rows, sprintf("%50s", $entry);
}
return \@rows;
}
my @top;
push @top, make_top("Top %s resolved domains", \%domains);
push @top, make_top("Top %s cache misses", \%cache_misses);
push @top, make_top("Top %s cache hits", \%cache_hits);
push @top, make_top("Top %s resolvers", \%resolvers);
push @top, make_recent("Latest %s resolved domains", \@recent_domains);
push @top, make_recent("Latest %s resolvers", \@recent_resolvers);
while (@top) {
my ($x, $y) = splice(@top, 0, 2);
my ($header1, $header2) = (shift(@$x), shift(@$y));
printf("%50s %60s\n\n", "== $header1 == ", " == $header2 == ");
while (@$x or @$y) {
printf("%-60s %s\n", shift(@$x) // '', shift(@$y) // '');
}
print "\n";
}
if (@durations) {
say "\n:: Average resolving time: ", sprintf('%.2f', sum(@durations) / scalar(@durations)), "ms.";
say ":: Overall resolving time (including caching): ", sprintf('%.2f', sum(@durations) / sum(values %domains)), "ms.";
}