1

I'd like to put the htop status bars, that show the percentage of CPU usage and memory, in my MOTD, so that every time I log in I see an overview about the systems health status.

Any idea how to achieve this?

UPDATE: Especially the visualization with percentage bars is what I want. Not only the values.

1

I did it my way. (sorry for so much bash code)

#
# cpu usage
#
cpu_cores=$(cat /proc/cpuinfo | grep "processor" | wc -l)
i=0
cpu_out_1=($(cat /proc/stat | grep "cpu"))
# the longer we sleep the more accurate is the calculated percentage
sleep 0.3
cpu_out_2=($(cat /proc/stat | grep "cpu"))

cpu_out_colcount=($(cat /proc/stat | head -n 1))
cpu_out_colcount=${#cpu_out_colcount[@]}

while [ $i -lt $cpu_cores ]; do
    cpu_index=$(($i * $cpu_out_colcount + $cpu_out_colcount + 1))
    cpu_load_1=(${cpu_out_1[@]:$cpu_index:4})
    cpu_load_2=(${cpu_out_2[@]:$cpu_index:4})
    cpu_sum_1=$((cpu_load_1[0] + cpu_load_1[1] + cpu_load_1[2] + cpu_load_1[3]))
    cpu_sum_2=$((cpu_load_2[0] + cpu_load_2[1] + cpu_load_2[2] + cpu_load_2[3]))
    cpu_sum_diff=$((cpu_sum_2 - cpu_sum_1))
    cpu_idle_1=${cpu_load_1[3]}
    cpu_idle_2=${cpu_load_2[3]}
    cpu_idle_diff=$((cpu_idle_2 - cpu_idle_1))
    cpu_perc=$((100 - (100 * $cpu_idle_diff / $cpu_sum_diff)))
    printf "${COLOR_HEADER}"
    printf '  CPU %-2d ' "$i"
    printf "${COLOR_NONE}"
    print_bar $cpu_perc 30 60
    printf "\n"
    i=$((i+1));
done;

And print_bar is a function, that generates a colored percentage bar:

function print_bar() {
    prcntg=$1
    warn=$2
    crit=$3
    bar_color=$COLOR_OK
    if [ $prcntg -gt $warn ]; then
        bar_color=$COLOR_WARN
    fi
    if [ $prcntg -gt $crit ]; then
        bar_color=$COLOR_CRIT
    fi

    bar_width_fill=$((BAR_WIDTH * prcntg / 100))
    bar_width_empty=$((BAR_WIDTH - bar_width_fill))

    printf -v bar "%${bar_width_fill}s" ""
    printf -v nbar "%${bar_width_empty}s" ""
    printf "${bar_color}"
    printf '%3d%% %s' "$prcntg" "${bar// /█}"
    printf "${COLOR_NONE}"
    printf '%s' "${nbar// /▒}"
}
0

check this:

https://stackoverflow.com/questions/17534591/htop-output-to-human-readable-file

especially the answer from the author of htop - Hisham H M

short answer: you can't really do it, use ps instead

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.