From 4d8942d2ff700f1125c7ed422637e4e7b24fcb6c Mon Sep 17 00:00:00 2001 From: Andrew Johnson Date: Sun, 23 May 2021 10:17:57 +0200 Subject: [PATCH 1/2] #26: Add a comment/description field, that could be optionally used as a note for administrators --- infracheck/infracheck/model.py | 10 +++++++--- infracheck/infracheck/runner.py | 10 +++++++--- tests/unit_test_executed_checks_result_list.py | 18 ++++++++++++------ tests/unit_test_model_executed_check_result.py | 8 +++++--- 4 files changed, 31 insertions(+), 15 deletions(-) diff --git a/infracheck/infracheck/model.py b/infracheck/infracheck/model.py index a9f3dd3..569ca8c 100644 --- a/infracheck/infracheck/model.py +++ b/infracheck/infracheck/model.py @@ -12,21 +12,24 @@ class ExecutedCheckResult(object): hooks_output: str configured_name: str refresh_time: datetime + description: str - def __init__(self, configured_name: str, output: str, exit_status: bool, hooks_output: str): + def __init__(self, configured_name: str, output: str, exit_status: bool, hooks_output: str, description: str): self.configured_name = configured_name self.output = output self.exit_status = exit_status self.hooks_output = hooks_output self.refresh_time = datetime.now() + self.description = description @classmethod - def from_not_ready(cls, configured_name: str): + def from_not_ready(cls, configured_name: str, description: str): check = cls( configured_name=configured_name, output='Check not ready', exit_status=False, - hooks_output='' + hooks_output='', + description=description ) check.refresh_time = None @@ -37,6 +40,7 @@ def to_hash(self) -> dict: return { 'status': self.exit_status, 'output': self.output, + 'description': self.description, 'hooks_output': self.hooks_output, 'ident': self.configured_name + '=' + str(self.exit_status), 'checked_at': self.refresh_time.strftime('%Y-%m-%d %H-%M-%S') if self.refresh_time else '' diff --git a/infracheck/infracheck/runner.py b/infracheck/infracheck/runner.py index 0fd54ca..0e11dac 100644 --- a/infracheck/infracheck/runner.py +++ b/infracheck/infracheck/runner.py @@ -99,7 +99,8 @@ def run_single_check(self, configured_name: str, check_name: str, input_data: di output=output.decode('utf-8'), exit_status=exit_status, hooks_output=hooks_out, - configured_name=configured_name + configured_name=configured_name, + description=config.get('description', '') ) def run_checks(self, enabled_configs: list) -> None: @@ -116,7 +117,8 @@ def run_checks(self, enabled_configs: list) -> None: if not result: try: - result = self.run_single_check(config_name, config['type'], config['input'], config.get('hooks', {}), config) + result = self.run_single_check(config_name, config['type'], config['input'], + config.get('hooks', {}), config) except CheckNotReadyShouldBeSkippedSignal: continue @@ -138,9 +140,11 @@ def get_checks_results(self, enabled_configs: list) -> ExecutedChecksResultList: for config_name in enabled_configs: result = self.repository.retrieve_from_cache(config_name) + config = self.config_loader.load(config_name) if not result: - result = ExecutedCheckResult.from_not_ready(configured_name=config_name) + result = ExecutedCheckResult.from_not_ready(configured_name=config_name, + description=config.get('description', '')) results.add(config_name, result) diff --git a/tests/unit_test_executed_checks_result_list.py b/tests/unit_test_executed_checks_result_list.py index ebbc092..9c4e12b 100644 --- a/tests/unit_test_executed_checks_result_list.py +++ b/tests/unit_test_executed_checks_result_list.py @@ -21,13 +21,15 @@ def test_is_global_status_success(self): configured_name='first', output='Test', exit_status=False, - hooks_output='' + hooks_output='', + description='First in test' )) results.add('second', ExecutedCheckResult( configured_name='second', output='Test', exit_status=True, - hooks_output='' + hooks_output='', + description='Second in test' )) self.assertFalse(results.is_global_status_success()) @@ -38,13 +40,15 @@ def test_is_global_status_success(self): configured_name='first', output='Test', exit_status=True, - hooks_output='' + hooks_output='', + description='First in test' )) results.add('second', ExecutedCheckResult( configured_name='second', output='Test', exit_status=True, - hooks_output='' + hooks_output='', + description='Second in test' )) self.assertTrue(results.is_global_status_success()) @@ -55,13 +59,15 @@ def test_is_global_status_success(self): configured_name='first', output='Test', exit_status=False, - hooks_output='' + hooks_output='', + description='First in test' )) results.add('second', ExecutedCheckResult( configured_name='second', output='Test', exit_status=False, - hooks_output='' + hooks_output='', + description='Second in test' )) self.assertFalse(results.is_global_status_success()) diff --git a/tests/unit_test_model_executed_check_result.py b/tests/unit_test_model_executed_check_result.py index b8e2dae..c638e1e 100644 --- a/tests/unit_test_model_executed_check_result.py +++ b/tests/unit_test_model_executed_check_result.py @@ -13,7 +13,7 @@ class ExecutedCheckResultTest(BasicTestingCase): def test_from_not_ready(self): - result = ExecutedCheckResult.from_not_ready('Durruti') + result = ExecutedCheckResult.from_not_ready('Durruti', description='Buenaventura') self.assertEqual(False, result.exit_status) self.assertIsNone(result.refresh_time) @@ -23,7 +23,8 @@ def test_to_hash(self): configured_name='Durruti', output='Viva la revolution!', exit_status=True, - hooks_output='A las barricadas!' + hooks_output='A las barricadas!', + description='For the triumph of the libertarian confederation!' ) check.refresh_time = datetime(2020, 11, 27, 23, 40, 18) # mock the time @@ -34,5 +35,6 @@ def test_to_hash(self): 'hooks_output': 'A las barricadas!', 'ident': 'Durruti=True', 'output': 'Viva la revolution!', - 'status': True + 'status': True, + 'description': 'For the triumph of the libertarian confederation!' }, as_hash) From 7962b5f25c83e14a9d832b3cbb8c8f1714d2a4b7 Mon Sep 17 00:00:00 2001 From: Andrew Johnson Date: Mon, 24 May 2021 07:46:05 +0200 Subject: [PATCH 2/2] #26: Add docs --- docs/source/check-configuration-reference.rst | 61 +++++++++++++++++++ docs/source/index.rst | 1 + 2 files changed, 62 insertions(+) create mode 100644 docs/source/check-configuration-reference.rst diff --git a/docs/source/check-configuration-reference.rst b/docs/source/check-configuration-reference.rst new file mode 100644 index 0000000..275bd08 --- /dev/null +++ b/docs/source/check-configuration-reference.rst @@ -0,0 +1,61 @@ +Check configuration reference +############################# + +.. code:: json + + { + "type": "http", + "description": "IWA-AIT check", + "input": { + "url": "http://iwa-ait.org", + "expect_keyword": "iwa", + "not_expect_keyword": "Server error" + }, + "hooks": { + "on_each_up": [ + "rm -f /var/www/maintenance.html" + ], + "on_each_down": [ + "echo \"Site under maintenance\" > /var/www/maintenance.html" + ] + } + } + + +type +**** + +Name of the binary/script file placed in the "checks" directory. At first will look at path specified by "--directory" +CLI parameter, then will fallback to Infracheck internal check library. + +Example values: + +- disk-space +- load-average +- http +- smtp_credentials_check.py + + +description +*********** + +Optional text field, there can be left a note for other administrators to exchange knowledge in a quick way in case +of a failure. + + +input +***** + +Parameters passed to the binary/script file (chosen in "type" field). Case insensitive, everything is converted +to UPPERCASE and passed as environment variables. + +**Notice:** *Environment variables and internal variables can be injected using templating feature - check* :ref:`Templating` + +hooks +***** + +Execute shell commands on given events. + +- on_each_up: Everytime the check is OK +- on_each_down: Everytime the check is FAILING + diff --git a/docs/source/index.rst b/docs/source/index.rst index 5f18c06..2e22400 100644 --- a/docs/source/index.rst +++ b/docs/source/index.rst @@ -46,6 +46,7 @@ Simple, easy to setup, easy to understand. Works perfectly with Docker. A perfec first-steps hooks reference + check-configuration-reference templating custom-scripts cache