-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreport.sh
More file actions
executable file
·179 lines (161 loc) · 4.33 KB
/
report.sh
File metadata and controls
executable file
·179 lines (161 loc) · 4.33 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/bin/bash
##
#
# Install:
# make this file executable
# copy `config.template` to `config` and edit values
# Usage
# `./xapi-server-report.sh`
# Do not use bourne shell, i.e `sh ./xapi-server-report.sh`, it will likely throw "bad variable errors" due to function syntax
#
##
##
# load configuration
##
source "$(dirname "${0}")/config"
##
# disk space
# https://linux.die.net/man/1/du
# arg $1 source
##
dfcmd() {
local message
message=$(df -B G --output=source,size,used,avail,pcent "$1" | awk '
BEGIN {
print "{"
}
NR==1 {
}
NR!=1{
printf " \"%s\": {\n", $1
printf " \"size\": \"%s\",\n", $2
printf " \"used\": \"%s\",\n", $3
printf " \"available\": \"%s\",\n", $4
printf " \"percent\": \"%s\"\n", $5
printf " }%s\n", block_separator
block_separator = ","
}
END {
print " }"
}
')
echo "$message"
}
##
# folder size and subfolders
# https://linux.die.net/man/1/du
# arg $1 folder
# arg $2 max-depth
##
ducmd() {
local message
message=$(du -h --max-depth="$2" "$1" 2>/dev/null | sort -h | awk '
BEGIN {
separator = " "
print "{"
}
{
printf "%s\"%s\": \"%s\"", separator, $2, $1
separator = ",\n "
}
END {
print "\n }"
}
')
echo "$message"
}
##
# memory usage
# https://linux.die.net/man/1/free
##
freecmd() {
local message
message=$(free -t | awk '
BEGIN {
separator = " "
print "{"
}
END{
printf "%s\"used\": \"%sM\"", separator, $3
separator = ",\n "
printf "%s\"total\": \"%sM\"", separator, $2
printf "%s\"percent\": \"%.2f%%\"", separator, $3*100/$2
print "\n }"
}
')
echo "$message"
}
##
# cpu usage
# https://linux.die.net/man/1/top
##
topcmd() {
local message
message=$(top -bn1 | grep load | awk '
{
print "{"
printf " \"cpu-load\": \"%.2f\"", $(NF-2)
print "\n }"
}
' )
echo "$message"
}
JSON_ISODATE_NOW=$(date --utc +%FT%T.%3NZ)
START_TIME=$(($(date +%s%N)/1000000))
# build json
JSON_DRIVE=$(dfcmd "${CONFIG_SYSTEM_DRIVE}")
JSON_DIR=$(ducmd "${CONFIG_SYSTEM_HOME_DIR}" 1)
JSON_MEMORY=$(freecmd)
JSON_CPU=$(topcmd)
JSON_ISODATE_DONE=$(date --utc +%FT%TZ)
END_TIME=$(($(date +%s%N)/1000000))
ELAPSED_TIME=$((END_TIME - START_TIME))
JSON_EXTENSION="{
\"${CONFIG_XAPI_EXTENSION_IRI}\": {
\"machine\": \"${CONFIG_SYSTEM_MACHINE}\",
\"disk\": ${JSON_DRIVE},
\"home\": ${JSON_DIR},
\"memory\": ${JSON_MEMORY},
\"cpu\": ${JSON_CPU},
\"initialized\": \"${JSON_ISODATE_NOW}\",
\"duration\": \"${ELAPSED_TIME}ms\"
}
}"
JSON_STATEMENT="{
\"actor\":{
\"name\":\"${CONFIG_XAPI_ACTOR_NAME}\",
\"mbox\":\"mailto:${CONFIG_XAPI_ACTOR_EMAIL}\"
},
\"verb\":{
\"id\":\"${CONFIG_XAPI_VERB_IRI}\",
\"display\":{
\"en-US\":\"${CONFIG_XAPI_VERB_NAME}\"
}
},
\"object\":{
\"id\":\"${CONFIG_XAPI_ACTIVITY_IRI}\",
\"definition\":{
\"name\":{
\"en-US\":\"${CONFIG_XAPI_ACTIVITY_NAME}\"
}
}
},
\"result\": {
\"completion\": true,
\"response\": \"Server check finished in ${ELAPSED_TIME} milliseconds.\",
\"extensions\": ${JSON_EXTENSION}
},
\"timestamp\": \"${JSON_ISODATE_DONE}\"
}"
# debug
# echo "$JSON_STATEMENT"
# exit
STATUS=$(curl -s -w " status: %{http_code}, " \
-H "Accept: application/json" \
-H "Content-Type:application/json" \
-H "X-Experience-API-Version:${CONFIG_LRS_XAPI_VERSION}" \
--data "${JSON_STATEMENT}" \
-u "${CONFIG_LRS_AUTH_USER}:${CONFIG_LRS_AUTH_PASS}" \
-X POST "${CONFIG_LRS_ENDPOINT}/statements"; echo "exit code: ${?}")
echo "${STATUS}"
logger "xapi-server-report: ${STATUS}"