conf-99-basic_config_debian/root/.profile.d/00_functions.conf

159 lines
5.8 KiB
Plaintext
Raw Normal View History

# Hard Drive usage
function hddtop() {
df -TPh \
| grep '^/dev' \
| grep -v squashfs \
| sort -k 6,6 -n \
| awk \
-v red="$(tput setaf 1)" \
-v yellow="$(tput setaf 3)" \
-v green="$(tput setaf 2)" \
-v reset="$(tput sgr0)" \
'{printf OFS"%s %s %s %s %s %s %s %s %s\n", \
(+$6>90)?red:(+$6>80?yellow:green),$1,$2,$3,$4,$5,$6,$7,reset \
}' \
| column -t
}
function ip4all()
{
echo -e "\033[32mInterfaces and IP\033[00m :"
# Search all interfaces
LISTE_INTERFACES=$(ip -4 a s|grep -P '^[0-9]+'|grep -v '^1:'|awk -F: '{print $2}'|sed 's/@.*//')
# For all interfaces, get all ip's
for INTERFACE in $LISTE_INTERFACES
do
echo -e "\t- \033[33m$INTERFACE\033[00m"
ip -4 a s $INTERFACE |grep inet|awk '{print "\011\011" $2}'
done
}
2019-03-26 08:19:37 +01:00
function template_check_cpu(){
# Nombre de cpu
nb_cpu=$(cat /proc/cpuinfo | grep -c processor)
# Initialisation d'un warning
warning_cpu=$(bc -l <<< "$nb_cpu * 0.75")
# Initialisation d'un critical
critical_cpu=$(bc -l <<< "$nb_cpu * 0.9")
# Affichage du pourcentage CPU utilisée, avec coloration
echo $(awk -v red="$(tput setaf 1)" \
-v yellow="$(tput setaf 3)" \
-v green="$(tput setaf 2)" \
-v reset="$(tput sgr0)" \
'{printf "%s%s%s\n", \
(+$1>'$critical_cpu'?red:(+$1>'$warning_cpu')?yellow:green), $1, reset}' \
/proc/loadavg) \
/ $nb_cpu CPU
}
function template_uptime(){
# Affichage familier du uptime
if lsb_release -d |grep -Ei 'Debian|Red Hat|Ubuntu' > /dev/null;then
echo "$(uptime --pretty)"
# Suse n'embarque pas encore cette option
elif lsb_release -d |grep -i 'SUSE' > /dev/null;then
echo "Depuis le $(who -b |awk '{print $3, "-" ,$4}')"
else
# Tant pis, vous aurez un affichage moche
echo "$(uptime)"
fi
}
function template_check_swappiness(){
# cette fonction nous sera utile pour les deux suivantes : check_memory et check_swap
# vm.swappiness = 0 Linux utilisera le HD en dernière limite pour éviter un manque de RAM.
# vm.swappiness = 60 Valeur par défaut de Linux : à partir de 40% doccupation de Ram, le noyau écrit sur le disque.
# vm.swappiness = 100 tous les accès se font en écriture dans la SWAP.
swapiness=$(cat /proc/sys/vm/swappiness)
(( swapiness = 100 - swapiness))
(( mi_swapiness = swapiness / 2 ))
}
function template_check_memory(){
# Récupération des valeurs de swapiness
template_check_swappiness
# Mémoire disponible = Cached + Buffers + MemFree
memory_avail=$(grep 'MemAvailable' /proc/meminfo | awk '{print $2}')
# Mémoire total
memory_total=$(grep 'MemTotal' /proc/meminfo | awk '{print $2}')
# Calcul en pourcentage de la mémoire utilisée
pourcent_memory_used=$(bc -l <<< " 100 - ($memory_avail / $memory_total * 100)")
# Selon la variable LANG, le printf acceptera les '.' ou les ',' pour un affichage clair et précis
if [[ "$LANG" == 'fr_FR.UTF-8' ]];then
pourcent_memory_used=$(printf %1.1f%% $(echo $pourcent_memory_used|sed 's/\./\,/'))
else
pourcent_memory_used=$(printf %1.1f%% $(echo $pourcent_memory_used))
fi
# Affichage du pourcentage de la mémoire utilisée, avec coloration
echo "$pourcent_memory_used" \
|awk -v red="$(tput setaf 1)" \
-v yellow="$(tput setaf 3)" \
-v green="$(tput setaf 2)" \
-v reset="$(tput sgr0)" \
'{printf "%s%s%s\n", \
(+$1>'$swapiness'?red:(+$1>'$mi_swapiness')?yellow:green), \
$1, \
reset}'
}
function template_check_swap(){
# Calcul de l'utilisation de la SWAP si présente
if [[ $(grep SwapTotal /proc/meminfo | awk '{print $2}') -ne 0 ]];then
template_check_swappiness
# Swap libre
SwapFree=$(grep SwapFree /proc/meminfo | awk '{print $2}')
# Swap totale
SwapTotal=$(grep SwapTotal /proc/meminfo | awk '{print $2}')
# Calcul du pourcentage de la swap utilisée
pourcent_swap_used=$(bc -l <<< "($SwapTotal - $SwapFree) / $SwapTotal * 100")
# Selon la variable LANG, le printf acceptera les '.' ou les ',' pour un affichage clair et précis
if [[ "$LANG" == 'fr_FR.UTF-8' ]];then
pourcent_swap_used=$(printf %1.1f%% $(echo $pourcent_swap_used|sed 's/\./\,/'))
else
pourcent_swap_used=$(printf %1.1f%% $(echo $pourcent_swap_used))
fi
# Affichage du pourcentage de la swap utilisée, avec coloration
echo $pourcent_swap_used \
|awk -v red="$(tput setaf 1)" \
-v yellow="$(tput setaf 3)" \
-v green="$(tput setaf 2)" \
-v reset="$(tput sgr0)" \
'{printf "%s%s%s\n", \
(+$1>'$swapiness'?red:(+$1>'$mi_swapiness')?yellow:green), \
$1, \
reset}'
else
# Si aucune swap présente
echo 'NO SWAP'
fi
}
function check_cron() {
nb_cron=$(wc -l <<< $(grep -Ev 'SHEL|PATH|MAILTO|anacron|run-parts|#|^$' /etc/crontab))
echo $nb_cron
}
# Show top usage memory
function memtop () {
ps -eo size,pid,user,command --sort -size \
| awk '{ hr=$1/1024 ; printf("%13.2f Mb ",hr) } { for ( x=4 ; x <=NF ;x++ ) { printf("%s ",$x) } print ""}' \
| cut -d "" -f2 \
| cut -d "-" -f1 \
| head -20
}