diff --git a/README.md b/README.md index 6e93df2..87d61bf 100644 --- a/README.md +++ b/README.md @@ -104,7 +104,12 @@ You can setup env through `.env` file or environment variables with defined as d ## 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 - all to configure supervised interfaces using `--opnsense-interfaces` diff --git a/opnsense_exporter/server.py b/opnsense_exporter/server.py index 08d40cc..d2037a1 100644 --- a/opnsense_exporter/server.py +++ b/opnsense_exporter/server.py @@ -91,6 +91,12 @@ class OPNSensePrometheusExporter: backup_ha_state.labels( instance=self.exporter_instance, **self.backup.labels ).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 if main_state == OPNSenseHAState.ACTIVE: active_opnsense = self.main diff --git a/tests/test_server.py b/tests/test_server.py index f14c941..5e67518 100644 --- a/tests/test_server.py +++ b/tests/test_server.py @@ -125,6 +125,7 @@ def test_process_requests(): main_ha_state_mock = FakePromEnum() backup_ha_state_mock = FakePromEnum() + opnsense_server_ha_state_mock = FakePromEnum() opnsense_active_server_traffic_rate_mock = FakePromGauge() 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 ): with mock.patch( - "opnsense_exporter.server.opnsense_active_server_traffic_rate", - new=opnsense_active_server_traffic_rate_mock, + "opnsense_exporter.server.opnsense_server_ha_state", + new=opnsense_server_ha_state_mock, ): - OPNSensePrometheusExporter( - OPNSenseAPI(OPNSenseRole.MAIN, MAIN_HOST, LOGIN, PASSWORD), - OPNSenseAPI(OPNSenseRole.BACKUP, BACKUP_HOST, LOGIN, PASSWORD), - "wan", - ).process_requests() + with mock.patch( + "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._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.count_state_calls == 1, main_ha_state_mock._state_calls assert main_ha_state_mock._labels == { @@ -197,6 +216,7 @@ def test_process_requests_backup_active(): main_ha_state_mock = FakePromEnum() backup_ha_state_mock = FakePromEnum() + opnsense_server_ha_state_mock = FakePromEnum() opnsense_active_server_traffic_rate_mock = FakePromGauge() 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 ): with mock.patch( - "opnsense_exporter.server.opnsense_active_server_traffic_rate", - new=opnsense_active_server_traffic_rate_mock, + "opnsense_exporter.server.opnsense_server_ha_state", + new=opnsense_server_ha_state_mock, ): - OPNSensePrometheusExporter( - OPNSenseAPI(OPNSenseRole.MAIN, MAIN_HOST, LOGIN, PASSWORD), - OPNSenseAPI(OPNSenseRole.BACKUP, BACKUP_HOST, LOGIN, PASSWORD), - "wan", - ).process_requests() + with mock.patch( + "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._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.count_state_calls == 1 assert main_ha_state_mock._labels == { @@ -269,6 +308,7 @@ def test_process_no_active(): main_ha_state_mock = FakePromEnum() backup_ha_state_mock = FakePromEnum() + opnsense_server_ha_state_mock = FakePromEnum() opnsense_active_server_traffic_rate_mock = FakePromGauge() 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 ): with mock.patch( - "opnsense_exporter.server.opnsense_active_server_traffic_rate", - new=opnsense_active_server_traffic_rate_mock, + "opnsense_exporter.server.opnsense_server_ha_state", + new=opnsense_server_ha_state_mock, ): - OPNSensePrometheusExporter( - OPNSenseAPI(OPNSenseRole.MAIN, MAIN_HOST, LOGIN, PASSWORD), - OPNSenseAPI(OPNSenseRole.BACKUP, BACKUP_HOST, LOGIN, PASSWORD), - "wan", - ).process_requests() + with mock.patch( + "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._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.count_state_calls == 1 assert main_ha_state_mock._labels == { @@ -325,6 +386,7 @@ def test_process_with_falsy_value(): main_ha_state_mock = FakePromEnum() backup_ha_state_mock = FakePromEnum() + opnsense_server_ha_state_mock = FakePromEnum() opnsense_active_server_traffic_rate_mock = FakePromGauge() 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 ): with mock.patch( - "opnsense_exporter.server.opnsense_active_server_traffic_rate", - new=opnsense_active_server_traffic_rate_mock, + "opnsense_exporter.server.opnsense_server_ha_state", + new=opnsense_server_ha_state_mock, ): - OPNSensePrometheusExporter( - OPNSenseAPI(OPNSenseRole.MAIN, MAIN_HOST, LOGIN, PASSWORD), - OPNSenseAPI(OPNSenseRole.BACKUP, BACKUP_HOST, LOGIN, PASSWORD), - "wan", - ).process_requests() + with mock.patch( + "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._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.count_state_calls == 1 assert main_ha_state_mock._labels == {