remove main_ha_state and backup_ha_state

those metrics have been replaced by opnsense_server_ha_state in v0.5.1
This commit is contained in:
Pierre Verkest 2023-09-04 00:32:02 +02:00
parent 57d2ff9485
commit 4fbd6c94c7
3 changed files with 55 additions and 165 deletions

View file

@ -104,12 +104,18 @@ You can setup env through `.env` file or environment variables with defined as d
## Changelog ## Changelog
### Version 0.5.1 (2023-09-01) ### Version 1.0.0 (UNRELEASED)
- remove `opnsense_main_ha_state` and `opnsense_backup_ha_state`
metrics marked as deprecated on version 0.5.0 and replace
by `opnsense_server_ha_state` and `role` label
### Version 0.5.1 (2023-09-04)
- FIX `opnsense_server_ha_state` calls were not - FIX `opnsense_server_ha_state` calls were not
implemented implemented
### Version 0.5.0 (2023-09-01) ### Version 0.5.0 (2023-09-04)
- 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

@ -15,33 +15,6 @@ load_dotenv()
HA_STATES = [enum.value for enum in list(OPNSenseHAState)] HA_STATES = [enum.value for enum in list(OPNSenseHAState)]
class DeprecatedPromEnum(Enum, DeprecationWarning):
def state(self, *args, **kwargs):
super().state(*args, **kwargs)
logger.warning("This metric %s will be removed in v1.0.0", self._name)
main_ha_state = DeprecatedPromEnum(
"opnsense_main_ha_state",
"OPNSense HA state of the MAIN server",
[
"instance",
"host",
"role",
],
states=HA_STATES,
)
backup_ha_state = DeprecatedPromEnum(
"opnsense_backup_ha_state",
"OPNSense HA state of the BACKUP server",
[
"instance",
"host",
"role",
],
states=HA_STATES,
)
opnsense_server_ha_state = Enum( opnsense_server_ha_state = Enum(
"opnsense_server_ha_state", "opnsense_server_ha_state",
"OPNSense server HA state", "OPNSense server HA state",
@ -85,12 +58,6 @@ class OPNSensePrometheusExporter:
"""A dummy function that takes some time.""" """A dummy function that takes some time."""
main_state = self.main.get_interface_vip_status() main_state = self.main.get_interface_vip_status()
backup_sate = self.backup.get_interface_vip_status() backup_sate = self.backup.get_interface_vip_status()
main_ha_state.labels(instance=self.exporter_instance, **self.main.labels).state(
main_state.value
)
backup_ha_state.labels(
instance=self.exporter_instance, **self.backup.labels
).state(backup_sate.value)
opnsense_server_ha_state.labels( opnsense_server_ha_state.labels(
instance=self.exporter_instance, **self.main.labels instance=self.exporter_instance, **self.main.labels
).state(main_state.value) ).state(main_state.value)
@ -111,9 +78,10 @@ class OPNSensePrometheusExporter:
**traffic.labels **traffic.labels
).set(traffic.value) ).set(traffic.value)
def start_server(self): def start_server(self, port=8000):
# Start up the server to expose the metrics. # Start up the server to expose the metrics.
start_http_server(8000) start_http_server(port)
logger.info("listen port %s", port)
# Generate some requests. # Generate some requests.
while True: while True:
self.process_requests() self.process_requests()

View file

@ -123,28 +123,22 @@ def test_process_requests():
body=generate_diagnostics_traffic_interface_paylaod(), body=generate_diagnostics_traffic_interface_paylaod(),
) )
main_ha_state_mock = FakePromEnum()
backup_ha_state_mock = FakePromEnum()
opnsense_server_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.opnsense_server_ha_state",
new=opnsense_server_ha_state_mock,
):
with mock.patch( with mock.patch(
"opnsense_exporter.server.backup_ha_state", new=backup_ha_state_mock "opnsense_exporter.server.opnsense_active_server_traffic_rate",
new=opnsense_active_server_traffic_rate_mock,
): ):
with mock.patch( OPNSensePrometheusExporter(
"opnsense_exporter.server.opnsense_server_ha_state", OPNSenseAPI(OPNSenseRole.MAIN, MAIN_HOST, LOGIN, PASSWORD),
new=opnsense_server_ha_state_mock, OPNSenseAPI(OPNSenseRole.BACKUP, BACKUP_HOST, LOGIN, PASSWORD),
): "wan",
with mock.patch( ).process_requests()
"opnsense_exporter.server.opnsense_active_server_traffic_rate",
new=opnsense_active_server_traffic_rate_mock,
):
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.count_state_calls == 2
assert opnsense_server_ha_state_mock._labels_calls == [ assert opnsense_server_ha_state_mock._labels_calls == [
@ -160,21 +154,6 @@ def test_process_requests():
}, },
] ]
assert opnsense_server_ha_state_mock._state_calls == ["active", "hot_standby"] assert opnsense_server_ha_state_mock._state_calls == ["active", "hot_standby"]
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._labels == {
"instance": "",
"host": MAIN_HOST,
"role": "main",
}
assert backup_ha_state_mock._state == "hot_standby"
assert backup_ha_state_mock.count_state_calls == 1
assert backup_ha_state_mock._labels == {
"instance": "",
"host": BACKUP_HOST,
"role": "backup",
}
assert opnsense_active_server_traffic_rate_mock.count_set_calls == 2 assert opnsense_active_server_traffic_rate_mock.count_set_calls == 2
assert opnsense_active_server_traffic_rate_mock._labels_calls == [ assert opnsense_active_server_traffic_rate_mock._labels_calls == [
@ -214,28 +193,22 @@ def test_process_requests_backup_active():
body=generate_diagnostics_traffic_interface_paylaod(), body=generate_diagnostics_traffic_interface_paylaod(),
) )
main_ha_state_mock = FakePromEnum()
backup_ha_state_mock = FakePromEnum()
opnsense_server_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.opnsense_server_ha_state",
new=opnsense_server_ha_state_mock,
):
with mock.patch( with mock.patch(
"opnsense_exporter.server.backup_ha_state", new=backup_ha_state_mock "opnsense_exporter.server.opnsense_active_server_traffic_rate",
new=opnsense_active_server_traffic_rate_mock,
): ):
with mock.patch( OPNSensePrometheusExporter(
"opnsense_exporter.server.opnsense_server_ha_state", OPNSenseAPI(OPNSenseRole.MAIN, MAIN_HOST, LOGIN, PASSWORD),
new=opnsense_server_ha_state_mock, OPNSenseAPI(OPNSenseRole.BACKUP, BACKUP_HOST, LOGIN, PASSWORD),
): "wan",
with mock.patch( ).process_requests()
"opnsense_exporter.server.opnsense_active_server_traffic_rate",
new=opnsense_active_server_traffic_rate_mock,
):
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.count_state_calls == 2
assert opnsense_server_ha_state_mock._labels_calls == [ assert opnsense_server_ha_state_mock._labels_calls == [
@ -251,21 +224,6 @@ def test_process_requests_backup_active():
}, },
] ]
assert opnsense_server_ha_state_mock._state_calls == ["maintenancemode", "active"] assert opnsense_server_ha_state_mock._state_calls == ["maintenancemode", "active"]
assert main_ha_state_mock._state == "maintenancemode"
assert main_ha_state_mock.count_state_calls == 1
assert main_ha_state_mock._labels == {
"instance": "",
"host": MAIN_HOST,
"role": "main",
}
assert backup_ha_state_mock._state == "active"
assert backup_ha_state_mock.count_state_calls == 1
assert backup_ha_state_mock._labels == {
"instance": "",
"host": BACKUP_HOST,
"role": "backup",
}
assert opnsense_active_server_traffic_rate_mock.count_set_calls == 2 assert opnsense_active_server_traffic_rate_mock.count_set_calls == 2
opnsense_active_server_traffic_rate_mock._labels_calls == [ opnsense_active_server_traffic_rate_mock._labels_calls == [
@ -306,28 +264,22 @@ def test_process_no_active():
body=generate_diagnostics_traffic_interface_paylaod(), body=generate_diagnostics_traffic_interface_paylaod(),
) )
main_ha_state_mock = FakePromEnum()
backup_ha_state_mock = FakePromEnum()
opnsense_server_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.opnsense_server_ha_state",
new=opnsense_server_ha_state_mock,
):
with mock.patch( with mock.patch(
"opnsense_exporter.server.backup_ha_state", new=backup_ha_state_mock "opnsense_exporter.server.opnsense_active_server_traffic_rate",
new=opnsense_active_server_traffic_rate_mock,
): ):
with mock.patch( OPNSensePrometheusExporter(
"opnsense_exporter.server.opnsense_server_ha_state", OPNSenseAPI(OPNSenseRole.MAIN, MAIN_HOST, LOGIN, PASSWORD),
new=opnsense_server_ha_state_mock, OPNSenseAPI(OPNSenseRole.BACKUP, BACKUP_HOST, LOGIN, PASSWORD),
): "wan",
with mock.patch( ).process_requests()
"opnsense_exporter.server.opnsense_active_server_traffic_rate",
new=opnsense_active_server_traffic_rate_mock,
):
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.count_state_calls == 2
assert opnsense_server_ha_state_mock._labels_calls == [ assert opnsense_server_ha_state_mock._labels_calls == [
@ -346,21 +298,6 @@ def test_process_no_active():
"maintenancemode", "maintenancemode",
"unavailable", "unavailable",
] ]
assert main_ha_state_mock._state == "maintenancemode"
assert main_ha_state_mock.count_state_calls == 1
assert main_ha_state_mock._labels == {
"instance": "",
"host": MAIN_HOST,
"role": "main",
}
assert backup_ha_state_mock._state == "unavailable"
assert backup_ha_state_mock.count_state_calls == 1
assert backup_ha_state_mock._labels == {
"instance": "",
"host": BACKUP_HOST,
"role": "backup",
}
assert opnsense_active_server_traffic_rate_mock.count_set_calls == 0 assert opnsense_active_server_traffic_rate_mock.count_set_calls == 0
@ -384,28 +321,22 @@ def test_process_with_falsy_value():
status=404, status=404,
) )
main_ha_state_mock = FakePromEnum()
backup_ha_state_mock = FakePromEnum()
opnsense_server_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.opnsense_server_ha_state",
new=opnsense_server_ha_state_mock,
):
with mock.patch( with mock.patch(
"opnsense_exporter.server.backup_ha_state", new=backup_ha_state_mock "opnsense_exporter.server.opnsense_active_server_traffic_rate",
new=opnsense_active_server_traffic_rate_mock,
): ):
with mock.patch( OPNSensePrometheusExporter(
"opnsense_exporter.server.opnsense_server_ha_state", OPNSenseAPI(OPNSenseRole.MAIN, MAIN_HOST, LOGIN, PASSWORD),
new=opnsense_server_ha_state_mock, OPNSenseAPI(OPNSenseRole.BACKUP, BACKUP_HOST, LOGIN, PASSWORD),
): "wan",
with mock.patch( ).process_requests()
"opnsense_exporter.server.opnsense_active_server_traffic_rate",
new=opnsense_active_server_traffic_rate_mock,
):
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.count_state_calls == 2
assert opnsense_server_ha_state_mock._labels_calls == [ assert opnsense_server_ha_state_mock._labels_calls == [
@ -421,20 +352,5 @@ def test_process_with_falsy_value():
}, },
] ]
assert opnsense_server_ha_state_mock._state_calls == ["active", "hot_standby"] assert opnsense_server_ha_state_mock._state_calls == ["active", "hot_standby"]
assert main_ha_state_mock._state == "active"
assert main_ha_state_mock.count_state_calls == 1
assert main_ha_state_mock._labels == {
"instance": "",
"host": MAIN_HOST,
"role": "main",
}
assert backup_ha_state_mock.count_state_calls == 1
assert backup_ha_state_mock._state == "hot_standby"
assert backup_ha_state_mock._labels == {
"instance": "",
"host": BACKUP_HOST,
"role": "backup",
}
assert opnsense_active_server_traffic_rate_mock.count_set_calls == 0 assert opnsense_active_server_traffic_rate_mock.count_set_calls == 0