diff --git a/swh/icinga_plugins/base_check.py b/swh/icinga_plugins/base_check.py --- a/swh/icinga_plugins/base_check.py +++ b/swh/icinga_plugins/base_check.py @@ -18,3 +18,8 @@ return (1, 'WARNING') else: return (0, 'OK') + + def print_result(self, status_type, status_string, **metrics): + print(f'{self.TYPE} {status_type} - {status_string}') + for (metric_name, metric_value) in sorted(metrics.items()): + print(f"| '{metric_name}' = {metric_value:.2f}s") diff --git a/swh/icinga_plugins/tests/test_vault.py b/swh/icinga_plugins/tests/test_vault.py --- a/swh/icinga_plugins/tests/test_vault.py +++ b/swh/icinga_plugins/tests/test_vault.py @@ -79,7 +79,7 @@ assert re.match( rf'^VAULT OK - cooking directory {dir_id} took ' r'[0-9]\.[0-9]{2}s and succeeded.\n' - r"\| 'total time' = [0-9]\.[0-9]{2}s$", + r"\| 'total_time' = [0-9]\.[0-9]{2}s$", result.output) assert result.exit_code == 0, result.output @@ -112,7 +112,7 @@ assert re.match( rf'^VAULT OK - cooking directory {dir_id} took ' r'[0-9]\.[0-9]{2}s and succeeded.\n' - r"\| 'total time' = [0-9]\.[0-9]{2}s$", + r"\| 'total_time' = [0-9]\.[0-9]{2}s$", result.output) assert result.exit_code == 0, result.output @@ -144,7 +144,7 @@ assert re.match( rf'^VAULT CRITICAL - cooking directory {dir_id} took ' r'[0-9]\.[0-9]{2}s and failed with: foobar\n' - r"\| 'total time' = [0-9]\.[0-9]{2}s\n$", + r"\| 'total_time' = [0-9]\.[0-9]{2}s\n$", result.output) assert result.exit_code == 2, result.output @@ -188,7 +188,7 @@ assert re.match( rf'^VAULT CRITICAL - cooking directory {dir_id} took more than ' r'[0-9]+\.[0-9]{2}s and has status: foo\n' - r"\| 'total time' = [0-9]{4}\.[0-9]{2}s\n$", + r"\| 'total_time' = [0-9]{4}\.[0-9]{2}s\n$", result.output) assert result.exit_code == 2, result.output diff --git a/swh/icinga_plugins/vault.py b/swh/icinga_plugins/vault.py --- a/swh/icinga_plugins/vault.py +++ b/swh/icinga_plugins/vault.py @@ -17,6 +17,7 @@ class VaultCheck(BaseCheck): + TYPE = 'VAULT' DEFAULT_WARNING_THRESHOLD = 0 DEFAULT_CRITICAL_THRESHOLD = 3600 @@ -46,7 +47,9 @@ try: dir_id = self._pick_uncached_directory() except NoDirectory: - print('VAULT CRITICAL - No directory exists in the archive') + self.print_result( + 'CRITICAL', + 'No directory exists in the archive') return 2 start_time = time.time() @@ -63,27 +66,33 @@ total_time = time.time() - start_time if total_time > self.critical_threshold: - print(f'VAULT CRITICAL - cooking directory {dir_id.hex()} ' - f'took more than {total_time:.2f}s and has status: ' - f'{result["progress_message"]}') - print(f"| 'total time' = {total_time:.2f}s") + self.print_result( + 'CRITICAL', + f'cooking directory {dir_id.hex()} took more than ' + f'{total_time:.2f}s and has status: ' + f'{result["progress_message"]}', + total_time=total_time) return 2 if result['status'] == 'done': (status_code, status) = self.get_status(total_time) - print(f'VAULT {status} - cooking directory {dir_id.hex()} ' - f'took {total_time:.2f}s and succeeded.') - print(f"| 'total time' = {total_time:.2f}s") + self.print_result( + status, + f'cooking directory {dir_id.hex()} took {total_time:.2f}s ' + f'and succeeded.', + total_time=total_time) return status_code elif result['status'] == 'failed': - print(f'VAULT CRITICAL - cooking directory {dir_id.hex()} ' - f'took {total_time:.2f}s and failed with: ' - f'{result["progress_message"]}') - print(f"| 'total time' = {total_time:.2f}s") + self.print_result( + 'CRITICAL', + f'cooking directory {dir_id.hex()} took {total_time:.2f}s ' + f'and failed with: {result["progress_message"]}', + total_time=total_time) return 2 else: - print(f'VAULT CRITICAL - cooking directory {dir_id.hex()} ' - f'took {total_time:.2f}s and resulted in unknown: ' - f'status: {result["status"]}') - print(f"| 'total time' = {total_time:.2f}s") + self.print_result( + 'CRITICAL', + f'cooking directory {dir_id.hex()} took {total_time:.2f}s ' + f'and resulted in unknown status: {result["status"]}', + total_time=total_time) return 2