Skip to content

Instantly share code, notes, and snippets.

@Foo-Manroot
Last active August 31, 2019 14:40
Show Gist options
  • Save Foo-Manroot/a0a1c1fe494df4c5cbf211ef2abfe212 to your computer and use it in GitHub Desktop.
Save Foo-Manroot/a0a1c1fe494df4c5cbf211ef2abfe212 to your computer and use it in GitHub Desktop.
Little script to gather current system info with only standard dependencies. It's meant as a replace for the landscape-sysinfo on the MOTD on non-Ubuntu systems
#!/bin/sh
####
# COLOURS
# for i in {1..255}; do tput setaf $i ; echo "$i: The quick fox jumps over the lazy dog"; done
####
CLR_RED="$(tput setaf 9)"
CLR_BLUE="$(tput setaf 213)"
CLR_GRN="$(tput setaf 46)"
CLR_YLLW="$(tput setaf 11)"
CLR_RESET="$(tput sgr0)"
# Dependencies
DEPS="grep uptime tr bc xargs lsblk awk tput ifconfig"
# Checks that all needed programs are installed (they should be available on any system,
# even on minimal distros
for d in $DEPS
do
if ! command -v "$d" >/dev/null 2>&1
then
# Prints the message in red
tput setaf 196
printf "Dependency not available: %s\n" "$d"
tput sgr0
exit 1
fi
done
#---------------------
# Current load of the CPU
sysload="$(
uptime \
| grep -Po "load average:\s?[0-9]+.[0-9]{2}" \
| tr -d \ \
| tr , . \
| cut -d: -f 2
)"
# Time that the system has been running
uptime="$(uptime -p)"
# Number of available processors
# /proc/cpuinfo shows information about every available CPU; but it starts with indes 0,
# so the total count is last_index + 1
num_cpus="$(
grep -P "^processor[ \t:]*[0-9]+$" /proc/cpuinfo \
| tr -d '\t ' \
| cut -d: -f 2 \
| tail -n 1 \
| xargs -n 1 printf "%s + 1\n" \
| bc -l
)"
#---------------------
# `w` shows each logged in user per line, with two extra lines for the headers. Hence the
# substraction at the end to get the current number of users
logged_users="$(w -is | wc -l | xargs -n 1 printf "%s - 2\n" | bc -l )"
#---------------------
# Current number of processes
num_proc="$(ps --no-headers -e | wc -l)"
# Available memory
mem_avail="$(free -th | tail -n1 | awk '{ print $2 }')"
# Percentage of used memory
mem_usage="$(free | grep "^Mem:" | awk '{ printf "%05.2f",($3 / $2) * 100 }')"
swap_usage="$(free | grep "^Swap:" | awk '{
if ($2 > 0)
{
printf "%05.2f",($3 / $2) * 100;
}
else
{
printf "00.00"
}
}'
)"
#---------------------
####
## Prints all the information about the system's network interfaces
####
print_ifaces () {
ifaces="$(ls /sys/class/net/)"
for i in $ifaces
do
# Some versions of ifconfig say 'inet addr:' and other say 'inet '
inetaddr="$(ifconfig "$i" \
| grep -Po "inet (addr:)?([0-9]{1,3}[.]?){4}" \
| sed -re 's/inet (addr:)?//'
)"
inet6addr="$(ifconfig "$i" \
| grep -Po "inet6( addr:)?[ ][^ ]+" \
| sed -re 's/inet6( addr:)? //'
)"
# Discards the loopback interface and inactive interfaces
if [ -n "$inetaddr" ] && [ "$inetaddr" != "127.0.0.1" ]
then
printf " %s%s%s:\n" "$CLR_BLUE" "$i" "$CLR_RESET"
printf " IPv4: %s%s%s\n" \
"$CLR_YLLW" "$inetaddr" "$CLR_RESET"
if [ -n "$inet6addr" ]
then
printf " IPv6: %s%s%s\n" \
"$CLR_YLLW" "$inet6addr" "$CLR_RESET"
fi
fi
done
}
####
## Prints all the info about the partitions and disks
####
print_disks () {
for disk in $(lsblk --nodeps --noheadings | awk '{ print $1 }')
do
# Physical disk info
printf "\n Physical disk %s%s%s:" "$CLR_GRN" "$disk" "$CLR_RESET"
printf "\n%s\n\n" "$(
lsblk --nodeps \
--output LABEL,RO,MODEL,SIZE,ROTA,TYPE,TRAN,REV,VENDOR \
/dev/"$disk" \
| awk '{ printf " %s\n",$0 }'
)"
# A single disk may have multiple partitions, LVM mappings and stuff
for mountpoint in $(lsblk --output MOUNTPOINT --noheadings "/dev/$disk")
do
if [ -n "$mountpoint" ] && [ -d "$mountpoint" ]
then
left="$(df -h "$mountpoint" --output=avail \
| tail -n 1
)"
usage="$(df -h "$mountpoint" --output=pcent,size \
| tail -n 1 \
| xargs -n 2 printf "%s of %s"
)"
printf " Usage of %s%-30s%s: %s%15s%s (%s%s%s left)\n" \
"$CLR_BLUE" "$mountpoint" "$CLR_RESET" \
"$CLR_GRN" "$usage" "$CLR_RESET" \
"$CLR_YLLW" "$left" "$CLR_RESET"
fi
done
done
}
# String with the usage of the root directory
root_usage_str="$(
df -h / --output=pcent,size \
| tail -n 1 \
| xargs -n 2 printf "%s of %s\n"
)"
#lsblk_partitions="$(lsblk \
# --inverse --nodeps \
# --output NAME,MOUNTPOINT,LABEL,RO,MODEL,SIZE,ROTA,TYPE,TRAN,REV,VENDOR
#)"
#lsblk_disks="$(lsblk \
# --nodeps \
# --output NAME,LABEL,RO,MODEL,SIZE,ROTA,TYPE,TRAN,REV,VENDOR
#)"
# Prints the temperature of all the ACPI components.
# All the information regarding the temperature of the devices is available
# under /sys/class/thermal
print_temps () {
cd /sys/class/thermal || return
printf "\n %sTemperatures%s:\n" "$CLR_GRN" "$CLR_RESET"
for zone in thermal_zone*
do
# The temperature is stored in miliCelsius
temp="$(awk '{ printf "%05.2f",($1 / 1000) }' "$zone/temp")"
# There may be multiple trim points, but usually is only the last when
# 'trip_point_type = critical', and that is the max temperature that can
# be reached
max_temp="$(cat "$zone"/trip_point_*_temp 2>/dev/null \
| sort -rn \
| head -n 1 \
| awk '{ printf "%05.2f",($1 / 1000) }'
)"
printf " Thermal %s: " "$(printf "%s" "$zone" \
| sed 's/[^0-9]//g'
)"
if [ -n "$max_temp" ] \
&& \
[ "$(printf "%s > (%s * 0.8)\n" "$temp" "$max_temp" | bc -l)" = "1" ]
then
printf "%s%s%s ºC" "$CLR_RED" "$temp" "$CLR_RESET"
else
printf "%s ºC" "$temp"
fi
if [ -n "$max_temp" ]
then
printf " (max: %6.2f ºC)\n" "$max_temp"
else
printf " (max: N/A)\n"
fi
done
}
#----------------------------------------------------------------------------------------
#----------------------------------------------------------------------------------------
#----------------------------------------------------------------------------------------
# Prints all the information
#----------------------------------------------------------------------------------------
#----------------------------------------------------------------------------------------
#----------------------------------------------------------------------------------------
# ---- First section: CPU stats ----
printf " %sCPU STATS%s\n" "$CLR_GRN" "$CLR_RESET"
# First line
printf " %sSystem load%s: " "$CLR_BLUE" "$CLR_RESET"
# Adds colour for an overloaded system
if [ "$(printf "%s > 1\n" "$sysload" | bc -l)" = "1" ]
then
printf "%s" "$CLR_RED"
elif [ "$(printf "%s > 0.8\n" "$sysload" | bc -l)" = "1" ]
then
printf "%s" "$CLR_YLLW"
fi
printf "%-15s" "$sysload"
printf " %sProcesses%s: %-15s\n" "$CLR_BLUE" "$CLR_RESET" "$num_proc"
# Second line
printf " %sN. of CPUs%s: %-15s" "$CLR_BLUE" "$CLR_RESET" "$num_cpus"
printf " %sUsers logged in%s: %-15s\n" "$CLR_BLUE" "$CLR_RESET" "$logged_users"
# Third line
printf " %sUptime%s: %-15s\n" "$CLR_BLUE" "$CLR_RESET" "$uptime"
# Temperatures
print_temps
#--------------------------------------
printf "%s%60s\n" "$CLR_RESET" " " | tr " " "-"
#--------------------------------------
# ---- Second section: Memory stats ----
printf " %sMEMORY STATS%s\n" "$CLR_GRN" "$CLR_RESET"
# First line
printf " %sMemory + Swap%s: %-15s" "$CLR_BLUE" "$CLR_RESET" "$mem_avail"
printf " %sMemory usage%s: " "$CLR_BLUE" "$CLR_RESET"
# Adds colour for an overloaded system
if [ "$(printf "%s > 80\n" "$mem_usage" | bc -l)" = "1" ]
then
printf "%s" "$CLR_RED"
elif [ "$(printf "%s < 50\n" "$mem_usage" | bc -l)" = "1" ]
then
# Infrautilised
printf "%s" "$CLR_YLLW"
fi
printf "%-15s\n" "$mem_usage %"
# Second line
printf " "
printf " %sSwap usage%s: " "$CLR_BLUE" "$CLR_RESET"
# Adds colour for an overloaded system (any swap usage is a system overloaded)
if [ "$(printf "%s > 0\n" "$swap_usage" | bc -l)" = "1" ]
then
printf "%s" "$CLR_RED"
elif [ "$(printf "%s == 0\n" "$swap_usage" | bc -l)" = "1" ]
then
printf "%s" "$CLR_GRN"
fi
printf "%-15s\n" "$swap_usage %"
#--------------------------------------
printf "%s%60s\n" "$CLR_RESET" " " | tr " " "-"
#--------------------------------------
# ---- Third section: Disks ----
printf " %sDISKS%s\n" "$CLR_GRN" "$CLR_RESET"
# First line
printf " %sUsage of /%s: %-15s\n" "$CLR_BLUE" "$CLR_RESET" "$root_usage_str"
# Disks
print_disks
#--------------------------------------
printf "%s%60s\n" "$CLR_RESET" " " | tr " " "-"
#--------------------------------------
# ---- Fourth section: Networking ----
printf " %sNETWORKING%s\n" "$CLR_GRN" "$CLR_RESET"
print_ifaces
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment