Friday 10 June 2016

Unix scripts for monitoring

1.To find and delete the files from results of find command

Before deleting files check the output to make sure that you are going to delete  run the command below

find . -name *.gz

Once you are sure then delete the files using following command

find . -name *.gz| xargs rm


2.To find the size of biggest files in your directory

for i in G M K; do du -ah | grep [0-9]$i | sort -nr -k 1; done | head -n 1
 
 
 3.To monitor CPU and Memory of top processes.

This script will run in the loop and wait for 10 sec before capturing memory and cpu of
top 10 process.
Output of the script can be sent out to a file for later analysis. In order to stop 
this script press control C.
 
 #!/bin/bash

while true
do
    echo "-------`date`--------"
    echo "\t\t%MEM\t%CPU"
    ps ax -o comm,%mem,%cpu | sort -nk3 | tail -n 5
    sleep 10
done




Script below is to monitor CPU and memory utilisation by an application. So if an application
has 10 processes then it will sums up the results and provide total memory and cpu utilisation. Frequency and 
amount of script run can be modified by changing parameters within the script.

-----------------------------------------------------------------------------------------------
#!/usr/bin/env bash

#Script to check memory and process usage by a service
#Accepts maximum of 3 arguments (default)
#Usage ./script process-name memory-threshold(in MB) CPU-threshold(in percentage)

###set -xv #uncomment to debug
cpu=$3
mem=$2

if [ $# -ne 3 ]  #script must be run with 3 arguments
then
    echo "Usage: ./script process memory-threshold(in MB) CPU-threshold(in percent)"
    echo "Example: ./script chrome 2000 30"
    exit 1
fi

total_memory=`grep MemTotal /proc/meminfo | awk '{print $2}'`
in_mb=`echo \$total_memory / 1024 | bc`
echo "Total memory is $in_mb MB" 

#monitor cpu usage by a process for n seconds and average the result , this will give better metrics
nPid=`top -b -n 1 | grep $1 | awk '{print $1}' | sort -nr | tail -n1` #get the PPID if the process has sub-processes
nTimes=5 # no of snapshots
delay=1 # with delay in seconds
calc=`top -d $delay -b -n $nTimes -p $nPid \
  |grep $nPid \
  |sed -r -e "s;\s\s*; ;g" -e "s;^ *;;" \
  |cut -d' ' -f9 \
  |tr '\n' '+' \
  |sed -r -e "s;(.*)[+]$;\1;" -e "s/.*/scale=2;(&)\/$nTimes/"`;
cpu_usage=`echo "$calc" |bc -l`

ps -C $1 -o %mem | grep -iv "MEM" | awk '{print $1}' > /tmp/mem  # redirect memory usage to a file

FILENAME=/tmp/mem
total=0
while read LINE
do
        total=`echo \$LINE + \$total | bc`  #if the process has sub processes then sum them
done < $FILENAME

mem_used2=$(bc -l <<< 'scale=4; '$total/100'*'$in_mb'') #calculate memory usage in MB
echo "[$(date '+%Y-%m-%d %T')] CPU % usage for $1:" $cpu_usage "--------" "Memory usage for $1: $mem_used2 MB"
echo "Load averages for the last 1, 5, 15 minutes are:" [$(uptime | awk -v f=8 -v t=10 '{for(i=f;i<=t;i++) printf("%s%s",$i,(i==t)?"\n":OFS)}')]
#echo "$1 memory usage in MB is $mem_used2"

if [[ $(echo "$mem_used2 >= $2" | bc) -eq "1" ]]; then
    echo "$1 critical memory usage alert"
fi
if [[ $(echo "$cpu_usage >= $3" | bc) -eq "1" ]]; then
    echo "$1 critical CPU usage alert"
fi
exit 0
-----------------------------------------------------------------------------------------------


To run the script to monitor cpu and memory for firefox
./monitor.sh firefox 10 10