Event handlers are applications that can be used to restart crashed services, change device configuration or similar, based on the result of service checks in op5 Monitor.
In this how-to we will look at creating a custom event handler in Python.
If you're not familiar with Python, check out our check plugin tutorial for more information.
How does event handlers work?
Every time a service change state, op5 Monitor will run the event handler that's associated with that service or host.
Just like check plugins, an event handler is a external program or script executed by the scheduler. It's given some arguments and then performs some logic.
The event handler is defined as a command and can use the built in Naemon or Nagios macros.
In this example we create a Python script that will increase the memory of a virtual machine in VMWare, if the service monitoring memory usage changes to a critical state.
Step 1, Create the event handler
First off, we need to create the event handler. In steps, it will do the following:
- Take arguments of the current state, kind of state (hard or soft), name and check attempt.
- If it's a softstate, the current attempt is the first and it's a critical alert.
- Increase memory using an API.
The above would look somehow like this in Python: (Download)
Once we have created our event handler, it should be placed in /opt/plugins/custom/.
Step 2, create the command and associate with service in op5 Monitor
In op5 Monitor, go to Configure -> Commands and make sure that "Add New" is selected.
command_line: $USER3$/custom/increase-memory.py --service-state $SERVICESTATE$ --service-state-type $SERVICESTATETYPE$ --service-attempt $SERVICEATTEMPT$ --host-name $HOSTNAME$
Press Save, and then open up the service wich you want to use to notify the ticket system.
Click Advanced to expand advanced properties
Change event_handler to increase-memory
And lastly, save your changes in the configuration export dialog.
Now you can watch the tickets role in to the ticket system!
Explanation of the event handler code
As always, try to utilize argparse when using arguments. It makes things alot easier. In short we are importing the argparse module and adding the arguments we need.
This is a example function for upgrading memory, but for now and while we are testing, it just prints to stdout.
This is the place where we decide what to do, depending on if it's a hard or soft state and what type of error we recieve.