-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpu_process_monitor.sh
44 lines (40 loc) · 1.25 KB
/
cpu_process_monitor.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
#!/bin/bash
SCRIPTPATH="`readlink -f $0`"
BASEDIR="`dirname $SCRIPTPATH`"
LOG_NAME="cpu_process_monitor_""$HOSTNAME"".log"
LOG_BAK_NAME="$LOG_NAME"".bak"
LOG_PID_NAME="pid_""$HOSTNAME"".log"
LOG_PNAME_NAME="pname_""$HOSTNAME"".log"
LOG="$BASEDIR""/logs/""$LOG_NAME"
LOG_BAK="$BASEDIR""/logs/""$LOG_BAK_NAME"
LOG_PID="$BASEDIR""/logs/""$LOG_PID_NAME"
LOG_PNAME="$BASEDIR""/logs/""$LOG_PNAME_NAME"
LOG_LIMIT="10485760"
function get_filesize() {
local size=$(stat -c%s "$1")
echo "$size"
}
function truncate_log() {
`cat "$1" > "$2"; truncate "$1" --size 0`
}
while (true); do
echo "*******start to check at `date +%Y-%m-%d_%H:%M:%S`" >> $LOG
top -bn 1|sed '1,7d'|sort -nrk 9|head -5|awk '{print $9,$10,$1,$2,$NF}' > $LOG_PID
pids=`cat $LOG_PID |awk 'BEGIN {ORS = " "} {print $3}'|sed 's/,*$//g'`
for pid in ${pids[*]}; do
pname=`ps -p $pid -f|sed 1d|awk '{if($0 != "") {print $8,$NF}}'`
if [[ "$pname" == "" ]]; then
echo "NoSuchProcess" >> $LOG_PNAME
else
echo "$pname" >> $LOG_PNAME
fi
done
paste -d " " $LOG_PID $LOG_PNAME >> $LOG
truncate "$LOG_PNAME" --size 0
LOGSIZE=$(get_filesize "$LOG")
if [[ "$LOGSIZE" -gt "$LOG_LIMIT" ]]; then
$(truncate_log "$LOG" "$LOG_BAK")
fi
echo "*******done" >> $LOG
sleep 5s
done