How to restart a service like haproxy using Hooks in Checks?

I planned to restart any service in case it is off.

{
“type”: “CheckConfig”,
“api_version”: “core/v2”,
“metadata”: {
“name”: “sensu-check-haproxy-hook”,
“namespace”: “default”
},
“spec”: {
“check_hooks”: [
{
“0”: [
“passing-hook”,“always-run-this-hook”
]
},
{
“critical”: [
“failing-hook”,“collect-diagnostics”,“always-run-this-hook”
]
}
],
“command”: “/opt/sensu-plugins-ruby/embedded/bin/check-process.rb -p haproxy”,
“env_vars”: null,
“handlers”: ,
“high_flap_threshold”: 0,
“interval”: 300,
“low_flap_threshold”: 0,
“output_metric_format”: “graphite_plaintext”,
“output_metric_handlers”: [
“influx-db”, “sensuemail-repeated”
],
“proxy_entity_name”: “”,
“publish”: true,
“round_robin”: false,
“runtime_assets”: ,
“stdin”: false,
“subdue”: null,
“subscriptions”: [
“system”
],
“timeout”: 300,
“ttl”: 0
}
}

I could not passing hook and failing hook part. Why there are three hooks or may I am not perceiving correctly.

Please elaborate a bit more.

It’s not advised to use hooks to restart services. Hooks are better used to add event enrichment data (think a ps table). You want the remediation handler, which you can find in Bonsai https://bonsai.sensu.io/assets/sensu/sensu-remediation-handler

Thanks for the reply. I go through this handler. I have to build it from code, dont know it is fine or not because there is no output while running following commands.
sensu-remediation-handler -v or
sensu-remediation-handler–help.

Second, in this link, checks definition has publish = false so how my entity will use this these checks.
I stop crond in one of my entity, it did not start by the check as assuming nginx in the link.

It is seeming Ad-hoc scheduling. Please elaborate it a bit more.

Hi @aaronsachs, Would love to see words from you. I need to test it by stopping crond.

@masifpak can you elaborate on why you’re building the handler from scratch, and not using the asset that was linked earlier in the thread? Using the asset ensures that you download exactly what you need and don’t need to go through the added steps of compiling the handler and placing it on a system.

This handler does make use of ad-hoc scheduling, and for good reason. Using hooks to restart a service is a bit of a foot-gun. If a check fails and calls the hook, the hook doesn’t have any built-in logic to ensure that it doesn’t run. So let’s say crond stops or the service isn’t able to start for some reason. If you have a hook that you’re relying on to restart the process, every time the check fails, the process could be restarted. This isn’t helpful and you’re effectively just restarting the service over and over. There’s no remediatory action taken. The handler, however, does have logic built into it that would take failure into account, as well as the number of failure occurrences and then take action.

That said, the readme for the asset (not the version that’s currently in bonsai) https://github.com/sensu/sensu-remediation-handler provides usesful examples of how to set this all up.

This link is updated recently. Actually when I ran following commands, no response.
sensu-remediation-handler --help . or -v

I am receiving the following message.

We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:

#1) Respect the privacy of others.
#2) Think before you type.
#3) With great power comes great responsibility.

sudo: no tty present and no askpass program specified

What if I am monitoring a java process and Java process is running but throwing some exception or stuch somewhere but check showing it OK because it is running. Can this handler or help is this regard.