Skip to content

Commit

Permalink
Merge pull request #19 from MiguelNdeCarvalho/hotfix-outputparse
Browse files Browse the repository at this point in the history
Fix crashing and other minor issues caused by failed cli attempt
  • Loading branch information
MiguelNdeCarvalho authored Oct 8, 2020
2 parents 028fd2d + 55da636 commit 5cf5561
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 16 deletions.
17 changes: 11 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
# Speedtest Exporter

## Description
Simple **Speedtest exporter** for **Prometheus** written in **Python** using the official CLI from **Ookla**

This is a simple **speedtest exporter** for **Prometheus** written in **Python**
## Quick Start

## Running the Container

To run the container via **CLI**:
**Docker**:

```bash
docker run -d \
Expand All @@ -29,4 +27,11 @@ services:
restart: unless-stopped
```

Then just acess the page `http://localhost:9112/` and you will have the metrics.
Then just acess the page `http://localhost:9112/` and you will have the metrics.

### Environment Variables

The following environment variables configure the exporter:

* `SPEEDTEST_SERVER`
Custom server ID from Speedtest server list like [https://telcodb.net/explore/speedtest-servers/](https://telcodb.net/explore/speedtest-servers/)
36 changes: 26 additions & 10 deletions src/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,34 @@ def is_json(myjson):
return True

def run_speedtest():
print('Starting speedtest')
cmd = ["speedtest", "--format=json-pretty", "--progress=no", "--accept-license", "--accept-gdpr"]
server = os.environ.get('SPEEDTEST_SERVER')
if server:
if server.isnumeric():
print("Using custom server ID: "+str(server))
cmd.append("--server-id="+str(server))
output = subprocess.check_output(cmd)
if is_json(output):
data = json.loads(output)
actual_ping = int(data['ping']['latency'])
download = bytes_to_bits(data['download']['bandwidth'])
upload = bytes_to_bits(data['upload']['bandwidth'])
return (actual_ping, download, upload)
cmd.append("--server-id=" + str(server))

while True:
try:
output = subprocess.check_output(cmd)
except subprocess.CalledProcessError as e:
output = e.output
if is_json(output):
data = json.loads(output)
if "error" in data:
# If we get here it probably means that socket timed out(Network issues?)
print('Something went wrong')
print(data['error'])
return None
if "type" in data:
if data['type'] == 'log':
print(str(data["timestamp"]) + " - " + str(data["message"]))
if data['type'] == 'result':
actual_ping = int(data['ping']['latency'])
download = bytes_to_bits(data['download']['bandwidth'])
upload = bytes_to_bits(data['upload']['bandwidth'])
return (actual_ping, download, upload)

def update_results(test_done):
ping.set(test_done[0])
Expand All @@ -46,8 +61,9 @@ def run(http_port, sleep_time):
print("Successfully started Speedtest Exporter on http://localhost:" + str(http_port))
while True:
test = run_speedtest()
update_results(test)
time.sleep(sleep_time)
if isinstance(test, tuple):
update_results(test)
time.sleep(sleep_time)

if __name__ == '__main__':
# Create the Metrics
Expand Down

0 comments on commit 5cf5561

Please sign in to comment.