Monday, September 9, 2013

GeekTool CPU+Mem+Disk

Today I add a new geeklet to my desktop (http://projects.tynsoe.org/en/geektool/), it shows cpu+mem+disk and i needed it, because I'm tired to open Monitor application on my Mac.







I download the base version from:
http://www.macosxtips.co.uk/geeklets/modules/upload/attachments/CPUMemDskUsage.glet
And then make some adjustments to make it work with Lion and to improve performance. At the end the script look like this:

myTop=`top -l 1 -n 0`
myCPU=`echo "$myTop" | awk '/CPU usage/ {print $3}' | sed s/%//`
myCPU=`echo "tmp=$myCPU; tmp /= 1; tmp" | bc`

typeset -i b=9
echo "CPU Usage    \c"
while [ $b -lt $myCPU ]
do
echo "\033[1;36m*\033[0m\c"
b=`expr $b + 10`
done
echo "\033[1;39m*\033[0m\c"
while [ $b -lt 99 ]
do
echo "\033[1;37m*\033[0m\c"

b=`expr $b + 10`
done
echo " $myCPU%\c"

echo "\r"
unset myCPU
unset b


myMem=`echo "$myTop" | grep PhysMem` 
myUsedMem=`echo $myMem | awk '{print $8}' | sed s/M// `
myFreeMem=`echo $myMem | awk '{print $10}' | sed s/M// `
myActiveMem=`echo $myMem | awk '{print $4}' | sed s/M// `
myTotalMem=` expr $myUsedMem + $myFreeMem`
myUsedPer=`echo |awk '{print f / t * 100}' f=$myActiveMem t=$myTotalMem`
myUsedPer=`echo "tmp=$myUsedPer; tmp /= 1; tmp" | bc`

typeset -i c=9
echo "Memory Usage \c"
while [ $c -lt $myUsedPer ]
do
        echo "\033[1;36m*\033[0m\c"
        c=`expr $c + 10`
done

echo "\033[1;39m*\033[0m\c"
while [ $c -lt 99 ]
do
        echo "\033[1;37m*\033[0m\c"
        c=`expr $c + 10`
done
echo " $myUsedPer%\c"

echo "\r"

unset myUsedMem
unset myFreeMem
unset myTotalMem
unset myUsedPer
unset c


myDisk=`df | awk '/dev\/disk1/ && NF>1 {print $5}' | sed 's/\%//'`
myDisk=`expr 100 - $myDisk`

typeset -i a=9
echo "Disk Usage   \c"
while [ $a -lt $myDisk ]
do
echo "\033[1;36m*\033[0m\c"
a=`expr $a + 10`
done
echo "\033[1;39m*\033[0m\c"
while [ $a -lt 99 ]
do
echo "\033[1;37m*\033[0m\c"
a=`expr $a + 10`
done
echo " $myDisk%\c"

echo "\r"
unset myDisk
unset a

No comments: