citadel-core/scripts/memory-usage
Aaron Dewes 9054194ab7
Citadel 0.1.0 (#85)
Co-authored-by: nolim1t - f6287b82CC84bcbd <nolim1t@users.noreply.github.com>
Co-authored-by: Aaron Dewes <aaron.dewes@protonmail.com>
Co-authored-by: Philipp Walter <philippwalter@pm.me>
Co-authored-by: Lele <emanuele.lele.calo@gmail.com>
2022-10-26 10:28:31 +02:00

75 lines
2.4 KiB
Bash
Executable File

#!/usr/bin/env bash
# SPDX-FileCopyrightText: 2020 Citadel and contributors
#
# SPDX-License-Identifier: GPL-3.0-or-later
CITADEL_ROOT="$(readlink -f $(dirname "${BASH_SOURCE[0]}")/..)"
# Fail if not running as root
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root" 1>&2
exit 1
fi
# Get the memory usage of a docker container by name
function get_memory_usage() {
docker stats --no-stream --format "{{.MemPerc}}" "$1" | sed "s/%//"
}
# Convert a memory usage in MiB to the percentage of the total memory
# With two decimal places
function mem_usage_to_percent() {
local mem_usage="$1"
local total_mem="$(free -m | awk 'NR==2 {print $2}')"
echo "$(awk "BEGIN {printf \"%.1f\", ${mem_usage/,/.} / ${total_mem/,/.} * 100}")"
}
function app_mem_usage() {
# For every container of the app, get the mem usage, save it, and at the end, print the total mem usage of the app
local mem_usage=0
for container in $(get_app_containers "$1"); do
# Use awk to add, it supports floating point numbers
mem_usage=$(awk "BEGIN {printf \"%.2f\", $mem_usage + $(get_memory_usage "$container")}")
done
echo "${1}: $mem_usage%"
}
get_total_used_mem_raw() {
free -m | awk 'NR==2 {print $3}'
}
get_total_used_mem() {
echo "$(mem_usage_to_percent "$(get_total_used_mem_raw)")"
}
# To get the containers of the app, list every container whose name starts with the name of the app
get_app_containers () {
local app_name="$1"
"${CITADEL_ROOT}/scripts/app" compose "${app_name}" ps | awk '{print $1}' | grep -v 'NAME'
}
# Get the memory usage of the whole system, excluding docker containers
get_system_memory_usage() {
local docker_usage_all="$(docker stats --no-stream --format "{{.MemUsage}}" | awk '{sum+=$1} END {print sum}')"
# Now, subtract that from get_total_used_mem_raw, and convert the output to a percentage
local total_usage="$(get_total_used_mem_raw)"
local system_usage="$(awk "BEGIN {printf \"%.1f\", $total_usage - $docker_usage_all}")"
echo "$(mem_usage_to_percent "$system_usage")"
}
main() {
echo "total: $(get_total_used_mem)%"
echo "system: $(get_system_memory_usage)%"
for service in bitcoin lightning tor; do
echo "${service}: $(get_memory_usage "$service")%" &
done
for app in $("${CITADEL_ROOT}/scripts/app" ls-installed); do
app_mem_usage "${app}" &
done
wait
}
echo "Calculating memory usage..."
echo "This may take a while, please wait..."
main | sort --key 2 --numeric-sort --reverse