Skip to content

Instantly share code, notes, and snippets.

@aejii
Created July 2, 2021 15:27
Show Gist options
  • Select an option

  • Save aejii/707a0296514dcfcf1fc1c4c015b86e95 to your computer and use it in GitHub Desktop.

Select an option

Save aejii/707a0296514dcfcf1fc1c4c015b86e95 to your computer and use it in GitHub Desktop.
information %ram, %cpu + nbr cpu + nom actuel (ubuntu/debian)
#!/bin/bash
display-frame() {
TERMW=$(tput cols)
TERMH=$(tput lines)
BUFFER=""
print-line
print-line "\t\e[1mUser:\e[0m $(whoami)"
print-line "\t\e[1mKernel:\e[0m $(uname -sr)"
print-line "\t\e[1mCPU:\e[0m $(nproc) x$(get-cpu-name)"
#show-ip
print-line
#show-errors
# 7 lines on top, 8 line at bottom
TOJUMP=$((TERMH - 15))
for (( i=0; i < $TOJUMP; i++ )) do
print-line
done
show-disk
print-line
show-ram
print-line
show-cpu
tput cup 0 0
echo -ne "$BUFFER"
}
get-cpu-name() {
grep "model name" /proc/cpuinfo | cut -d: -f2 | head -1
}
show-ip() {
MYIP=$(cat $IPFILE)
if [ -z $MYIP ];then
print-line "\t\e[1mCurrent IP:\e[0m Pinging ifconfig.me ..."
else
print-line "\t\e[1mCurrent IP:\e[0m $MYIP"
fi
}
# $1: file where address will be saved
show-ip-background() {
curl ifconfig.me 2>/dev/null 1>"$1"
}
show-errors() {
ERRORS=$(journalctl -k -p crit -o cat --no-pager)
if [ -z "$ERRORS" ]; then
print-line "\t\e[32mNo critical errors at boot\e[0m"
else
print-line "\t\e[1m\e[31mThere was critical errors at boot !\e[0m"
fi
}
show-disk() {
TOTAL=$(df -k /home | tail -1 | awk '{print $2}')
USED=$( df -k /home | tail -1 | awk '{print $3}')
SEG1=$(($USED * $TERMW / $TOTAL))
SEG2=$(($TERMW - $SEG1))
print-line "\t\e[1m/home:\e[0m $(human-units $USED) / $(human-units $TOTAL)"
draw-bar $SEG1 31
draw-bar $SEG2 37
draw-bar-end
}
show-ram() {
TOTAL=$(grep MemTotal /proc/meminfo | grep -Po '\d+')
FREE=$( grep MemFree /proc/meminfo | grep -Po '\d+')
AVAIL=$(grep MemAvailable /proc/meminfo | grep -Po '\d+')
USED=$(($TOTAL - $AVAIL))
CACHED=$(($AVAIL - $FREE))
SEG1=$(($USED * $TERMW / $TOTAL))
SEG2=$(($CACHED * $TERMW / $TOTAL))
SEG3=$(($TERMW - $SEG1 - $SEG2))
print-line "\t\e[1mRAM:\e[0m $(human-units $USED) / $(human-units $TOTAL)"
draw-bar $SEG1 31
draw-bar $SEG2 90
draw-bar $SEG3 37
draw-bar-end
}
show-cpu() {
# show-cpu-background $CPUFILE &
print-line "\t\e[1mCPU:\e[0m $CPULOAD%"
LOAD=$(echo $CPULOAD | awk '{print int($1)}')
SEG1=$(($LOAD * $TERMW / 100))
SEG2=$(($TERMW - $SEG1))
draw-bar $SEG1 31
draw-bar $SEG2 37
#draw-bar-end
}
# $1: delay (seconds) between the 2 queries
get-cpu-load() {
top -bn2 -d $1 | grep Cpu | tail -1 | sed s/,/./g | awk '{print $2 + $4 + $6}'
}
# $1: input in kB
human-units() {
if [ $1 -lt 1000 ]; then echo "${1}KB"; return; fi
if [ $1 -lt 1000000 ]; then
echo $1 | awk '{printf "%.1fMB", $1 / 1000}'
return;
fi
echo $1 | awk '{printf "%.1fGB", $1 / 1000000}'
}
# $1: length of the bar
# $2: bash color code (ex. 33 for red)
draw-bar() {
# %.0s will print 0 characters of a string
# \u2588 is a "plain block" unicode character
# printf will loop the template string until there are no more args
# seq 1 n will eval to n strings
[[ $1 > 0 ]] && BUFFER+="\e[${2}m$(printf "%.0s\u2588" $(seq 1 $1))\e[0m"
}
draw-bar-end() {
BUFFER+="\n"
}
# $1: content of the line
print-line() {
LINE=$(printf "%.0s " $(seq 1 $TERMW))
BUFFER+="$LINE\r$1\n"
}
### MAIN ###
clear
IPFILE="/tmp/fetch-ip"
touch $IPFILE
show-ip-background $IPFILE &
CPULOAD=$(get-cpu-load 0)
for (( ;; )) do
display-frame
CPULOAD=$(get-cpu-load 1)
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment