Add custom service check with python script

Whre can i find information and syntax on how to properly implement a python script as a new service check command ?

I previously had configured a script with the old nagios configs, but this is no longer working, so im looking for a proper and up to date documentation to configure icinga2 to use a PYHTHON script as custom check command. NOT a bash script or plugin.

Previously, i edited the config files in the following paths, used another service as a template, but there is no real documentation on how to use python scripts, every part of the documentation leaves out the proper syntax on how to register the script with icinga2.

/usr/lib/nagios/plugins/
/etc/nagios-plugins/config/
/etc/icinga2/conf.d/commands.conf

Whre can i find information and syntax on how to properly implement a
python script as a new service check command ?

https://pypi.org/project/nagiosplugin/ may help you with the Python part, and
https://www.monitoring-plugins.org/doc/guidelines.html should help with the
general design of a plugin.

im looking for a proper and up to date documentation to configure icinga2 to
use a PYHTHON script as custom check command. NOT a bash script or plugin.

A custom check command is a plugin. You can write it in any language you
like (some of the standard ones are in Bash, some in Perl, many in C; there
might be some in Python for all I know, although I think that’s less likely
because that requires a Python interpreter to be installed, which is not
standard for many Linux distributions). You just need to ensure that the exit
code and the output conform to the guidelines above.

the proper syntax on how to register the script with icinga2

Scipts don’t need “registering”. You just have to put them in the correct
directory, and then call them with the appropriate parameters (which are
determined by your script, for whatever it is that it does) from your
CheckCommand definition.

https://icinga.com/docs/icinga2/latest/doc/09-object-types/
#objecttype-checkcommand

Let us know if you require any further more specific assistance (although
whether there are many Python people around here, I have no idea; I’m not
one).

Antony.

1 Like

Additional to the answer from @Pooh you could also take a look into https://icinga.com/docs/icinga2/latest/doc/05-service-monitoring/#create-a-new-plugin

And it’s not necessary to put your script in the standard directory, if you write the full path into the check_command-definition. We for example separted our own scripts from the scripts from the monitoring-plugins. For us it’s better to maintain.

1 Like

I’d assume you like to know how to define a command object for this python script? If so here is an simple example:

object CheckCommand "python-script1" {
   command = [ PluginDir + "/your_python_script" ]

   arguments = {
      "-s" = "$address$"
      "-q" = "$db_server$"
      "-u" = "$db_username$"
      "-p" = "$db_password$"
   }

An corresponding service object could look like:

apply Service "python_check1" {
   check_command = "python-script1"

   vars.db_server = "Default"
   vars.db_username = host.vars.db_username
   vars.db_password = host.vars.db_password

   assign where ...
}
2 Likes

Hi @dippan :slight_smile:

Did any of the suggested solutions work for you?
Would love to hear back from you and if you got that script to work :wink:

Have a nice day!
Feu

Hi, there must been some misunderstanding, this is not what i asked about.
This is not about plugin guidelines, exit codes, etc, the script was allready working before. Its just about on how to register / create the check command so the command that triggers the script and handles the response shows up in icinga2 director.

You just have to put them in the correct
directory, and then call them with the appropriate parameters (which are
determined by your script, for whatever it is that it does) from your
CheckCommand definition.

Maybe noch the techincally “correct” term, but thats what i would call registering a script as a check command.

you could also take a look into https://icinga.com/docs/icinga2/latest/doc/05-service-monitoring/#create-a-new-plugin

I tried that link a while ago, the examples there are not helpfull at all regarding pyhon scripts and variables

The examples worked fine, but neither the defined services or the check commands showed up in icinga director.
I was able to apply them to certain hosts with the assign where function, but they dont show up in the list of checks/services that can be applied to other hosts or templates in icinga director.

Atm, i ended up defining the service checks and services in dirctor, so they actually show up in the list of available checks there.

Not sure why the checks defined in /etc/icinga2/conf.d/commands.conf dont show up in the service/check commands list, but are applied to hosts and show their status correctly.

Maybe is discribed this not correctly, but i want to define check commands that later show up as options in icinga2 director. In my understading, this is defining a check command / or registering a script with the system, so it and its variables are available to use in dirctor

Hi, this is not what i asked about.
This is not about plugin guidelines, exit codes, etc, just about on how
to register / create the check command so the command that triggers the
script and handles the response shows up in icinga2 director.

Aha, I didn’t notice that you were asking about this for Icinga Director.

You just have to put them in the correct directory, and then call them
with the appropriate parameters (which are determined by your script, for
whatever it is that it does) from your CheckCommand definition.

That’s what i would call registering a script as a check command.

Hm, okay then. I’ve never seen that terminology in any of the documentation.

Where can i find information and syntax on how to properly implement a
python script as a new service check command ?

Now that I know you’re specifically asking about Icinga Director, which I do
not use, I’ll let someone else with knowledge of that to answer further.

Regards,

Antony.