Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

executable file 108 lines (87 sloc) 2.252 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
#!/bin/bash
############################################################
# Linux CPU Usage Monitor #
# #
# Author: Moises P. Sena (http://moisespsena.com) #
# Original Author: Paul Colby (http://colby.id.au) #
# #
# no rights reserved :) #
############################################################

PREV_TOTAL=0
PREV_IDLE=0

_s=0
_f=0
_l=0

do_cmd() {
  local CPU=(`cat /proc/stat | grep '^cpu '`) # Get the total CPU statistics.
  unset CPU[0] # Discard the "cpu" prefix.
  local IDLE=${CPU[4]} # Get the idle CPU time.

  # Calculate the total CPU time.
  local TOTAL=0
  for VALUE in "${CPU[@]}"; do
    let "TOTAL=$TOTAL+$VALUE"
  done

  # Calculate the CPU usage since we last checked.
  let "DIFF_IDLE=$IDLE-$PREV_IDLE"
  let "DIFF_TOTAL=$TOTAL-$PREV_TOTAL"
  let "DIFF_USAGE=(1000*($DIFF_TOTAL-$DIFF_IDLE)/$DIFF_TOTAL+5)/10"

  if [ $_l = 1 ]
  then
    if [ $_s = 1 ]; then
        echo -en "\r$DIFF_USAGE \b\b"
    else
        echo -en "\rCPU: $DIFF_USAGE% \b\b"
    fi
  elif [ $_s = 1 ]; then
    echo "$DIFF_USAGE"
  else
    echo "CPU: $DIFF_USAGE%"
  fi

  # Remember the total and idle CPU times for the next check.
  PREV_TOTAL="$TOTAL"
  PREV_IDLE="$IDLE"
}

do_help() {
   cat <<EOF
Linux CPU Usage Monitor
Author: Moises P. Sena (http://moisespsena.com)
Original Author: Paul Colby (http://colby.id.au)
Usage $0 [-l [-s|-f] |-h]

Options

-h: Print this messsage

-s: Print the percent number. Example: 10

-f: Print the formated percent number. Example: CPU: 10%

-l: Print with infinit loop

Examples:

$0 -s
$0 -f
$0 -l -s
$0 -l -f
$0 -h
EOF
}

do_error() {
    do_help 1>2
    exit 1
}

while getopts "sflh" op; do
    case "$op" in
        s) _s=1
            ;;
        f) _f=1
            ;;
        l) _l=1
            ;;
        h) do_help
            exit
            ;;
        *) do_error
            ;;
    esac
done

[ "$1" = "" ] && _f=1 && _l=1

if [ $_l = 1 ]; then
    while true; do do_cmd ; sleep .5; done
else
    do_cmd
fi
Something went wrong with that request. Please try again.