diff options
-rwxr-xr-x | bin/speedtest.py | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/bin/speedtest.py b/bin/speedtest.py new file mode 100755 index 0000000..2c9a529 --- /dev/null +++ b/bin/speedtest.py | |||
@@ -0,0 +1,31 @@ | |||
1 | #!/home/mcrute/.local/bin/python3 | ||
2 | |||
3 | import json | ||
4 | import subprocess | ||
5 | from urllib import request | ||
6 | |||
7 | def bits_per_second(bytes, seconds): | ||
8 | return round(((bytes/seconds) * 8)) | ||
9 | |||
10 | r = subprocess.run( | ||
11 | ["/usr/bin/iperf3", "-J", "-c", "23.149.16.1"], capture_output=True) | ||
12 | |||
13 | results = json.loads(r.stdout) | ||
14 | |||
15 | sent_seconds = results["end"]["sum_sent"]["seconds"] | ||
16 | sent_bytes = results["end"]["sum_sent"]["bytes"] | ||
17 | |||
18 | received_seconds = results["end"]["sum_received"]["seconds"] | ||
19 | received_bytes = results["end"]["sum_received"]["bytes"] | ||
20 | |||
21 | req = request.Request( | ||
22 | "http://monitoring-1.sea1.crute.me:9091/metrics/job/wave_bandwidth_monitor", | ||
23 | method="POST") | ||
24 | |||
25 | req.add_header("Content-Type", "text/plain") | ||
26 | request.urlopen(req, | ||
27 | data="bandwidth_sent_bps {}\n".format( | ||
28 | bits_per_second(sent_bytes, sent_seconds)).encode("us-ascii")) | ||
29 | request.urlopen(req, | ||
30 | data="bandwidth_received_bps {}\n".format( | ||
31 | bits_per_second(received_bytes, received_seconds)).encode("us-ascii")) | ||