Debian Codegraph @

plugins/rssm/dnscap-rssm-rssac002

raw ◀ ▶
1 Debian patch touch this file
•1 #!/usr/bin/perl
2 #
3 # Copyright (c) 2018-2026 OARC, Inc.
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
9 #
10 # 1. Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 #
13 # 2. Redistributions in binary form must reproduce the above copyright
14 # notice, this list of conditions and the following disclaimer in
15 # the documentation and/or other materials provided with the
16 # distribution.
17 #
18 # 3. Neither the name of the copyright holder nor the names of its
19 # contributors may be used to endorse or promote products derived
20 # from this software without specific prior written permission.
21 #
22 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 # POSSIBILITY OF SUCH DAMAGE.
34
35 use strict;
36 use warnings;
37 use YAML;
38
39 unless (scalar @ARGV > 1) {
40 print "usage: dnscap-rssm-rssac002 [--no-recompile|--keep-dnscap-rssm|--sort|--skip-unsupported] <YAML files to merge...>\n";
41 exit(1);
42 }
43
44 my %service = ();
45 my $earliest_start_period;
46 my $recompile = 1;
47 my $keep_dnscap_rssm = 0;
48 my $sort = 0;
49 my $skip_unsupported = 0;
50
51 foreach my $file (@ARGV) {
52 if ($file eq '--no-recompile') {
53 $recompile = 0;
54 next;
55 } elsif ($file eq '--keep-dnscap-rssm') {
56 $keep_dnscap_rssm = 1;
57 next;
58 } elsif ($file eq '--sort') {
59 $sort = 1;
60 next;
61 } elsif ($file eq '--skip-unsupported') {
62 $skip_unsupported = 1;
63 next;
64 }
65 foreach my $doc (YAML::LoadFile($file)) {
66 my $version = delete $doc->{version};
67 my $service = delete $doc->{service};
68 my $start_period = delete $doc->{'start-period'};
69 my $metric = delete $doc->{metric};
70 unless ($version) {
71 die "$file: not valid RSSAC002 YAML, missing version";
72 }
73 unless ($service) {
74 die "$file: not valid RSSAC002 YAML, missing service";
75 }
76 unless ($start_period) {
77 die "$file: not valid RSSAC002 YAML, missing start-period";
78 }
79 unless ($metric) {
80 die "$file: not valid RSSAC002 YAML, missing metric";
81 }
82 unless ($version eq 'rssac002v3') {
83 if ($skip_unsupported) {
84 next;
85 }
86 die "$file: unsupported RSSAC002 version $version";
87 }
88
89 push(@{$service{$service}->{$metric}}, $doc);
90
91 if (!$earliest_start_period or $start_period lt $earliest_start_period) {
92 $earliest_start_period = $start_period;
93 }
94 }
95 }
96
97 foreach my $service (keys %service) {
98 foreach my $metric (keys %{$service{$service}}) {
99 my %doc = ();
100 foreach (@{$service{$service}->{$metric}}) {
101 eval {
102 merge(\%doc, $_);
103 };
104 if ($@) {
105 die "service $service metric $metric: $@";
106 }
107 }
108 $service{$service}->{$metric} = \%doc;
109 }
110 }
111
112 if ($recompile) {
113 foreach my $service (keys %service) {
114 my ($ipv4, $ipv6, $aggregated) = (0, 0, 0);
115 my $metric;
116
117 if ($keep_dnscap_rssm) {
118 $metric = $service{$service}->{'dnscap-rssm-sources'};
119 } else {
120 $metric = delete $service{$service}->{'dnscap-rssm-sources'};
121 }
122 if ($metric) {
123 if (ref($metric->{sources}) eq 'HASH') {
124 foreach my $ip (keys %{$metric->{sources}}) {
125 if ($ip =~ /:/o) {
126 $ipv6++;
127 } else {
128 $ipv4++;
129 }
130 }
131 }
132 }
133
134 if ($keep_dnscap_rssm) {
135 $metric = $service{$service}->{'dnscap-rssm-aggregated-sources'};
136 } else {
137 $metric = delete $service{$service}->{'dnscap-rssm-aggregated-sources'};
138 }
139 if ($metric) {
140 if (ref($metric->{'aggregated-sources'}) eq 'HASH') {
141 my @keys = keys %{$metric->{'aggregated-sources'}};
142 $aggregated += scalar @keys;
143 }
144 }
145
146 $service{$service}->{'unique-sources'} = {
147 'num-sources-ipv4' => $ipv4,
148 'num-sources-ipv6' => $ipv6,
149 'num-sources-ipv6-aggregate' => $aggregated,
150 };
151 }
152 }
153
154 if ($sort) {
155 my $first = 1;
156 $YAML::SortKeys = 1;
157 foreach my $service (sort keys %service) {
158 foreach my $metric (sort keys %{$service{$service}}) {
159 if ($first) {
160 $first = 0;
161 } else {
162 print "\n";
163 }
164 print YAML::Dump({
165 version => "rssac002v3",
166 service => $service,
167 'start-period' => $earliest_start_period,
168 metric => $metric,
169 %{ $service{$service}->{$metric} },
170 });
171 }
172 }
173 } else {
174 my $first = 1;
175 $YAML::SortKeys = 0;
176 foreach my $service (keys %service) {
177 foreach my $metric (keys %{$service{$service}}) {
178 if ($first) {
179 $first = 0;
180 } else {
181 print "\n";
182 }
183 print YAML::Dump({
184 version => "rssac002v3",
185 service => $service,
186 'start-period' => $earliest_start_period,
187 metric => $metric,
188 %{ $service{$service}->{$metric} },
189 });
190 }
191 }
192 }
193
194 sub merge {
195 my ( $doc, $measurements ) = @_;
196
197 foreach my $key (keys %$measurements) {
198 if (ref($doc->{$key}) eq 'HASH') {
199 unless (ref($measurements->{$key}) eq 'HASH') {
200 die "invalid measurement types for key $key: not a hash";
201 }
202 eval {
203 merge($doc->{$key}, $measurements->{$key});
204 };
205 die $@ if ($@);
206 next;
207 }
208 if (defined($doc->{$key})) {
209 if (defined($measurements->{$key}) and $measurements->{$key} ne '') {
210 $doc->{$key} += $measurements->{$key};
211 }
212 } else {
213 $doc->{$key} = $measurements->{$key};
214 }
215 }
216 }