Package: healthcheckbot
Healthcheck Bot is a standalone, highly configurable and extendable, application for verifying the status of your software products.
The user is free to configure which targets to test, which metrics to monitor, and where to store check outcomes. Healthcheck Bot also supports custom assertions written in Python for more sophisticated check scenarios.When Should I Use Healthcheck Bot?Consider using Healthcheck Bot if:you need proactive monitoring of your software. This means you have an existing application in place, and you need to query it, regularly capture its state and compare it to the expected one;you want to load the outcomes of proactive healthchecks into existing infrastructure (Graylog, Nagios, whatever)the validation of your system state requires more complex comparations than the ones defined in DSLYamlJSON, which are easier to express with code.you are familiar with Python.Alternative SolutionsIf none of the above describes your case, you might want to consider one of the following alternatives:Nagiosand its application checksGossif you need just servers pings, ports checks, server configuration etc.Healthcheck Bot UsageThe package provides executablehealthcheckbotas the main entry point. You must pass a configuration file containing definitions of
your watchers and other options. In order to start the application, run:healthcheckbot -c examples/config.yaml runIf you prefer Docker, there is a pre-built image available. You need to create a config file somewhere on host machine and run Docker as follows:dockerrun--rm-it-v$PWD/myconfigs:/srv/configlogicify/healthcheckbotIn this case, your config must be namedconfig.yaml; it must be located in./myconfigsdirectory on your host machine.If you want to use a different file, you could set it in env variableCONFIG_FILE:dockerrun--rm-it-v$PWD/myconfigs:/srv/config-eCONFIG_FILE=/srv/config/advanced.yamllogicify/healthcheckbotFor the cases when you also need to pass your custom modules, e.g. custom assertions, you need to mount data directory as well. Check the following example:docker-compose.yamlhealthcheckbot:image:logicify/healthcheckbotvolumes:-./myconfig:/srv/config-./mydata:/srv/dataenvironment:CONFIG_FILE:/srv/config/my_config_name.yamlmy_config_name.yamlapp:classpath:-/srv/dataoutputs:console:provider:healthcheckbot.outputs.ConsoleOutputtriggers:each_1_minute:provider:healthcheckbot.triggers.SimpleTimerinterval:60watchers:google_home:provider:healthcheckbot.watchers.HttpRequesturl:http://google.comassert_response_time:2assert_status:200triggers:-each_1_minutecustom_assertions:check_something_interesting:provider:mypackage.assertions.CustomAssertmy_param:'val1'In this sample, we mount directory/srv/datafrom the host and declare it as a part of classpath, so all Python modules from this dir are accessible from the application in runtime. Thus, we can implementCustomAssertmodule and use it in our configuration. SeeCustomizationsection for details.ConceptsConsider the following configuration example:outputs:console:provider:healthcheckbot.outputs.ConsoleOutputtriggers:each_1_minute:provider:healthcheckbot.triggers.SimpleTimerinterval:60watchers:google_home:provider:healthcheckbot.watchers.HttpRequesturl:http://google.comassert_status:200triggers:-each_1_minuteIn this example, we define a single watcher that will send HTTP request tohttp://google.comeach minute.
Healthcheck will be treated as failed when the response status is not200.
The result of the watcher evaluation will be printed to STDOUT.Generally, there are 4 types of entities (module types) Healthcheck Bot works with: Outputs, Triggers, Watchers, WatcherAsserts.
Sections below describe each of them. Please also note that user is able to implement their own module to extend or override default behaviour and connect it without modifying the core code. SeeCustomizationsection below.
Regardless of the module type you define, there is a mandatory component calledprovider. It defines fully qualified name
of the class implementing corresponding module. The rest of options are parameters for module instance.OutputsOutput defines the way watcher’s evaluation result will be delivered to the end user. It might be as simple as just console output
or a more real-life and common record in a database, or a centralized metric collection for a system like CloudWatch or Graylog2.There is a couple of implementations of outputs built in the package.Console OutputJust prints serialized JSON output to the STDOUT. There are no configuration parameters.Usage Example:outputs:console:provider:healthcheckbot.outputs.ConsoleOutputLogger OutputThis one is very similar to console output, but the serialized result will be passed to the logger.ParametersParameterDescriptionDefault ValueRequiredlog_levelLog level to be used when outputting resultINFONologer_nameName of the logger to useOUTNoTriggersTriggers are responsible for initiation of worker execution. The most common use case is periodic run, but other scenarios are possible as well, e.g. execution after HTTP call.Simple TimerThis implementation of the trigger is pretty self-explanatory - all it does is periodic watchers execution with constant interval specified as a parameter.ParameterDescriptionDefault ValueRequiredintervalTime interval in seconds between iterations300Nostart_immediatelyIf set to True, the first iteration will be triggered immediately after application starts; otherwise, inintervalsecondsTrueNoExampletriggers:each_1_minute:provider:healthcheckbot.triggers.SimpleTimerinterval:60each_5_minutes:provider:healthcheckbot.triggers.SimpleTimerinterval:300WatchersWatchers are modules that actually read the system state and could optionally run some assertions over a certain state. Their parameters mostly depend on implementation, but there is a couple of options common for all watchers.triggers- the list of trigger names that will invoke the given watcher. It is important to list at least one trigger, otherwise, the watcher will never be invoked.custom_assertions- the dictionary containing assertions to be applied as a part of state verification after regular module assertions. See sectionWatcher Assertsfor details.Watcher AssertsTBDCustomizationUser’s ability to extend the behavior of any module is a key feature of Healthcheck Bot. In order to make it easier to load modules from the outside, user could extend classpath (folders to be scanned for classes) with a simple configuration option. Consider the following example:app:classpath:-/tmpoutputs:console:provider:healthcheckbot.outputs.ConsoleOutputtriggers:each_1_minute:provider:healthcheckbot.triggers.SimpleTimerinterval:60watchers:system_time:provider:logicify.watchers.SystemTimeWatchertriggers:-each_1_minuteOur/tmp/logicifyfolder looks as follows:/tmp/logicify/
├── watchers.py
└── __init__.pyFilewatchers.pycontains classSystemTimeWatcherthat implementsWatcherModule:classSystemTimeWatcher(WatcherModule):def__init__(self,application):super().__init__(application)self.error_when_midnight=Falsedefobtain_state(self,trigger)->object:current_time=datetime.now()returncurrent_timedefserialize_state(self,state:datetime)->[dict,None]:return{"time":state.isoformat()}defdo_assertions(self,state:datetime,reporter:ValidationReporter):ifself.error_when_midnight:ifstate.time()==time(0,0):reporter.error('its_midnight','Must be any time except of 00:00')PARAMS=(ParameterDef('error_when_midnight',validators=(validators.boolean,)),)This implementation illustrates how you could create your own watchers. While this example shows only a watcher module, many concepts apply to the Triggers, Outputs and Asserts too.PARAMStuple gives you a way to configure arguments for your module. During application, bootstrap parameters fromyamlwill be sanitized, validated and assigned to the module instance according to definition configured withParameterDef.Methodobtain_statewill be invoked by the trigger. You should implement your state gathering logic here. The result could be any object.do_assertionswill be invoked on state verification stage.stateparameter here is what was returned fromobtain_state, andreporterinstance must be used to report assertion errors (if any).And finally,serialize_statewill be called before passing the result to output. It should convert state object to simple types (dictionaries, lists, primitives).ContributionThe initial configuration of dev environment:virtualenv-ppython3 venvsource ./venv/bin/activatepip install-r./requirements.txtCreditsDmitry Berezovsky, Logicify (http://logicify.com/)LicenseThis plug-in is licensed under GPLv3. This means you are free to use it even in commercial projects.
Also note there is no warranty for this free software. Please see the includedLICENSEfile for details.