-
Notifications
You must be signed in to change notification settings - Fork 283
/
1_cpu-hog
executable file
·36 lines (32 loc) · 1.37 KB
/
1_cpu-hog
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
#!/bin/bash
# Script that monitors the top-active process. The script sends an email to the user root if
# utilization of the top active process goed beyond 80%. Of course, this script can be tuned to
# do anything else in such a case.
#
# Start the script, and it will run forever.
while true
do
# Check every 60 seconds if we have a process causing high CPU load
sleep 60
USAGE=`ps -eo pcpu,pid -o comm= | sort -k1 -n -r | head -1 | awk '{ print $1 } '`
USAGE=${USAGE%.*}
PID=`ps -eo pcpu,pid -o comm= | sort -k1 -n -r | head -1 | awk '{print $2 }'`
PNAME=`ps -eo pcpu,pid -o comm= | sort -k1 -n -r | head -1 | awk '{print $3 }'`
# Only if we have a high CPU load on one process, run a check within 7 seconds
# In this check, we should monitor if the process is still that active
# If that's the case, root gets a message
if [ $USAGE -gt 80 ]
then
USAGE1=$USAGE
PID1=$PID
PNAME1=$PNAME
sleep 7
USAGE2=`ps -eo pcpu,pid -o comm= | sort -k1 -n -r | head -1 | awk '{ print $1 } '`
USAGE2=${USAGE2%.*}
PID2=`ps -eo pcpu,pid -o comm= | sort -k1 -n -r | head -1 | awk '{print $2 }'`
PNAME2=`ps -eo pcpu,pid -o comm= | sort -k1 -n -r | head -1 | awk '{print $3 }'`
# Now we have variables with the old process information and with the
# new information
[ $USAGE2 -gt 80 ] && [ $PID1 = $PID2 ] && mail -s "CPU load of $PNAME is above 80%" [email protected] < .
fi
done