Repository: riotkit-org/infracheck

Issue:
[Core] Add optional description field
Optional `description` field can be helpful to keep the note for other administrators


Comments:

Patch:
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
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 @@ class ExecutedCheckResult(object):
         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 @@ class Runner(object):
             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 @@ class Runner(object):
 
             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 @@ class Runner(object):
 
         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)
 


Test patch:
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 @@ class ExecutedChecksResultListTest(BasicTestingCase):
                 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 @@ class ExecutedChecksResultListTest(BasicTestingCase):
                 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 @@ class ExecutedChecksResultListTest(BasicTestingCase):
                 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 @@ from infracheck.infracheck.model import ExecutedCheckResult
 
 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 @@ class ExecutedCheckResultTest(BasicTestingCase):
             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 @@ class ExecutedCheckResultTest(BasicTestingCase):
             '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)