-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtrack_data_consumption.py
More file actions
100 lines (79 loc) · 3.14 KB
/
track_data_consumption.py
File metadata and controls
100 lines (79 loc) · 3.14 KB
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
import time
import matplotlib.pyplot as plt
import signal
import sys
import argparse
# Parse command-line arguments
parser = argparse.ArgumentParser(description="Monitor network data usage.")
parser.add_argument("interface", help="Name of the network interface (e.g., wlan0, eth0)")
args = parser.parse_args()
INTERFACE=args.interface
# Global data stores
timestamps = []
downloaded_data = []
uploaded_data = []
total_data = []
def get_network_data(interface=INTERFACE):
with open('/proc/net/dev', 'r') as f:
data = f.readlines()
for line in data:
if interface in line:
parts = line.split()
received_bytes = int(parts[1])
transmitted_bytes = int(parts[9])
return received_bytes, transmitted_bytes
return 0, 0
def create_graphs():
total_downloaded = sum(downloaded_data)
total_uploaded = sum(uploaded_data)
total_consumed = total_downloaded + total_uploaded
# Convert to MB
downloaded_mb = [d / (1024**2) for d in downloaded_data]
uploaded_mb = [u / (1024**2) for u in uploaded_data]
total_mb = [t / (1024**2) for t in total_data]
plt.figure(figsize=(10, 6))
plt.plot(timestamps, downloaded_mb, label='Downloaded (MB)', color='blue')
plt.plot(timestamps, uploaded_mb, label='Uploaded (MB)', color='green')
plt.plot(timestamps, total_mb, label='Total (MB)', color='orange')
plt.xlabel('Time')
plt.ylabel('Data (MB)')
plt.title('Internet Data Usage Over Time')
plt.legend()
# Add summary textbox
summary_text = (
f"Total Downloaded: {total_downloaded / (1024**2):.2f} MB\n"
f"Total Uploaded: {total_uploaded / (1024**2):.2f} MB\n"
f"Total: {total_consumed / (1024**2):.2f} MB"
)
plt.gcf().text(0.72, 0.55, summary_text, fontsize=10,
bbox=dict(facecolor='lightgrey', alpha=0.5))
# Save graph
plt.tight_layout()
plt.savefig("data_usage_summary.png")
print("📊 Graph saved as 'data_usage_summary.png'")
plt.show()
def signal_handler(sig, frame):
print("\nStopping data monitor and generating graph...")
create_graphs()
sys.exit(0)
def monitor_data(interface=INTERFACE, interval=10):
print(f"Monitoring interface: {interface}")
prev_received, prev_transmitted = get_network_data(interface)
signal.signal(signal.SIGINT, signal_handler)
while True:
time.sleep(interval)
new_received, new_transmitted = get_network_data(interface)
delta_received = new_received - prev_received
delta_transmitted = new_transmitted - prev_transmitted
total = delta_received + delta_transmitted
timestamp = time.strftime('%H:%M:%S')
print(f"[{timestamp}] Downloaded: {delta_received / 1024:.2f} KB, "
f"Uploaded: {delta_transmitted / 1024:.2f} KB, "
f"Total: {total / 1024:.2f} KB")
timestamps.append(timestamp)
downloaded_data.append(delta_received)
uploaded_data.append(delta_transmitted)
total_data.append(total)
prev_received, prev_transmitted = new_received, new_transmitted
# 👇 Start monitoring
monitor_data(interface=INTERFACE, interval=10)