Tuesday, 30 July 2013

Stats Bash Script

#!/bin/bash
cpuUsageM=$(top -bn 1 | awk '{print $9}' | tail -n +8 | awk '{s+=$1} END {print s}')
cpuFreqM=$(echo "scale=0; " `cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq` "/1000" | bc)
cpuTempM=$(echo "scale=1; " `cat /sys/class/thermal/thermal_zone0/temp` "/1000" | bc)

gpuTempM=$(/opt/vc/bin/vcgencmd measure_temp)
gpuTempM=${gpuTempM//\'C/}
gpuTempM=${gpuTempM//temp\=/}

memTotalM=$(cat /proc/meminfo | grep MemTotal | awk '{print $2}')
memTotal1=$memTotalM
memTotalM=$(echo "scale=1; $memTotal1 / 1024" | bc)

memUsageM=$(cat /proc/meminfo | grep MemFree | awk '{print $2}')
memUsageM=$(echo "scale=1; ($memTotal1 - $memUsageM) / 1024" | bc | sed 's/^\./0./')

memUsageP=$(echo "scale=1; (100/$memTotalM) * $memUsageM" | bc | sed 's/^\./0./')

rootTotalM=$(df -m / | grep / | awk '{print $2}')
rootTotalM=$(echo "scale=1; $rootTotalM / 1024" | bc)

rootUsageM=$(df -m / | grep / | awk '{print $3}')
rootUsageM=$(echo "scale=1; $rootUsageM / 1024" | bc | sed 's/^\./0./')

rootUsageP=$(echo "scale=1; (100/$rootTotalM) * $rootUsageM" | bc | sed 's/^\./0./')

echo "CPU Usage:  $cpuUsageM%"
echo "CPU Freq:   "$cpuFreqM"MHz"
echo "CPU Temp:   $cpuTempM°C"
echo ""
echo "GPU Temp:   $gpuTempM°C"
echo ""
echo "MEM Usage:  "$memUsageM"MB/"$memTotalM"MB ($memUsageP%)"
echo "Root Usage: "$rootUsageM"GB/"$rootTotalM"GB ($rootUsageP%)"

The output is as follows:
Code:
~$ ./sysinfo 
CPU Usage:  70.1%
CPU Freq:   800MHz
CPU Temp:   57.8°C

GPU Temp:   58.4°C

MEM Usage:  96.5MB/375.4MB (19.3%)
Root Usage: 0.9GB/7.2GB (12.4%)


Script is from XDA Developers by oribunokiyuusou

3 comments:

  1. Tried this, and the only problem I found was that running the degree symbol up against the $cpuTempM and $gpuTempM caused garbled output. Either adding a space between the variable and the degree symbol, or adding braces to the variable reference (i.e. echo "CPU Temp: ${cpuTempM}°C"") fixes the problem for me.

    It's a good practice to add the braces any time you are abutting a variable against other text; it reduces the confusion as to where the variable name ends.

    ReplyDelete
  2. Any idea how to add this bash scrip to the ssh banner?

    ReplyDelete
  3. i think it's /home/user/.bash_profile if i remember rightly.

    ReplyDelete

Amazon

Donate

Donate Towards More Raspberry PI's for Projects