opnsense-prom-exporter/opnsense_exporter/server.py

160 lines
4.7 KiB
Python
Raw Normal View History

2023-09-01 13:27:55 +02:00
import argparse
import os
import socket
import time
from dotenv import load_dotenv
from prometheus_client import Enum, Gauge, start_http_server
from opnsense_exporter.opnsense_api import OPNSenseAPI, OPNSenseRole
2023-09-01 13:27:55 +02:00
load_dotenv()
HA_STATES = ["active", "hot_standby", "unavailable", "maintenancemode"]
main_ha_state = Enum(
2023-09-01 16:23:20 +02:00
"opnsense_main_ha_state",
"OPNSense HA state of the MAIN server",
[
"instance",
"host",
"role",
2023-09-01 16:23:20 +02:00
],
states=HA_STATES,
2023-09-01 13:27:55 +02:00
)
backup_ha_state = Enum(
"opnsense_backup_ha_state",
"OPNSense HA state of the BACKUP server",
2023-09-01 16:23:20 +02:00
[
"instance",
"host",
"role",
2023-09-01 16:23:20 +02:00
],
2023-09-01 13:27:55 +02:00
states=HA_STATES,
)
active_server_bytes_received = Gauge(
"opnsense_active_server_bytes_received",
"Active OPNSense server bytes received on WAN interface",
2023-09-01 16:23:20 +02:00
[
"instance",
"host",
"role",
2023-09-01 16:23:20 +02:00
],
2023-09-01 13:27:55 +02:00
)
active_server_bytes_transmitted = Gauge(
"opnsense_active_server_bytes_transmitted",
"Active OPNSense server bytes transmitted on WAN interface",
2023-09-01 16:23:20 +02:00
[
"instance",
"host",
"role",
2023-09-01 16:23:20 +02:00
],
2023-09-01 13:27:55 +02:00
)
2023-09-01 16:23:20 +02:00
def process_requests(main, backup, exporter_instance: str = ""):
2023-09-01 13:27:55 +02:00
"""A dummy function that takes some time."""
main_state = main.get_interface_vip_status()
backup_sate = backup.get_interface_vip_status()
main_ha_state.labels(instance=exporter_instance, **main.labels).state(main_state)
backup_ha_state.labels(instance=exporter_instance, **backup.labels).state(
backup_sate
)
2023-09-01 16:23:20 +02:00
active_opnsense = None
2023-09-01 13:27:55 +02:00
if main_state == "active":
2023-09-01 16:23:20 +02:00
active_opnsense = main
2023-09-01 13:27:55 +02:00
if backup_sate == "active":
2023-09-01 16:23:20 +02:00
active_opnsense = backup
if active_opnsense:
bytes_received, bytes_transmitted = active_opnsense.get_wan_trafic()
if bytes_received or bytes_received == 0:
active_server_bytes_received.labels(
instance=exporter_instance, **active_opnsense.labels
).set(bytes_received)
2023-09-01 16:23:20 +02:00
if bytes_transmitted or bytes_transmitted == 0:
active_server_bytes_transmitted.labels(
instance=exporter_instance, **active_opnsense.labels
).set(bytes_transmitted)
2023-09-01 13:27:55 +02:00
2023-09-01 16:23:20 +02:00
def start_server(
main: OPNSenseAPI,
backup: OPNSenseAPI,
check_frequency: int = 1,
exporter_instance: str = "",
):
2023-09-01 13:27:55 +02:00
# Start up the server to expose the metrics.
start_http_server(8000)
# Generate some requests.
while True:
2023-09-01 16:23:20 +02:00
process_requests(main, backup, exporter_instance=exporter_instance)
2023-09-01 13:27:55 +02:00
time.sleep(check_frequency)
def run():
parser = argparse.ArgumentParser(
description="OPNSense prometheus exporter",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument(
"--check-frequency-seconds",
"-c",
type=int,
dest="frequency",
default=int(os.environ.get("CHECK_FREQUENCY_SECONDS", 2)),
help="How often (in seconds) this server requests OPNSense servers",
)
parser.add_argument(
"--main-host",
"-m",
type=str,
dest="main",
default=os.environ.get("OPNSENSE_MAIN_HOST", None),
help="MAIN OPNsense server that should be in `active` state in normal configuration.",
)
parser.add_argument(
"--backup-host",
"-b",
type=str,
dest="backup",
default=os.environ.get("OPNSENSE_BACKUP_HOST", None),
help="BACKUP OPNsense server that should be `hot_standby` state in normal configuration.",
)
parser.add_argument(
"--opnsense-user",
"-u",
type=str,
dest="user",
default=os.environ.get("OPNSENSE_USERNAME", None),
help="OPNsense user. Expect to be the same on MAIN and BACKUP servers",
)
parser.add_argument(
"--opnsense-password",
"-p",
type=str,
dest="password",
default=os.environ.get("OPNSENSE_PASSWORD", None),
help="OPNsense password. Expect to be the same on MAIN and BACKUP servers",
)
parser.add_argument(
"--prometheus-instance",
dest="prom_instance",
type=str,
default=socket.gethostname(),
help=(
2023-09-01 16:23:20 +02:00
"Exporter Instance name, default value computed with hostname "
2023-09-01 13:27:55 +02:00
"where the server is running. Use to set the instance label."
),
)
arguments = parser.parse_args()
start_server(
OPNSenseAPI(
OPNSenseRole.MAIN, arguments.main, arguments.user, arguments.password
),
OPNSenseAPI(
OPNSenseRole.BACKUP, arguments.backup, arguments.user, arguments.password
),
2023-09-01 13:27:55 +02:00
check_frequency=arguments.frequency,
2023-09-01 16:23:20 +02:00
exporter_instance=arguments.prom_instance,
2023-09-01 13:27:55 +02:00
)