mirror of
https://github.com/jokob-sk/NetAlertX.git
synced 2026-03-31 07:12:23 -07:00
feat: implement hybrid native/python speedtest engine
- Introduce native Ookla Speedtest binary support for Gigabit connections - Add intelligent engine detection with automatic fallback to python-cli version - Map full JSON payload to Watched_Value3 for n8n integration - Add Spanish (es_es) localizations and update README instructions
This commit is contained in:
@@ -1,11 +1,33 @@
|
||||
## Overview
|
||||
|
||||
A simple plugin allowing for executing regular internet speed tests.
|
||||
A plugin allowing for executing regular internet speed tests.
|
||||
|
||||
### Usage
|
||||
|
||||
- N/A
|
||||
This plugin supports two engines:
|
||||
1. **Baseline Engine**: Uses the Python `speedtest-cli` library (default).
|
||||
2. **Native Engine (Optimized)**: Uses the native Ookla Speedtest binary.
|
||||
|
||||
#### Opt-in for Native Engine
|
||||
To use the native engine:
|
||||
- Provide the native `speedtest` binary in your environment: [Speedtest CLI Homepage](https://www.speedtest.net/apps/cli)
|
||||
- Map the binary to `/usr/bin/speedtest` in the container.
|
||||
- **Ensure the native speedtest binary is installed on the host at the source path.**
|
||||
|
||||
```yaml
|
||||
volumes:
|
||||
- /usr/bin/speedtest:/usr/bin/speedtest:ro
|
||||
```
|
||||
- The plugin will automatically detect and use it for subsequent tests.
|
||||
|
||||
|
||||
### Data Mapping
|
||||
|
||||
- **Watched_Value1** — Download Speed (Mbps).
|
||||
- **Watched_Value2** — Upload Speed (Mbps).
|
||||
- **Watched_Value3** — Full JSON payload (useful for n8n or detailed webhooks).
|
||||
|
||||
### Notes
|
||||
|
||||
- N/A
|
||||
- The native binary is recommended for connections > 100 Mbps.
|
||||
- If the native binary is not detected, the plugin seamlessly falls back to the baseline library.
|
||||
@@ -240,18 +240,18 @@
|
||||
"default_value": "",
|
||||
"options": [],
|
||||
"localized": ["name"],
|
||||
"name": [
|
||||
"name": [
|
||||
{
|
||||
"language_code": "en_us",
|
||||
"string": "N/A"
|
||||
"string": "Full JSON"
|
||||
},
|
||||
{
|
||||
"language_code": "es_es",
|
||||
"string": "N/A"
|
||||
"string": "JSON completo"
|
||||
},
|
||||
{
|
||||
"language_code": "de_de",
|
||||
"string": "N/A"
|
||||
"string": "Vollständiges JSON"
|
||||
}
|
||||
]
|
||||
},
|
||||
@@ -591,11 +591,15 @@
|
||||
"description": [
|
||||
{
|
||||
"language_code": "en_us",
|
||||
"string": "Send a notification if selected values change. Use <code>CTRL + Click</code> to select/deselect. <ul> <li><code>Watched_Value1</code> is Download speed (not recommended)</li><li><code>Watched_Value2</code> is Upload speed (not recommended)</li><li><code>Watched_Value3</code> unused </li><li><code>Watched_Value4</code> unused </li></ul>"
|
||||
"string": "Send a notification if selected values change. Use <code>CTRL + Click</code> to select/deselect. <ul> <li><code>Watched_Value1</code> is Download speed (not recommended)</li><li><code>Watched_Value2</code> is Upload speed (not recommended)</li><li><code>Watched_Value3</code> is JSON payload for webhooks (schema varies by engine)</li><li><code>Watched_Value4</code> unused </li></ul>"
|
||||
},
|
||||
{
|
||||
"language_code": "es_es",
|
||||
"string": "Envíe una notificación si los valores seleccionados cambian. Use <code>CTRL + Clic</code> para seleccionar/deseleccionar. <ul> <li><code>Watched_Value1</code> es la velocidad de descarga (no recomendado)</li><li><code>Watched_Value2</code> es la velocidad de carga (no recomendado)</li><li><code>Watched_Value3</code> es la carga útil JSON para webhooks (el esquema varía según el motor)</li><li><code>Watched_Value4</code> no se usa </li></ul>"
|
||||
},
|
||||
{
|
||||
"language_code": "de_de",
|
||||
"string": "Sende eine Benachrichtigung, wenn ein ausgwählter Wert sich ändert. <code>STRG + klicken</code> zum aus-/abwählen. <ul> <li><code>Watched_Value1</code> ist die Download-Geschwindigkeit (nicht empfohlen)</li><li><code>Watched_Value2</code> ist die Upload-Geschwindigkeit (nicht empfohlen)</li><li><code>Watched_Value3</code> ist nicht in Verwendung </li><li><code>Watched_Value4</code> ist nicht in Verwendung </li></ul>"
|
||||
"string": "Sende eine Benachrichtigung, wenn ein ausgwählter Wert sich ändert. <code>STRG + klicken</code> zum aus-/abwählen. <ul> <li><code>Watched_Value1</code> ist die Download-Geschwindigkeit (nicht empfohlen)</li><li><code>Watched_Value2</code> ist die Upload-Geschwindigkeit (nicht empfohlen)</li><li><code>Watched_Value3</code> ist JSON-Payload für Webhooks (Schema variiert je nach Engine)</li><li><code>Watched_Value4</code> ist nicht in Verwendung </li></ul>"
|
||||
}
|
||||
]
|
||||
},
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
import os
|
||||
import sys
|
||||
import speedtest
|
||||
import subprocess
|
||||
import json
|
||||
|
||||
# Register NetAlertX directories
|
||||
INSTALL_PATH = os.getenv('NETALERTX_APP', '/app')
|
||||
@@ -14,8 +16,7 @@ from logger import mylog, Logger # noqa: E402 [flake8 lint suppression]
|
||||
from helper import get_setting_value # noqa: E402 [flake8 lint suppression]
|
||||
import conf # noqa: E402 [flake8 lint suppression]
|
||||
from pytz import timezone # noqa: E402 [flake8 lint suppression]
|
||||
from const import logPath # noqa: E402 [flake8 lint suppression]
|
||||
|
||||
from const import logPath, NATIVE_SPEEDTEST_PATH
|
||||
# Make sure the TIMEZONE for logging is correct
|
||||
conf.tz = timezone(get_setting_value('TIMEZONE'))
|
||||
|
||||
@@ -39,7 +40,7 @@ def main():
|
||||
secondaryId = timeNowDB(),
|
||||
watched1 = speedtest_result['download_speed'],
|
||||
watched2 = speedtest_result['upload_speed'],
|
||||
watched3 = 'null',
|
||||
watched3 = speedtest_result['full_json'],
|
||||
watched4 = 'null',
|
||||
extra = 'null',
|
||||
foreignKey = 'null'
|
||||
@@ -48,23 +49,62 @@ def main():
|
||||
|
||||
|
||||
def run_speedtest():
|
||||
native_path = NATIVE_SPEEDTEST_PATH
|
||||
if os.path.exists(native_path):
|
||||
mylog('verbose', ["[INTRSPD] Native speedtest binary detected, using it."])
|
||||
try:
|
||||
# Safe parsing of timeout setting
|
||||
try:
|
||||
raw_timeout = get_setting_value('INTRSPD_RUN_TIMEOUT')
|
||||
timeout = int(raw_timeout) if raw_timeout else 60
|
||||
except (ValueError, TypeError):
|
||||
timeout = 60
|
||||
|
||||
if timeout < 60:
|
||||
timeout = 60
|
||||
|
||||
cmd = [native_path, "--format=json", "--accept-license", "--accept-gdpr"]
|
||||
result = subprocess.run(cmd, capture_output=True, text=True, timeout=timeout)
|
||||
|
||||
if result.returncode == 0:
|
||||
try:
|
||||
data = json.loads(result.stdout)
|
||||
download_speed = round(data['download']['bandwidth'] * 8 / 10**6, 2)
|
||||
upload_speed = round(data['upload']['bandwidth'] * 8 / 10**6, 2)
|
||||
except (json.JSONDecodeError, KeyError, TypeError) as parse_error:
|
||||
mylog('none', [f"[INTRSPD] Failed to parse native JSON: {parse_error}"])
|
||||
# Fall through to baseline fallback
|
||||
else:
|
||||
mylog('verbose', [f"[INTRSPD] Native Result (down|up): {download_speed} Mbps|{upload_speed} Mbps"])
|
||||
return {
|
||||
'download_speed': download_speed,
|
||||
'upload_speed': upload_speed,
|
||||
'full_json': result.stdout.strip()
|
||||
}
|
||||
|
||||
except subprocess.TimeoutExpired:
|
||||
mylog('none', ["[INTRSPD] Native speedtest timed out, falling back to baseline."])
|
||||
except Exception as e:
|
||||
mylog('none', [f"[INTRSPD] Error running native speedtest: {e!s}, falling back to baseline."])
|
||||
|
||||
# Baseline fallback
|
||||
try:
|
||||
st = speedtest.Speedtest(secure=True)
|
||||
st.get_best_server()
|
||||
download_speed = round(st.download() / 10**6, 2) # Convert to Mbps
|
||||
upload_speed = round(st.upload() / 10**6, 2) # Convert to Mbps
|
||||
|
||||
mylog('verbose', [f"[INTRSPD] Result (down|up): {str(download_speed)} Mbps|{upload_speed} Mbps"])
|
||||
|
||||
download_speed = round(st.download() / 10**6, 2)
|
||||
upload_speed = round(st.upload() / 10**6, 2)
|
||||
mylog('verbose', [f"[INTRSPD] Baseline Result (down|up): {download_speed} Mbps|{upload_speed} Mbps"])
|
||||
return {
|
||||
'download_speed': download_speed,
|
||||
'upload_speed': upload_speed,
|
||||
'full_json': json.dumps(st.results.dict())
|
||||
}
|
||||
except Exception as e:
|
||||
mylog('verbose', [f"[INTRSPD] Error running speedtest: {str(e)}"])
|
||||
return {
|
||||
'download_speed': -1,
|
||||
'upload_speed': -1,
|
||||
'full_json': json.dumps({"error": str(e)})
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user