From 4ffd7052a54b4475c914498d0a1e19aa5c03f4d7 Mon Sep 17 00:00:00 2001 From: kalzu rekku Date: Sun, 26 May 2024 18:57:01 +0300 Subject: [PATCH] New database schema. --- traceroute_collector.py | 71 +++++++++++++++++++++++++++++------------ 1 file changed, 50 insertions(+), 21 deletions(-) diff --git a/traceroute_collector.py b/traceroute_collector.py index 44146a4..475108f 100755 --- a/traceroute_collector.py +++ b/traceroute_collector.py @@ -43,31 +43,60 @@ def parse_traceroute_output(output): previous_ip = ip_address return hops -def create_tables(): - conn = sqlite3.connect('traceroute.db') +def create_tables(databasefile): + # Connect to the SQLite database + conn = sqlite3.connect(databasefile) cursor = conn.cursor() - # Table to store unique links - cursor.execute(''' - CREATE TABLE IF NOT EXISTS links ( - id INTEGER PRIMARY KEY, - source_ip TEXT, - destination_ip TEXT, - UNIQUE(source_ip, destination_ip) - ) - ''') + # SQL statements to create the tables + create_links_table = """ + CREATE TABLE IF NOT EXISTS Links ( + id INTEGER PRIMARY KEY, + source_ip TEXT NOT NULL, + destination_ip TEXT NOT NULL, + UNIQUE(source_ip, destination_ip) + ); + """ - # Table to store latency and timestamp for each link - cursor.execute(''' - CREATE TABLE IF NOT EXISTS link_latency ( - id INTEGER PRIMARY KEY, - link_id INTEGER, - latency REAL, - timestamp TEXT, - FOREIGN KEY (link_id) REFERENCES links (id) - ) - ''') + create_paths_table = """ + CREATE TABLE IF NOT EXISTS Paths ( + id INTEGER PRIMARY KEY, + start_ip TEXT NOT NULL, + end_ip TEXT NOT NULL, + hops_json TEXT NOT NULL, + UNIQUE(start_ip, end_ip, hops_json) + ); + """ + create_latency_table = """ + CREATE TABLE IF NOT EXISTS Latency ( + id INTEGER PRIMARY KEY, + link_id INTEGER NOT NULL, + timestamp DATETIME DEFAULT CURRENT_TIMESTAMP, + latency_ms REAL NOT NULL, + FOREIGN KEY (link_id) REFERENCES Links(id) + ); + """ + + create_hopdetails_table = """ + CREATE TABLE IF NOT EXISTS HopDetails ( + id INTEGER PRIMARY KEY, + link_id INTEGER NOT NULL, + ttl INTEGER, + icmp_type INTEGER, + icmp_code INTEGER, + packet_loss REAL, + FOREIGN KEY (link_id) REFERENCES Links(id) + ); + """ + + # Execute the SQL statements + cursor.execute(create_links_table) + cursor.execute(create_paths_table) + cursor.execute(create_latency_table) + cursor.execute(create_hopdetails_table) + + # Commit changes and close the connection conn.commit() conn.close()