Skip to content

Commit

Permalink
Merge pull request #34 from riotkit-org/issue_26
Browse files Browse the repository at this point in the history
#26: Add a comment/description field, that could be optionally used as a note for administrators
  • Loading branch information
blackandred authored May 29, 2021
2 parents 17b2cac + 7962b5f commit 469d6e4
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 15 deletions.
61 changes: 61 additions & 0 deletions docs/source/check-configuration-reference.rst
Original file line number Diff line number Diff line change
@@ -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

1 change: 1 addition & 0 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 7 additions & 3 deletions infracheck/infracheck/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 ''
Expand Down
10 changes: 7 additions & 3 deletions infracheck/infracheck/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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)

Expand Down
18 changes: 12 additions & 6 deletions tests/unit_test_executed_checks_result_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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())
Expand All @@ -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())
8 changes: 5 additions & 3 deletions tests/unit_test_model_executed_check_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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)

0 comments on commit 469d6e4

Please sign in to comment.