From 8ced474edb5960b59759146967823eef37107903 Mon Sep 17 00:00:00 2001 From: Nick Leffler Date: Thu, 16 Sep 2021 20:52:19 +0000 Subject: [PATCH] init commit --- README.md | 12 + speedtest2influx.json | 542 ++++++++++++++++++++++++++++++++++++++++++ speedtest2influx.sh | 63 +++++ 3 files changed, 617 insertions(+) create mode 100644 speedtest2influx.json create mode 100644 speedtest2influx.sh diff --git a/README.md b/README.md index 0d9c78e..1e8f278 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,14 @@ # speedtest2influx + +This requires jq, curl, and speedtest-cli + +```apt install -y jq curl speedtest-cli``` + +--- + +Update the influx creds in the script then import the speedtest2influx.json into your dashboard. Make sure your data source is name Speedtest for this to work + +--- + +add ```0 * * * * root /path/to/speedtest2influx.sh``` to ```/etc/crontab``` diff --git a/speedtest2influx.json b/speedtest2influx.json new file mode 100644 index 0000000..ff94978 --- /dev/null +++ b/speedtest2influx.json @@ -0,0 +1,542 @@ +{ + "annotations": { + "list": [ + { + "builtIn": 1, + "datasource": "-- Grafana --", + "enable": true, + "hide": true, + "iconColor": "rgba(0, 211, 255, 1)", + "name": "Annotations & Alerts", + "target": { + "limit": 100, + "matchAny": false, + "tags": [], + "type": "dashboard" + }, + "type": "dashboard" + } + ] + }, + "editable": true, + "gnetId": null, + "graphTooltip": 0, + "id": 77, + "iteration": 1631825305930, + "links": [], + "panels": [ + { + "datasource": "Speedtest", + "fieldConfig": { + "defaults": { + "color": { + "mode": "palette-classic" + }, + "custom": { + "axisLabel": "", + "axisPlacement": "auto", + "barAlignment": 0, + "drawStyle": "line", + "fillOpacity": 0, + "gradientMode": "none", + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + }, + "lineInterpolation": "linear", + "lineWidth": 1, + "pointSize": 5, + "scaleDistribution": { + "type": "linear" + }, + "showPoints": "always", + "spanNulls": true, + "stacking": { + "group": "A", + "mode": "none" + }, + "thresholdsStyle": { + "mode": "off" + } + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": null + }, + { + "color": "red", + "value": 80 + } + ] + }, + "unit": "binbps" + }, + "overrides": [] + }, + "gridPos": { + "h": 8, + "w": 12, + "x": 0, + "y": 0 + }, + "id": 2, + "options": { + "legend": { + "calcs": [], + "displayMode": "list", + "placement": "bottom" + }, + "tooltip": { + "mode": "single" + } + }, + "targets": [ + { + "groupBy": [ + { + "params": [ + "$__interval" + ], + "type": "time" + }, + { + "params": [ + "null" + ], + "type": "fill" + } + ], + "measurement": "download", + "orderByTime": "ASC", + "policy": "default", + "query": "SELECT mean(\"value\") FROM \"download\" WHERE (\"host\" =~ /^$server$/) AND $timeFilter GROUP BY time($__interval) fill(null)", + "rawQuery": false, + "refId": "A", + "resultFormat": "time_series", + "select": [ + [ + { + "params": [ + "value" + ], + "type": "field" + }, + { + "params": [], + "type": "mean" + } + ] + ], + "tags": [ + { + "key": "host", + "operator": "=~", + "value": "/^$server$/" + } + ] + } + ], + "title": "Download", + "type": "timeseries" + }, + { + "datasource": "Speedtest", + "fieldConfig": { + "defaults": { + "color": { + "mode": "palette-classic" + }, + "custom": { + "axisLabel": "", + "axisPlacement": "auto", + "barAlignment": 0, + "drawStyle": "line", + "fillOpacity": 0, + "gradientMode": "none", + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + }, + "lineInterpolation": "linear", + "lineWidth": 1, + "pointSize": 5, + "scaleDistribution": { + "type": "linear" + }, + "showPoints": "always", + "spanNulls": true, + "stacking": { + "group": "A", + "mode": "none" + }, + "thresholdsStyle": { + "mode": "off" + } + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": null + }, + { + "color": "red", + "value": 80 + } + ] + }, + "unit": "binbps" + }, + "overrides": [] + }, + "gridPos": { + "h": 8, + "w": 12, + "x": 12, + "y": 0 + }, + "id": 3, + "options": { + "legend": { + "calcs": [], + "displayMode": "list", + "placement": "bottom" + }, + "tooltip": { + "mode": "single" + } + }, + "targets": [ + { + "groupBy": [ + { + "params": [ + "$__interval" + ], + "type": "time" + }, + { + "params": [ + "null" + ], + "type": "fill" + } + ], + "measurement": "upload", + "orderByTime": "ASC", + "policy": "default", + "query": "SELECT mean(\"value\") FROM \"download\" WHERE (\"host\" =~ /^$server$/) AND $timeFilter GROUP BY time($__interval) fill(null)", + "rawQuery": false, + "refId": "A", + "resultFormat": "time_series", + "select": [ + [ + { + "params": [ + "value" + ], + "type": "field" + }, + { + "params": [], + "type": "mean" + } + ] + ], + "tags": [ + { + "key": "host", + "operator": "=~", + "value": "/^$server$/" + } + ] + } + ], + "title": "Upload", + "type": "timeseries" + }, + { + "datasource": "Speedtest", + "fieldConfig": { + "defaults": { + "color": { + "mode": "palette-classic" + }, + "custom": { + "axisLabel": "", + "axisPlacement": "auto", + "barAlignment": 0, + "drawStyle": "line", + "fillOpacity": 0, + "gradientMode": "none", + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + }, + "lineInterpolation": "linear", + "lineWidth": 1, + "pointSize": 5, + "scaleDistribution": { + "type": "linear" + }, + "showPoints": "always", + "spanNulls": true, + "stacking": { + "group": "A", + "mode": "none" + }, + "thresholdsStyle": { + "mode": "off" + } + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": null + }, + { + "color": "red", + "value": 80 + } + ] + }, + "unit": "ms" + }, + "overrides": [] + }, + "gridPos": { + "h": 8, + "w": 12, + "x": 0, + "y": 8 + }, + "id": 4, + "options": { + "legend": { + "calcs": [], + "displayMode": "list", + "placement": "bottom" + }, + "tooltip": { + "mode": "single" + } + }, + "targets": [ + { + "groupBy": [ + { + "params": [ + "$__interval" + ], + "type": "time" + }, + { + "params": [ + "null" + ], + "type": "fill" + } + ], + "measurement": "ping", + "orderByTime": "ASC", + "policy": "default", + "query": "SELECT mean(\"value\") FROM \"download\" WHERE (\"host\" =~ /^$server$/) AND $timeFilter GROUP BY time($__interval) fill(null)", + "rawQuery": false, + "refId": "A", + "resultFormat": "time_series", + "select": [ + [ + { + "params": [ + "value" + ], + "type": "field" + }, + { + "params": [], + "type": "mean" + } + ] + ], + "tags": [ + { + "key": "host", + "operator": "=~", + "value": "/^$server$/" + } + ] + } + ], + "title": "Ping", + "type": "timeseries" + }, + { + "datasource": "Speedtest", + "fieldConfig": { + "defaults": { + "color": { + "mode": "palette-classic" + }, + "custom": { + "axisLabel": "", + "axisPlacement": "auto", + "barAlignment": 0, + "drawStyle": "line", + "fillOpacity": 0, + "gradientMode": "none", + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + }, + "lineInterpolation": "linear", + "lineWidth": 1, + "pointSize": 5, + "scaleDistribution": { + "type": "linear" + }, + "showPoints": "always", + "spanNulls": true, + "stacking": { + "group": "A", + "mode": "none" + }, + "thresholdsStyle": { + "mode": "off" + } + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": null + }, + { + "color": "red", + "value": 80 + } + ] + } + }, + "overrides": [] + }, + "gridPos": { + "h": 8, + "w": 12, + "x": 12, + "y": 8 + }, + "id": 6, + "options": { + "legend": { + "calcs": [], + "displayMode": "list", + "placement": "bottom" + }, + "tooltip": { + "mode": "single" + } + }, + "targets": [ + { + "groupBy": [ + { + "params": [ + "$__interval" + ], + "type": "time" + }, + { + "params": [ + "null" + ], + "type": "fill" + } + ], + "measurement": "server_distance", + "orderByTime": "ASC", + "policy": "default", + "refId": "A", + "resultFormat": "time_series", + "select": [ + [ + { + "params": [ + "value" + ], + "type": "field" + }, + { + "params": [], + "type": "mean" + } + ] + ], + "tags": [ + { + "key": "host", + "operator": "=~", + "value": "/^$server$/" + } + ] + } + ], + "title": "Server Distance", + "type": "timeseries" + } + ], + "schemaVersion": 30, + "style": "dark", + "tags": [], + "templating": { + "list": [ + { + "allValue": null, + "current": { + "selected": false, + "text": "pi02.homenet.leffler.me", + "value": "pi02.homenet.leffler.me" + }, + "datasource": "Speedtest", + "definition": "SHOW TAG VALUES WITH KEY=host", + "description": null, + "error": null, + "hide": 0, + "includeAll": false, + "label": null, + "multi": false, + "name": "server", + "options": [], + "query": "SHOW TAG VALUES WITH KEY=host", + "refresh": 1, + "regex": "", + "skipUrlSync": false, + "sort": 0, + "type": "query" + } + ] + }, + "time": { + "from": "now-6h", + "to": "now" + }, + "timepicker": {}, + "timezone": "", + "title": "Speedtest", + "uid": "ICgsxYS7z", + "version": 5 +} diff --git a/speedtest2influx.sh b/speedtest2influx.sh new file mode 100644 index 0000000..e029e3f --- /dev/null +++ b/speedtest2influx.sh @@ -0,0 +1,63 @@ +#!/bin/bash + + +# provisioned in mbps +provisioned_upload=200 +provisioned_download=10 + +influx_url="https://influx.domain.com" # without trailing slash and with proto (http/s) +influx_db="speedtest_db" +influx_user="speedtest_user" +influx_pass="speedtest_password" + +#### Starts here #### + +host=$(hostname --fqdn) + + +#if ! command -v bc &> /dev/null +#then +# echo "bc could not be found" +# exit 1 +#fi + +if ! command -v jq &> /dev/null +then + echo "jq could not be found" + exit 1 +fi + + +if ! command -v curl &> /dev/null +then + echo "curl could not be found" + exit +fi + + +if ! command -v speedtest-cli &> /dev/null +then + echo "speedtest-cli could not be found" + exit +fi + +data=$(speedtest-cli --json) + +ping=$(jq .ping <<< "${data}") +upload=$(jq .upload <<< "${data}") +download=$(jq .download <<< "${data}") +server_name=$(jq .server.name <<< "${data}") +server_sponsor=$(jq .server.sponsor <<< "${data}") +server_id=$(jq .server.id <<< "${data}") +server_distance=$(jq .server.d <<< "${data}") + +curl -XPOST "${influx_url}/write?db=${influx_db}" \ + -u "${influx_user}:${influx_pass}" \ + --data-binary \ +"upload,host=${host} value=${upload} +download,host=${host} value=${download} +ping,host=${host} value=${ping} +server_name,host=${host} value=${server_name} +server_sponsor,host=${host} value=${server_sponsor} +server_id,host=${host} value=${server_id} +server_distance,host=${host} value=${server_distance}"