/
iperf3mqtt.py
executable file
·169 lines (130 loc) · 4.68 KB
/
iperf3mqtt.py
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/usr/bin/env python
import sys
import time
import random
import logging
import yaml
import iperf3
import paho.mqtt.client as mqtt
from ping3 import ping
from pytimeparse import parse
stdout_handler = logging.StreamHandler(sys.stdout)
logging.basicConfig(
level=logging.DEBUG,
format='[%(asctime)s] {%(lineno)d} %(levelname)s - %(message)s',
handlers=[stdout_handler]
)
logger = logging.getLogger('SPEEDTEST_LOGGER')
def clamp(n, maxn, minn):
""" clamp value between min and max """
return max(minn, min(n, maxn))
def run_tests(config, mqtt_config, mqttc):
""" run both tests """
try_count = 0
result_down = None
while result_down is None:
result_down = run_test('received_bps', config)
if result_down is None:
try_count = try_count + 1
logger.error("Try %d", try_count)
time.sleep(clamp(try_count*5, 60, 5))
logger.info("Waiting for next try.")
try_count = 0
result_up = None
while result_up is None:
result_up = run_test('sent_bps', config)
if result_up is None:
try_count = try_count + 1
logger.error("Try %d", try_count)
time.sleep(clamp(try_count*5, 60, 5))
logger.info("Waiting for next try.")
logger.info("Pinging")
ping_result = ping(random.choice(config["hosts"])["host"], unit='ms')
if ping_result is None:
ping_result = 0
logger.info("Down: %f, Up: %f, Ping: %f", result_down, result_up,
ping_result)
logger.info("Publishing to MQTT %s:%d/%s",
mqtt_config.get("host", "127.0.0.1"),
mqtt_config.get("port", 1883),
config["mqtt"]["topic"])
mqtt_port = mqtt_config.get("port", 1883)
mqttc.connect(mqtt_config.get("host", "127.0.0.1"),
port=mqtt_port)
mqttc.publish(config["mqtt"]["topic"] + "/upload", "%f" % result_up,
retain=True)
mqttc.publish(config["mqtt"]["topic"] + "/download", "%f" %
result_down, retain=True)
mqttc.publish(config["mqtt"]["topic"] + "/ping", "%f" %
ping_result, retain=True)
def run_test(test_type, config):
""" run the actual test """
logger.info("Running %s", test_type)
server = random.choice(config["hosts"])
logger.info("Using %s", server.get("host", None))
client = iperf3.Client()
client.server_hostname = server["host"]
client.port = random.choice(server["ports"])
client.verbose = False
client.zerocopy = True
client.duration = 5
logger.info("Using port %s", client.port)
if test_type == 'received_bps':
client.reverse = True
result = client.run()
logger.info("Running test...")
if result is not None and \
hasattr(result, 'error') and \
result.error is not None:
logger.error("Iperf3 error: (%s:%d) - %s", client.server_hostname,
client.port, result.error)
return None
logger.info("Got a result!")
return getattr(result, test_type, None)
def run():
""" run the script """
try:
config_file = sys.argv[1]
except IndexError:
config_file = "/config.yml"
mqttc = mqtt.Client("Speedtest")
(config, mqtt_config) = load_config(config_file, mqttc)
while True:
run_tests(config, mqtt_config, mqttc)
logger.info("Sleeping for %ds", config.get("interval", 3600))
time.sleep(config.get("interval", 3600))
def load_config(config_file, mqttc):
""" load config from file and interpret values """
try:
logger.info('Loading config from %s', config_file)
config = yaml.load(open(config_file), Loader=yaml.SafeLoader)
except FileNotFoundError:
logger.info("Config not found. Using default values.")
config = {
"interval": "90m",
"hosts": [
{
"hostname": "ping.online.net",
"ports": [
5200,
5201,
5202,
]
}
],
"mqtt": {
"host": "127.0.0.1",
"port": "1883",
"username": "",
"password": "",
}
}
config["interval"] = parse(config.get("interval", "60m"))
mqtt_config = config.get("mqtt", {"username": "", "password": ""})
logger.info("Using MQTT-Server %s:%s",
mqtt_config.get("host", "127.0.0.1"),
mqtt_config.get("port", 1883))
mqttc.username_pw_set(mqtt_config.get("username", ""),
mqtt_config.get("password", ""))
return (config, mqtt_config)
run()