-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonitorbackend
executable file
·140 lines (119 loc) · 2.77 KB
/
monitorbackend
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/bin/bash
#https://pasteboard.co/IdOsZrs.png
inside(){
grep -q $2 <<<"${!1}" && return 0 || return 1
}
jsonify_rows(){
echo -ne "["
cat - | sed 's/\"/\\"/g' | sed "s/'/\\'/g" | sed "s/^/\"/;s/$/\"/" | tr "\n" ","
echo -ne "]"
}
j_dictkey(){
echo -ne "\"${1}\":"
cat -
echo -ne ","
}
j_sep(){
cat -
echo -ne ','
}
j_dict(){
{
echo -ne "{"
cat -
echo -ne "}"
} | sed "s/,]/]/g;s/,}/}/g"
}
j_list(){
{
echo -ne "["
cat -
echo -ne "]"
} | sed "s/,]/]/g;s/,}/}/g"
}
j_dict_namerows(){
local rmnames=(${*})
local i=0
local m="[^0-9]"
while read line; do
((i=0))
for x in ${line} ; do
[[ $x =~ $m ]] && x="\"${x}\""
echo -ne "${x}" | j_dictkey ${rmnames[${i}]}
((i++))
done | j_dict | j_sep
done | j_list
}
function clean {
rm $PIPE $CPUUS
kill $CPUWATCHPID
}
trap clean EXIT
cpuload(){
while read cpu usage ; do
echo $usage | j_dictkey $cpu
done <$CPUUS | j_dict
}
rsp(){
local len=$(wc -c <<<${!1})
local headers=(
"Access-Control-Allow-Origin: *"
"Content-Encoding: identity"
"Connection: keep-alive"
"Content-type: ${2}; charset=utf-8"
"Content-Length: ${len}"
)
echo -en "HTTP/1.1 200 OK\r\n"
printf '%s\n' "${headers[@]}"
echo -en "\n"
cat <<<"${!1}"
}
PIPE=$(mktemp -u)
CPUUS=$(mktemp)
mkfifo ${PIPE}
while true ; do
CPUSTATB=(${CPUSTAT[@]})
CPUSTAT=($(
grep cpu < /proc/stat | while read cpu user nice system idle iowait irq softirq steal guest ; do
cpu_active=$((user+system+nice+softirq+steal))
cpu_total=$((cpu_active+idle+iowait))
echo "${cpu}|${cpu_active}|${cpu_total}"
done
))
l=${#CPUSTAT[@]}
for (( i=0; i < l ; i++ )) do
IFS="|" read cpuN activeN totalN <<<"${CPUSTAT[${i}]}"
IFS="|" read cpuB activeB totalB <<<"${CPUSTATB[${i}]}"
[[ $((totalN - totalB)) == 0 ]] ||
echo $cpuN $(( 100 * (activeN-activeB)/(totalN - totalB) ))
done > $CPUUS
sleep 1
done & CPUWATCHPID=$!
while nc -l 8080 <$PIPE | {
header=$( head -n 1 | awk "{print \$2}" | tr "/" "\n" )
echo $header >&2
if [ -z "$header" ] ; then
read -r -d '' indexhtml <./index.html
rsp indexhtml text/html
else
RSP=$({
inside header ps && {
psb=$(ps -eo pid,pcpu,pmem,cmd)
sed "1d;s/[ \t]\+/ /g;s/^ //" <<<"${psb}" | jsonify_rows | j_dictkey ps;
}
inside header stat &&
cat /proc/stat | jsonify_rows | j_dictkey stat;
inside header mem &&
free -m | sed "1d;s/[ \t]\+/ /g;s/^ //;s/.*://" | \
j_dict_namerows all used free shared cache avaiable | j_dictkey mem;
inside header cpuload &&
cpuload | j_dictkey cpuload
inside header fs &&
df -m | grep "^/dev" | j_dict_namerows fs 1mbl used ava usedpr mp | j_dictkey fs
} | j_dict)
[ -z "${RSP}" ] && RSP="Invalid header"
rsp RSP application/json
fi
} > $PIPE || true; do
echo "================================================"
done