Skip to content

Instantly share code, notes, and snippets.

@DarthNerdus
Last active December 15, 2015 18:29
Show Gist options
  • Save DarthNerdus/5303901 to your computer and use it in GitHub Desktop.
Save DarthNerdus/5303901 to your computer and use it in GitHub Desktop.
tmux-mem-cpu-load; usable with Linux or 10.8+ #shell #tmux
#!/bin/bash
if [ -z $1 ]; then
echo "Usage: $0 interval [number of lines]"
exit 1
else
INTERVAL=$1
fi
NUM_LINES=10
if [ $2 ]; then
NUM_LINES=$2
fi
calc_lines() {
if [ $NUM_LINES -gt 0 ]; then
ACTIVE_LINES=$(($CPU/$NUM_LINES))
LINES=""
for (( i = 0; i < $NUM_LINES; i++ )); do
if [[ $i < $ACTIVE_LINES ]]; then
LINES="$LINES|"
else
LINES="$LINES "
fi
done
fi
}
if [[ $OSTYPE =~ darwin* ]]; then
# OS X
cpu_info=$(iostat -n0 -c${INTERVAL} | tail -n1)
CPU=$(echo $cpu_info | cut -d' ' -f1)
LOAD=$(echo $cpu_info | cut -d' ' -f4-6)
mem_info=($(vm_stat))
WIRED="${mem_info[24]%?}"
ACTIVE="${mem_info[14]%?}"
INACTIVE="${mem_info[17]%?}"
FREE="${mem_info[11]%?}"
TOTAL_MEM=$(( ($WIRED + $ACTIVE + $INACTIVE + $FREE) * 4096 / 1024 / 1024 ))
FREE_MEM=$(( ($INACTIVE + $FREE) * 4096 / 1024 / 1024 ))
USED_MEM=$(( ($WIRED + $ACTIVE) * 4096 / 1024 / 1024 ))
elif [[ $OSTYPE =~ linux* ]]; then
# Linux
CPU=$(vmstat 1 ${INTERVAL} | tail -n1 | awk '{print $13}')
LOAD=$(awk '{print $1,$2,$3}' /proc/loadavg)
mem_info=($(free -m))
TOTAL_MEM=${mem_info[7]}
USED_MEM=${mem_info[15]}
fi
if [ $NUM_LINES -gt 0 ]; then
calc_lines
echo "${USED_MEM}/${TOTAL_MEM}MB [${LINES}] ${CPU}% ${LOAD}"
else
echo "${USED_MEM}/${TOTAL_MEM}MB ${CPU}% ${LOAD}"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment