forked from pierdom/atlas-toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
udm-create.pl
executable file
·420 lines (289 loc) · 9.53 KB
/
udm-create.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
#!/usr/bin/perl
# udm-create -- Create a custom measurement on RIPE Atlas platform
# Author: Pierdomenico Fiadino <[email protected]>
### modules ###
use strict;
use warnings;
use diagnostics;
use JSON;
use Getopt::Long;
use LWP::UserAgent;
use HTTP::Request;
use Data::Dumper;
use Pod::Usage;
### declare arguments ###
my $help;
# probes-related arguments
my ($probe_list,$probe_file,@probes);
# common arguments
my $api_key;
my ($start,$stop,$interval);
my ($type,$target,$descr,$af);
my ($res_on_probe,$private);
# dns specific arguments
my ($dns_arg,$dns_class,$dns_type);
# ping and traceroute specific args
my ($packets);
# dns and traceroute specific args
my ($proto);
### read arguments ###
my $rc = GetOptions(
"help" =>\$help,
"api-key=s" =>\$api_key,
"probe-list=s" =>\$probe_list,
"probe-file=s" =>\$probe_file,
"start=i" =>\$start,
"stop=i" =>\$stop,
"interval=i" =>\$interval,
"af=i" =>\$af,
"type=s" =>\$type,
"description=s" =>\$descr,
"target=s" =>\$target,
"resolve-on-probe" =>\$res_on_probe,
"private" =>\$private,
"dns-arg=s" =>\$dns_arg,
"dns-type=s" =>\$dns_type,
"dns-class=s" =>\$dns_class,
"packets=i" =>\$packets,
"protocol=s" =>\$proto
) or die "Syntax error";
### print help ###
if($help) {
pod2usage(2);
}
### check if mandatory are present ###
if(!$api_key) {
die "Missing argument: --api-key is mandatory"
}
if(!$type) {
die "Missing argument: --type is mandatory"
}
if(($type eq "traceroute" || $type eq "ping") && !$target) {
die "Missing argument: --target is mandatory for ping/traceroute"
}
if($type eq "dns" && !$dns_arg) {
die "Missing argument: --dns-arg is mandatory for dns"
}
### check allowed values ###
if($type ne "dns" && $type ne "ping" && $type ne "traceroute") {
die "Error: unknown type ($type). Use dns, ping or traceroute"
}
if($proto && $type eq "traceroute" &&
$proto ne "ICMP" && $proto ne "TCP" && $proto ne "UDP") {
die "Error: unknown protocol ($proto) for traceroute. Use TCP/UDP/ICMP"
}
if($proto && $type eq "dns" && $proto ne "TCP" && $proto ne "UDP") {
die "Error: unknown protocol ($proto) for dns. Use TCP/UDP"
}
if($dns_type && $type eq "dns" && $dns_type ne "A" && $dns_type ne "CNAME") {
die "Error: unknown type ($type). Use 'A' or 'CNAME'"
}
if($af && $af != 4 && $af != 6) {
die "Error: unknown address family ($af). Use 4 for IPv4 or 6 for IPv6"
}
### check temporal constraints ###
if($start && !$stop) {
die "Error: if --start is used, also --stop must be present"
}
if($start && ($stop<=$start || $stop-$start<60)) {
die "Error: stop time must be greater than start time of at least 60s"
}
if($interval && $interval < 60) {
die "Error: Interval must be greater than 60 seconds"
}
### set defaults ###
if($type eq "traceroute" && !$proto) {
$proto = "ICMP";
}
if($type eq "dns" and !$dns_class) {
$dns_class = "IN";
}
if($type eq "dns" and !$dns_type) {
$dns_type = "A";
}
if(!$af) {
$af = 4;
}
if(!$descr) {
$descr = ($target)? "target=$target " : "";
if($type eq "dns") { $descr.= "resolve=$dns_arg " }
if($start) { $descr.= "[$start:$stop]" }
else { $descr.= "one-off" }
}
### read probes ###
if($probe_list) {
@probes = split /,/,$probe_list;
}
elsif($probe_file) {
open PF, "<", $probe_file or die $!;
@probes= <PF>;
}
elsif(! (-t STDIN)) {
@probes= <>;
}
else {
die "Error: provide a list of probe IDs with ".
"--probe-file, --probe-list or to the STDIN\n";
}
if(@probes<1) {
die "Error: empty probe list\n";
}
@probes = map {($_) = split /\t/,$_} @probes;
### wrap up request JSON ###
# json scheleton
my $container;
# common options
$container->{'definitions'}->[0]->{'type'} = $type;
$container->{'definitions'}->[0]->{'description'} = $descr;
$container->{'definitions'}->[0]->{'af'} = $af;
if($target) { # remember that it is not mandatory for dns
$container->{'definitions'}->[0]->{'target'} = $target
}
if($private) {
$container->{'definitions'}->[0]->{'is_public'} = "false"
}
if($res_on_probe) {
$container->{'definitions'}->[0]->{'resolve_on_probe'} = "true"
}
# time constraints
if($start and $stop) {
$container->{'start_time'} = $start;
$container->{'stop_time'} = $stop;
}
else {
$container->{'definitions'}->[0]->{'is_oneoff'} = "true"
}
if($interval) {
$container->{'definitions'}->[0]->{'interval'} = $interval
}
# dns specific options
if($type eq "dns") {
$container->{'definitions'}->[0]->{'query_class'} = $dns_class;
$container->{'definitions'}->[0]->{'query_type'} = $dns_type;
$container->{'definitions'}->[0]->{'query_argument'} = $dns_arg;
if(!$target) {
$container->{'definitions'}->[0]->{'use_probe_resolver'} =
"true"
}
}
# protocol (for dns and traceroute)
if($proto) {
$container->{'definitions'}->[0]->{'protocol'} = $proto;
}
# number of packets (for ping and traceroute)
if($packets) {
$container->{'definitions'}->[0]->{'packets'} = $packets;
}
# probes part
$container->{'probes'}->[0]->{'requested'} = scalar @probes;
$container->{'probes'}->[0]->{'type'} = "probes";
$container->{'probes'}->[0]->{'value'} = join(",",@probes);
### submit json ###
my $udm_json = to_json $container;
my $dumb = from_json $udm_json;
my $atlas_url = "https://atlas.ripe.net/api/v1/measurement/?key=$api_key";
my $request = HTTP::Request->new('POST' => $atlas_url);
$request->header('Content-Type' => 'application/json');
$request->content($udm_json);
my $ua = LWP::UserAgent->new;
my $response = $ua->request($request);
### check response ###
if($response->is_success) {
my $dec_content = $response->decoded_content;
my ($msm_list) = $dec_content =~ m/\[(.*)\]/;
print join("\n",(split /,/,$msm_list));
print "\n";
exit 1;
}
else {
print STDERR "Error submitting UDM json: ".$response->status_line."\n";
print STDERR "POST URL: $atlas_url\n";
print STDERR "JSON content: ";
print STDERR Dumper($dumb);
print STDERR "Message: " . $response->decoded_content . "\n";
exit 0;
}
__END__
=pod
=head1 NAME
udm-create - Create a custom User Defined Measurement on RIPE Atlas
=head1 VERSION
1.0
=head1 DESCRIPTION
This program allows to set a custom measurement on RIPE Atlas platform
using RIPE's REST APIs. To set-up a User Defined Measurement (UDM), an
API KEY with 'Measurement creation' permissions is required.
Available measurement types are: ping, dns and traceroute.
A DNS UDM instruments Atlas boxes to resolve
an hostname (i.e. query-argument) using a specified DNS server or
the probe's default resolver. The query type can be A, AAAA or CNAME.
The list of probes that will run the UDM must be provided either with
the --probe-file or --probe-list argument. The first allows to load
a file containing a list of probe IDs one per row
(e.g. --probe-file my_probe_list.txt), the latter allows to specify
a comma-separated list of probes (e.g. --probe-list 5234,134,1235).
A list of probes (one-per-row) can be also passed to the STDIN, usefull
in pipe with the B<probe_list.pl> program, e.g.:
./probe_list --country it --limit 3 | ./udm-create.pl --query-arg example.com --api 123
An UDM can be 'one-off' (i.e. executed just once) or executed periodically
in a specified time-window (see --start, --stop, --interval options).
A DNS query uses UDP protocol by default. TCP is also possibile using
--protocol TCP option.
For a full list of options, see 'Options' section below.
=head1 SYNOPSIS
Usage: perl udm-create.pl [OPTIONS]...
=head3 Generic options
=over 12
=item B<--help>
show help
=item B<--api-key=<KEY>> I<(mandatory)>
an API key with 'Measurement creation' permissions
=item B<--type=<dns|ping|traceroute>> I<(mandatory)>
type of measurement (supported ping/traceroute/dns)
=item B<--target=<TARGET>> I<(mandatory)>
for ping/traceroute, it is the target of measurement;
for dns it is the resolver (use probe's resolver if not specified)
=item B<--private>
if used, the measurement will be private
=item B<--probe-file=<FILE> or --probe-list=<id1,id2,..>> I<(mandatory)>
load a list of probes from file or pass a CSV list
=item B<--start=<time>, --stop=<time>, --interval=<seconds>>
start, stop timestamps for the measurement and interval in seconds
(if not specified, run a one-off measurement)
=item B<--protocol=<PROTOCOL>>
for traceroute can be ICMP/TCP/UDM; for dns can be UDP/TCP
(un-used for ping)
=item B<--packets=<NUM_OF_PACKETS>>
number of packets to send for traceroute and ping
(un-used for dns)
=back
=head3 DNS-specific options
=over 12
=item B<--dns-arg=<ARG>> I<(mandatory)>
the hostname to resolve (eg. www.example.com)
=item B<--dns-class=<IN|CHAOS>>
the class of the DNS query (default 'IN')
=item B<--query-type=<A|CNAME>>
the DNS query type (default 'A')
=back
=head1 Requirements
This program require the Perl interpreter (which comes with most systems)
and the libjson-perl library (for the REST APIs).
=head1 LICENSE
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
=head1 ACKNOWLEDGEMENT
This work has been partially funded by the European Commission
funded mPlane ICT-318627 project (www.ict-mplane.eu).
=head1 AUTHOR
Pierdomenico Fiadino <[email protected]>
Vienna - May, 2014
=cut