-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptimize_network.py
More file actions
87 lines (73 loc) · 2.47 KB
/
optimize_network.py
File metadata and controls
87 lines (73 loc) · 2.47 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
import joblib
import numpy as np
import os
import subprocess
# -------------------------------
# CONFIG
# -------------------------------
MODEL_PATH = "models/random_forest_regressor.pkl"
THROUGHPUT_THRESHOLD = 10.0 # Mbps
MININET_HOST = "h1"
INTERFACE = "h1-eth0"
# -------------------------------
# LOAD MODEL
# -------------------------------
try:
model = joblib.load(MODEL_PATH)
except Exception as e:
print("❌ Failed to load model:", e)
exit(1)
# -------------------------------
# USER INPUT
# -------------------------------
try:
rtt = float(input("Enter current RTT (ms): "))
jitter = float(input("Enter current Jitter (ms): "))
except ValueError:
print("❌ Invalid input. Please enter numeric values.")
exit(1)
X = np.array([[rtt, jitter]])
# -------------------------------
# PREDICT THROUGHPUT
# -------------------------------
predicted_throughput = model.predict(X)[0]
print(f"\n📡 Predicted Throughput: {predicted_throughput:.2f} Mbps")
# -------------------------------
# CONGESTION LOGIC
# -------------------------------
if predicted_throughput < THROUGHPUT_THRESHOLD:
status = "SEVERE CONGESTION"
action = "Reduce bandwidth, increase queue, reroute traffic"
else:
status = "NORMAL"
action = "No action needed"
print(f"📊 Network Status: {status}")
print(f"🛠 Recommended Action: {action}")
# -------------------------------
# APPLY TC (ONLY IF MININET RUNNING)
# -------------------------------
def is_mininet_running():
try:
output = subprocess.check_output(["pgrep", "-f", f"mininet:{MININET_HOST}"])
return True
except subprocess.CalledProcessError:
return False
if status == "SEVERE CONGESTION":
print("\n⚠️ Attempting to apply bandwidth rule (only works if Mininet is running)...")
if is_mininet_running():
try:
# Clear existing qdisc
os.system(
f"sudo mnexec -a $(pgrep -f 'mininet:{MININET_HOST}') "
f"tc qdisc del dev {INTERFACE} root 2>/dev/null"
)
# Apply delay (example action)
os.system(
f"sudo mnexec -a $(pgrep -f 'mininet:{MININET_HOST}') "
f"tc qdisc add dev {INTERFACE} root netem delay 50ms"
)
print("✅ Traffic control rule applied successfully on h1-eth0")
except Exception as e:
print("❌ Failed to apply tc rule:", e)
else:
print("ℹ️ Mininet not running → skipping tc command")