Thursday 3 September 2015

monitor system performance"CPU","MEMORY",FILESYSTEM"

Monitor Your CPU, MEMORY and File System

#!/bin/bash
#This script will be used  to monitor system performance"CPU","MEMORY",FILESYSTEM"
#> $LOG
LOG="/tmp/monitor_txt.log"
if [ $# -eq 0 ]
then
MONITOR_MEM ()
{
DATE=`date +%c`
FREE=`free -m  | awk -F' ' '/Mem/{printf "Mem used: %.0f% \nMem free: %.0f%\n", $3/$2*100, $4/$2*100}'`
echo "------------------------------------------------------------------------------------------------------" |tee -a $LOG
echo "Memory,CPU,Filesystem usage at : $DATE" |tee -a $LOG
echo $FREE  |tee -a $LOG
}

MONITOR_CPU ()
{
TOTAL_CPU=100
DATE=`date +%c`
FREE=`top -b -n1 | grep ^Cpu | awk -F ',' '{print $4}' | cut -d'%' -f1|cut -d'.' -f1`
USED=`expr $TOTAL_CPU - $FREE`
echo -e "CPU Used:\t $USED%" |tee -a $LOG
}

MONITOR_FILESYTM ()
{
ALERT=85
df -Ph | grep -vE '^Filesystem|tmpfs|cdrom|boot' | awk '{ print $5 " " $6 " " $1 }' | while read output;
do
usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1  )
partition=$(echo $output | awk '{ print $2 }' )
if [ $usep -ge $ALERT ]; then
echo -e "Team,Following Partition Running out of space \"$partition ($usep%)\" on $(hostname) as on $(date)" |tee -a $LOG
else
echo " Filesystem Status is normal " |tee -a $LOG
fi
done
}

MAIN ()
{
VAR=0
LIMIT=1440
while [ "$VAR" -le "$LIMIT" ]
do
MONITOR_MEM
if [ $? -eq 0 ]
then
MONITOR_CPU
if [ $? -eq 0 ]
then
MONITOR_FILESYTM
VAR=`expr $VAR + 1`
sleep 6
fi
fi
done
}

if [ `whoami` == `users|awk '{print $1}'` ]
then
MAIN
fi
fi

No comments:

Post a Comment