-
Notifications
You must be signed in to change notification settings - Fork 0
/
uptime.cgi
executable file
·94 lines (71 loc) · 1.95 KB
/
uptime.cgi
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
#!/bin/bash
# New CGI that handles stats as JSON
# Author: Thran Authored: 06/12/19
#==============================================================================#
# BEGIN COMMON FUNCTIONS
#==============================================================================#
function writeJSONHeader {
echo -e 'Content-type: application/json\n'
}
#JSON TAG
function jT {
echo -n "\"$1\":"
}
#JSON FIELD
function jF {
# JSON expects a comma after each field until the final
if [ "$2" = "f" ]; then
echo "\"$1\""
else
echo "\"$1\","
fi
}
#==============================================================================#
# END COMMON FUNCTIONS
#==============================================================================#
# CPU Usage in tenths of a percent
function getNginxCPU {
ps --no-headers -C nginx -o cp | awk '{cp += $1} END {print cp}'
}
# PS returns perc of CPU time / time program has been running.
function getNginxCPUPerc {
ps --no-headers -C nginx -o %cpu | awk '{cpu += $1} END {print cpu}'
}
function getUptime {
uptime -p | sed 's/up //' | tr -d '\n'
}
function getTotalMem {
free -m | grep Mem | awk '{ print $3 }' | tr -d '\n'
}
function getLoadAvg {
uptime | awk -F 'load average:' '{ print $2 }' | tr -d ',' | tr -d '\n'
}
# Resident set size; total of process in memory
function getNginxRSS {
ps --no-headers -C nginx -o rss | awk '{rss += $1} END {print rss}'
}
function getUname {
uname
}
#==============================================================================#
# Do JSON composition
#==============================================================================#
writeJSONHeader
echo "{"
jT "hostname"
jF "$(hostname)"
jT "uptime"
jF "$(getUptime)"
jT "totalMem"
jF "$(getTotalMem)"
jT "loadAvg"
jF "$(getLoadAvg)"
jT "uname"
jF "$(getUname)"
jT "nginxCPU"
jF "$(getNginxCPU)"
jT "nginxCPUPerc"
jF "$(getNginxCPUPerc)"
jT "nginxRssKb"
jF "$(getNginxRSS)" "f"
echo "}"