forked from vonProteus/InternetSpeedMonitor
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ism.py
42 lines (35 loc) · 1.28 KB
/
ism.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
import os
import re
import subprocess
from influxdb import InfluxDBClient
import json
import time
taghost = os.environ["TAGHOST"]
influxhost = os.environ["INFLUXHOST"]
influxport = os.environ["INFLUXPORT"]
influxuser = os.environ["INFLUXUSER"]
influxpass = os.environ["INFLUXPASS"]
influxdatabasename = os.environ["INFLUXDATABASENAME"]
sleeptime = int(os.environ["INTERVAL"])
while True:
response = subprocess.Popen('speedtest --accept-license --accept-gdpr -f json', shell=True, stdout=subprocess.PIPE).stdout.read().decode('utf-8')
data = json.loads(response)
speed_data = [
{
"measurement" : "internet_speed",
"tags" : {
"host": taghost
},
"fields" : {
"download" : float(data['download']['bandwidth']) * 8,
"upload" : float(data['upload']['bandwidth']) * 8,
"ping" : float(data['ping']['latency']),
"jitter" : float(data['ping']['jitter'])
}
}
]
print(json.dumps(data, indent = 1))
print(json.dumps(speed_data, indent = 1))
client = InfluxDBClient(influxhost, influxport, influxuser, influxpass, influxdatabasename)
client.write_points(speed_data)
time.sleep(sleeptime)