Open EmilianoMaina opened 2 years ago
Hey,
@EmilianoMaina can you give me some more context please? Which version are you using of Python and have you installed everything from requirements.txt? There is no need to try it with Python2 as it won't work!
Thanks, MiguelNdeCarvalho
At the very end I was able to make it work but I had to change the code as below:
import subprocess
import json
import os
import logging
from prometheus_client import make_wsgi_app, Gauge
from flask import Flask
from waitress import serve
app = Flask("Speedtest-Exporter") # Create flask app
# Setup logging values
format_string = 'level=%(levelname)s datetime=%(asctime)s %(message)s'
logging.basicConfig(encoding='utf-8', level=logging.DEBUG,
format=format_string)
# Disable Waitress Logs
log = logging.getLogger('waitress')
log.disabled = True
# Create Metrics
server = Gauge('speedtest_server_id', 'Speedtest server ID')
ping = Gauge('speedtest_ping_latency_milliseconds',
'Speedtest current Ping in ms')
download_speed = Gauge('speedtest_download_bits_per_second',
'Speedtest current Download Speed in bit/s')
upload_speed = Gauge('speedtest_upload_bits_per_second',
'Speedtest current Upload speed in bits/s')
up = Gauge('speedtest_up', 'Speedtest status whether the scrape worked')
def bits_to_megabits(bits_per_sec):
megabits = round(bits_per_sec * (10**-6), 2)
return str(megabits) + "Mbps"
def is_json(myjson):
try:
json.loads(myjson)
except ValueError:
return False
return True
def runTest():
serverID = os.environ.get('SPEEDTEST_SERVER')
timeout = int(os.environ.get('SPEEDTEST_TIMEOUT', 90))
cmd = ["speedtest", "--json"]
if serverID:
cmd.append("--server-id={serverID}")
try:
output = subprocess.check_output(cmd, timeout=timeout)
except subprocess.CalledProcessError as e:
output = e.output
if not is_json(output):
if len(output) > 0:
logging.error('Speedtest CLI Error occurred that' +
'was not in JSON format')
return (0, 0, 0, 0, 0, 0)
except subprocess.TimeoutExpired:
logging.error('Speedtest CLI process took too long to complete ' +
'and was killed.')
return (0, 0, 0, 0, 0, 0)
if is_json(output):
data = json.loads(output)
if "error" in data:
# Socket error
print('Something went wrong')
print(data['error'])
return (0, 0, 0, 0, 0, 0) # Return all data as 0
actual_server = int(data['server']['id'])
actual_ping = data['ping']
download = data['download']
upload = data['upload']
return (actual_server,
actual_ping, download, upload, 1)
@app.route("/metrics")
def updateResults():
r_server, r_ping, r_download, r_upload, r_status = runTest()
server.set(r_server)
ping.set(r_ping)
download_speed.set(r_download)
upload_speed.set(r_upload)
up.set(r_status)
logging.info(
"Server=" + str(r_server)
+ " Ping=" + str(r_ping) + "ms"
+ " Download=" + bits_to_megabits(r_download)
+ " Upload=" + bits_to_megabits(r_upload)
)
return make_wsgi_app()
@app.route("/")
def mainPage():
return ("<h1>Welcome to Speedtest-Exporter.</h1>" +
"Click <a href='/metrics'>here</a> to see metrics.")
if __name__ == '__main__':
PORT = os.getenv('SPEEDTEST_PORT', 9798)
logging.info("Starting Speedtest-Exporter on http://localhost:" + str(PORT))
serve(app, host='0.0.0.0', port=PORT)
Now the coe above is running smoothly with Python 3.10
Sorry for only answering right now, but is this still occurring in the latest version?
-----------I have a similar issue
ubuntu:~/speedtest-exporter$ python src/exporter.py File "src/exporter.py", line 66 cmd.append(f"--server-id={serverID}") ^ SyntaxError: invalid syntax
changed to this to remove the end(f"-- if serverID: cmd.append("--server-id={serverID}")
----------after making that change I now get this issue
python exporter.py
Traceback (most recent call last):
File "exporter.py", line 6, in
~/speedtest-exporter/src$ pip install -r requirements.txt Requirement already satisfied: Werkzeug==2.3.6 in /home/jbrooksii/.local/lib/python3.8/site-packages (from -r requirements.txt (line 1)) (2.3.6) Requirement already satisfied: Flask==2.3.2 in /home/jbrooksii/.local/lib/python3.8/site-packages (from -r requirements.txt (line 2)) (2.3.2) Requirement already satisfied: prometheus_client==0.17.0 in /home/jbrooksii/.local/lib/python3.8/site-packages (from -r requirements.txt (line 3)) (0.17.0) Requirement already satisfied: waitress==2.1.2 in /home/jbrooksii/.local/lib/python3.8/site-packages (from -r requirements.txt (line 4)) (2.1.2) Requirement already satisfied: MarkupSafe>=2.1.1 in /home/jbrooksii/.local/lib/python3.8/site-packages (from Werkzeug==2.3.6->-r requirements.txt (line 1)) (2.1.3) Requirement already satisfied: importlib-metadata>=3.6.0; python_version < "3.10" in /home/jbrooksii/.local/lib/python3.8/site-packages (from Flask==2.3.2->-r requirements.txt (line 2)) (6.8.0) Requirement already satisfied: blinker>=1.6.2 in /home/jbrooksii/.local/lib/python3.8/site-packages (from Flask==2.3.2->-r requirements.txt (line 2)) (1.6.2) Requirement already satisfied: Jinja2>=3.1.2 in /home/jbrooksii/.local/lib/python3.8/site-packages (from Flask==2.3.2->-r requirements.txt (line 2)) (3.1.2) Requirement already satisfied: click>=8.1.3 in /home/jbrooksii/.local/lib/python3.8/site-packages (from Flask==2.3.2->-r requirements.txt (line 2)) (8.1.7) Requirement already satisfied: itsdangerous>=2.1.2 in /home/jbrooksii/.local/lib/python3.8/site-packages (from Flask==2.3.2->-r requirements.txt (line 2)) (2.1.2) Requirement already satisfied: zipp>=0.5 in /usr/lib/python3/dist-packages (from importlib-metadata>=3.6.0; python_version < "3.10"->Flask==2.3.2->-r requirements.txt (line 2)) (1.0.0) :~/speedtest-exporter/src$
Hello,
I installed manually on Raspberry Pi the exporter following instructions here.
At first launch of the explorer with Python 3 I have the following error:
pi@raspberrypi:~/speedtest-exporter $ python3 src/exporter.py Traceback (most recent call last): File "src/exporter.py", line 15, in <module> format=format_string) File "/usr/lib/python3.7/logging/__init__.py", line 1921, in basicConfig raise ValueError('Unrecognised argument(s): %s' % keys) ValueError: Unrecognised argument(s): encoding
If I launch with Python 2 the error become the following:
pi@raspberrypi:~/speedtest-exporter $ python src/exporter.py File "src/exporter.py", line 59 cmd.append(f"--server-id={serverID}") ^ SyntaxError: invalid syntax
Python version installed:pi@raspberrypi:~/speedtest-exporter $ python --version Python 2.7.16 pi@raspberrypi:~/speedtest-exporter $ python3 --version Python 3.7.3