HOWTO: system load status in screen(1)

To be precise, not the load itself (0.00 0.00 0.00) but ...
[cmd=]CPU: 5.2% user, 0.0% nice, 19.0% system, 0.0% interrupt, 75.8% idle[/cmd]

... but like that (generally you may customize output as you need):
[cmd=]user: 5.2% | nice: 0.0% | system: 19.0% | interrupt: 0.0% | idle: 75.8%[/cmd]

Like on this window:


Now as you know what the end result will look like, lets describe needed steps to achieve this.

First, you need to have the uniload.sh script running in the background:
% uniload.sh &

The uniload.sh script itself:
Code:
#! /bin/sh

STATS_FILE=/var/tmp/${USER}_stats_top
DELAY=1

# FreeBSD uses jot(1) while Linux uses seq(1)
which jot 1> /dev/null 2> /dev/null || alias jot=seq

__freebsd() {
  top -s ${DELAY} -d 2 0  \
    | grep -m 1 CPU \
    | sed 's/,//g' \
    | awk '{ print $4": "$3" | "$6": "$5" | "$8": "$7" | "$10": "$9" | "$12": "$11 }'
  }

__linux() {
  top -d ${DELAY} -n 2 -b \
    | grep -m 2 Cpu \
    | tail -1 \
    | sed 's/%/ /g' \
    | awk '{ print "user: " $2 " | system: " $4 " | nice: " $6 " | idle: " $8 }'
  }

__exit() {
  rm -rf ${STATS_FILE}
  exit 0
  }

trap '__exit' 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
OS=$( uname )
while true
do
  for I in $( jot 128 ); do
    case ${OS} in
      (FreeBSD) __freebsd >> ${STATS_FILE} 2>&1 ;;
      (Linux)   __linux   >> ${STATS_FILE} 2>&1 ;;
      (*)       echo "supported systems: FreeBSD Linux"; exit 1 ;;
    esac
  done
  sleep 1
  :> ${STATS_FILE}
done

The script periodically writes load data to /var/tmp/${USER}_stats_top file, which is later displayed by screen(1). To display these in screen(1) you need to add these lines into the ~/.screenrc config:

It will be like that for vermaden user:
Code:
backtick 100 5 5 tail -1 /var/tmp/vermaden_stats_top
caption always '%{= wk} %= %100` %='

Currently it works only on FreeBSD and Linux, but it will not be hard to add Solaris or Mac OS X support, Linux support has been added by DNAeon.
 
Back
Top