-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcpuPerformanceCheck.sh
More file actions
executable file
·63 lines (56 loc) · 2.62 KB
/
cpuPerformanceCheck.sh
File metadata and controls
executable file
·63 lines (56 loc) · 2.62 KB
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
59
60
61
62
63
#!/bin/bash
#PU=$(echo $t|awk '{split($1,a,"."); print a[1]}')
#today=`date +%Y-%m-%d.%H:%M:%S` # or whatever pattern you desire
today=$(date "+%m%d%H%s")"-"$(date +"%Z")"-"$(hostname) #adding date,timezone,hostname to output files.
log=performance.out
function usage()
{
clear
echo
echo "#######################################################################################################"
echo "# Nothing special required for installation ##"
echo "# This is a watch script that runs all of the time like a daemon ##"
echo "# and only wakes when cpu utiliztion exceeds the max threshold in x ##"
echo "# When high cpu utiliazion occurs script wakes and starts collecting info from ##"
echo "# top, free, and ifconfig (throughput) which is written to performance.out for later analysis. ##"
echo "# ##"
echo "#######################################################################################################"
exit
}
function perf()
while true; do
t=$(top -b -n1|grep -A10 PID|grep -v %CPU|awk '{ sum += $9 } END { sum > $thresh;print sum}')
MEM=$(echo $t|awk '{split($1,a,"."); print a[1]}')
while [[ $MEM -gt $thresh ]]
do
top -b -n1|grep -A10 PID|grep -v %CPU|awk '{ sum += $9 } END { sum > $thresh;system("iostat -dxmt"); system("free -m"); system("top -b -n1")}'>>$today.$log
t=$(top -b -n1|grep -A10 PID|grep -v %CPU|awk '{ sum += $9 } END { sum > $thresh;print sum}')
MEM=$(echo $t|awk '{split($1,a,"."); print a[1]}')
done
done
function killscript()
{
echo killing process
#kill -9 $(ps -ef|grep memPerformance|grep -v grep|awk '{print $2}'|sort|head -n1)
kill -9 $(cat /tmp/cpuPerformance.pid)
# kill -9 $(ps -ef |grep awk |grep -v grep |awk '{print $2}'|head -n1)
}
function runscript()
{
echo Max memory threshold in "%" "(h|help|? provides usage)":&read thresh
if [[ $thresh = "?" || $thresh == "help" || $thresh == "h" ]]; then
usage
fi
perf &
echo "$!" > /tmp/cpuPerformance.pid
}
#clear
echo " 'execute' to run CPU checker, 'stop' to stop run existing process:" & read answer
if [[ "$answer" == "execute" ]]; then
runscript
elif [[ "$answer" == "stop" ]]; then
killscript
elif [[ "$answer" != "execute" || "$answer" != "stop" || -z "$answer" ]]; then
echo You must select execute or stop
fi
#Selection input by user