Event handler definition

Good morning everyone.

I have no experience in shell scripting and event handlers and currently I am searching for some guidance.

In one of our servers, we are required to do an event handler for 20 of the machines.
Is it possible to define them as follow?

define command {
command_name event-handler-name-goes-here

       command_line script1 /opt/scripts/eventhandlerscript.sh hostname1, hostname2, and so on... (EXAMPLE)

}

Hi joaoe33,
can you explain which problem you want to solve with the event handler?

It is quite complex.

In the server we have several machines, but the event handler is to work when a certain group of machines goes into critical state in a given service.

I have never worked with event handlers and scripts but I have managed to do a barebone script. My question is how to handle the macros.

I was thinking of calling a Python script to the check if the hostname belongs to the list of hosts required, and to do some other operations, such as creating a JSON file.

Hi joaoe33,
thanks for the explanation.
When using the (paid) Entreprise Edition, you can use the Alert Handler for this purpose. But with the Raw Edition you can use the Event Handlers with standard configuration under ~/etc/nagios/conf.d/. See also the (short) doc here:


See also the other files there. The hostname shall be denoted as $HOSTNAME$ e.g…

Currently we are using the Raw version, hence why we are trying to configure the Event Handlers.

So basicly i can do a define such as:

define command {
command_name event-handler-name-goes-here

       command_line script1 /folder/script.sh $HOSTNAME$

}

And when executing the code have a line like this

python script.py $HOSTNAME$

The event handler needs to be defined rather like this:

define command {
        command_name myeventhandler01
        command_line /myfolder01/myscript01.sh $HOSTNAME$
}

The file /myfolder01/myscript01.sh may be a python file. Ensure that the script is executable when run with a proper argument.

Understood.

Thank you for your help.

I do have a few more questions.

To allow check_mk to run the event handler, what file in conf.d folder should be altered? PNP4Nagios.cfg?

And also is it required to do an extra_service_conf in the main.mk file to declare the event handler?

Just create a new file, e.g. eventhandler.cfg, and enter the definition there.

Yes, you need an extra_service_conf. See also here (in german): https://www.geekbundle.org/selbstheilung-mit-check_mk-event-handler/

Hello Dennis. Thank you for your reply. I did the following changes to main.mk file:

extra_service_conf[“event_handler_enabled”]=[(“1”, ALL_HOSTS, ALL_SERVICES)]

extra_service_conf[“event_handler”]=[(“jira”,ALL_HOSTS,ALL_SERVICES)]

extra_nagios_conf += r"""

    define command {
            command_name jira
            command_line /omd/sites/ovh/etc/nagios/conf.d/jira.sh $SERVICESTATE$ $SERVICESTATETYPE$ $SERVICEDURATIONSEC$ $HOSTNAME$ $SERVICEDESC$ $HOSTSTATEID$
            }

“”"
And when I try to apply the changes by typing cmk -O I get the following error:

Generating configuration for core (type nagios)…OK
Validating Nagios configuration…ERROR:
An error occurred: not all arguments converted during string formatting

What am I doing wrong?

I have also tried:

extra_service_conf[“event_handler_enabled”]=[(“1”, ALL_HOSTS, ALL_SERVICES)]

extra_service_conf[“event_handler”]=[(“jira”,ALL_HOSTS,ALL_SERVICES)]

on main.mk file.

And

define command {
            command_name jira
            command_line /omd/sites/ovh/etc/nagios/conf.d/jira.sh $SERVICESTATE$ $SERVICESTATETYPE$ $SERVICEDURATIONSEC$ $HOSTNAME$ $SERVICEDESC$ $HOSTSTATEID$
            }

on a .cfg file under the conf.d/ folder where my scripts are also located.

Which of the following configurations is the correct one?

Good morning Dennis.

Apparently my issue was not with the definitions I passed to check mk. My issue was related with the OS installed and the python versions running. Everything is fixed and working.

Thank you for your time and advice.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.