-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpu_json.sh
107 lines (90 loc) · 2.52 KB
/
cpu_json.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/bin/bash
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"
# 5M * 2
readonly log_limit="5242880"
readonly log_size="2"
readonly log_info=" Info "
readonly log_error=" Error "
readonly log_warn=" Warning "
readonly log_debug=" Debug "
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
mv $log_name"."$(( $i-1 )) $log_name"."$i
fi
done
}
function main() {
#$(make_dir "$log_dir")
#$(make_file "$first_log")
local result=$(top -bn 1|sed '1,7d'|sort -nrk 9|head -3|awk 'BEGIN{ORS="__";} {print$9,$10,$1,$2}')
local pids=$(echo $result | awk 'BEGIN{RS="__";ORS=" "} {print $3}')
local i=1
local proc_name=""
local proc_cpu=-1
local timestamp="$(date +"%F %T:%3N")"
local cpu_total=$(sar -u 1 1 | awk '{if($1 ~/Average/) print substr($NF,1,length($NF)-3)}')
cpu_total=$((100 - cpu_total))
local cpu_json="{\"CPU\":[{\"name\":\"total\",\"id\":\"-1\",\"cpu\":\"$cpu_total%\",\"timestamp\":\"$timestamp\"},"
for pid in ${pids[*]}; do
if [[ "$pid" != "" ]]; then
proc_cpu=$(echo $result | awk -v cnt="$i" 'BEGIN{RS="__";ORS=""} NR==cnt{print $1}')
proc_name=$(ps -p $pid -f|sed 1d|awk '{if($0 != "") {print $8"--"$NF}}')
if [[ "$proc_name" == "" ]]; then
proc_name="Null"
fi
cpu_json=$cpu_json"{\"name\":\"$proc_name\",\"id\":\"$pid\",\"cpu\":\"$proc_cpu%\",\"timestamp\":\"$timestamp\"},"
fi
i=$((i+1))
done
cpu_json=$(echo $cpu_json | sed 's\,$\\g')"]}"
#printf "%s %-10s %-20s\t%s\n" "$(date +"%F %T:%3N")" $log_info ${FUNCNAME[0]} "$cpu_json" >> $first_log
#local size=$(get_filesize "$first_log")
#if [[ "$size" -gt "$log_limit" ]]; then
# $(roll_log)
#fi
echo $cpu_json
}
result=$(main)
if [[ "$result" != "" ]]; then
echo $result
fi