---
title: "How to create an event handler with Python"
description: "Creating an event handlers with Python in OP5 Monitor automates responses to service state changes. The process involves scripting the handler, defining it as a command, and associating it with a service in OP5 Monitor."
url: "https://docs.itrsgroup.com/docs/faq/op5/360020056194-op5-monitor-how-to-create-an-event-handler-with-python/index.html"
---
## Introduction

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.

## How do event handlers work?

Every time a service change its state, *OP5 Monitor* will run the event handler that's associated with that service or host.
Just like check plugins, an event handler is an 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](http://nagios.sourceforge.net/docs/3_0/macrolist.html).

## Example

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:

1. Take arguments of the current state, kind of state (hard or soft), name, and check attempt.
2. If it's a soft state, the current attempt is the first and it's a critical alert.
   1. Increase memory using an API.

The above would look somehow like this in Python: ([Download](/faq/downloads/op5/360020056194-inline-attachment-1-increase-memory.py))

```
#!/usr/bin/env python

import argparse

parser = argparse.ArgumentParser(description="This event handler will increase the memory of a ESX guest when triggered.")

parser.add_argument("--service-state", help='The service state, use $SERVICESTATE$', required=True)

parser.add_argument("--service-state-type", help='The type of the state, use $SERVICESTATETYPE$', required=True)

parser.add_argument("--service-attempt", help='Current attempt number, $SERVICEATTEMPT$', required=True, type=int)

parser.add_argument("--host-name", help='The host name, use $HOSTNAME$', required=True)

args = parser.parse_args()

def increaseEsxGuestMemory(aHost):

    #Do some stuff to increase memory here.

    notifySysAdmin("Nils Nilsson", "We just increased some memory on %s." % (aHost))

    return 0

def notifySysAdmin(aAdmin, aMessage):

    #Notify sysadmin here.

    print "To: %s; %s" % (aAdmin, aMessage)

    return 0

if args.service_attempt == 1 and args.service_state == "CRITICAL" and args.service_state_type == "SOFT":

    increaseEsxGuestMemory(args.host_name)
```

Once we have created our event handler, it should be placed in /opt/plugins/custom/.

Step 2, create the command and associate it with the service in OP5 Monitor

In op5 Monitor, go to Configure -> Commands and make sure that "Add New" is selected.

command\_name: increase-memory
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

Press Save

And lastly, save your changes in the configuration export dialog.

### **Step 3**

Now you can watch the tickets roll into the ticket system!

If you check your naemon.log you will see similar lines like below.

```
[1659661031] SERVICE ALERT: server1;PING;CRITICAL;SOFT;1;CRITICAL - 127.0.0.1: rta 0.023ms, lost 0%
[1659661031] SERVICE EVENT HANDLER: server1;PING;CRITICAL;SOFT;1;increase-memory
```

## Explanation of the event handler code

```
#!/usr/bin/env python

import argparse

parser = argparse.ArgumentParser(description="This event handler will increase the memory of a ESX guest when triggered.")

parser.add_argument("--service-state", help='The service state, use $SERVICESTATE$', required=True)

parser.add_argument("--service-state-type", help='The type of the state, use $SERVICESTATETYPE$', required=True)

parser.add_argument("--service-attempt", help='Current attempt number, $SERVICEATTEMPT$', required=True, type=int)

parser.add_argument("--host-name", help='The host name, use $HOSTNAME$', required=True)

args = parser.parse_args()
```

As always, try to utilize argparse when using arguments. It makes things a lot easier. In short, we are importing the argparse module and adding the arguments we need.

```
def increaseEsxGuestMemory(aHost):

    #Do some stuff to increase memory here.

    notifySysAdmin("Nils Nilsson", "We just increased some memory on %s." % (aHost))

    return 0

def notifySysAdmin(aAdmin, aMessage):

    #Notify sysadmin here.

    print "To: %s; %s" % (aAdmin, aMessage)

    return 0
```

This is an example function for upgrading memory, but for now and while we are testing, it just prints to stdout.

```
if args.service_attempt == 1 and args.service_state == "CRITICAL" and args.service_state_type == "SOFT":

    increaseEsxGuestMemory(args.host_name)
```

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 receive.

## Related downloads

- [increase-memory.py](/faq/downloads/op5/360020056194-360029602414-increase-memory.py)


