From 63c7c505f2c199a895a634fc98da8f81773c2e2b Mon Sep 17 00:00:00 2001 From: Cyberes Date: Sun, 19 Nov 2023 16:38:20 -0700 Subject: [PATCH] finish up opn watcher --- opnsense_traffic_watcher.py | 8 ++++++-- requirements.txt | 3 +++ services/opnsense-watcher-daemon.service | 15 +++++++++++++++ services/opnsense-watcher.service | 13 +++++++++++++ 4 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 services/opnsense-watcher-daemon.service create mode 100644 services/opnsense-watcher.service diff --git a/opnsense_traffic_watcher.py b/opnsense_traffic_watcher.py index 942950c..6b21e5f 100644 --- a/opnsense_traffic_watcher.py +++ b/opnsense_traffic_watcher.py @@ -1,6 +1,7 @@ import argparse import json import os +import sys import threading import time from typing import List @@ -15,14 +16,17 @@ from checker.units import filesize requests.packages.urllib3.disable_warnings(category=InsecureRequestWarning) -# pip install redis flask flask-caching - MAX_POINTS_PER_IP = 1900 OPNSENSE_URL = os.environ.get('OPN_URL') OPNSENSE_KEY = os.environ.get('OPN_KEY') OPNSENSE_SECRET = os.environ.get('OPN_SECRET') +if not OPNSENSE_URL or not OPNSENSE_KEY or not OPNSENSE_URL: + print('Missing environment variables') + sys.exit(1) +OPNSENSE_URL = OPNSENSE_URL.strip('/') + class TrafficEntry: def __init__(self, interface: str, address: str, rate_bits_in: int, rate_bits_out: int, rate_bits: int, cumulative_bytes_in: int, cumulative_bytes_out: int, cumulative_bytes: int, connections: dict, timestamp: float): diff --git a/requirements.txt b/requirements.txt index 20b8d09..ebb4747 100644 --- a/requirements.txt +++ b/requirements.txt @@ -19,3 +19,6 @@ dnspython~=2.3.0 cf-speedtest pytz~=2023.3 proxmoxer==2.0.1 +gunicorn +redis +flask-caching \ No newline at end of file diff --git a/services/opnsense-watcher-daemon.service b/services/opnsense-watcher-daemon.service new file mode 100644 index 0000000..ec85388 --- /dev/null +++ b/services/opnsense-watcher-daemon.service @@ -0,0 +1,15 @@ +[Unit] +Description=OPNsense watcher daemon +After=network.target + +[Service] +User=flask +Environment="OPN_URL=https://10.0.0.1 +Environment="OPN_KEY=[your OPNsense key]" +Environment="OPN_SECRET=[your OPNsense secret]" +ExecStart=/opt/venvs/icinga2/icinga2_checks/bin/python3 /opt/venvs/icinga2/icinga2_checks/opnsense_traffic_watcher.py --daemon +Restart=always + +[Install] +WantedBy=multi-user.target + diff --git a/services/opnsense-watcher.service b/services/opnsense-watcher.service new file mode 100644 index 0000000..ed0ec8e --- /dev/null +++ b/services/opnsense-watcher.service @@ -0,0 +1,13 @@ +[Unit] +Description=OPNsense watcher server +After=network.target + +[Service] +User=flask +WorkingDirectory=/opt/icinga2-checks +ExecStart=/opt/venvs/icinga2/icinga2_checks/bin/gunicorn -b 0.0.0.0:8082 -w 4 opnsense_traffic_watcher:app +Restart=always + +[Install] +WantedBy=multi-user.target +