diff --git a/check_speedtest.py b/check_speedtest.py new file mode 100644 index 0000000..83e3c0e --- /dev/null +++ b/check_speedtest.py @@ -0,0 +1,66 @@ +#!/usr/bin/env python3 +import argparse +import sys +import traceback + +from cloudflarepycli import cloudflareclass + +import checker.nagios as nagios + + +def main(): + """ + Copied from __main__.py + https://github.com/tevslin/cloudflarepycli/blob/main/src/cloudflarepycli/__main__.py + """ + parser = argparse.ArgumentParser(description='Check internet speed.') + parser.add_argument('--warn-up', type=int, default=999999, help='Warn level of upload speed in Mbps.') + parser.add_argument('--critical-up', type=int, default=999999, help='Critical level of upload speed in Mbps.') + parser.add_argument('--warn-down', type=int, default=999999, help='Warn level of download speed in Mbps.') + parser.add_argument('--critical-down', type=int, default=999999, help='Critical level of download speed in Mbps.') + parser.add_argument('--warn-latency', type=int, default=60, help='Warn level of latency in ms.') + parser.add_argument('--critical-latency', type=int, default=100, help='Critical level of latency in ms.') + args = parser.parse_args() + + speedtest_results = cloudflareclass.cloudflare(printit=False).runalltests() + + out_str = f"upload: {speedtest_results['90th_percentile_upload_speed']['value']} Mbps, download: {speedtest_results['90th_percentile_download_speed']['value']} Mbps, latency: {speedtest_results['latency_ms']['value']} ms, Jitter: {speedtest_results['Jitter_ms']['value']} ms" + perf_data = f"'upload'={speedtest_results['90th_percentile_upload_speed']['value'] * 1e+6}B;{args.warn_up * 1e+6};{args.critical_up * 1e+6};0; 'download'={speedtest_results['90th_percentile_download_speed']['value'] * 1e+6}B;{args.warn_down * 1e+6};{args.critical_down * 1e+6};0; 'latency_ms'={speedtest_results['latency_ms']['value']}ms;{args.warn_latency};{args.critical_latency};0; 'Jitter_ms'={speedtest_results['Jitter_ms']['value']}ms;;;0;" + + exit_code = nagios.OK + + if speedtest_results['90th_percentile_upload_speed']['value'] <= args.warn_up and exit_code < nagios.WARNING: + exit_code = nagios.WARNING + if speedtest_results['90th_percentile_upload_speed']['value'] <= args.critical_up and exit_code < nagios.CRITICAL: + exit_code = nagios.CRITICAL + + if speedtest_results['90th_percentile_download_speed']['value'] <= args.warn_down and exit_code < nagios.WARNING: + exit_code = nagios.WARNING + if speedtest_results['90th_percentile_download_speed']['value'] <= args.critical_down and exit_code < nagios.CRITICAL: + exit_code = nagios.CRITICAL + + if speedtest_results['latency_ms']['value'] <= args.warn_latency and exit_code < nagios.WARNING: + exit_code = nagios.WARNING + if speedtest_results['latency_ms']['value'] <= args.warn_latency and exit_code < nagios.CRITICAL: + exit_code = nagios.CRITICAL + + if exit_code == nagios.OK: + status_str = 'OK' + elif exit_code == nagios.WARNING: + status_str = 'WARN' + elif exit_code == nagios.CRITICAL: + status_str = 'CRITICAL' + else: + status_str = 'UNKNOWN' + + print(f'{status_str} - {out_str} |{perf_data}') + sys.exit(exit_code) + + +if __name__ == "__main__": + try: + main() + except Exception as e: + print(f'UNKNOWN: exception "{e}"') + print(traceback.format_exc()) + sys.exit(nagios.UNKNOWN) diff --git a/install.sh b/install.sh index 747cf30..88a0c4a 100755 --- a/install.sh +++ b/install.sh @@ -12,8 +12,9 @@ ln_existing() { } apt update -apt install -y python3-venv git sysstat bc +apt install -y python3-venv git sysstat bc smartmontools curl recode python3-pip pip install -U pip wheel setuptools +pip install psutil check_systemd mkdir -p /usr/lib64/nagios/plugins/ diff --git a/requirements.txt b/requirements.txt index 5ffbed1..c53f027 100644 --- a/requirements.txt +++ b/requirements.txt @@ -13,4 +13,5 @@ aiofiles~=0.6.0 markdown~=3.4.1 psutil~=5.9.4 hurry.filesize -certifi \ No newline at end of file +certifi +cloudflarepycli \ No newline at end of file