Nagios script to monitor memory uses

Purpose:
###########################################
Develop a nagios script, which will monitor Linux memory uses.
###########################################

This script will check following :
#############################################
#1.If free memory is more then the defined memory as free: Status Done
#2.If System is using swap memory : Status:Done
##############################################

#!/bin/bash
 
#Version 1.0
#######################################
#Nagios scrept to check memory status##
#Commands : fre -m#####################
#######################################
 
 
#Status check for nagios script
 
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3
STATE_DEPENDENT=4
 
 
#Define All the variables for commands
 
declare -rx SCRIPT=${0##*/}
declare -rx CMD_AWK="/bin/awk"
declare  -rx CMD_CAT="/bin/cat"
declare  -rx CMD_FREE="/usr/bin/free"
declare  -rx CMD_VMSTAT="/usr/bin/vmstat"
declare  -rx CMD_GREP="/bin/grep"
 
#####Section 1.1 :Definning function for free memory checking########
#Definning function to check free memory status#####################
##########################################
 
function FUNC_FREE_CMD
 
{
 
MEM_STATUS=$( $CMD_FREE -m | grep buffers/cache | awk '{print $4}')
 
 
########Checking if Current memory is critial or normal ######
 
if [ $MEM_STATUS -le 325 ]
then
 
#echo "Critical,Memory Level: $MEM_STATUS"
echo "Critical,Memory Level: $MEM_STATUS|Memory_level=$MEM_STATUS;350;325;0"
exit $STATE_CRITICAL
fi
 
if [ $MEM_STATUS -le 350 ]
then
 
echo "Warnings,Memory Level: $MEM_STATUS|Memory_level=$MEM_STATUS;350;325;0"
exit $STATE_WARNING
 
else
echo "Memory Seems Ok,Total Memory is: $MEM_STATUS|Memory_level=$MEM_STATUS;350;325;0"
#echo "Critical,Memory Level: $MEM_STATUS|Memory_level=$MEM_STATUS"
#exit $STATE_OK
fi
 
}
 
#####Section 2.1 Definning function for checking swap uses###########
#### Commands: free -m | grep Swap | awk '{print $3}################
###################################################################
 
function FUNC_FREE_SWAP_CMD
{
 
SWAP_STATUS=$( $CMD_FREE -m | grep Swap | awk '{print $3}')
 
if [ $SWAP_STATUS -ne 0 ]
then
echo "System is using swap:$SWAP_STATUS"
echo "Lets Try to find out how much swap system using by using vmstat output"
 
fi
 
}
 
######Section 3.1, Definning funtion , to check how much swap in and swap out for  5 seconds####
#####Commands : vmstat
###############################################################################################
 
 
 
function FUNC_VMSTAT_CMD
{
 
#echo $( $CMD_VMSTAT 3 5 | $CMD_GREP "^[ ][0-9]"|  $CMD_AWK 'BEGIN{for(n=1;n<=8;n++){printf("%s ", "Average Uses:" [3]/5)}}')
echo $( $CMD_VMSTAT 3 5 | $CMD_GREP "^[ ][0-9]"|  $CMD_AWK 'BEGIN{for(n=1;n<=8;n++){printf("Average Uses:" s[3]/5)}}')
 
}
 
 
#############Section 3.1 calling  all  functions###############
###Function from section 1.1:To Calculate Free memory##############
###Funciton from section 2.1:To calucate  Swap uses ###############
FUNC_FREE_CMD
FUNC_FREE_SWAP_CMD
FUNC_VMSTAT_CMD

Leave a Reply

*