New parser and storage functions to support the new database schema.
This commit is contained in:
		@@ -4,6 +4,7 @@
 | 
			
		||||
import subprocess
 | 
			
		||||
import sqlite3
 | 
			
		||||
import re
 | 
			
		||||
import json
 | 
			
		||||
from datetime import datetime
 | 
			
		||||
 | 
			
		||||
def run_traceroute(host):
 | 
			
		||||
@@ -11,36 +12,25 @@ def run_traceroute(host):
 | 
			
		||||
    return result.stdout.decode()
 | 
			
		||||
 | 
			
		||||
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
 | 
			
		||||
        timestamp = datetime.now().isoformat()
 | 
			
		||||
        if previous_ip:
 | 
			
		||||
            hops.append({
 | 
			
		||||
                'hop_number': hop_number,
 | 
			
		||||
                'source_ip': previous_ip,
 | 
			
		||||
                'destination_ip': ip_address,
 | 
			
		||||
                'latency': avg_latency,
 | 
			
		||||
                'timestamp': timestamp,
 | 
			
		||||
            })
 | 
			
		||||
        previous_ip = ip_address
 | 
			
		||||
    lines = output.strip().split('\n')
 | 
			
		||||
    hops = []
 | 
			
		||||
    for line in lines[1:]:
 | 
			
		||||
        hop_info = line.split()
 | 
			
		||||
        hop_number = int(hop_info[0])
 | 
			
		||||
        hop_ips = []
 | 
			
		||||
        hop_latencies = []
 | 
			
		||||
        for part in hop_info[1:]:
 | 
			
		||||
            if re.match(r'\d+\.\d+\.\d+\.\d+', part):  # Match IP address
 | 
			
		||||
                hop_ips.append(part)
 | 
			
		||||
            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
 | 
			
		||||
 | 
			
		||||
def create_tables(databasefile):
 | 
			
		||||
@@ -100,32 +90,40 @@ def create_tables(databasefile):
 | 
			
		||||
    conn.commit()
 | 
			
		||||
    conn.close()
 | 
			
		||||
 | 
			
		||||
def store_traceroute(hops):
 | 
			
		||||
    conn = sqlite3.connect('traceroute.db')
 | 
			
		||||
def store_traceroute(db_file, start_ip, end_ip, hops):
 | 
			
		||||
    conn = sqlite3.connect(db_file)
 | 
			
		||||
    cursor = conn.cursor()
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    # Insert links and get their IDs
 | 
			
		||||
    link_ids = []
 | 
			
		||||
    for hop in hops:
 | 
			
		||||
        # Insert or ignore link into links table
 | 
			
		||||
        cursor.execute('''
 | 
			
		||||
            INSERT OR IGNORE INTO links (source_ip, destination_ip)
 | 
			
		||||
            VALUES (?, ?)
 | 
			
		||||
        ''', (hop['source_ip'], hop['destination_ip']))
 | 
			
		||||
        
 | 
			
		||||
        # Retrieve the link_id
 | 
			
		||||
        cursor.execute('''
 | 
			
		||||
            SELECT id FROM links WHERE source_ip = ? AND destination_ip = ?
 | 
			
		||||
        ''', (hop['source_ip'], hop['destination_ip']))
 | 
			
		||||
        source_ip = start_ip if not link_ids else hops[len(link_ids)-1][0]
 | 
			
		||||
        destination_ip = hop[0]
 | 
			
		||||
        latency = hop[1]
 | 
			
		||||
 | 
			
		||||
        cursor.execute("""
 | 
			
		||||
            INSERT OR IGNORE INTO Links (source_ip, destination_ip) VALUES (?, ?)
 | 
			
		||||
        """, (source_ip, destination_ip))
 | 
			
		||||
        cursor.execute("""
 | 
			
		||||
            SELECT id FROM Links WHERE source_ip = ? AND destination_ip = ?
 | 
			
		||||
        """, (source_ip, destination_ip))
 | 
			
		||||
        link_id = cursor.fetchone()[0]
 | 
			
		||||
        
 | 
			
		||||
        # Insert latency data into link_latency table
 | 
			
		||||
        cursor.execute('''
 | 
			
		||||
            INSERT INTO link_latency (link_id, latency, timestamp)
 | 
			
		||||
            VALUES (?, ?, ?)
 | 
			
		||||
        ''', (link_id, hop['latency'], hop['timestamp']))
 | 
			
		||||
    
 | 
			
		||||
        link_ids.append(link_id)
 | 
			
		||||
 | 
			
		||||
        cursor.execute("""
 | 
			
		||||
            INSERT INTO Latency (link_id, latency_ms) VALUES (?, ?)
 | 
			
		||||
        """, (link_id, latency))
 | 
			
		||||
 | 
			
		||||
    # 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.close()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def retrieve_traceroute():
 | 
			
		||||
    conn = sqlite3.connect('traceroute.db')
 | 
			
		||||
    cursor = conn.cursor()
 | 
			
		||||
@@ -139,17 +137,21 @@ def retrieve_traceroute():
 | 
			
		||||
    conn.close()
 | 
			
		||||
    return rows
 | 
			
		||||
 | 
			
		||||
# Usage
 | 
			
		||||
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)
 | 
			
		||||
#    for hop in hops:
 | 
			
		||||
#        print(hop)
 | 
			
		||||
    store_traceroute(hops)
 | 
			
		||||
    stored_hops = retrieve_traceroute()
 | 
			
		||||
    for hop in stored_hops:
 | 
			
		||||
        print(f"Link: {hop[0]} -> {hop[1]}, Latency: {hop[2]} ms, Timestamp: {hop[3]}")
 | 
			
		||||
    if hops:
 | 
			
		||||
        start_ip = hops[0][0]
 | 
			
		||||
        store_traceroute(databasefile, start_ip, target, hops)
 | 
			
		||||
#    stored_hops = retrieve_traceroute()
 | 
			
		||||
#    for hop in stored_hops:
 | 
			
		||||
#        print(f"Link: {hop[0]} -> {hop[1]}, Latency: {hop[2]} ms, Timestamp: {hop[3]}")
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
exit(0)
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user