Skip to content

Instantly share code, notes, and snippets.

@benad
Last active April 19, 2020 21:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benad/9a73d5b92b740c5260e0609bd7748bb8 to your computer and use it in GitHub Desktop.
Save benad/9a73d5b92b740c5260e0609bd7748bb8 to your computer and use it in GitHub Desktop.
Byobu Sample Custom Status Scripts
#!/bin/bash
#Place this file in ~/.byobu/bin/5_cputiny
TMP=$HOME/.byobu
cpu_last=($(head -n1 $TMP/cpulast))
cpu_last_sum=$(head -n1 $TMP/cpulastsum)
# Get the first line with aggregate of all CPUs
cpu_now=($(head -n1 /proc/stat))
# Get all columns but skip the first (which is the "cpu" string)
cpu_sum="${cpu_now[@]:1}"
# Replace the column seperator (space) with +
cpu_sum=$((${cpu_sum// /+}))
# Get the delta between two reads
cpu_delta=$((cpu_sum - cpu_last_sum))
# Get the idle time Delta
cpu_idle=$((cpu_now[4]- cpu_last[4]))
# Calc time spent working
cpu_used=$((cpu_delta - cpu_idle))
# Calc percentage
cpu_usage=$((100 * cpu_used / cpu_delta))
# Keep this as last for our next read
cpu_last=("${cpu_now[@]}")
cpu_last_sum=$cpu_sum
echo "${cpu_last[@]}" > $TMP/cpulast
echo $cpu_last_sum > $TMP/cpulastsum
echo $cpu_usage >> $TMP/cpugraph
printf "C:%s" "$( byobu-ugraph -f $TMP/cpugraph )" ; echo
#!/bin/sh -e
# Place this file in ~/.byobu/bin/5_memtiny
#
# mem_available: grab the current memory and usage
#
# Copyright (C) 2008 Canonical Ltd.
# Copyright (C) 2011-2014 Dustin Kirkland
# Copyright (C) 2020 Benoit Nadeau
#
# Authors: Dustin Kirkland <kirkland@byobu.org>, Benoit Nadeau <benad@me.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
. /usr/lib/byobu/include/shutil
memory() {
local free="" total="" buffers="" cached=""
local kb_main_used=0 buffers_plus_cached=0 fo_buffers=0 fo_cached=0
if [ -r /proc/meminfo ]; then
while read tok val unit; do
case "$tok" in
MemTotal:) total=${val};;
MemFree:) free=${val};;
Buffers:) buffers=${val};;
Cached:) cached=${val};;
esac
[ -n "${free}" -a -n "${total}" -a -n "${buffers}" -a -n "${cached}" ] && break;
done < /proc/meminfo
elif eval $BYOBU_TEST vm_stat >/dev/null 2>&1; then
# MacOS support
# calculation borrowed from http://apple.stackexchange.com/a/48195/18857
free_blocks=$(vm_stat | grep free | awk '{ print $3 }' | sed -e 's/\.//')
inactive_blocks=$(vm_stat | grep inactive | awk '{ print $3 }' | sed -e 's/\.//')
speculative_blocks=$(vm_stat | grep speculative | awk '{ print $3 }' | sed -e 's/\.//')
free=$((($free_blocks+speculative_blocks)*4))
inactive=$(($inactive_blocks*4))
total=$((($free+$inactive)))
buffers=0
cached=0
fi
kb_main_used=$(($total-$free))
buffers_plus_cached=$(($buffers+$cached))
# "free output" buffers and cache (output from 'free')
fo_buffers=$(($kb_main_used - $buffers_plus_cached))
fpdiv $((100*${fo_buffers})) "${total}" 0;
usage=${_RET}
if [ $usage -gt 90 ]; then
# Change foreground to yellow, if usage over 90%
fg="Y"
else
fg="W"
fi
[ -n "$total" ] || return
printf "M:%s" "$(byobu-ulevel $usage)" ; echo
}
memory
# vi: syntax=sh ts=4 noexpandtab
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment