Skip to content

Instantly share code, notes, and snippets.

@benjaminbrassart
Last active July 25, 2021 20:23
Show Gist options
  • Save benjaminbrassart/d99d6f53e76dec2efd14b8c508cd645a to your computer and use it in GitHub Desktop.
Save benjaminbrassart/d99d6f53e76dec2efd14b8c508cd645a to your computer and use it in GitHub Desktop.
born2beroot monitoring script
#!/bin/bash
# **************************************************************************** #
# #
# ::: :::::::: #
# monitoring.sh :+: :+: :+: #
# +:+ +:+ +:+ #
# By: bbrassar <bbrassar@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2021/07/25 12:50:16 by bbrassar #+# #+# #
# Updated: 2021/07/25 13:07:33 by bbrassar ### ########.fr #
# #
# **************************************************************************** #
export TERM='xterm-256color'
red=$(tput setaf 1)
green=$(tput setaf 10)
yellow=$(tput setaf 11)
blue=$(tput setaf 12)
purple=$(tput setaf 13)
cyan=$(tput setaf 14)
reset=$(tput init)
# Make sure every output will be in english
export LC_ALL=C
# Fetch a specific information on the CPU from `lscpu`
cpuinfo () {
CPU=$(lscpu)
# Here, the regex means 'a colon, then any whitespace character, 0 time or more'
# It is better to use this instead of the default delimiter ('[\t ]+')
# because `lscpu` prints its values colon-separated.
#
# For example, doing `lscpu | grep '^CPU(s)' | awk '$2'` will indeed print the
# wanted value. But doing `lscpu | grep '^CPU MHz' | awk '$2'` will print 'MHz'
# instead of the CPU frequency.
grep "^$1" <<< "$CPU" | awk -F ":[ \t]*" '{ print $2 }'
}
perccolor () {
val_rnd=$(printf '%.0f' $1)
if test "$val_rnd" -lt "33"; then
echo -e "${green}"
elif test "$val_rnd" -lt "66"; then
echo -e "${yellow}"
else
echo -e "${red}"
fi
}
# Get system information (kernel, kernel version, OS name, OS version, OS build, architecture)
arch=$(uname -a)
# Get the CPU cores
cpu_cores=$(awk '{ print $1 * $2}' <<< "$(cpuinfo 'Core(s) per socket') $(cpuinfo 'Socket(s)')")
# Get the CPU threads (virtual cores)
cpu_threads=$(awk '{ print $1 * $2 }' <<< "$cpu_cores $(cpuinfo 'Thread(s) per core')")
# Get the CPU load as a percentage
cpu_load=$(top -bn1 | grep '^%Cpu(s)' | cut -c 9- | awk '{ printf "%.1f", $1 + $3 }')
cpu_load_color=$(perccolor $cpu_load)
# Get the memory as kilobytes
# We use kilobytes because megabytes is not accurate enough, and the number of
# bytes will probably be too high for awk (int overflow).
__mem=$(free -k | grep '^Mem:' | awk '{ print $3 " " $2 }')
# Get the used memory as a percentage
mem_perc=$(awk '{ printf "%.1f", $1 / $2 * 100 }' <<< "$__mem")
# Get the used memory in a human-readable format
mem_usage=$(numfmt --from='iec' --from-unit='1024' --to='iec' --field='1-2' <<< "$__mem" | awk '{ print $1 "/" $2 }')
mem_color=$(perccolor $mem_perc)
# Get the disk as kilobytes
# We use kilobytes for the same reasons as RAM.
__disk=$(df -k | grep '^/dev' | grep -v ' /boot*' | awk '{ usg += $3 } { blk += $2 } END { print usg " " blk }')
disk_perc=$(awk '{ printf "%.1f", $1 / $2 * 100 }' <<< "$__disk")
disk_usage=$(numfmt --from='iec' --from-unit='1024' --to='iec' --field='1-2' <<< "$__disk" | awk '{ print $1 "/" $2 }')
disk_color=$(perccolor $disk_perc)
# Get the last boot date
# This is independent of the locale you are using.
last_boot=$(uptime -s)
lvm="no"
lvm_color=${red}
if test -f '/sbin/lvscan'; then
lvm="yes"
lvm_color=${green}
fi
# Get the established TCP connections
# The sed expression means 'delete the first line'
tcp_connections=$(ss -nt state established | sed '1d' | wc -l)
# Get the logged users count
logged_users=$(who | wc -l)
# Get the first IPv4
ipv4=$(hostname -I | awk '{ print $1 }')
# Get the first MAC address
# This should match the first network interface's MAC address (except loopback)
mac=$(ip address | grep 'ether' | head -n 1 | awk '{ print $2 }')
# Get the number of executed sudo commands
# Will be 0 even if the folder does not exist
# See at the bottom of the script why we use `find -iwholename` instead of bash for wildcards
sudo_count=$(find /var/log/sudo/ -iwholename '*/*/*/log' 2> /dev/null | wc -l)
sudo_count_text="command"
# This is just to set an 's' at the end of the string if there are more than 1 command
if test "$sudo_count" -gt "1"; then
sudo_count_text="$sudo_count_text"s
fi
# Print all this beautiful crap to the standard output
cat << EOF
${blue}#Architecture: ${purple}$arch
${blue}#CPU cores: ${cyan}$cpu_cores
${blue}#CPU threads: ${cyan}$cpu_threads
${blue}#Memory usage: ${purple}$mem_usage (${mem_color}$mem_perc%${purple})
${blue}#Disk usage: ${purple}$disk_usage (${disk_color}$disk_perc%${purple})
${blue}#CPU load: ${cpu_load_color}$cpu_load%
${blue}#Last boot: ${purple}$last_boot
${blue}#LVM use: ${lvm_color}$lvm
${blue}#TCP connections: ${cyan}$tcp_connections
${blue}#Logged users: ${cyan}$logged_users
${blue}#Network: ${purple}$ipv4 ${blue}(${purple}$mac${blue})
${blue}#Sudo: ${cyan}$sudo_count ${purple}$sudo_count_text${reset}
EOF
# while pretty ugly, find is noticeably faster than
# bash at parsing wildcards (+30ms for 14102 files)
#
#$ time find /var/log/sudo/ -iwholename '*/*/*/log'
# real 0m0.282s
# user 0m0.118s
# sys 0m0.166s
#
#$ time find /var/log/sudo/*/*/* -name 'log'
# real 0m0.312s
# user 0m0.104s
# sys 0m0.209s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment