Add initial version
This commit is contained in:
		
							
								
								
									
										1
									
								
								meas-ids.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								meas-ids.json
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1 @@ | |||||||
|  | {"IR": [4212086, 4212087], "TR": [4212108, 4212109], "IQ": [4212121, 4212122], "SA": [4212133, 4212134], "SY": [4212985, 4212986], "AE": [4212997, 4212998], "IL": [4213010, 4213011], "JO": [4213435, 4213436], "PS": [4213437, 4213438], "LB": [4213440, 4213441], "OM": [4213443, 4213444], "KW": [4213783, 4213784], "QA": [4213786, 4213787], "BH": [4213789, 4213790]} | ||||||
							
								
								
									
										132
									
								
								root-reachability.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										132
									
								
								root-reachability.py
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,132 @@ | |||||||
|  | import json | ||||||
|  | import os | ||||||
|  | import sys | ||||||
|  | from datetime import datetime | ||||||
|  |  | ||||||
|  | from ripe.atlas.cousteau import Traceroute, Dns, AtlasCreateRequest, AtlasSource, AtlasResultsRequest | ||||||
|  |  | ||||||
|  | # Globals | ||||||
|  | COUNTRIES = ['IR', 'EG', 'TR', 'IQ', 'SA', 'YE', 'SY', 'AE', 'IL', 'JO', 'PS', 'LB', 'OM', 'KW', 'QA', 'BH', 'AF'] | ||||||
|  | API_CREATE_KEY = '' | ||||||
|  |  | ||||||
|  |  | ||||||
|  | def create_measurement(cc): | ||||||
|  |     global API_CREATE_KEY | ||||||
|  |  | ||||||
|  |     traceroute = Traceroute(af=4, target="193.0.14.129", protocol="ICMP", | ||||||
|  |                             description="Traceroute from %s to K Root" % cc) | ||||||
|  |     dns = Dns(af=4, target="193.0.14.129", query_argument="com.", query_type="NS", query_class="IN", | ||||||
|  |               description="DNS Response time fromt %s to K Root" % cc) | ||||||
|  |  | ||||||
|  |     source = AtlasSource(value=cc, | ||||||
|  |                          requested=50, | ||||||
|  |                          type="country", | ||||||
|  |                          action="add") | ||||||
|  |     request = AtlasCreateRequest(start_time=datetime.utcnow(), | ||||||
|  |                                  key=API_CREATE_KEY, | ||||||
|  |                                  measurements=[traceroute, dns], | ||||||
|  |                                  sources=[source], | ||||||
|  |                                  is_oneoff=True) | ||||||
|  |  | ||||||
|  |     (is_success, response) = request.create() | ||||||
|  |  | ||||||
|  |     if is_success: | ||||||
|  |         print("- Created measurement for %s" % cc) | ||||||
|  |         return list(response['measurements']) | ||||||
|  |     else: | ||||||
|  |         print("- Failed to create measurement for %s: %s" % (cc, response)) | ||||||
|  |         return None | ||||||
|  |  | ||||||
|  |  | ||||||
|  | def load_ids(fn): | ||||||
|  |     ids = {} | ||||||
|  |  | ||||||
|  |     if os.path.exists(fn): | ||||||
|  |         with open(fn, "r") as fp:  # save all ids to json | ||||||
|  |             try: | ||||||
|  |                 ids = json.load(fp) | ||||||
|  |             except Exception as e: | ||||||
|  |                 print(e) | ||||||
|  |                 exit(1) | ||||||
|  |     return ids | ||||||
|  |  | ||||||
|  |  | ||||||
|  | def save_ids(fn, ids): | ||||||
|  |     with open(fn, "w") as fp:  # save all ids to json | ||||||
|  |         json.dump(ids, fp) | ||||||
|  |         fp.flush() | ||||||
|  |  | ||||||
|  |  | ||||||
|  | def create_all(countries, ids): | ||||||
|  |     for cc in countries: | ||||||
|  |         if cc in ids: | ||||||
|  |             print("- measurement for %s already exists" % cc) | ||||||
|  |         else: | ||||||
|  |             res = create_measurement(cc) | ||||||
|  |             if res is not None: | ||||||
|  |                 ids[cc] = res | ||||||
|  |  | ||||||
|  |         return ids | ||||||
|  |  | ||||||
|  |  | ||||||
|  | def dump_dns(ids): | ||||||
|  |     print("cc, num_probes,  avg_rtt") | ||||||
|  |     for cc, mid in ids.items(): | ||||||
|  |         is_success, results = AtlasResultsRequest(msm_id=mid[1]).create() | ||||||
|  |         rt_list = [] | ||||||
|  |         if is_success: | ||||||
|  |             for r in results: | ||||||
|  |                 try: | ||||||
|  |                     rt = r.get("result")["rt"] | ||||||
|  |                     if rt > 0: | ||||||
|  |                         rt_list.append(rt) | ||||||
|  |                 except: | ||||||
|  |                     pass | ||||||
|  |  | ||||||
|  |             average = sum(rt_list) // len(rt_list) | ||||||
|  |             print("%s, %d, %d" % (cc, len(rt_list), average)) | ||||||
|  |  | ||||||
|  |  | ||||||
|  | def dump_trace(ids): | ||||||
|  |     print("cc, num_probes,  num_hops") | ||||||
|  |     for cc, mid in ids.items(): | ||||||
|  |         is_success, results = AtlasResultsRequest(msm_id=mid[0]).create() | ||||||
|  |         hop_counts = [] | ||||||
|  |         if is_success: | ||||||
|  |             for r in results: | ||||||
|  |                 try: | ||||||
|  |                     trace = r.get("result") | ||||||
|  |                     hops = len(trace) | ||||||
|  |                     hop_counts.append(hops) | ||||||
|  |                 except: | ||||||
|  |                     hops = 0 | ||||||
|  |  | ||||||
|  |             average = sum(hop_counts) // len(hop_counts) | ||||||
|  |             print("%s, %d, %d" % (cc, len(hop_counts), average)) | ||||||
|  |  | ||||||
|  |  | ||||||
|  | def main(): | ||||||
|  |     global COUNTRIES | ||||||
|  |     fn = "meas-ids.json" | ||||||
|  |  | ||||||
|  |     if len(sys.argv) < 2: | ||||||
|  |         print("please run with one of the following parameters: [create|load-dns|load-trace]") | ||||||
|  |         exit(0) | ||||||
|  |     _cmd = sys.argv[1].lower() | ||||||
|  |     if _cmd == 'create': | ||||||
|  |         ids = load_ids(fn) | ||||||
|  |         ids = create_all(countries=COUNTRIES, ids=ids) | ||||||
|  |         save_ids(fn, ids) | ||||||
|  |     elif _cmd == 'load-dns': | ||||||
|  |         ids = load_ids(fn) | ||||||
|  |         dump_dns(ids) | ||||||
|  |     elif _cmd == 'load-trace': | ||||||
|  |         ids = load_ids(fn) | ||||||
|  |         dump_trace(ids) | ||||||
|  |     else: | ||||||
|  |         print("invalid parameter:", _cmd) | ||||||
|  |         exit(1) | ||||||
|  |  | ||||||
|  |  | ||||||
|  | if __name__ == '__main__': | ||||||
|  |     main() | ||||||
		Reference in New Issue
	
	Block a user