Tuesday, 27 August 2013

Create a simple Nagios plugin support graph (active check)

In this article, I will post an example of a Nagios plugin support graph.
First, you must install Nagios core and pnp4nagios (I suggest you install like these article because other ways can make some differences).

This code is written by Python, will make random number and return result. You can do with any language can return the same result.


Create plugin

Code

#!/usr/bin/python
import os, sys, random

random = random.randrange(0, 120)

if random < 50:
        print "OK - %s%% cpu|'cpu'=%s%%" % (random, random)
        sys.exit(0)
elif random < 90 and random >= 50:
        print "WARNING - %s%% cpu|'cpu'=%s%%" % (random, random)
        sys.exit(1)
elif random <= 100 and random >= 90:
        print "CRITICAL - %s%% cpu|'cpu'=%s%%" % (random, random)
        sys.exit(2)
else:
        print "UKNOWN - %s%% cpu|'cpu'=%s%%" % (random, random)
        sys.exit(3)

Example result
OK - 47% cpu|'cpu'=47%


Create file

cd /home/
vi plugin.py  (  paste code in vi editor)

chmod 777 plugin.py

Check result
./plugin.py -> UKNOWN - 111% cpu|'cpu'=111%


Config in Nagios (local)
Create a host with service use our plugin
cd /usr/local/nagios/etc
vi test.cfg
paste this content

define host{
        use                     linux-server,host-pnp         ; Name of host template to use
                                                        ; This host definition will inherit all variables that are defined
                                                        ; in (or inherited by) the linux-server host template definition.
        host_name               localhost2
        alias                   localhost2
        address                 127.0.0.1
        }

define service{
        use                             local-service,service-pnp          ; Name of service template to use
        host_name                       localhost2
        service_description             Check cpu
        check_command                   check_cpu
        }

define command{
        command_name    check_cpu
        command_line    /home/plugin.py
        }

vi /usr/local/nagios/etc/nagios.cfg
and this line
cfg_file=/usr/local/nagios/etc/test.cfg


Check config error and restart
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

service nagios restart


Result
And this is the result:


Click to check cpu:

You can see Status information and Performance Data , this is the result of above red plugin result


Graph will be created from Performance Data with this struct: 'label'=value[UOM];[warn];[crit];[min];[max]

See more here: http://nagiosplug.sourceforge.net/developer-guidelines.html

Wait about half of hour or more, and this is my plugin graph :




Config from remote host, via nrpe:
Install in client (ip: 192.168.0.33)
apt-get install -y python nagios-nrpe-server

Disable nagios3 service
Because I use nagios from source (not nagios3 - dependences of nagios-nrpe-server), so, to disable (to get server lighter), we need:
update-rc.d nagios3 disable (for disable service startup with server)
service nagios3 stop (to stop running service)
update-rc.d nagios3 enable (for enable service startup)

Install and check
client:
create plugin.py at /home/plugin.py like above at client

Config
vi /etc/nagios/nrpe.cfg

add any monitor service or host can get infor at allowed_hosts:
allowed_hosts=127.0.0.1,192.168.0.222
add this plugin command:
command[check_cpu]=/home/plugin.py
Save config file and restart
service nagios-nrpe-server restart 


Install in server (ip: 192.168.0.222)
sudo apt-get install nagios-nrpe-plugin

Check nrpe
/usr/lib/nagios/plugins/check_nrpe -H 192.168.0.33 -c check_cpu
And this is the result:
WARNING - 69% cpu|'cpu'=69%

With this command, you can change command in above local config to check remote host.
In fact, we usually use command like:
/usr/lib/nagios/plugins/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
and check_command like:
check_cpu!check_cpu
 with ip will be replaced in $HOSTADDRESS$ and check_cpu after ! will be replaced in $ARG1$.
Go to document of nagios to view more.

No comments:

Post a Comment