#!/bin/bash progname=dwm_bar version=0.2 author=mt declare -i sec declare -i slow_cycle declare -i res declare -i nc declare -i batt_charge declare -i batt_full declare -i ram_occ declare -i ram_free declare -i ram_tot declare -i nrate stdout=false loop=true spaces=false sec=5 nc=3 interface=eth0 root=true xset=`whereis -b xsetroot | awk '{print $2}'` printhelp() { echo "$progname [Options]" echo "Options:" echo " -h|--help: print this message and exit" echo " -v|--version: print version number and exit" echo " -o: print to standard output" echo " -c: print constant length numbers (with spaces)" echo " -1: only one run" echo " -i : number of seconds between runs" return } # funzione per il conteggio dei byte ricevuti/trasmessi byte_count(){ while read line; do if [[ $line == ${interface}:* ]]; then [[ $1 == r ]] && { echo $line | awk -F':' '{print $2}' | awk '{print $1}'; } [[ $1 == t ]] && { echo $line | awk '{print $9}'; } fi done < /proc/net/dev } update(){ if [[ $slow_cycle -eq 0 ]]; then # informazioni da aggiornare solo ogni nc cicli batt_charge=`cat /proc/acpi/battery/BAT1/state | awk '/remaining\ capacity:/ {print $3}'` batt_perc=$(($batt_charge*100/$batt_full)) # temperatures tp="`sensors | grep Core | head -n 1 | awk -F: '{print $2}' | awk '{print $1}' | cut -b 2-3`°C" #tp=`echo $tp | cut -b 2-3` # questo mostra il carico medio del sistema nell'ultimo minuto lvg=`cat /proc/loadavg | awk '{print $1}'` # informazioni sul traffico di rete received1=$(byte_count r) transmit1=$(byte_count t) received=$((received1-received0)) transmit=$((transmit1-transmit0)) received0=$received1 transmit0=$transmit1 nrate=$(((received+transmit)/(sec*nc))) # problema ... if [ $nrate -gt 0 ]; then net="[Net $nrate B/s]" fi lcd_br=`cat /proc/acpi/video/VGA/LCD/brightness | awk '/current:/ {print $2}'` lcd_br=$((lcd_br/10)) vol=`my_change_vol` slow_cycle=$nc fi # informazioni da aggiornare ad ogni ciclo # va sostituito con le informazioni contenute in /proc/stat # $ head -n 1 /proc/stat | awk '{print $??}' # $ vmstat | awk '{print $??}' # cpu_perc=`cat /proc/loadavg | awk -F. '{print $2}' | awk '{print $1}'` cpu_line=`head -n 1 /proc/stat` cpu_u1=`echo $cpu_line | awk '{print $2}'` cpu_n1=`echo $cpu_line | awk '{print $3}'` cpu_s1=`echo $cpu_line | awk '{print $4}'` cpu_i1=`echo $cpu_line | awk '{print $5}'` cpu_w1=`echo $cpu_line | awk '{print $6}'` cpu_x1=`echo $cpu_line | awk '{print $7}'` cpu_y1=`echo $cpu_line | awk '{print $8}'` #cpu_z=`echo $cpu_line | awk '{print $9}'` # lo trascuro ... cpu_tot1=$((cpu_u1+cpu_n1+cpu_s1+cpu_i1+cpu_w1+cpu_x1+cpu_y1)) cpu_tot=$((cpu_tot1-cpu_tot0)) cpu_idle=$((cpu_i1-cpu_i0)) if [ $cpu_tot -lt 1 ]; then cpu_tot=1 fi if $first_cycle; then cpu_perc=0 # nt="[Net 0 B/s]" net="" else cpu_perc=$((100 - 100*cpu_idle/cpu_tot)) # problema se cpu_idle > cpu_tot (non dovrebbe accadere ma...) fi cpu_i0=$cpu_i1 cpu_tot0=$cpu_tot1 ram_free=`cat /proc/meminfo | awk '/MemFree:/ {print $2}'` ram_occ=$((ram_tot-ram_free)) ram_perc=$((ram_occ*100/ram_tot)) swap_free=`cat /proc/meminfo | awk '/SwapFree:/ {print $2}'` swap_occ=$((swap_tot-swap_free)) swap_perc=$((swap_occ*100/swap_tot)) if $spaces; then if [[ $batt_perc -lt 10 ]]; then sb=" " #sb=" 0" elif [[ $batt_perc -lt 100 ]]; then sb=" " else sb="" fi # aggiustamento degli spazi nella stringa di output if [[ $cpu_perc -lt 10 ]]; then ## toglie gli eventuali zeri davanti # cpu_perc=$((cpu_perc)) -> non funziona con 08 e 09 perche' considera # il numero come ottale sc=" " #sc=" 0" elif [[ $cpu_perc -lt 100 ]]; then sc=" " else sc="" fi if [[ $ram_perc -lt 10 ]]; then sr=" " #sr=" 0" elif [[ $ram_perc -lt 100 ]]; then sr=" " else sr="" fi if [[ $swap_perc -lt 10 ]]; then sp=" " #sp=" 0" elif [[ $swap_perc -lt 100 ]]; then sp=" " else sp="" fi # sl=" " fi # date "+%a %_d %b %y %H:%M" if $spaces; then stringa="$user@${host} ${vol}[BA $sb$batt_perc%|BR $lcd_br/$max_br|TP $tp][LA $lvg|CP $sc$cpu_perc%|RA $sr$ram_perc%|SW $sp$swap_perc%]$net `date "+%a %_d %b %H:%M"`" else stringa="$user@${host} ${vol}[BA $sb$batt_perc%|BR $lcd_br/$max_br|TP $tp][LA $lvg|CP $sc$cpu_perc%|RA $sr$ram_perc%|SW $sp$swap_perc%]$net `date "+%a %-d %b %H:%M"`" fi } output(){ update if $stdout; then echo "$stringa" fi if $root; then $xset -name "$stringa" res=$? else res=0 fi speed_cycle=$sec slow_cycle=$((slow_cycle-1)) } force_output(){ speed_cycle=0 slow_cycle=0 output } trap force_output 1 trap force_output 10 trap force_output 22 # Controllo opzioni while [ "$1" != "" ]; do case $1 in "-h"|"--help") printhelp exit 0 ;; "-v"|"--version") echo "$progname version $version (by $author)" exit 0 ;; "-o") stdout=true ;; "-c") spaces=true ;; "-1") loop=false ;; "-t") root=false ;; "-i") shift sec=$1 if [[ $sec -le 0 || $sec -gt 60 ]]; then echo "Errore nelle opzioni!" >&2 exit 1 fi ;; *) echo "Errore nelle opzioni!" >&2 exit 1 ;; esac shift done # informazioni fisse da acquisire una sola volta all'inizio batt_full=`cat /proc/acpi/battery/BAT1/info | awk '/full\ capacity:/ {print $4}'` ram_tot=`cat /proc/meminfo | awk '/MemTotal:/ {print $2}'` swap_tot=`cat /proc/meminfo | awk '/SwapTotal:/ {print $2}'` host=`hostname -s 2> /dev/null` # oppure uname -n if [[ "$host" = "" || "$host" = "localhost" ]]; then host=`hostname` fi user=$USERNAME max_br=`head -n 1 /proc/acpi/video/VGA/LCD/brightness | awk '{print $NF}'` max_br=$((max_br/10)) # ciclo slow_cycle=0 speed_cycle=0 res=0 first_cycle=true while [ $res -eq 0 ]; do if [[ $speed_cycle -eq 0 ]] || $first_cycle; then output fi if $loop; then sleep 1 else res=1 fi speed_cycle=$((speed_cycle-1)) first_cycle=false done # TODO # - migliorare prestazioni eliminando cose inutili # - problema: quando si aggiorna a causa del segnale esterno, il tempo trascorso è minore...