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,6 +125,7 @@ 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):
@ -132,15 +133,33 @@ def test_process_requests():
"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( with mock.patch(
"opnsense_exporter.server.opnsense_active_server_traffic_rate", "opnsense_exporter.server.opnsense_server_ha_state",
new=opnsense_active_server_traffic_rate_mock, new=opnsense_server_ha_state_mock,
): ):
OPNSensePrometheusExporter( with mock.patch(
OPNSenseAPI(OPNSenseRole.MAIN, MAIN_HOST, LOGIN, PASSWORD), "opnsense_exporter.server.opnsense_active_server_traffic_rate",
OPNSenseAPI(OPNSenseRole.BACKUP, BACKUP_HOST, LOGIN, PASSWORD), new=opnsense_active_server_traffic_rate_mock,
"wan", ):
).process_requests() OPNSensePrometheusExporter(
OPNSenseAPI(OPNSenseRole.MAIN, MAIN_HOST, LOGIN, PASSWORD),
OPNSenseAPI(OPNSenseRole.BACKUP, BACKUP_HOST, LOGIN, PASSWORD),
"wan",
).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,6 +216,7 @@ 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):
@ -204,14 +224,33 @@ def test_process_requests_backup_active():
"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( with mock.patch(
"opnsense_exporter.server.opnsense_active_server_traffic_rate", "opnsense_exporter.server.opnsense_server_ha_state",
new=opnsense_active_server_traffic_rate_mock, new=opnsense_server_ha_state_mock,
): ):
OPNSensePrometheusExporter( with mock.patch(
OPNSenseAPI(OPNSenseRole.MAIN, MAIN_HOST, LOGIN, PASSWORD), "opnsense_exporter.server.opnsense_active_server_traffic_rate",
OPNSenseAPI(OPNSenseRole.BACKUP, BACKUP_HOST, LOGIN, PASSWORD), new=opnsense_active_server_traffic_rate_mock,
"wan", ):
).process_requests() OPNSensePrometheusExporter(
OPNSenseAPI(OPNSenseRole.MAIN, MAIN_HOST, LOGIN, PASSWORD),
OPNSenseAPI(OPNSenseRole.BACKUP, BACKUP_HOST, LOGIN, PASSWORD),
"wan",
).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,6 +308,7 @@ 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):
@ -276,15 +316,36 @@ def test_process_no_active():
"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( with mock.patch(
"opnsense_exporter.server.opnsense_active_server_traffic_rate", "opnsense_exporter.server.opnsense_server_ha_state",
new=opnsense_active_server_traffic_rate_mock, new=opnsense_server_ha_state_mock,
): ):
OPNSensePrometheusExporter( with mock.patch(
OPNSenseAPI(OPNSenseRole.MAIN, MAIN_HOST, LOGIN, PASSWORD), "opnsense_exporter.server.opnsense_active_server_traffic_rate",
OPNSenseAPI(OPNSenseRole.BACKUP, BACKUP_HOST, LOGIN, PASSWORD), new=opnsense_active_server_traffic_rate_mock,
"wan", ):
).process_requests() OPNSensePrometheusExporter(
OPNSenseAPI(OPNSenseRole.MAIN, MAIN_HOST, LOGIN, PASSWORD),
OPNSenseAPI(OPNSenseRole.BACKUP, BACKUP_HOST, LOGIN, PASSWORD),
"wan",
).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,6 +386,7 @@ 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):
@ -332,14 +394,33 @@ def test_process_with_falsy_value():
"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( with mock.patch(
"opnsense_exporter.server.opnsense_active_server_traffic_rate", "opnsense_exporter.server.opnsense_server_ha_state",
new=opnsense_active_server_traffic_rate_mock, new=opnsense_server_ha_state_mock,
): ):
OPNSensePrometheusExporter( with mock.patch(
OPNSenseAPI(OPNSenseRole.MAIN, MAIN_HOST, LOGIN, PASSWORD), "opnsense_exporter.server.opnsense_active_server_traffic_rate",
OPNSenseAPI(OPNSenseRole.BACKUP, BACKUP_HOST, LOGIN, PASSWORD), new=opnsense_active_server_traffic_rate_mock,
"wan", ):
).process_requests() OPNSensePrometheusExporter(
OPNSenseAPI(OPNSenseRole.MAIN, MAIN_HOST, LOGIN, PASSWORD),
OPNSenseAPI(OPNSenseRole.BACKUP, BACKUP_HOST, LOGIN, PASSWORD),
"wan",
).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 == {