runtipi/scripts/system-info.sh

36 lines
1.6 KiB
Bash
Raw Normal View History

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"
# 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"
exit 0
fi
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}')
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
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
cat "${TEMP_JSON_FILE}" >"${STATE_FOLDER}/system-info.json"
chmod -R a+rwx "${ROOT_FOLDER}/state/system-info.json"