Last active
June 3, 2020 23:53
-
-
Save b1oki/6c8f1d2b925fa80f47728429174b3cbc to your computer and use it in GitHub Desktop.
System Monitor (CPU average frequency, maximum temperature and free total memory)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
function cpufreq() { | |
cat /proc/cpuinfo | grep MHz | awk -F ': ' '{print $2}' | sort -u | |
} | |
function temperature() { | |
sensors | awk -F ':' '{print $2}' | awk '{print $1}' | grep "°C" | sed "s/+\|°C//g" | sort -u | |
} | |
function memory() { | |
free -ht | grep 'Total' | awk '{print $4}' | sed "s/,/./g" | |
} | |
function calculateAverage() { | |
local rows=$@ | |
total=0 | |
sum=0 | |
for row in ${rows}; do | |
sum=$(python3 -c "print(float($sum) + float($row))") | |
total=$(expr $total + 1) | |
done | |
average=$(python3 -c "print('{:.2f}'.format(float($sum) / float($total)))") | |
echo $average | |
} | |
function calculateMax() { | |
local rows=$@ | |
max=0 | |
for row in ${rows}; do | |
if (( $(echo "$row > $max" | bc -l) )); then | |
max=$row | |
fi | |
done | |
echo $max | |
} | |
function iteration() { | |
averageFreq=$(calculateAverage $(cpufreq)) | |
maxTemp=$(calculateMax $(temperature)) | |
echo "${averageFreq}㎒ ${maxTemp}°C $(memory)" | |
} | |
function cleanup() { | |
kill -s SIGTERM $! | |
exit 0 | |
} | |
trap cleanup SIGINT SIGTERM | |
while [ 1 ]; do | |
iteration | |
sleep 3 & | |
wait $! | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment