2022-05-17 10:51:59 +00:00
|
|
|
#!/usr/bin/env bash
|
2022-05-18 18:13:58 +00:00
|
|
|
set -e # Exit immediately if a command exits with a non-zero status.
|
2022-05-17 10:51:59 +00:00
|
|
|
|
2022-10-06 16:28:29 +00:00
|
|
|
ROOT_FOLDER="${PWD}"
|
|
|
|
STATE_FOLDER="${ROOT_FOLDER}/state"
|
|
|
|
|
2022-10-03 06:22:04 +00:00
|
|
|
# if not on linux exit
|
|
|
|
if [[ "$(uname)" != "Linux" ]]; then
|
2022-10-06 16:28:29 +00:00
|
|
|
echo '{"cpu": { "load": 0 },"memory": { "available": 0, "total": 0, "used": 0 },"disk": { "available": 0, "total": 0, "used": 0 }}' >"${STATE_FOLDER}/system-info.json"
|
2022-10-03 06:22:04 +00:00
|
|
|
exit 0
|
|
|
|
fi
|
2022-09-21 07:06:38 +00:00
|
|
|
|
2022-09-21 21:18:30 +00:00
|
|
|
ROOT_FOLDER="$(pwd)"
|
2022-05-17 10:51:59 +00:00
|
|
|
STATE_FOLDER="${ROOT_FOLDER}/state"
|
|
|
|
|
|
|
|
# Available disk space
|
|
|
|
TOTAL_DISK_SPACE_BYTES=$(df -P -B 1 / | tail -n 1 | awk '{print $2}')
|
|
|
|
AVAILABLE_DISK_SPACE_BYTES=$(df -P -B 1 / | tail -n 1 | awk '{print $4}')
|
2022-09-21 07:06:38 +00:00
|
|
|
USED_DISK_SPACE_BYTES=$((TOTAL_DISK_SPACE_BYTES - AVAILABLE_DISK_SPACE_BYTES))
|
2022-05-17 10:51:59 +00:00
|
|
|
|
|
|
|
# CPU info
|
|
|
|
CPU_LOAD_PERCENTAGE=$(top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{print 100 - $1}')
|
|
|
|
|
|
|
|
# Memory info
|
2022-09-21 07:06:38 +00:00
|
|
|
MEM_TOTAL_BYTES=$(($(grep </proc/meminfo MemTotal | awk '{print $2}') * 1024))
|
|
|
|
MEM_AVAILABLE_BYTES=$(($(grep </proc/meminfo MemAvailable | awk '{print $2}') * 1024))
|
|
|
|
MEM_USED_BYTES=$((MEM_TOTAL_BYTES - MEM_AVAILABLE_BYTES))
|
2022-05-17 10:51:59 +00:00
|
|
|
|
|
|
|
# Create temporary json file
|
|
|
|
TEMP_JSON_FILE=$(mktemp)
|
2022-05-18 18:13:58 +00:00
|
|
|
echo '{ "cpu": { "load": '"${CPU_LOAD_PERCENTAGE}"' }, "memory": { "total": '"${MEM_TOTAL_BYTES}"' , "used": '"${MEM_USED_BYTES}"', "available": '"${MEM_AVAILABLE_BYTES}"' }, "disk": { "total": '"${TOTAL_DISK_SPACE_BYTES}"' , "used": '"${USED_DISK_SPACE_BYTES}"', "available": '"${AVAILABLE_DISK_SPACE_BYTES}"' } }' >"${TEMP_JSON_FILE}"
|
2022-05-17 10:51:59 +00:00
|
|
|
|
|
|
|
# Write to state file
|
2022-09-21 07:06:38 +00:00
|
|
|
cat "${TEMP_JSON_FILE}" >"${STATE_FOLDER}/system-info.json"
|
2022-10-11 22:20:51 +00:00
|
|
|
chmod -R a+rwx "${ROOT_FOLDER}/state/system-info.json"
|