Reputation: 590
This question is a follow up one as per Ed's comments. While running the script as-is provided by Ed, I see the below output:
Current time: 1702993714
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 64 100 64 0 0 179 0 --:--:-- --:--:-- --:--:-- 179
jq: error (at <stdin>:1): Cannot iterate over null (null)
jq: error (at <stdin>:1): Cannot iterate over null (null)
idle:
total:
(standard_in) 2: syntax error
used:
No change in total, can't calculate CPU Usage
Current time: 1702993714
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 64 100 64 0 0 152 0 --:--:-- --:--:-- --:--:-- 152
jq: error (at <stdin>:1): Cannot iterate over null (null)
jq: error (at <stdin>:1): Cannot iterate over null (null)
idle:
total:
(standard_in) 2: syntax error
used:
No change in total, can't calculate CPU Usage
After set -x, based on Ed's comments:
root@poc-vm:~# ./mon-2.sh
+ true
++ date +%s
+ end_time=1703004066
+ printf 'Current time: %s\n' 1703004066
Current time: 1703004066
+ start_time=1703003886
+ printf -v api_endpoint 'https://api.digitalocean.com/v2/monitoring/metrics/droplet/cpu?host_id=%s&start=%s&end=%s' 390488586 1703003886 1703004066
++ curl -X GET -H 'Content-Type: application/json' -H 'Authorization: Bearer token' 'https://api.digitalocean.com/v2/monitoring/metrics/droplet/cpu?host_id=390488586&start=1703003886&end=1703004066'
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 888 0 888 0 0 1807 0 --:--:-- --:--:-- --:--:-- 1808
+ response='{"status":"success","data":{"resultType":"matrix","result":[{"metric":{"host_id":"390488586","mode":"idle"},"values":[[1703003880,"353393.29"],[1703004000,"353512.14"]]},{"metric":{"host_id":"390488586","mode":"iowait"},"values":[[1703003880,"90.82"],[1703004000,"90.87"]]},{"metric":{"host_id":"390488586","mode":"irq"},"values":[[1703003880,"0"],[1703004000,"0"]]},{"metric":{"host_id":"390488586","mode":"nice"},"values":[[1703003880,"55.35"],[1703004000,"55.35"]]},{"metric":{"host_id":"390488586","mode":"softirq"},"values":[[1703003880,"11.5"],[1703004000,"11.5"]]},{"metric":{"host_id":"390488586","mode":"steal"},"values":[[1703003880,"34.5"],[1703004000,"34.51"]]},{"metric":{"host_id":"390488586","mode":"system"},"values":[[1703003880,"348.72"],[1703004000,"349.04"]]},{"metric":{"host_id":"390488586","mode":"user"},"values":[[1703003880,"8530.23"],[1703004000,"8530.77"]]}]}}'
++ jq -r '[.data.result[] | select(.metric.mode == "idle") | .values[0][1]] | add'
++ printf '%s\n' '{"status":"success","data":{"resultType":"matrix","result":[{"metric":{"host_id":"390488586","mode":"idle"},"values":[[1703003880,"353393.29"],[1703004000,"353512.14"]]},{"metric":{"host_id":"390488586","mode":"iowait"},"values":[[1703003880,"90.82"],[1703004000,"90.87"]]},{"metric":{"host_id":"390488586","mode":"irq"},"values":[[1703003880,"0"],[1703004000,"0"]]},{"metric":{"host_id":"390488586","mode":"nice"},"values":[[1703003880,"55.35"],[1703004000,"55.35"]]},{"metric":{"host_id":"390488586","mode":"softirq"},"values":[[1703003880,"11.5"],[1703004000,"11.5"]]},{"metric":{"host_id":"390488586","mode":"steal"},"values":[[1703003880,"34.5"],[1703004000,"34.51"]]},{"metric":{"host_id":"390488586","mode":"system"},"values":[[1703003880,"348.72"],[1703004000,"349.04"]]},{"metric":{"host_id":"390488586","mode":"user"},"values":[[1703003880,"8530.23"],[1703004000,"8530.77"]]}]}}'
+ idle=353393.29
++ jq -r '[.data.result[] | .values[0][1] | tonumber] | add'
++ printf '%s\n' '{"status":"success","data":{"resultType":"matrix","result":[{"metric":{"host_id":"390488586","mode":"idle"},"values":[[1703003880,"353393.29"],[1703004000,"353512.14"]]},{"metric":{"host_id":"390488586","mode":"iowait"},"values":[[1703003880,"90.82"],[1703004000,"90.87"]]},{"metric":{"host_id":"390488586","mode":"irq"},"values":[[1703003880,"0"],[1703004000,"0"]]},{"metric":{"host_id":"390488586","mode":"nice"},"values":[[1703003880,"55.35"],[1703004000,"55.35"]]},{"metric":{"host_id":"390488586","mode":"softirq"},"values":[[1703003880,"11.5"],[1703004000,"11.5"]]},{"metric":{"host_id":"390488586","mode":"steal"},"values":[[1703003880,"34.5"],[1703004000,"34.51"]]},{"metric":{"host_id":"390488586","mode":"system"},"values":[[1703003880,"348.72"],[1703004000,"349.04"]]},{"metric":{"host_id":"390488586","mode":"user"},"values":[[1703003880,"8530.23"],[1703004000,"8530.77"]]}]}}'
+ total=362464.4099999999
+ printf 'idle: %s\n' 353393.29
idle: 353393.29
+ printf 'total: %s\n' 362464.4099999999
total: 362464.4099999999
++ bc
++ printf '%s - %s\n' 362464.4099999999 353393.29
+ used=9071.1199999999
+ printf 'used: %s\n' 9071.1199999999
used: 9071.1199999999
+ (( total == 0 ))
./mon-2.sh: line 34: ((: 362464.4099999999: syntax error: invalid arithmetic operator (error token is ".4099999999")
++ bc
++ printf 'scale=2; (%s / %s) * 100\n' 9071.1199999999 362464.4099999999
+ cpu_usage=2.00
+ printf 'CPU Usage: %s\n' 2.00
CPU Usage: 2.00
This time its 2%, whereas the actual utilization is less than 1 %.
Upvotes: 0
Views: 47