-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpu_to_web.sh
executable file
·73 lines (60 loc) · 1.32 KB
/
cpu_to_web.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
#!/bin/bash
if [ ! -f lib/lib.sh ]
then
echo "Error: lib/lib.sh not found!"
echo "make sure you are in the root of the server repo"
exit 1
fi
source lib/lib.sh
function ts_log() {
echo "[$(date '+%m-%d-%Y %T')] $1"
}
ts_log "starting cpu to web script ..."
CPU_LOG=./lib/tmp/log_cpu.txt
function write_cpu_log() {
local pid
local delay
pid="$(pgrep "$CFG_SRV_NAME")"
delay=1
ts_log "logging cpu to file pid=$pid delay=$delay ..."
top -b -d "$delay" -p "$pid" | \
awk -v OFS="," '$1+0>0 {
# print strftime("%Y-%m-%d %H:%M:%S"),$NF,$9,$10;
print strftime("%Y-%m-%d %H:%M:%S") " " $9 "%";
fflush()
}' > "$CPU_LOG"
}
function upload_log() {
# echo "[*] uploading log ..."
if [ -f "$CPU_LOG" ]
then
{
echo '<html>'
echo '<meta http-equiv="Refresh" content="2">'
echo '<code style="white-space: pre;">'
echo "timestamp cpu"
tail "$CPU_LOG" | tac
echo '</code>'
echo '</html>'
} > "$CFG_POST_LOGS_DIR"/cpu.html
local len
len="$(wc -l "$CPU_LOG" | cut -d' ' -f1 )"
if [ "$len" -gt 15 ]
then
tail "$CPU_LOG" > "$CPU_LOG".tmp
cat "$CPU_LOG".tmp > "$CPU_LOG"
rm "$CPU_LOG".tmp
fi
fi
}
function cleanup_cpu_script() {
ts_log "stopping cpu script ..."
pkill -f write_cpu_log
}
trap cleanup_cpu_script EXIT
write_cpu_log &
while true;
do
upload_log
sleep 1
done