Search
 
SCRIPT & CODE EXAMPLE
 

SHELL

CPU usage alert generator bash script

PATHS="/"
Hname=$(hostname | awk -F. '{print $1}')
criticalpr=98
warningpr=80
logloc="cpuusagehist"

criticalmail="ptclhr@gmail.com"
warmail="ptcladmin@gmail.com"

sudo mkdir -p /var/log/$logloc
logfilename=/var/log/$logloc/$Hname.`date +%u.%I.%m:%y`.log
sudo touch $logfilename

for i in $PATHS
do
        cpuload=`top -b -n 2 -d1 | grep 'Cpu(s)' | tail -n1 | awk '{print $2}' | awk -F . '{print $1}'`

if [ -n $criticalpr -a -n $warningpr ]; then
      if [ "$cpuload" -ge "$warningpr" -a "$cpuload" -le "$criticalpr" ]; then
        
        echo "Warning! -- Cpuload: $cpuload% on Host: $Hname -- `date "+%I:%M:%S:%F"`" | sudo tee -a $logfilename &>/dev/null
        echo "Warning! -- Cpuload: $cpuload% on Host: $Hname" | mail -s "Reaching $cpuload CPUload" $warmail
        exit 1

elif [ "$cpuload" -ge "$criticalpr" ]; then 
        
        echo "Critical! -- Cpuload: $cpuload% on Host: $Hname -- `date "+%I:%M:%S:%F"`" | sudo tee -a $logfilename &>/dev/null
        echo "Critical! -- Cpuload: $cpuload% on Host: $Hname" | mail -s "Reaching $cpuload CPUload" $criticalmail
        exit 2

else

        echo "OK -- Cpuload: $cpuload% on Host: $Hname -- `date "+%I:%M:%S:%F"`" | sudo tee -a $logfilename &>/dev/null
        exit 0
fi
fi
done
Comment

PREVIOUS NEXT
Code Example
Shell :: remove file kali 
Shell :: remove file kali linux shell 
Shell :: git push with topic 
Shell :: scan all venriabilty usign NMAP 
Shell :: isomanager installation script 
Shell :: lst conflit files gt 
Shell :: enable hhtpd to start at boot 
Shell :: print every line that contains a pattern 
Shell :: webgpu chrome linux 
Shell :: virtual machine linux error several modules to compile 
Shell :: git clone with new name 
Shell :: docker run name container 
Shell :: docker linux install 
Shell :: pwd windows 
Shell :: bash scripting tutorial 
Shell :: stop valet auto srart in mac 
Shell :: check git remote address 
Shell :: sudo bash 
Shell :: run command and Exit cts 
Shell :: Revert Unstaged and Staged Changes in git command 
Shell :: tree command in hpc 
Shell :: how to record or log commands on the command-line 
Shell :: white sur icon theme for ubuntu 22.04 
Shell :: pop os headphones stopped working playing out of monitor 
Shell :: exit 0, run container in shell mode 
Shell :: how to Login Database Server as the root user 
Shell :: github cli on rpi 
Shell :: git lab remote 
Shell :: shell sort visualization 
Shell :: powershell or bash 
ADD CONTENT
Topic
Content
Source link
Name
2+1 =