Home | Linuxmon.comhttps://linuxmon.com/2024-03-29T12:01:15+00:00HomePlugin for Nagios in Bash2017-04-04T19:42:46+00:002024-03-28T20:02:11+00:00mahhttps://linuxmon.com/author/mah/https://linuxmon.com/plugin-for-nagios-in-bash/<p><a href="https://linuxmon.com/plugin-for-nagios-in-bash/"><img alt="" height="37" src="https://linuxmon.com/static/media/uploads/Blog/.thumbnails/nagios.png/nagios-157x37.png" width="157"/></a></p>
<p>Today we make simple plugin in Bash</p>
<p>We need to know that Nagios’ plugins should return code:</p>
<table border="0">
<tbody>
<tr>
<td><strong>Exit Code</strong></td>
<td><strong>Status</strong></td>
</tr>
<tr>
<td>0</td>
<td>OK</td>
</tr>
<tr>
<td>1</td>
<td>WARNING</td>
</tr>
<tr>
<td>2</td>
<td>CRITICAL</td>
</tr>
<tr>
<td>3</td>
<td>UNKNOWN</td>
</tr>
</tbody>
</table>
<p>For example, we need count of number something process in Linux</p>
<pre> #ps ax |grep collector | wc -l
33</pre>
<p><!--more--></p>
<p>we have 33 processes of ‘collector’ , we assume that is correct number of processes and if this value not much then plugin should return CRITICAL code</p>
<pre class="brush: bash; gutter: true; first-line: 1">#!/bin/bash
#do work#######
COUNT=`ps ax |grep collector | grep -v grep |grep -v check_collector| wc -l`
CNTSHOULDBE=$1
if
[ $COUNT -eq $CNTSHOULDBE ] ; then
echo "OK - $COUNT connections, should be $CNTSHOULDBE "
exit 0
elif
[ $COUNT -lt $CNTSHOULDBE ] ; then
echo "CRITICAL - $COUNT connections should be $CNTSHOULDBE "
exit 2
elif
[ $COUNT -ge $CNTSHOULDBE ] ; then
echo "CRITICAL - $COUNT connections should be $CNTSHOULDBE "
exit 2
else
echo "UNKNOWN - $COUNT connections"
exit 3
fi
</pre>
<p>argument for this plugin will be some number</p>
<p>save this code in</p>
<p><strong>/usr/local/nagios/libexec</strong></p>
<p>And make executable</p>
<p><strong>chmod +x check_collector</strong></p>
<p>We are ready to test</p>
<h2><b>Adding plugins in NRPE</b></h2>
<p>Add plugin in NRPE config file with argument for example</p>
<p><strong>command[check_collector]=/usr/local/nagios/libexec/check_collector 33</strong></p>
<p><b>33 - </b> in this case correct value for our system.</p>
<p>restart nrpe:</p>
<p><strong>#killall nrpe</strong></p>
<p><strong>#/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d</strong></p>
<p>go to the nagios host to set up for this plugin.</p>
<p>Add in config file :</p>
<pre>define service{
use generic-service ; Name of service template to use
host_name rec-1
service_description Check Collector
check_command check_nrpe!check_collector
}</pre>
<p>restart nagios:</p>
<p><strong>#server nagios restart</strong></p>
<p>in WEB interface nagios we should see:</p>
<p><a href="https://linuxmon.com/static/media/uploads/Blog/check_collector-300x7.png" target="_blank"><img alt="check_collector" class="aligncenter size-full wp-image-220" height="19" src="https://linuxmon.com/static/media/uploads/Blog/.thumbnails/check_collector.png/check_collector-736x19.png" width="736"/></a></p>
<p>Setup plugin for Nagios done.</p>Install and setup Nagios. Part 12017-04-04T19:04:21+00:002024-03-29T12:01:15+00:00mahhttps://linuxmon.com/author/mah/https://linuxmon.com/nagios-part-1/<p><!--:en--></p>
<p><a href="https://linuxmon.com/nagios-part-1/"><img alt="nagios" class="alignnone size-full wp-image-22" height="37" src="https://linuxmon.com/static/media/uploads/Blog/.thumbnails/nagios.png/nagios-157x37.png" width="157"/></a></p>
<p><strong>Tasks</strong></p>
<p>1. Install Nagios</p>
<p>2. Setup and add hosts Nagios for monitoring</p>
<p>3. Install and setup NRPE</p>
<h5> </h5>
<h5>---------------------------</h5>
<p><strong>Nagios</strong> is an open source computer system monitoring, network monitoring and infrastructure monitoring software application. Nagios offers monitoring and alerting services for servers, switches, applications and services. It alerts the users when things go wrong and alerts them a second time when the problem has been resolved.<!--:--><!--:ru--></p>
<p> </p>
<h5><!--more--></h5>
<h5>---------------------------</h5>
<p>We will be install Nagios on server Ubuntu 12.04.2 LTS from repository</p>
<p><span>sudo apt-get udate</span></p>
<p><span>sudo apt-get install nagios3 nagios3-cgi</span></p>
<p>After input admin password <strong>nagiosadmin</strong> and after installation all depends we can log on by address:</p>
<p><span>http://192.168.0.1/nagios3</span></p>
<p>Input password nagiosadmin, then should be open website nagios</p>
<p></p>
<p>By default, nagios monitors localhost only, for monitoring any hosts, we need setup config file each host:</p>
<p>Go to the host by ssh consoleubder root to <strong>/etc/nagios3/conf.d</strong></p>
<p>We need explain about files in this directory:</p>
<p>contacts_nagios2.cfg - describes all contacts for notification, leave by default, and we can change email for notification</p>
<pre>define contact{
contact_name root
alias Root
service_notification_period 24x7
host_notification_period 24x7
service_notification_options w,u,c,r
host_notification_options d,r
service_notification_commands notify-service-by-email
host_notification_commands notify-host-by-email
email root@localhost
}</pre>
<p>generic-host_nagios2.cfg - General template for host</p>
<pre># Generic host definition template - This is NOT a real host, just a template!
define host{
name generic-host ; The name of this host template
notifications_enabled 1 ; Host notifications are enabled
event_handler_enabled 1 ; Host event handler is enabled
flap_detection_enabled 1 ; Flap detection is enabled
failure_prediction_enabled 1 ; Fail prediction is enabled
process_perf_data 1 ; Process performance data
retain_status_information 1 ; Retain status information across program restarts
retain_nonstatus_information 1 ; Retain non-status information across program restarts
check_command check-host-alive
max_check_attempts 10
notification_interval 0
notification_period 24x7
notification_options d,u,r
contact_groups admins
register 0 ; DONT REGISTER THIS DEFINITION - ITS NOT A REA HOST, JUST A TEMPLATE!
}</pre>
<p>This template leave as is. Based on this file we will generate files for hosts</p>
<p>generic-service_nagios2.cfg - General template for services</p>
<p>Object Definitions, wich used in config files , <span style="text-decoration: underline;">here</span></p>
<p style="text-align: left;">------------------</p>
<p style="text-align: left;">In <a href="http://lnxmon.com/nagios-part-2/">second part</a> we'll add host to nagios for monitoring</p>
<p><!--:--></p>Install and setup Nagios. Part 22017-04-04T15:35:16+00:002024-03-28T05:33:04+00:00mahhttps://linuxmon.com/author/mah/https://linuxmon.com/nagios-part-2/<address><a href="http://exchange.nagios.org/directory/Addons/Monitoring-Agents/NRPE--2D-Nagios-Remote-Plugin-Executor/details">NRPE - Nagios Remote Plugin Executor</a></address><address></address>
<p><a href="https://linuxmon.com/nagios-part-2/"><img alt="nrpe" class="aligncenter size-medium wp-image-121" height="74" src="https://linuxmon.com/static/media/uploads/Blog/.thumbnails/nrpe-300x74.png/nrpe-300x74-300x74.png" width="300"/></a></p>
<p></p>
<p>Tasks: 1. Install plugin NRPE Nagios 2. adding host to Nagios for remote monitor hosts, sometime instead SNMP better use <strong>Nagios plugins + NRPE</strong></p>
<address><a href="http://exchange.nagios.org/directory/Addons/Monitoring-Agents/NRPE--2D-Nagios-Remote-Plugin-Executor/details"></a>Allows remotely run plugins nagios in Linux/Unix hosts</address><!--more-->
<p>Download <a href="http://downloads.sourceforge.net/project/nagios/nrpe-2.x/nrpe-2.15/nrpe-2.15.tar.gz?r=&ts=1363788540&use_mirror=hivelocity">NRPE 2.15</a> Download <a href="https://www.nagios-plugins.org/download/nagios-plugins-1.5.tar.gz">nagios-plugins-1.5.tar.gz</a> and extract it</p>
<pre>$tar -zxvf nagios-plugins-1.5.tar.gz
$tar zxvf nrpe-2.15.tar.gz
$cd nagios-plugins-1.5</pre>
<p> before compiling we need to create user for run daemon nrpe</p>
<pre>$sudo useradd nagios
$sudo passwd nagios (input strong password!)
$./configure
$make
$sudo make install
$cd /usr/local/nagios/libexec</pre>
<p>here should be a lot of files - plugins wich we can run remotely from nagios host , ??? ? ???? <strong>Compilling NRPE</strong> Go to the dir where extracted nrpe plugin We need OpenSSL for connection between nagios host and remote host</p>
<pre>$sudo apt-get install libssl-dev</pre>
<p>$./configure --with-ssl-lib=/usr/lib/x86_64-linux-gnu if need help:</p>
<pre>$./configure -help</pre>
<p>After success install we see:</p>
<pre>*** Configuration summary for nrpe 2.15 09-06-2013 ***:
General Options:
-------------------------
NRPE port: 5666
NRPE user: nagios
NRPE group: nagios
Nagios user: nagios
Nagios group: nagios
Review the options above for accuracy. If they look okay,
type 'make all' to compile the NRPE daemon and client.</pre>
<pre>$make all
$sudo make install-plugin
$sudo make install-daemon
$sudo make install-daemon-config</pre>
<p>We need edit config file</p>
<pre>/usr/local/nagios/etc/nrpe.cfg</pre>
<p>looking for the line:</p>
<pre>#server_address=127.0.0.1 # change to remote host ip</pre>
<p>this is host with nrep plugin, change on actual IP, uncomment it and change to nagios host IP:</p>
<pre> <strong>allowed_hosts=127.0.0.1 #change to nagios IP </strong></pre>
<p>Go to bottom :</p>
<pre>command[check_users]=/usr/local/nagios/libexec/check_users -w 5 -c 10
command[check_load]=/usr/local/nagios/libexec/check_load -w 15,10,5 -c 30,25,20
<strong>command[check_hda1]</strong>=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p <strong>/dev/hda1</strong>
command[check_zombie_procs]=/usr/local/nagios/libexec/check_procs -w 5 -c 10 -s Z
command[check_total_procs]=/usr/local/nagios/libexec/check_procs -w 150 -c 200</pre>
<p>in <strong>[ .. ] </strong>this is command witch used in nagios host in config file. we can use any plugins in this directory <strong>/usr/local/nagios/libexec</strong> also we can use own plugins. we can change values as we want: <strong>/dev/sda6</strong> <strong>/dev/sdb3 </strong> are in our server</p>
<pre><strong>command[check_sda6]</strong>=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p <strong>/dev/sda6
</strong><strong>command[check_sdb3]</strong>=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p <strong>/dev/sdb3
</strong></pre>
<p>here should be your disks run nrpe as daemon in remote host: <strong>$sudo /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d</strong> check nrpe on 5666 port:</p>
<pre>$ sudo netstat -anp |grep nrpe
<strong>tcp 0 0 127.0.0.1:5666 0.0.0.0:* LISTEN 27377/nrpe</strong></pre>
<p>All are OK. Go to in nagios host console and check connectivity with remote host</p>
<pre>/usr/local/nagios/libexec/check_nrpe -H <ip remote host></pre>
<pre>NRPE v2.15</pre>
<p>OK. Configure config file for using nrpe for this remote host</p>
<pre>define host{
use linux-server ; 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 rec-1
alias rec-1
address 192.168.1.28 #here ip address remote host with nrpe plugin
}
define service{
use generic-service ; Name of service template to use
host_name rec-1
service_description Total Processes
check_command check_nrpe!check_total_procs
}
define service{
use generic-service ; Name of service template to use
host_name rec-1
service_description Current Load
check_command check_nrpe!check_load
}
define service{
use generic-service ; Name of service template to use
host_name rec-1
service_description Free Space /lasxdays2
check_command check_nrpe!check_sda6
}
define service{
use generic-service ; Name of service template to use
host_name rec-1
service_description Free Space /lastxdays3
check_command check_nrpe!check_sdb3
}</pre>
<p>Then restart nagios: #service nagios restart Now we check available space in remote host <strong>/dev/sda6</strong> <strong>/dev/sdb3</strong> if something wrong alerts to admins. End.</p>