New parser and storage functions to support the new database schema.

This commit is contained in:
kalzu rekku 2024-05-26 19:10:16 +03:00
parent 4ffd7052a5
commit 0e0cba8426

View File

@ -4,6 +4,7 @@
import subprocess import subprocess
import sqlite3 import sqlite3
import re import re
import json
from datetime import datetime from datetime import datetime
def run_traceroute(host): def run_traceroute(host):
@ -11,36 +12,25 @@ def run_traceroute(host):
return result.stdout.decode() return result.stdout.decode()
def parse_traceroute_output(output): def parse_traceroute_output(output):
hops = []
lines = output.strip().split('\n')[1:] # Skip the first line (traceroute to ...)
previous_ip = None
for line in lines:
parts = line.split()
hop_number = int(parts[0])
ip_address = parts[1]
latencies = []
for part in parts[2:]:
if re.match(r'^[0-9+\s+ms$]', part):
latency_str = re.sub(r'[^0-9.]', '', part)
if latency_str and not latency_str == '':
try:
print(part)
print(latency_str)
latencies.append(float(latency_str))
except ValueError:
print(f"Could not convert '{latency_str}' to float.")
avg_latency = sum(latencies) / len(latencies) if latencies else None lines = output.strip().split('\n')
timestamp = datetime.now().isoformat() hops = []
if previous_ip: for line in lines[1:]:
hops.append({ hop_info = line.split()
'hop_number': hop_number, hop_number = int(hop_info[0])
'source_ip': previous_ip, hop_ips = []
'destination_ip': ip_address, hop_latencies = []
'latency': avg_latency, for part in hop_info[1:]:
'timestamp': timestamp, if re.match(r'\d+\.\d+\.\d+\.\d+', part): # Match IP address
}) hop_ips.append(part)
previous_ip = ip_address elif re.match(r'\d+(\.\d+)? ms', part): # Match latency
hop_latencies.append(float(part.replace(' ms', '')))
# If multiple IPs are present, we consider the first as primary
primary_ip = hop_ips[0] if hop_ips else None
avg_latency = sum(hop_latencies) / len(hop_latencies) if hop_latencies else None
hops.append((primary_ip, avg_latency))
return hops return hops
def create_tables(databasefile): def create_tables(databasefile):
@ -100,32 +90,40 @@ def create_tables(databasefile):
conn.commit() conn.commit()
conn.close() conn.close()
def store_traceroute(hops): def store_traceroute(db_file, start_ip, end_ip, hops):
conn = sqlite3.connect('traceroute.db') conn = sqlite3.connect(db_file)
cursor = conn.cursor() cursor = conn.cursor()
# Insert links and get their IDs
link_ids = []
for hop in hops: for hop in hops:
# Insert or ignore link into links table source_ip = start_ip if not link_ids else hops[len(link_ids)-1][0]
cursor.execute(''' destination_ip = hop[0]
INSERT OR IGNORE INTO links (source_ip, destination_ip) latency = hop[1]
VALUES (?, ?)
''', (hop['source_ip'], hop['destination_ip'])) cursor.execute("""
INSERT OR IGNORE INTO Links (source_ip, destination_ip) VALUES (?, ?)
# Retrieve the link_id """, (source_ip, destination_ip))
cursor.execute(''' cursor.execute("""
SELECT id FROM links WHERE source_ip = ? AND destination_ip = ? SELECT id FROM Links WHERE source_ip = ? AND destination_ip = ?
''', (hop['source_ip'], hop['destination_ip'])) """, (source_ip, destination_ip))
link_id = cursor.fetchone()[0] link_id = cursor.fetchone()[0]
link_ids.append(link_id)
# Insert latency data into link_latency table
cursor.execute(''' cursor.execute("""
INSERT INTO link_latency (link_id, latency, timestamp) INSERT INTO Latency (link_id, latency_ms) VALUES (?, ?)
VALUES (?, ?, ?) """, (link_id, latency))
''', (link_id, hop['latency'], hop['timestamp']))
# Insert path
hops_json = json.dumps(link_ids)
cursor.execute("""
INSERT INTO Paths (start_ip, end_ip, hops_json) VALUES (?, ?, ?)
""", (start_ip, end_ip, hops_json))
conn.commit() conn.commit()
conn.close() conn.close()
def retrieve_traceroute(): def retrieve_traceroute():
conn = sqlite3.connect('traceroute.db') conn = sqlite3.connect('traceroute.db')
cursor = conn.cursor() cursor = conn.cursor()
@ -139,17 +137,21 @@ def retrieve_traceroute():
conn.close() conn.close()
return rows return rows
# Usage
if __name__ == '__main__': if __name__ == '__main__':
create_tables()
traceroute_output = run_traceroute('vi.fi') databasefile="./traceroute.db"
create_tables(databasefile)
target='vi.fi'
traceroute_output = run_traceroute(target)
hops = parse_traceroute_output(traceroute_output) hops = parse_traceroute_output(traceroute_output)
# for hop in hops: if hops:
# print(hop) start_ip = hops[0][0]
store_traceroute(hops) store_traceroute(databasefile, start_ip, target, hops)
stored_hops = retrieve_traceroute() # stored_hops = retrieve_traceroute()
for hop in stored_hops: # for hop in stored_hops:
print(f"Link: {hop[0]} -> {hop[1]}, Latency: {hop[2]} ms, Timestamp: {hop[3]}") # print(f"Link: {hop[0]} -> {hop[1]}, Latency: {hop[2]} ms, Timestamp: {hop[3]}")
exit(0) exit(0)