forked from michael.heier/citadel-core
56 lines
1.7 KiB
Bash
Executable File
56 lines
1.7 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
|
|
# SPDX-FileCopyrightText: 2021-2022 Citadel and contributors
|
|
#
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
CITADEL_ROOT="$(readlink -f $(dirname "${BASH_SOURCE[0]}")/../..)"
|
|
|
|
memory_total_bytes() {
|
|
free --bytes | awk 'NR==2 {print $2}'
|
|
}
|
|
|
|
memory_used_bytes() {
|
|
free --bytes | awk 'NR==2 {print $3}'
|
|
}
|
|
|
|
get_container_memory_use() {
|
|
docker stats --no-stream --format "{{.MemPerc}}" "$1" | sed "s/%//"
|
|
}
|
|
|
|
get_app_memory_use() {
|
|
local app="${1}"
|
|
|
|
local app_memory=0
|
|
|
|
local app_containers=$("${CITADEL_ROOT}/app/app-manager.py" compose "${app}" ps | awk '{print $1}' | grep -v 'Name\|-----')
|
|
for container in $app_containers; do
|
|
local container_memory=$(get_container_memory_use "${container}")
|
|
app_memory=$(awk "BEGIN {print ${app_memory}+${container_memory}}")
|
|
done
|
|
|
|
# Above values return % of total memory used so we need to convert to bytes
|
|
total=$(memory_total_bytes)
|
|
awk "BEGIN {print int(${total}*${app_memory}/100)}"
|
|
}
|
|
|
|
json="{}"
|
|
|
|
total=$(memory_total_bytes)
|
|
json=$(echo $json | jq --arg total "${total}" '. + {total: $total|tonumber}')
|
|
|
|
used=$(memory_used_bytes)
|
|
json=$(echo $json | jq --arg used "${used}" '. + {used: $used|tonumber}')
|
|
|
|
cumulative_app_memory="0"
|
|
for app in $( "${CITADEL_ROOT}/app/app-manager.py" ls-installed); do
|
|
app_memory=$(get_app_memory_use "${app}")
|
|
cumulative_app_memory=$(($cumulative_app_memory+$app_memory))
|
|
json=$(echo $json | jq --arg app "${app}" --arg app_memory "${app_memory}" '.breakdown |= .+ [{id: $app, used: $app_memory|tonumber}]')
|
|
done
|
|
|
|
citadel=$(($used-$cumulative_app_memory))
|
|
json=$(echo $json | jq --arg citadel "${citadel}" '.breakdown |= .+ [{id: "citadel", used: $citadel|tonumber}]')
|
|
|
|
echo $json | jq '.breakdown |= sort_by(-.used)'
|