http://www.unixmen.com/write-nagios-plugin-using-bash-script
Nagios
is a popular open source computer system and network monitoring
software application. It watches hosts and services, alerting users when
things go wrong and again when they get better.
It was
originally designed to run under Linux, but also runs well on other
Unix variants. It is free software, licensed under the terms of the GNU
General Public License version 2 as published by the Free Software
Foundation.
This tool is designed as a
client/server model application needs two components to work – Nagios
server and Nagios client or agent (NRPE for linux or NSClient++ for
Windows). Nagios server is the master host how connects to Nagios client
and uses a Nagios plugin to get information from Nagios client.
Nagios plugin is a script which can execute on the Nagios client machine (perl, php, python, bash script).
On
this artilce we will expand on this idea and create Nagios plugins
using Bash. These plugins will be running on nagios client VPS, and be
executed via NRPE.
Prerequisites:
Before to start, if you still didn’t install Nagios Core , check the following articles.
- Install and Configure Nagios On CentOS, RHEL, Scientific Linux 6.5/6.4
- Install and configure Nagios 4 In CentOS 7
- Install and configure Nagios 4 in Ubuntu 14.10
And also you need to install nrpe agent in the linux using the following command for ubuntu.
apt-get install -y nagios-nrpe-server useradd nrpe && update-rc.d nagios-nrpe-server defaults
Good Practices To Develop Nagios Plugin
Before
we show you how to develop nagios plugin using bash script let’s give
you the best practices to develop monitoring plugin.
Return codes
A
plugin have to send a return code. This interpreted code is the result
of the plugin execution. We call this result “status”. This is two
summary tables about return codes for hosts and services :
Hosts:
Plugin return code | Host status |
0 | UP |
1 | DOWN |
Other | Maintains last known state |
Services:
Return code | Service status |
0 | OK |
1 | WARNING |
2 | CRITICAL |
3 | UNKNOWN |
Other | CRITICAL : unknown return code |
Plugin Output
The output message helps the user to understand the information. There are two types of output for plugin:
- OUTPUT: displayed on monitoring screen in a real-time hosts and services. Its size is limited to 255 characters.
- LONGOUTPUT: displayed in details page of host and service. Its size is limited to 8192 characters.
The
plugin can provide performance data which are optional. However, if you
want to have a graph showing the evolution of the result, it is
mandatory that the plugin generates performance data.
Performance data are described after the “|” (pipe). This feature is available through the keystroke AltGR 6.
The performance data should be displayed as :
‘label’=value[UOM];[warn] ;[crit];[min];[max]
Performance data are described after the “|” (pipe). This feature is available through the keystroke AltGR 6.
The performance data should be displayed as :
‘label’=value[UOM];[warn] ;[crit];[min];[max]
- UOM: measure unit (octets, bits/s, volts, …)
- warn: WARNING threshold
- crit: CRITICAL threshold
- min: minimal value of control
- max: maximal value of control
Plugin Options
A well written plugin should have –help as a way to get verbose help.
There are a few reserved options that should not be used for other purposes:
- -V version (–version) ;
- -h help (–help) ;
- -t timeout (–timeout) ;
- -w warning threshold (–warning) ;
- -c critical threshold (–critical) ;
- -H hostname (–hostname) ;
- -v verbose (–verbose).
In addition to the reserved options above, some other standard options are:
- -C SNMP community (–community) ;
- -a authentication password (–authentication) ;
- -l login name (–logname) ;
- -p port or password (–port or –passwd/–password)monitors operational ;
- -u url or username (–url or –username).
Language used
You can write your script using any language like perl , c , php, python and bash. But you shloud use a language that:
- easy-to-learn
- easy and generally know by the administrators
- many free library available on the web
- often installed on Unix or linux system by default
- easy to manage system start-up command and recovered results
- advanced treatment characters strings
- relatively efficient
Create Bash Script
For this article, we present plugin in bash.
For our example, we will create a script that checks the stats for any service like mysql, apache etc.
#!/bin/bash # Nagios Plugin Bash Script - check_service.sh # This script checks if program is running # Check for missing parameters if [[ -z "$1" ]] then echo "Missing parameters! Syntax: ./check_service.sh service_name" exit 3 fi if ps ax | grep -v grep | grep $1 > /dev/null then echo "OK, $SERVICE service is running" exit 0 else echo "CRITICAL , $SERVICE service is not running" exit 2 fi
We will save this script in /usr/lib/nagios/plugins/check_service.sh and make it executable:
chmod +x /usr/lib/nagios/plugins/check_service.sh
To
make this script work properly when run via “check_nrpe” from Nagios
Server you must add this line to /etc/sudoers file on the Host Client side! This is because of the sudo command used in script.
nagios ALL=(ALL) NOPASSWD:/usr/sbin/lsof,/usr/lib/nagios/plugins/check_service.sh
And you need to configure /etc/nagios/nrpe.cfg and add this line
command[check_service]=/usr/lib/nagios/plugins/check_service.sh
Now let’s test our plugin locally from our Nagios Client machine:
# /usr/lib/nagios/plugins/check_service.sh Missing parameters! Syntax: ./check_service.sh service_name # /usr/lib/nagios/plugins/check_service.sh mysqld CRITICAL - mysqld service is not running # /usr/lib/nagios/plugins/check_service.sh apache2 OK - apache2 service is running
Now you need to define new command in your nagios server command file /etc/nagios/objects/commands.cfg
define command{ command_name check_service command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c check_service }
And then you must add a new service check on Nagios Server side.
define service { use generic-service host_name unixmen service_description service check check_command check_service }
In order to verify your configuration, run Nagios with the -v command line option like so:
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
The last step is to restart the nagios service using this command
service nagios restart
That’s all. Thank you!!!
No comments:
Post a Comment