-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinux_Utilisation_with_Email_Trigger.sh
58 lines (51 loc) · 1.87 KB
/
Linux_Utilisation_with_Email_Trigger.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/bin/bash
## CPU Utilisation & Memory Utilisation
## DATE: 4th April 2020
## Author: Nandhakumar Madheshwaran
#COLOR CODES
NOCOLOR="\e[39m"
RED="\e[31m"
YELLOW="\e[33m"
GREEN="\e[32m"
#VARIABLE DECLARATIONS
SERVER_HOSTNAME=`uname -n`
ADMIN_EMAIL_ID=root@localhost
DATE=$(date "+%F %H:%M:%S")
CPUUSAGE=$(sar -u 3 3 | grep -i Average | awk '{print $NF}')
MEMUSAGE=$(sar -r 3 3 | grep Average: | awk '{print $4}')
#To convert decimal point Value to integer
SAR_CPU=${CPUUSAGE/.*}
SAR_MEM=${MEMUSAGE/.*}
########## CPU conditions ########
if [ $SAR_CPU -le 10 ]
then
printf "${RED}CRITICAL ${NOCOLOR} CPU Usage : ${SAR_CPU}%% Idle ${DATE}\n" >> /opt/cpu.out
tail -n5 /opt/cpu.out > /tmp/cpuusage.tmp
echo Sending Email....
mail -s "CPU Utilization of ${SERVER_HOSTNAME}" "$ADMIN_EMAIL_ID" < /tmp/cpusage.tmp
elif [ $SAR_CPU -ge 10 ] && [ $SAR_CPU -le 20 ]
then
printf "${YELLOW}WARNING ${NOCOLOR} CPU Usage : ${SAR_CPU}%% Idle ${DATE}\n" >> /opt/cpu.out
tail -n5 /opt/cpu.out > /tmp/cpuusage.tmp
echo Sending Email....
mail -s "CPU Utilization of ${SERVER_HOSTNAME}" "$ADMIN_EMAIL_ID" < /tmp/cpusage.tmp
else
printf "${GREEN}OK ${NOCOLOR} CPU Usage : ${SAR_CPU}%% Idle ${DATE}\n" >> /opt/cpu.out
fi
printf ""
########## memory conditions ########
if [ $SAR_MEM -ge 90 ]
then
printf "${RED}CRITICAL ${NOCOLOR} Mem Usage : ${SAR_MEM}%% Used ${DATE}\n" >> /opt/mem.out
tail -n5 /opt/mem.out > /tmp/memusage.tmp
echo Sending Email....
mail -s "Mem Utilization of ${SERVER_HOSTNAME}" "$ADMIN_EMAIL_ID" < /tmp/memusage.tmp
elif [ $SAR_MEM -ge 80 ] && [ $SAR_MEM -le 90 ]
then
printf "${YELLOW}WARNING ${NOCOLOR} Mem Usage : ${SAR_CPU}%% Used ${DATE}\n" >> /opt/mem.out
tail -n5 /opt/mem.out > /tmp/memusage.tmp
echo Sending Email....
mail -s "Mem Utilization of ${SERVER_HOSTNAME}" "$ADMIN_EMAIL_ID" < /tmp/memusage.tmp
else
printf "${GREEN}OK ${NOCOLOR} Mem Usage : ${SAR_MEM}%% Used ${DATE}\n" >> /opt/mem.out
fi