-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeasurements_to_gelf.py
193 lines (173 loc) · 6.4 KB
/
measurements_to_gelf.py
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
#!/usr/bin/python
#
# Prototype of a RIPE Atlas to GELF parser by awlx
#
# Usage: python measurements-to-gelf.py <id> <min>
#
# Imports
from gelfclient import UdpClient
from urllib2 import urlopen
import syslog
import json
import time
import sys
import requests
import sqlite3
from os.path import isfile, getsize
try:
from urllib.parse import urlencode
except ImportError:
from urllib import urlencode
# Configuration
gelf_server = 'localhost'
gelf_port = 5555
api_key = '<apikey>'
db_file = 'geocache.db'
# Parse Args
if len(sys.argv) == 1:
print("# Prototype of a RIPE Atlas to GELF parser")
print("#")
print("# Usage: python measurements-to-gelf.py <measurement id> <timeframe in min>")
print("#")
print("# Example: python measurements-to-gelf.py 12323 5")
print("#")
print("# Please define Server and Port inside the script.")
print("# Get an API Key for Geolocation from https://geocoder.opencagedata.com/pricing")
exit(1)
measurement_id = str(sys.argv[1])
time_frame_secs = int(sys.argv[2]) * 60
current_time = int(time.time())
time_frame = current_time - time_frame_secs
def do_db_check():
if not isfile(db_file):
connection = sqlite3.connect(db_file)
cursor = connection.cursor()
sql_command = """
CREATE TABLE geocache (
id INTEGER PRIMARY KEY,
probeid INTEGER,
country_code VARCHAR(30),
expiry INTEGER,
lat FLOAT,
lon FLOAT,
country VARCHAR(30),
state VARCHAR(30),
city VARCHAR(30));"""
cursor.execute(sql_command)
else:
connection = sqlite3.connect(db_file)
return connection
def do_db_insert(connection, probeid,country_code, expiry, lat, lon, country, state, city):
cursor = connection.cursor()
format_str = """INSERT INTO geocache (id, probeid,country_code, expiry, lat, lon, country, state, city)
VALUES (NULL, {probeid}, "{country_code}", {expiry}, {lat}, {lon}, "{country}", "{state}", "{city}");"""
sql_command = format_str.format(probeid=probeid,country_code=country_code.encode('utf8'), expiry=expiry, lat=lat, lon=lon, country = country.encode('utf8'), state=state.encode('utf8'), city=city.encode('utf8'))
cursor.execute(sql_command)
connection.commit()
def do_db_select(cursor, lat, lon):
format_str = """SELECT expiry,country,state,city FROM geocache WHERE lat="{lat}" AND lon="{lon}";"""
sql_command = format_str.format(lat=lat, lon=lon)
cursor.execute(sql_command)
location = cursor.fetchone()
return location
def do_db_check_probe(cursor, probeid):
format_str = """SELECT probeid FROM geocache WHERE probeid={probeid};"""
sql_command = format_str.format(probeid=probeid)
cursor.execute(sql_command)
probe = cursor.fetchone()
return probe
def do_db_select_geodata(cursor, probeid):
format_str = """SELECT lat,lon,country_code FROM geocache WHERE probeid={probeid};"""
sql_command = format_str.format(probeid=probeid)
cursor.execute(sql_command)
geodata = cursor.fetchone()
return geodata
def do_db_delete(connection, expiry):
cursor = connection.cursor()
format_str = """DELETE FROM geocache WHERE expiry = "{expiry}";"""
sql_command = format_str.format(expiry=expiry)
cursor.execute(sql_command)
connection.commit()
# Get things done
def get_place(probeid, country_code, lat, lon, current_time):
url = "http://api.opencagedata.com/geocode/v1/json?q="
url += "%s+%s&key=%s" % (lat, lon, api_key)
connection = do_db_check()
cursor = connection.cursor()
location = do_db_select(cursor, lat, lon)
expiry = current_time + 3600;
if location is None:
try:
content_geo_raw = urlopen(url).read()
content_geo_json = json.loads(content_geo_raw)
components = content_geo_json['results'][0]['components']
country = town = None
country = components['country']
state = components['state']
city = components['city']
except Exception as e:
country = "N/A"
state = "N/A"
city = "N/A"
do_db_insert(connection,probeid, country_code, expiry, lat, lon, country, state, city)
else:
expiry = location[0]
country = location[1]
state = location[2]
city = location[3]
if expiry <= current_time:
do_db_delete(connection, expiry)
return country, state, city
measurement_url = "https://atlas.ripe.net/api/v2/measurements/{}/results?{}".format(
measurement_id,
urlencode({
"start": time_frame,
"stop": current_time,
"format": "json"
})
)
print(measurement_url)
try:
d = requests.get(measurement_url)
except Exception as e:
print e
exit(1)
data = d.json()
gelf = UdpClient(gelf_server, port=gelf_port)
for probe in data:
print(probe['prb_id'])
log = {}
connection = do_db_check()
cursor = connection.cursor()
probeid = do_db_check_probe(cursor, probe['prb_id'])
if probeid is None:
content_probes_raw = requests.get('https://atlas.ripe.net/api/v2/probes/' + str(probe['prb_id']) + '/')
details = content_probes_raw.json()
longitude = details['geometry']['coordinates'][0]
latitude = details['geometry']['coordinates'][1]
country_code = details['country_code']
else:
geodata = do_db_select_geodata(cursor, probe['prb_id'])
longitude = geodata[0]
latitude = geodata[1]
country_code = geodata[2]
connection.close()
location = get_place(probe['prb_id'], country_code, latitude, longitude, current_time)
log['short_message'] = 'RIPE Atlas Data of Probe ' + str(probe['prb_id'])
log['host'] = 'ripe-atlas'
log['level'] = syslog.LOG_INFO
log['timestamp'] = probe['timestamp']
log['_ripe_atlas_prbid'] = probe['prb_id']
log['_ripe_atlas_dst_addr'] = probe['dst_addr']
log['_ripe_atlas_src_addr'] = probe['from']
log['_ripe_atlas_type'] = probe['type']
log['_ripe_atlas_proto'] = probe['proto']
log['_ripe_atlas_sent_pkts'] = probe['sent']
log['_ripe_atlas_country'] = country_code
log['_ripe_atlas_location'] = location[0] + ',' + location[1]
log['_ripe_atlas_location_extended'] = location[0] + ',' + location[1] + ',' + location[2]
log['_ripe_atlas_rcvd_pkts'] = probe['rcvd']
log['_ripe_atlas_avg_rtt'] = probe['avg']
log['_ripe_atlas_max_rtt'] = probe['max']
log['_ripe_atlas_min_rtt'] = probe['min']
gelf.log(log)