Home | Linuxmon.comhttps://linuxmon.com/2024-03-28T12:59:22+00:00HomePlugin for Nagios in Bash2017-04-04T19:42:46+00:002024-03-28T12:59:22+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>