-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_client.sh
More file actions
33 lines (25 loc) · 742 Bytes
/
api_client.sh
File metadata and controls
33 lines (25 loc) · 742 Bytes
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
#!/bin/bash
: ${HOST:=127.0.0.1}
: ${PORT:=8001}
get_cpu_utilization() {
local tempfile1=$(mktemp)
local tempfile2=$(mktemp)
grep 'cpu ' /proc/stat > "$tempfile1"
sleep 1
grep 'cpu ' /proc/stat > "$tempfile2"
local cpu_utilization=$(LC_NUMERIC="en_US.UTF-8" awk '
NR==FNR {u1=$2+$4; t1=$2+$4+$5; next}
{u2=$2+$4; t2=$2+$4+$5}
{printf "%.2f\n", ((u2-u1)*100) / (t2-t1)}
' "$tempfile1" "$tempfile2")
rm "$tempfile1" "$tempfile2"
curl -X POST \
http://{$HOST}:{$PORT}/api/cpu/ \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d "{\"data\": \"$cpu_utilization\"}"
}
while true; do
get_cpu_utilization
sleep 9
done