From b51100bafb0d4d39ed8096d1b8e3b77a551b4029 Mon Sep 17 00:00:00 2001 From: ami-chuu Date: Fri, 27 Mar 2026 12:27:30 +0200 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D1=82?= =?UTF-8?q?=D1=8C=20server-stats.sh?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server-stats.sh | 111 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 server-stats.sh diff --git a/server-stats.sh b/server-stats.sh new file mode 100644 index 0000000..ec18866 --- /dev/null +++ b/server-stats.sh @@ -0,0 +1,111 @@ +#!/usr/bin/env bash + +set -euo pipefail +if [[ "${TRACE-0}" == "1" ]]; then + set -o xtrace +fi + +# Colors +color_reset="\033[0m" +color_red="\033[0;31m" +color_green="\033[0;32m" +color_yellow="\033[0;33m" +color_bright_black="\033[1;30m" + +# Help message +print_help() { + echo "Usage: ./server-stats.sh [OPTIONS...]" + echo " -h --help Print help" +} + +# Early argument parsing +case "${1:-}" in +"-h" | "--help") + print_help + exit 0 + ;; +"") + # do script below + ;; +*) + echo -e "${color_red}unrecognized option: ${1}${color_reset}" >&2 + echo "" >&2 + print_help >&2 + exit 1 + ;; +esac + +# System stats +cpu_total=$(LC_ALL=C top -bn1 | awk '/Cpu\(s\)/ {printf "%.0f", 100 - $8}') +read -r ram_total ram_used <<<"$(free --mebi | awk '/Mem:/ {print $2, $3}')" +ram_percentage=$(awk "BEGIN {printf \"%d\", ($ram_used/$ram_total)*100}") +read -r disk_total disk_used disk_percentage <<<"$(LC_ALL=C df --human-readable / | awk 'NR==2{print $2, $3, $5}')" +disk_percentage="${disk_percentage%\%}" + +# Tops +top_cpu=$(ps -e --format comm --sort=-%cpu | head -6 | tail -5) +top_ram=$(ps -e --format comm --sort=-%mem | head -6 | tail -5) + +# System info +os_version=$(grep '^PRETTY_NAME=' /etc/os-release | cut -d= -f2 | tr -d '"') +uptime=$(uptime --pretty) +load_avg=$(uptime | awk -F'load average:' '{print $2}' | sed 's/^ //') +users_logged_in=$(who | awk '{print $1}' | sort -u | paste -sd ", ") + +### Failed login attempts +# if [ -f /var/log/auth.log ]; then +# failed_logins=$(grep -ci "Failed password" /var/log/auth.log) +# last_failed=$(grep -i "Failed password" /var/log/auth.log | tail -3 | sed 's/^/ /') +# elif [ -f /var/log/secure ]; then +# failed_logins=$(grep -ci "Failed password" /var/log/secure) +# last_failed=$(grep -i "Failed password" /var/log/secure | tail -3 | sed 's/^/ /') +# else +# failed_logins="N/A" +# last_failed="Log file not found" +# fi + +# Conditional display +if ((cpu_total > 85)); then + cpu_color=$color_red +elif ((cpu_total > 70)); then + cpu_color=$color_yellow +else + cpu_color=$color_green +fi + +if ((ram_percentage > 85)); then + ram_color=$color_red +elif ((ram_percentage > 70)); then + ram_color=$color_yellow +else + ram_color=$color_green +fi + +if ((disk_percentage > 85)); then + disk_color=$color_red +elif ((disk_percentage > 70)); then + disk_color=$color_yellow +else + disk_color=$color_green +fi + +# Output +echo -e "${color_bright_black}╭───────────────── System stats ─────────────────╮${color_reset}" +echo -e " CPU Usage: ${cpu_color}${cpu_total}%${color_reset}" +echo -e " RAM Usage: ${ram_color}${ram_used}/${ram_total} MB (${ram_percentage}%)${color_reset}" +echo -e " Disk Usage: ${disk_color}${disk_used}/${disk_total} (${disk_percentage}%)${color_reset}" +echo -e "${color_bright_black}├───────── Top 5 Processes by CPU usage ─────────┤${color_reset}" +echo "${top_cpu}" | awk '{printf " %d. %s\n", NR, $0}' +echo -e "${color_bright_black}├───────── Top 5 Processes by RAM usage ─────────┤${color_reset}" +echo "${top_ram}" | awk '{printf " %d. %s\n", NR, $0}' +echo -e "${color_bright_black}├────────────── System information ──────────────┤${color_reset}" +echo " OS Version: ${os_version}" +echo " Uptime: ${uptime}" +echo " Load Average: ${load_avg}" +echo " Logged in Users: ${users_logged_in}" +# echo -e "${color_bright_black}├──────────── Failed login attempts ─────────────┤${color_reset}" +# echo " Failed logins: ${failed_logins}" +# echo " Last failed: ${last_failed}" +echo -e "${color_bright_black}╰────────────────────────────────────────────────╯${color_reset}" + +exit 0 \ No newline at end of file