Completed NMAP & Refactoring
This commit is contained in:
@@ -16,6 +16,11 @@ def store_data(new_data):
|
||||
with open(DATA_FILE, 'w') as f:
|
||||
json.dump(data, f, indent=4)
|
||||
|
||||
def _calculate_average(data, key1, key2):
|
||||
"""Helper function to calculate the average of a nested key in a list of dicts."""
|
||||
values = [d[key1][key2] for d in data if key1 in d and key2 in d[key1] and d[key1][key2] != "N/A"]
|
||||
return sum(values) / len(values) if values else 0
|
||||
|
||||
def calculate_baselines():
|
||||
data = load_data()
|
||||
if not data:
|
||||
@@ -29,23 +34,23 @@ def calculate_baselines():
|
||||
return {}
|
||||
|
||||
baseline_metrics = {
|
||||
'avg_rtt': sum(d['network_metrics']['rtt_avg'] for d in recent_data if 'rtt_avg' in d['network_metrics']) / len(recent_data),
|
||||
'packet_loss': sum(d['network_metrics']['packet_loss_rate'] for d in recent_data if 'packet_loss_rate' in d['network_metrics']) / len(recent_data),
|
||||
'avg_cpu_temp': sum(d['cpu_temperature']['cpu_temperature'] for d in recent_data if d['cpu_temperature']['cpu_temperature'] != "N/A") / len(recent_data),
|
||||
'avg_gpu_temp': sum(d['gpu_temperature']['gpu_temperature'] for d in recent_data if d['gpu_temperature']['gpu_temperature'] != "N/A") / len(recent_data),
|
||||
'avg_rtt': _calculate_average(recent_data, 'network_metrics', 'rtt_avg'),
|
||||
'packet_loss': _calculate_average(recent_data, 'network_metrics', 'packet_loss_rate'),
|
||||
'avg_cpu_temp': _calculate_average(recent_data, 'cpu_temperature', 'cpu_temperature'),
|
||||
'avg_gpu_temp': _calculate_average(recent_data, 'gpu_temperature', 'gpu_temperature'),
|
||||
}
|
||||
|
||||
# Baseline for open ports from nmap scans
|
||||
host_ports = {}
|
||||
for d in recent_data:
|
||||
if 'nmap_results' in d and 'scan' in d['nmap_results']:
|
||||
for host, scan_data in d['nmap_results']['scan'].items():
|
||||
if host not in host_ports:
|
||||
host_ports[host] = set()
|
||||
if 'tcp' in scan_data:
|
||||
for port, port_data in scan_data['tcp'].items():
|
||||
if port_data['state'] == 'open':
|
||||
host_ports[host].add(port)
|
||||
if 'nmap_results' in d and 'hosts' in d.get('nmap_results', {}):
|
||||
for host_info in d['nmap_results']['hosts']:
|
||||
host_ip = host_info['ip']
|
||||
if host_ip not in host_ports:
|
||||
host_ports[host_ip] = set()
|
||||
|
||||
for port_info in host_info.get('open_ports', []):
|
||||
host_ports[host_ip].add(port_info['port'])
|
||||
|
||||
# Convert sets to sorted lists for JSON serialization
|
||||
for host, ports in host_ports.items():
|
||||
|
||||
Reference in New Issue
Block a user