Files
Server-MsHossa/SCRIPTS/MONITOR.sh
T
2026-10-22 20:03:58 +02:00

110 lines
3.1 KiB
Bash

#!/bin/bash
# -------------------------------
# Server Status Report Script
# -------------------------------
# Datum/Zeit
DATE=$(date +"%Y-%m-%d %H:%M:%S")
# -------------------------------
# Systemauslastung
# -------------------------------
CPU_LOAD=$(uptime | awk -F 'load average:' '{ print $2 }' | sed 's/^ *//')
MEMORY_USAGE=$(free -h | awk '/Mem:/ {print $3 " used / " $2 " total"}')
DISK_USAGE=$(df -h /)
# -------------------------------
# Docker Status
# -------------------------------
docker_list() {
local name image status ports
local short_image short_status short_ports host_ports
{
printf '%s\x1f%s\x1f%s\x1f%s\n' "NAME" "IMAGE" "STATUS" "PORTS"
docker ps --format '{{.Names}}'$'\x1f''{{.Image}}'$'\x1f''{{.Status}}'$'\x1f''{{.Ports}}' |
while IFS=$'\x1f' read -r name image status ports; do
short_image=$(sed -E 's#^.*/##' <<<"$image")
short_status=$(sed -E \
-e 's/^Up //' \
-e 's/^Exited \(([0-9]+)\)/exit \1,/' \
-e 's/\(healthy\)/ok/' \
-e 's/\(unhealthy\)/!!/' \
-e 's/\(health: starting\)/../' \
-e 's/ ago$//' <<<"$status")
host_ports=$( (grep -oE ':[0-9]+->' <<<"$ports" || true) | (grep -oE '[0-9]+' || true) | sort -un | paste -sd, - )
if [[ -n "$host_ports" ]]; then
short_ports="$host_ports"
elif [[ -n "$ports" ]]; then
short_ports="(intern)"
else
short_ports="-"
fi
printf '%s\x1f%s\x1f%s\x1f%s\n' "$name" "$short_image" "$short_status" "$short_ports"
done
} | awk -F'\x1f' '
{
for (i = 1; i <= NF; i++) {
cell[NR, i] = $i
if (length($i) > width[i]) width[i] = length($i)
}
cols = NF
rows = NR
}
END {
for (r = 1; r <= rows; r++) {
line = ""
for (i = 1; i <= cols; i++) {
line = line cell[r, i]
if (i < cols) {
pad = width[i] - length(cell[r, i]) + 2
for (p = 0; p < pad; p++) line = line " "
}
}
print line
}
}
'
}
if command -v speedtest-cli &> /dev/null
then
SPEEDTEST=$(speedtest-cli --simple 2>/dev/null)
else
SPEEDTEST="speedtest-cli nicht installiert (apt install speedtest-cli)"
fi
# -------------------------------
# Ausgabe auf Konsole
# -------------------------------
DOCKER_STATS=$(docker_list)
echo "Server Status Report"
echo =====================================================
echo
echo "Hostname : $(hostname)"
echo "Datum : $DATE"
echo "CPU Load : $CPU_LOAD"
echo "RAM : $MEMORY_USAGE"
echo
echo "Internet-Geschwindigkeit:"
echo "$SPEEDTEST"
echo
echo "System Festplatten:"
echo "$DISK_USAGE"
echo
echo "Docker Stats:"
echo "$DOCKER_STATS"
echo
echo "====================================================="