Advertisement
ajcorrea

CPU Stats Ubiquiti/Busybox

Dec 22nd, 2015
117
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/sh
  2. # by Paul Colby (http://colby.id.au), no rights reserved ;)
  3.  
  4. PREV_TOTAL=0
  5. PREV_IDLE=0
  6.  
  7. while true; do
  8.   # cpu  104325271 51180286 42979351 12633300744 133867895 0 10275060 0 0
  9.   #  0       1        2        3        4           5      6   7      8 9
  10.   cat /proc/stat | grep '^cpu ' > /tmp/cpu
  11.   TOTAL=`awk '{print $2+$3+$4+$5+$6+$7+$8+$9+$10}' /tmp/cpu` # Get the total CPU statistics.
  12.   IDLE=`awk '{print $5}' /tmp/cpu`                        # Get the idle CPU time.
  13.  
  14.   # Calculate the total CPU time.
  15.   #TOTAL=0
  16.   #for VALUE in "${CPU[@]}"; do
  17.   #  let "TOTAL=$TOTAL+$VALUE"
  18.   #done
  19.  
  20.   # Calculate the CPU usage since we last checked.
  21.   let "DIFF_IDLE=$IDLE-$PREV_IDLE"
  22.   let "DIFF_TOTAL=$TOTAL-$PREV_TOTAL"
  23.   let "DIFF_USAGE=(1000*($DIFF_TOTAL-$DIFF_IDLE)/$DIFF_TOTAL+5)/10"
  24.   echo -en "\rCPU: $DIFF_USAGE%  \b\b"
  25.  
  26.   # Remember the total and idle CPU times for the next check.
  27.   PREV_TOTAL="$TOTAL"
  28.   PREV_IDLE="$IDLE"
  29.  
  30.   # Wait before checking again.
  31.   sleep 1
  32. done
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement