Last active
January 6, 2016 15:05
-
-
Save tcaddy/e50491d73e0db98b5d39 to your computer and use it in GitHub Desktop.
Linux CPU and Memory Logging
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
############################################################# | |
# Log memory and CPU usage | |
############################################################# | |
*/5 * * * * /usr/local/bin/log_memory_usage.sh > /dev/null | |
*/5 * * * * /usr/local/bin/log_cpu_usage.sh > /dev/null |
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 | |
sum=0.0 | |
count=0 | |
today=`date +"%x"` | |
for x in `cat /tmp/cpu_usage.csv | grep $today` | |
do | |
if [[ $x == *","* ]] | |
then | |
count=`bc <<< $count+1` | |
val=`echo $x | awk -F, '{print $2}'` | |
sum=`bc <<< $sum+$val` | |
fi | |
done | |
avg=`bc <<< "scale=2;$sum/$count"` | |
echo "avg: $avg%" |
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 | |
sum=0.0 | |
count=0 | |
today=`date +"%x"` | |
for x in `cat /tmp/memory_usage.csv | grep $today` | |
do | |
if [[ $x == *","* ]] | |
then | |
count=`bc <<< $count+1` | |
val=`echo $x | awk -F, '{print $2}'` | |
sum=`bc <<< $sum+$val` | |
fi | |
done | |
avg=`bc <<< "scale=2;$sum/$count"` | |
avggb=`bc <<< "scale=2;$avg/(1024*1024)"` | |
echo "avg: $avggb GB" |
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 | |
max=0 | |
today=`date +"%x"` | |
for x in `cat /tmp/cpu_usage.csv | grep $today` | |
do | |
if [[ $x == *","* ]] | |
then | |
val=`echo $x | awk -F, '{print $2}'` | |
if (( $(echo "$val > $max" |bc -l) )) | |
then | |
max=$val | |
fi | |
fi | |
done | |
echo "max: $max%" |
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 | |
max=0 | |
today=`date +"%x"` | |
for x in `cat /tmp/memory_usage.csv | grep $today` | |
do | |
if [[ $x == *","* ]] | |
then | |
val=`echo $x | awk -F, '{print $2}'` | |
if [ "$val" -gt "$max" ] | |
then | |
max=$val | |
fi | |
fi | |
done | |
maxgb=`bc <<< "scale=2;$max/(1024*1024)"` | |
echo "max: $maxgb GB" |
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/sh | |
sum=0.0 | |
cpucount=`lscpu | grep "^CPU(s)" | awk '{print $2}'` | |
for x in `ps -e -o "%cpu" --sort=pcpu --no-headers | grep -v " 0.0"` | |
do | |
sum=`bc <<< $sum+$x` | |
done | |
avg=`bc <<< "scale=2;$sum/$cpucount"` | |
exec echo `date +"%x %X"`,$avg >> /tmp/cpu_usage.csv |
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/sh | |
total=`cat /proc/meminfo | grep MemTotal | awk '{print $2}'` | |
free=`cat /proc/meminfo | grep MemFree | awk '{print $2}'` | |
cached=`cat /proc/meminfo | grep ^Cached | awk '{print $2}'` | |
swaptotal=`cat /proc/meminfo | grep SwapTotal | awk '{print $2}'` | |
swapfree=`cat /proc/meminfo | grep SwapFree | awk '{print $2}'` | |
swapcached=`cat /proc/meminfo | grep SwapCached | awk '{print $2}'` | |
memoryused=$((total-free-cached+swaptotal-swapfree-swapcached)) | |
exec echo `date +"%x %X"`,$memoryused >> /tmp/memory_usage.csv |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment