-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnifi_thread_cpu.sh
84 lines (73 loc) · 1.79 KB
/
nifi_thread_cpu.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/bin/sh
# awk '{if($1 !~/^*/) {key[$1]+=$9; cnt[$1]++; k++}} END{for(j in key){printf("%d\t%5.2f\t%d\t%d\n", j,key[j]/cnt[j],cnt[j],k/10)}}'| sort -nk 2
# strace -c -p $pid
# vmstat 2 5
# iostat 2 5
# sar -u 2 5
readonly script_path=$(readlink -f $0)
readonly base_dir=$(dirname $script_path)
# get file name without extension
base_name="${script_path##*/}"
readonly base_name="${base_name%.*}"
readonly cfg="$base_dir/$base_name.cfg"
readonly log_dir="$base_dir/logs"
readonly log_name="$log_dir/$base_name.log"
readonly first_log="$log_name"".0"
readonly log_limit="20971520"
readonly log_size="5"
function is_dir() {
if [ -d "$1" ]; then
echo 0
else
echo -1
fi
}
function is_file() {
if [ -f "$1" ]; then
echo 0
else
echo -1
fi
}
function make_dir() {
if [ ! -d "$1" ]; then
mkdir -pm 755 "$1"
fi
}
function make_file() {
if [ ! -f "$1" ]; then
touch "$1"
chmod 744 "$1"
fi
}
function get_filesize() {
local size=$(stat -c%s "$1")
echo "$size"
}
# roll logs if exceeded limit size
function roll_log() {
local j=$(( $log_size-1 ))
for ((i=$j; i>0; i--)); do
if [ $(is_file "$log_name"".""$(( $i-1 ))") -eq 0 ]; then
cp $log_name"."$(( $i-1 )) $log_name"."$i
fi
done
truncate $log_name".0" --size 0
}
$(make_dir "$log_dir")
$(make_file "$first_log")
while (true); do
pid=$(ps -ef|grep -i nifi-properties|grep -v grep|awk '{print $2}')
if [[ "$pid" == "" ]]; then
sleep 1m
continue
fi
echo "*******start to check $pid at $(date +%Y-%m-%d_%H:%M:%S)" >> $first_log
top -H -p $pid -bn 1| sed '1,7d'| sort -nrk 9,9| head -10 >> $first_log
echo "*******end checking $pid at $(date +%Y-%m-%d_%H:%M:%S)" >> $first_log
size=$(get_filesize "$first_log")
if [[ "$size" -gt "$log_limit" ]]; then
$(roll_log)
fi
sleep 1m
done