Skip to content

Instantly share code, notes, and snippets.

@jhyland87
Created May 13, 2016 20:25
Show Gist options
  • Save jhyland87/caac727cf604d38cd8cfa806ca24be01 to your computer and use it in GitHub Desktop.
Save jhyland87/caac727cf604d38cd8cfa806ca24be01 to your computer and use it in GitHub Desktop.
System Summary Script
#!/bin/bash
# Summary: Script to quickly show a summary of the servers stats, showing any possible issues (mainly regarding to resource utilization)
# Author: Justin Hyland
# Created: 05/11/16
# Screenshot: http://d.pr/i/2nTm
# Settings and thresholds
# These are set at odd limits just for testing
disk_thresh=10 # Percent
cpu_thresh='0.02'
mem_thresh=10 # Percent
# Font Weights
fg_bold="\033[1m"
fg_normal="\e[0m"
# Font Colors
fg_light_red="\e[1;31m"
fg_dark_red="\e[0;31m"
fg_light_green="\e[1;32m"
fg_dark_green="\e[0;32m"
fg_light_blue="\e[1;34m"
fg_dark_blue="\e[0;34m"
fg_light_yellow="\e[1;33m"
fg_dark_yellow="\e[0;33m"
fg_light_cyan="\e[1;36m"
fg_dark_cyan="\e[0;36m"
fg_light_pink="\e[1;35m"
fg_dark_pink="\e[0;35m"
fg_light_grey="\e[1;37m"
fg_dark_grey="\e[0;37m"
# Color to highlight any text
fg_highlight=$fg_light_yellow
fn_attention=$fn_light_grey
# Background colors
bg_black="\e[40m"
bg_red="\e[41m"
bg_green="\e[42m"
bg_yellow="\e[43m"
bg_blue="\e[44m"
bg_pink="\e[45m"
bg_cyan="\e[46m"
bg_grey="\e[47m"
notice_prefix="${fg_light_cyan}==>${fg_normal}"
# Base ps command
ps_cmd="ps -eo pid,ppid,time,user,tty,%cpu,%mem,vsize,command"
# Process limit to show when summarizing memory or cpu usage (if needed)
ps_limit=5
ipv4_cfg=$(nmcli con show eno16777984 | grep -E '^ipv4\.')
# ------------------------------
# Server Details
echo -e "\n${notice_prefix} ${fg_light_blue}SERVER DETAILS${fg_normal}"
# Grab some details about the server based off of the hostname
hostname_env=$(hostname -s | awk -F- '{print $2}')
hostname_farm=$(hostname -s | awk -F- '{print $3}' | sed -r 's/^[a-z]//')
# Determine the environment based on the hostname
case $hostname_env in
dev)
svr_env="Development"
env_col=$fg_light_green
;;
stg)
svr_env="Staging"
env_col=$fg_light_yellow
;;
prd)
svr_env="Production"
env_col=$fg_light_red
;;
*)
svr_env="Unknown"
env_col=$fg_light_blue
;;
esac
function details_align { echo "\t%-19s: ${1:-$fg_bold}%-20s${fg_normal}\n"; }
#uptime_cmd=$(uptime | grep -o -P '(?<=up\s)([0-9]+)\sdays,\s([0-9]+):([0-9]+)(?=\,)')
uptime_cmd=$(uptime | grep -o -P '(?<=up\s)([0-9]+)\sdays?(,\s[0-9]+\smin)?.*(?=,\s+([0-9]+)\suser?)')
printf "$(details_align)" "Hostname" $(hostname -f)
printf "$(details_align)" "OS" "$(cat /etc/redhat-release)"
printf "$(details_align)" "IPv4 Address" $(echo "$ipv4_cfg" | grep -E '^ipv4\.address' | awk '{print $2}')
printf "$(details_align $env_col)" "Environment" "$svr_env"
[[ $(echo $hostname_farm | wc -m) -eq 3 ]] && printf "$(details_align)" "Farm" $hostname_farm
printf "$(details_align)" "Uptime" "$uptime_cmd"
printf "$(details_align)" "CPU Cores" "$(cat /proc/cpuinfo | grep -E '^processor' | wc -l)"
printf "$(details_align)" "Memory" "$(cat /proc/meminfo | grep -E '^MemTotal' | awk '{print $2,$3}')"
# ------------------------------
# Session Utilization
sessions_all=$(who)
sessions_others=$(echo "$sessions_all" | awk "\$2 != $(tty | sed 's%/dev/%%')")
sessions_total=$(echo $sessions_others | wc -l)
if [[ $sessions_total -gt 0 ]]
then
echo -e "\n${notice_prefix} ${fg_light_blue}ACTIVE SESSIONS${fg_normal}"
echo -e "There $([[ $sessions_total -eq 1 ]] && echo "is ${fg_highlight}1${fg_normal} other active session" || echo "are ${fg_highlight}${sessions_total}${fg_normal} other active sessions" ) on this server"
who | sed 's/^/\t/'
else
echo "Only you are active"
fi
# ------------------------------
# Disk space utilization warning
disk_usage=$(/bin/df -T)
disk_exceeding=$(echo "$disk_usage" | sed 1d | tr -d '%' | awk "\$6 >= $disk_thresh" | wc -l)
if [[ $disk_exceeding -gt 0 ]]
then
echo -e "\n${notice_prefix} ${fg_light_red}DISK UTILIZATION WARNING${fg_normal}"
echo -e "There $([[ $disk_exceeding -eq 1 ]] && echo "is ${fg_highlight}${disk_exceeding}${fg_normal} disk" || echo "are ${fg_highlight}${disk_exceeding}${fg_normal} disks") exceeding the space utilization threshold of ${fg_bold}${disk_thresh}%${fg_normal}"
echo "$disk_usage" | sed 's/^/\t/'
fi
# ------------------------------
# CPU Utilization
cpu_usage=$(uptime | grep -o -P '(?<=load average: )([0-9].[0-9]{2}), ([0-9].[0-9]{2}), ([0-9].[0-9]{2})' | tr -d ' ')
cpu_highest=$(echo "$cpu_usage" | tr ',' '\n' | sort | tail -n 1)
# Get the separate load averages
cpu_5=$(echo $cpu_usage | awk -F, '{print $1}')
cpu_10=$(echo $cpu_usage | awk -F, '{print $2}')
cpu_15=$(echo $cpu_usage | awk -F, '{print $3}')
# If any of the CPU load average intervals are over the threshold, then show the CPU details
if (( $(echo "$cpu_highest > $cpu_thresh" | bc -l) ))
then
# Highlight the load average intervals that are high
(( $(echo "$cpu_5 > $cpu_thresh" | bc -l) )) && cpu_5_col=$fg_highlight
(( $(echo "$cpu_10 > $cpu_thresh" | bc -l) )) && cpu_10_col=$fg_highlight
(( $(echo "$cpu_15 > $cpu_thresh" | bc -l) )) && cpu_15_col=$fg_highlight
echo -e "\n${notice_prefix} ${fg_light_red}CPU ITILIZATION WARNING${fg_normal}"
echo -e "The CPU utilization is exceeding the utilization threshold of ${fg_bold}${cpu_thresh}${fg_normal}"
function cpu_load_align { echo "\t%s${1:-$fg_bold}%5s${fg_normal}\n"; }
printf "$(cpu_load_align $cpu_5_col)" "5 min load avg:" $cpu_5
printf "$(cpu_load_align $cpu_10_col)" "10 min load avg:" $cpu_10
printf "$(cpu_load_align $cpu_15_col)" "15 min load avg:" $cpu_15
echo -e "\n${fg_bold}Top ${ps_limit} CPU intensive processes${fg_normal}:"
$ps_cmd --sort -%cpu | head -n $((ps_limit+1)) | sed -e $'s/ *[^ ]* /\033[0;33m&\033[0m/6'
fi
# ------------------------------
# Memory Utiliation
mem_output=$(free | grep -E '^Mem')
mem_usage=$(($(echo "$mem_output" | awk '{print $3}')*100/$(echo "$mem_output" | awk '{print $2}')))
#mem_usage=$(echo "$mem_output" | grep -E '^Mem' | awk '{print $3}')
if [[ $mem_usage -gt $mem_thresh ]]
then
if [[ $mem_usage -gt 90 ]]
then
mem_color=$fg_light_red
else
mem_color=$fg_light_yellow
fi
echo -e "\n${notice_prefix} ${fg_light_red}MEMORY ITILIZATION WARNING${fg_normal}"
echo -e "The memory utilization is at ${mem_color}${mem_usage}%${fg_normal}, which exceeds the utilization threshold of ${fg_bold}${mem_thresh}%${fg_normal}"
free -m | sed 's/^/\t/'
echo -e "\n${fg_bold}Top ${ps_limit} memory intensive processes${fg_normal}:"
$ps_cmd --sort -%mem | head -n $((ps_limit+1)) | sed -e $'s/ *[^ ]* /\033[0;33m&\033[0m/7' | sed 's/^/\t/'
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment