Introduction to Linux - A Hands on Guide | Linux Bible | Linux From Scratch | A Newbie's Getting Started Guide to Linux | Linux Command Line Cheat Sheet | More Linux eBooks



Sunday, 21 September 2014

Nagios XI: A Bash Plugin to Monitor CPU Utilization on Remote Linux Server

  • This is a plugin designed using Bash script.
  • This plugin is created so that 'Performance Gauges' in Nagios XI work properly, showing appropriate 'Warning' and 'Critical' regions.

See Also: 



For Performance Gauges to show details properly, performance data should be in following format:

'VarName'=CurrentValue Unit;WarningValue;CriticalValue;MininumValue;MaximumValue

Important:

1. It uses the output of 'sar' command.
2. Warning and Critical thresholds are 80% and 90% for %user, %system, % nice, %iowait and %steal time.
3. Warning and Critical thresholds for %idle time makes no sense.

Script:


#!/bin/bash

SAR_OUTPUT=`sar 1 3 | grep -i average`

USER=`echo $SAR_OUTPUT | awk '{print $3}'`
NICE=`echo $SAR_OUTPUT | awk '{print $4}'`
SYS=`echo $SAR_OUTPUT | awk '{print $5}'`
IOWAIT=`echo $SAR_OUTPUT | awk '{print $6}'`
STEAL=`echo $SAR_OUTPUT | awk '{print $7}'`
IDLE=`echo $SAR_OUTPUT | awk '{print $8}'`

INTUSER=`echo $USER | cut -d "." -f 1`
INTNICE=`echo $NICE | cut -d "." -f 1`
INTSYS=`echo $SYS | cut -d "." -f 1`
INTIOWAIT=`echo $IOWAIT | cut -d "." -f 1`
INTSTEAL=`echo $STEAL | cut -d "." -f 1`

warn=80
crit=90

if [ "$INTUSER" -lt "$warn" -a "$INTNICE" -lt "$warn" -a "$INTSYS" -lt "$warn" -a "$INTIOWAIT" -lt "$warn" -a "$INTSTEAL" -lt "$warn" ]
then
    STATUS="OK"
    EXIT=0
elif [ "$INTUSER" -ge "$warn" -o "$INTNICE" -ge "$warn" -o "$INTSYS" -ge "$warn" -o "$INTIOWAIT" -ge "$warn" -o "$INTSTEAL" -ge "$warn" ]
then
    if [ "$INTUSER" -ge "$crit" -o "$INTNICE" -ge "$crit" -o "$INTSYS" -ge "$crit" -o "$INTIOWAIT" -ge "$crit" -o "$INTSTEAL" -ge "$crit" ]
    then
    STATUS="CRITICAL"
    EXIT=2
    else
    STATUS="WARNING"
    EXIT=1
    fi
else
    STATUS="UNKNOWN"
    EXIT=3
fi

if [ $STATUS = "UNKNOWN" ]
then
echo "$STATUS- No data" && exit $EXIT
else
echo "$STATUS- %user=${USER}%, %nice=${NICE}%, %system=${SYS}%, %iowait=${IOWAIT}%, %steal=${STEAL}%, %idle=${IDLE}% | '%user'=${USER}%;$warn;$crit;0;100 '%nice'=${NICE}%;$warn;$crit;0;100 '%system'=${SYS}%;$warn;$crit;0;100 '%iowait'=${IOWAIT}%;$warn;$crit;0;100 '%steal'=${STEAL}%;$warn;$crit;0;100 %idle=${IDLE}%;;;0;100"
fi

How To Use:

To monitor remote Linux server:
1. Keep the plugin in /usr/local/nagios/libexec directory.
2. Add following line to the nrpe.cfg file:

command[check_cputime.sh]=sudo /usr/local/nagios/libexec/check_cputime.sh
3. Add the following line to /etc/sudoers file:

nagios ALL=(ALL) NOPASSWD:/usr/local/nagios/libexec/check_cputime.sh

Usage:

On Monitoring Server:

./check_nrpe -H 128.9.45.13 -c check_cputime.sh
Output:

OK- %user=8.03%, %nice=0.00%, %system=21.07%, %iowait=0.00%, %steal=0.00%, %idle=70.90% | '%user'=8.03%;80;90;0;100 '%nice'=0.00%;80;90;0;100 '%system'=21.07%;80;90;0;100 '%iowait'=0.00%;80;90;0;100 '%steal'=0.00%;80;90;0;100 %idle=70.90%;;;0;100

0 comments:

Post a Comment