ankit tyagi
ankit tyagi

Reputation: 846

How to integrate my python plugin with nagios

I have a python script written that hits a url to my application which is behind HAPROXY and returns the version of application.

I want to integrate this script/plugin with my nagios server and display the version information on nagios. It would solve two purposes that it would check the availability and also show the version info.

Appreciate any help.

Thanks, Ankit

Upvotes: 0

Views: 4680

Answers (1)

Joe Young
Joe Young

Reputation: 5875

Edited to address comments

To use a python script as a custom Nagios plugin, your script just needs to follow the Nagios Plugin API. The important thing is that your script exit with correct return code for the state it's reporting -- meaning your python script needs to exit with return code 0 if the check is reporting that everything is OK. Exit with return code 2 to indicate a CRITICAL state. You should also format the output of your script to conform with the Plugin Output Spec

Below is an example. Please note that this is only for EXAMPLE purposes and probably will not work for your site unless your site just so happens to have the same div id as stackoverflow.com ;) (Updated to include configurable URL parameter.)

check_version.py

#!/usr/bin/env python
import re, sys, subprocess, shlex
from bs4 import BeautifulSoup
from optparse import OptionParser

def main():
    usage = "usage: %prog [options]"
    parser = OptionParser(usage)
    parser.add_option("-u", "--url",
                      action="store", dest="url",
                      help="URL of host to check version number of.")
    (options, args) = parser.parse_args()
    # Make warning and critical command line arguments mandatory.
    if options.url is None:
        print "Incorrect usage. URL is mandatory."
        parser.print_help()
        sys.exit(3)
    try:
        url = options.url
        curl_command = '/usr/bin/curl {url}'.format(url=url)
        args = shlex.split(curl_command)
        curl = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
        (html_doc, err) = curl.communicate()
        if err:
            print "CRITICAL - ERROR connecting to url: %s | 'rev'='NA';;;;" % (str(err))
            sys.exit(2)
        soup = BeautifulSoup(html_doc, 'html.parser')
        soup_svnrev = soup.find(id='svnrev')
        if soup_svnrev is None:
            print "CRITICAL - ERROR - Revision number could not be found on site! | 'rev'='NA';;;;"
            sys.exit(2)
        svnrev = soup_svnrev.get_text().strip()
        rev_num = svnrev.split()[1]
        print "OK. revision number = {rev_num} | 'rev_num'={rev_num};;;;".format(rev_num=rev_num)
        sys.exit(0)
    except Exception, e:
        print "CRITICAL - Unexpected error: %s | 'rev'='NA';;;;" % (str(e))
        sys.exit(2)

if __name__ == '__main__':
    main()

With a working Nagios plugin script, you just need to add it as Nagios Command and finally schedule its execution by defining a Nagios Service for it.

Edit with example configuration

Of course you need to configure Nagios with everything Nagios would need to execute a check. Meaning that at minimum you need to define host, service, and command definitions in some Nagios configuration file that your Nagios installation knows about. That may differ for each Nagios installation so here I'll use /usr/local/nagios/etc/nagios.cfg as an example, adding in the following:

/usr/local/nagios/etc/nagios.cfg

define host{
    host_name           stackoverflow
    alias               stackoverflow
    address             stackoverflow.com
    check_command           check-host-alive
    check_interval          5
    retry_interval          1
    max_check_attempts      5
    check_period            24x7
    process_perf_data       0
    retain_nonstatus_information    0
    contacts            nagiosadmin
    notification_interval       30
    notification_period     24x7
    notification_options        d,u,r
}

define service{
    host_name       stackoverflow
    service_description check_version
    check_command       check_version!http://stackoverflow.com
    max_check_attempts  5
    check_interval  5
    retry_interval  3
    check_period        24x7
    notification_interval   30
    notification_period 24x7
    notification_options    w,c,r
    contacts        nagiosadmin
}

define command{
    command_name    check_version
    command_line    /path/to/check_version.py -u "$ARG1$"
}

If there are no errors in the script, your script has "execute" permissions, and there are no errors in the configuration, you should be able to run a verification check on your main Nagios configuration file:

nagios -v /usr/local/nagios/etc/nagios.cfg

Restart the Nagios server to reload the configuration:

service nagios restart

And your site version should be visible when you view the service details for the "check_version" service of the "stackoverflow" host.

Upvotes: 2

Related Questions