This repository has been archived by the owner on Nov 14, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathudm-result.pl
executable file
·252 lines (188 loc) · 6.42 KB
/
udm-result.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
#!/usr/bin/perl
# Ger results of a RIPE Atlas measurement
# Author: Pierdomenico Fiadino <[email protected]>
### modules ###
use warnings;
use strict;
use diagnostics;
use JSON;
use Pod::Usage;
use LWP::Simple;
use Getopt::Long;
use Data::Dumper;
use MIME::Base64;
use Net::DNS::Packet;
### variables ###
my $help;
my $udm_id;
my $api_key;
my $latest;
my ($start,$stop);
### read options ###
my $rc = GetOptions(
"help" =>\$help,
"udm-id=i" =>\$udm_id,
"api-key=s" =>\$api_key,
"latest" =>\$latest,
"start=i" =>\$start,
"stop=i" =>\$stop
) or die "Syntax error";
### print help
if($help) {
pod2usage(1);
}
### check arguments ###
if(!$udm_id) {
die "Missing argument: --udm is mandatory"
}
### submit requested ###
my $url = "https://atlas.ripe.net/api/v1/measurement/$udm_id/result/?";
if($start) { $url.="start=$start&" }
if($stop) { $url.="stop=$stop&" }
if($latest) {
#TO-DO
}
else {
my $udm_result_json = get $url;
my $udm_result_array = from_json $udm_result_json or exit 1;
for my $result (@$udm_result_array) {
print_result($result);
}
}
exit 0;
### subroutines ###
# print a line of results with UDMtype-specific format
sub print_result {
my ($res) = @_;
##print Dumper($res);
# read measurement type
my $type = $res->{'type'};
# read probe-related info
my $timestamp= $res->{'timestamp'} || "NA";
my $prb_id = $res->{'prb_id'} || "NA";
my $from = $res->{'from'} || "NA";
my $probe = "$timestamp\t$prb_id\t$from";
# if type is ping, print result line(s) accordingly
if($type eq "ping") {
my $dst_addr = $res->{'dst_addr'} || "NA";
my $dst_name = $res->{'dst_name'} || "NA";
my $res_set = $res->{'result'};
foreach my $ping (@$res_set) {
my $rtt = $ping->{'rtt'} || "*";
print "$probe\t$dst_name\t$dst_addr\t$rtt\n";
}
}
# if type is dns, print result line(s) accordingly
elsif($type eq "dns") {
my $res_set = $res->{'resultset'};
foreach my $dns (@$res_set) {
my $dst_addr = $dns->{'dst_addr'};
my $abuf = $dns->{'result'}->{'abuf'};
my $dec_buff = decode_base64 $abuf;
if(defined $abuf && defined $dec_buff) {
my ($dns_pack)= new Net::DNS::Packet(\$dec_buff);
my @ans = $dns_pack->answer;
foreach my $ans (@ans) {
my $res_ip = $ans->{'address'} || '*';
my $res_ttl = $ans->{'ttl'} || '*';
my $req_name = $ans->{'name'} || '*';
print "$probe\t$dst_addr\t".
"$req_name\t$res_ip\t$res_ttl\n";
}
}
}
}
# if type is traceroute, print result line(s) accordingly
elsif($type eq "traceroute") {
my $dst_addr = $res->{'dst_addr'} || "NA";
my $dst_name = $res->{'dst_name'} || "NA";
my $res_set = $res->{'result'};
my $hop_count= scalar @$res_set;
#print "$probe\t$dst_name\t$dst_addr\t$hop_count\n";
my @res_buffer;
foreach my $hop (@$res_set) {
my $hop_num = $hop->{'hop'};
my $hop_set = $hop->{'result'};
my $avg_rtt = 0; my $pack_count = 0;
my ($from_ip,$ttl,$rtt);
foreach my $hop_step (@$hop_set) {
$from_ip = $hop_step->{'from'} || '*';
$ttl = $hop_step->{'ttl'} || '*';
$rtt = $hop_step->{'rtt'} || '0';
$avg_rtt += $rtt;
$pack_count++;
}
$avg_rtt = ($avg_rtt>0) ? ($avg_rtt/$pack_count)
: '*';
#print "\t$hop_num\t$from_ip\t$ttl\t$avg_rtt\n";
if(defined $from_ip && defined $ttl && defined
$avg_rtt) {
push @res_buffer,
"\t$hop_num\t$from_ip\t$ttl\t$avg_rtt\n";
}
}
if(scalar(@res_buffer) > 0) {
print
"$probe\t$dst_name\t$dst_addr\t$hop_count\n";
foreach my $res (@res_buffer) {
print $res;
}
}
}
else {
die "UDM type $type is currenty unsupported"
}
}
__END__
=head1 NAME
udm-result - Get a measurement result from RIPE Atlas in CSV
=head1 DESCRIPTION
Get a text tab-separated result of a User Defined Measurement (UDM)
from the RIPE Atlas platform using the REST APIs.
If the measurement is private, an API key with suitable permissions
has to be provided.
By default, the program gets all available results associated
to a measurement. It is also possible to refine the query in
a specified time range or just get the latest available result.
The results have different have different output format depending
on the measurement type, as showed below:
=head2 ping output fields (tab-separated)
timestamp | probe_id | probe_ip | dst_name | dst_addr | rtt
=head2 dns output fields (tab-separated)
timestamp | probe_id | probe_ip | dns_server | req_name | res_ip | ttl
=head2 traceroute output fields (tab-separated)
timestamp | probe_id | probe_ip | dst_name | dst_addr | hop_count
hop_num | hop_ip | ttl | avg_rtt
=head1 SYNOPSIS
Usage: udm-result [OPTIONS]...
=head2 Options
=over 12
=item B<--help>
show help
=item B<--udm=<ID>> I<(mandatory)>
the ID of the measurement
=item B<--api-key=<KEY>>
the API key (could be needed for private measurements)
=item B<--latest>
retrieve only latest results
=item B<--start=<TIMESTAMP>>, --stop=<TIMESTAMP>>
retrieve results in specific time-range
=back
=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