In this article, we will describe what the event handler is, and how to set it up.
What is event handler?
What could be this event handler thing? It’s the big question humanity try to solved for centuries.
in a simple way it’s a process that will try to solve a problem before sending a notification to the platform users.
A service or a host when their state change from OK to any other state, will pass though a SOFT state, which mean that the state just changes, and it could be a fluke!!
the service or host will be checked several times (3 or 4 times) and if the state still doesn’t change, that SOFT state will become a HARD state which mean that we are sure that there is a problem.
It's now that our little event handler takes his role. It will when the state is a critical and HARD do something, it can be a restart command or anything else to try and solve the problem.
You’re probably wondering “why the hell does it handles only critical state and not any other states”.
Well, we don’t really care about the OK status because everything is working so event handler is useless, as for warning the service or host is still running so there’s no problem and finally the unknown state could be from an unknown error (configuration, outside service downed…) so it’s better to do nothing.
Finally, the last question you’re having (yes I can read in your mind) “why should I use this event handling thing?”, simply put it will lower the number of notification you’ll get and will makes you gain time, because if you don’t have a problem to fix you can use this time to do something more useful, am i right?
How do I implement it?
1. Step 1 Create a command !
first create a command in configuration > commands > miscellaneous
That will use the script in step 4 so be careful and put the good path to the script
2. Step 2 enable event handler on your platform
you need to enable the event handler option on the Poller (or central it depends where is supervised the host or services) you can do it via configuration > Poller > engine configuration
3. Step 3 configure it
you can configure where you want to use event handler, globally or specifically on a host or service.
Globally: stay in the engine configuration of your Poller and select the command you created like this
Specifically: go to your host in configuration > hosts > hosts in the Data Processing tab then enable it here and put the command name
Psssst you can configure it on the template if you want to affect a large number of hosts
4. Step 4 it’s script time
#!/bin/sh
#
# Event handler script for restarting the web server on the local machine
#
# Note: This script will only restart the web server if the service is
# retried 3 times (in a "soft" state) or if the web service somehow
# manages to fall into a "hard" error state.
#
# What state is the HTTP service in?
case "$1" in
OK)
# The service just came back up, so don't do anything...
;;
WARNING)
# We don't really care about warning states, since the service is probably still running...
;;
UNKNOWN)
# We don't know what might be causing an unknown error, so don't do anything...
;;
CRITICAL)
# Aha! The HTTP service appears to have a problem - perhaps we should restart the server...
# Is this a "soft" or a "hard" state?
case "$2" in
# We're in a "soft" state, meaning that Centreon is in the middle of retrying the
# check before it turns into a "hard" state and contacts get notified...
SOFT)
# What check attempt are we on? We don't want to restart the web server on the first
# check, because it may just be a fluke!
case "$3" in
# Wait until the check has been tried 3 times before restarting the web server.
# If the check fails on the 4th time (after we restart the web server), the state
# type will turn to "hard" and contacts will be notified of the problem.
# Hopefully this will restart the web server successfully, so the 4th check will
# result in a "soft" recovery. If that happens no one gets notified because we
# fixed the problem!
3)
echo -n "Restarting HTTP service (3rd soft critical state) ..."
# Call the init script to restart the HTTPD server
/etc/rc.d/init.d/httpd restart
;;
esac
;;
# The HTTP service somehow managed to turn into a hard error without getting fixed.
# It should have been restarted by the code above, but for some reason it didn't.
# Let's give it one last try, shall we?
# Note: Contacts have already been notified of a problem with the service at this
# point (unless you disabled notifications for this service)
HARD)
echo -n "Restarting HTTP service..."
# Call the init script to restart the HTTPD server
/etc/rc.d/init.d/httpd restart
;;
esac
;;
esac
exit 0
here’s the script i used for this article, here the event handler will restart the HTTP service once 3 checks have been made, and when the state is HARD.
How to test
If you want to test this amazing invention that the event handler is, you’ll need a few more steps
1. Create a host with different services that you can easily make it unworking
2. Turn on the event handler on your host (globally or specifically as you want ^^)
3. Create the command and add the script in /usr/lib/centreon/plugins/event-handlers/ (you may need to create the directory /event-handlers be carefull) of course the script need to be adapted to what you want to restart \o/
4. Now you can try by putting in a critical state your host/service.
5. Tadaaaaa your host or service have been restarted, mindblowing isn’t it?
See also
● Read more about event handler in our official documentation