FIX opnsense_server_ha_state metric

I've forgot to implement the main part calling state() on metric
This commit is contained in:
Pierre Verkest 2023-09-04 00:21:56 +02:00
parent 98f832ec3b
commit bea721a275
3 changed files with 121 additions and 29 deletions

View file

@ -104,7 +104,12 @@ You can setup env through `.env` file or environment variables with defined as d
## Changelog ## Changelog
### Version 0.5.0 (UNRELEASED) ### Version 0.5.1 (2023-09-01)
- FIX `opnsense_server_ha_state` calls were not
implemented
### Version 0.5.0 (2023-09-01)
- add role label in metrics - add role label in metrics
- all to configure supervised interfaces using `--opnsense-interfaces` - all to configure supervised interfaces using `--opnsense-interfaces`

View file

@ -91,6 +91,12 @@ class OPNSensePrometheusExporter:
backup_ha_state.labels( backup_ha_state.labels(
instance=self.exporter_instance, **self.backup.labels instance=self.exporter_instance, **self.backup.labels
).state(backup_sate.value) ).state(backup_sate.value)
opnsense_server_ha_state.labels(
instance=self.exporter_instance, **self.main.labels
).state(main_state.value)
opnsense_server_ha_state.labels(
instance=self.exporter_instance, **self.backup.labels
).state(backup_sate.value)
active_opnsense = None active_opnsense = None
if main_state == OPNSenseHAState.ACTIVE: if main_state == OPNSenseHAState.ACTIVE:
active_opnsense = self.main active_opnsense = self.main

View file

@ -125,11 +125,16 @@ def test_process_requests():
main_ha_state_mock = FakePromEnum() main_ha_state_mock = FakePromEnum()
backup_ha_state_mock = FakePromEnum() backup_ha_state_mock = FakePromEnum()
opnsense_server_ha_state_mock = FakePromEnum()
opnsense_active_server_traffic_rate_mock = FakePromGauge() opnsense_active_server_traffic_rate_mock = FakePromGauge()
with mock.patch("opnsense_exporter.server.main_ha_state", new=main_ha_state_mock): with mock.patch("opnsense_exporter.server.main_ha_state", new=main_ha_state_mock):
with mock.patch( with mock.patch(
"opnsense_exporter.server.backup_ha_state", new=backup_ha_state_mock "opnsense_exporter.server.backup_ha_state", new=backup_ha_state_mock
):
with mock.patch(
"opnsense_exporter.server.opnsense_server_ha_state",
new=opnsense_server_ha_state_mock,
): ):
with mock.patch( with mock.patch(
"opnsense_exporter.server.opnsense_active_server_traffic_rate", "opnsense_exporter.server.opnsense_active_server_traffic_rate",
@ -141,6 +146,20 @@ def test_process_requests():
"wan", "wan",
).process_requests() ).process_requests()
assert opnsense_server_ha_state_mock.count_state_calls == 2
assert opnsense_server_ha_state_mock._labels_calls == [
{
"instance": "",
"host": MAIN_HOST,
"role": "main",
},
{
"instance": "",
"host": BACKUP_HOST,
"role": "backup",
},
]
assert opnsense_server_ha_state_mock._state_calls == ["active", "hot_standby"]
assert main_ha_state_mock._state == "active" assert main_ha_state_mock._state == "active"
assert main_ha_state_mock.count_state_calls == 1, main_ha_state_mock._state_calls assert main_ha_state_mock.count_state_calls == 1, main_ha_state_mock._state_calls
assert main_ha_state_mock._labels == { assert main_ha_state_mock._labels == {
@ -197,11 +216,16 @@ def test_process_requests_backup_active():
main_ha_state_mock = FakePromEnum() main_ha_state_mock = FakePromEnum()
backup_ha_state_mock = FakePromEnum() backup_ha_state_mock = FakePromEnum()
opnsense_server_ha_state_mock = FakePromEnum()
opnsense_active_server_traffic_rate_mock = FakePromGauge() opnsense_active_server_traffic_rate_mock = FakePromGauge()
with mock.patch("opnsense_exporter.server.main_ha_state", new=main_ha_state_mock): with mock.patch("opnsense_exporter.server.main_ha_state", new=main_ha_state_mock):
with mock.patch( with mock.patch(
"opnsense_exporter.server.backup_ha_state", new=backup_ha_state_mock "opnsense_exporter.server.backup_ha_state", new=backup_ha_state_mock
):
with mock.patch(
"opnsense_exporter.server.opnsense_server_ha_state",
new=opnsense_server_ha_state_mock,
): ):
with mock.patch( with mock.patch(
"opnsense_exporter.server.opnsense_active_server_traffic_rate", "opnsense_exporter.server.opnsense_active_server_traffic_rate",
@ -212,6 +236,21 @@ def test_process_requests_backup_active():
OPNSenseAPI(OPNSenseRole.BACKUP, BACKUP_HOST, LOGIN, PASSWORD), OPNSenseAPI(OPNSenseRole.BACKUP, BACKUP_HOST, LOGIN, PASSWORD),
"wan", "wan",
).process_requests() ).process_requests()
assert opnsense_server_ha_state_mock.count_state_calls == 2
assert opnsense_server_ha_state_mock._labels_calls == [
{
"instance": "",
"host": MAIN_HOST,
"role": "main",
},
{
"instance": "",
"host": BACKUP_HOST,
"role": "backup",
},
]
assert opnsense_server_ha_state_mock._state_calls == ["maintenancemode", "active"]
assert main_ha_state_mock._state == "maintenancemode" assert main_ha_state_mock._state == "maintenancemode"
assert main_ha_state_mock.count_state_calls == 1 assert main_ha_state_mock.count_state_calls == 1
assert main_ha_state_mock._labels == { assert main_ha_state_mock._labels == {
@ -269,11 +308,16 @@ def test_process_no_active():
main_ha_state_mock = FakePromEnum() main_ha_state_mock = FakePromEnum()
backup_ha_state_mock = FakePromEnum() backup_ha_state_mock = FakePromEnum()
opnsense_server_ha_state_mock = FakePromEnum()
opnsense_active_server_traffic_rate_mock = FakePromGauge() opnsense_active_server_traffic_rate_mock = FakePromGauge()
with mock.patch("opnsense_exporter.server.main_ha_state", new=main_ha_state_mock): with mock.patch("opnsense_exporter.server.main_ha_state", new=main_ha_state_mock):
with mock.patch( with mock.patch(
"opnsense_exporter.server.backup_ha_state", new=backup_ha_state_mock "opnsense_exporter.server.backup_ha_state", new=backup_ha_state_mock
):
with mock.patch(
"opnsense_exporter.server.opnsense_server_ha_state",
new=opnsense_server_ha_state_mock,
): ):
with mock.patch( with mock.patch(
"opnsense_exporter.server.opnsense_active_server_traffic_rate", "opnsense_exporter.server.opnsense_active_server_traffic_rate",
@ -285,6 +329,23 @@ def test_process_no_active():
"wan", "wan",
).process_requests() ).process_requests()
assert opnsense_server_ha_state_mock.count_state_calls == 2
assert opnsense_server_ha_state_mock._labels_calls == [
{
"instance": "",
"host": MAIN_HOST,
"role": "main",
},
{
"instance": "",
"host": BACKUP_HOST,
"role": "backup",
},
]
assert opnsense_server_ha_state_mock._state_calls == [
"maintenancemode",
"unavailable",
]
assert main_ha_state_mock._state == "maintenancemode" assert main_ha_state_mock._state == "maintenancemode"
assert main_ha_state_mock.count_state_calls == 1 assert main_ha_state_mock.count_state_calls == 1
assert main_ha_state_mock._labels == { assert main_ha_state_mock._labels == {
@ -325,11 +386,16 @@ def test_process_with_falsy_value():
main_ha_state_mock = FakePromEnum() main_ha_state_mock = FakePromEnum()
backup_ha_state_mock = FakePromEnum() backup_ha_state_mock = FakePromEnum()
opnsense_server_ha_state_mock = FakePromEnum()
opnsense_active_server_traffic_rate_mock = FakePromGauge() opnsense_active_server_traffic_rate_mock = FakePromGauge()
with mock.patch("opnsense_exporter.server.main_ha_state", new=main_ha_state_mock): with mock.patch("opnsense_exporter.server.main_ha_state", new=main_ha_state_mock):
with mock.patch( with mock.patch(
"opnsense_exporter.server.backup_ha_state", new=backup_ha_state_mock "opnsense_exporter.server.backup_ha_state", new=backup_ha_state_mock
):
with mock.patch(
"opnsense_exporter.server.opnsense_server_ha_state",
new=opnsense_server_ha_state_mock,
): ):
with mock.patch( with mock.patch(
"opnsense_exporter.server.opnsense_active_server_traffic_rate", "opnsense_exporter.server.opnsense_active_server_traffic_rate",
@ -340,6 +406,21 @@ def test_process_with_falsy_value():
OPNSenseAPI(OPNSenseRole.BACKUP, BACKUP_HOST, LOGIN, PASSWORD), OPNSenseAPI(OPNSenseRole.BACKUP, BACKUP_HOST, LOGIN, PASSWORD),
"wan", "wan",
).process_requests() ).process_requests()
assert opnsense_server_ha_state_mock.count_state_calls == 2
assert opnsense_server_ha_state_mock._labels_calls == [
{
"instance": "",
"host": MAIN_HOST,
"role": "main",
},
{
"instance": "",
"host": BACKUP_HOST,
"role": "backup",
},
]
assert opnsense_server_ha_state_mock._state_calls == ["active", "hot_standby"]
assert main_ha_state_mock._state == "active" assert main_ha_state_mock._state == "active"
assert main_ha_state_mock.count_state_calls == 1 assert main_ha_state_mock.count_state_calls == 1
assert main_ha_state_mock._labels == { assert main_ha_state_mock._labels == {