Skip to content

Instantly share code, notes, and snippets.

@igorhrq
Last active July 3, 2020 12:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save igorhrq/af3e86a2825cb3efa12b7333f756baca to your computer and use it in GitHub Desktop.
Save igorhrq/af3e86a2825cb3efa12b7333f756baca to your computer and use it in GitHub Desktop.
#!/bin/bash
# Version 0.2 21/06/2020
# Version 0.3 25/06/2020 - adjusted mem,cpu and disk showing everything in columns for gain screen space
# Version 0.4 29/06/2020 - adjusted regex for be more clean.
# and be more user-friendly.
# I have get the memory sar calculate from script of Joe Still from HostDime US and and made
# several updates to make the script more complete
#
#
#Fast SAR results
#Author Igor A. / Abuse & Security - HostDime Brasil
#usage $ fastsarresults.sh
# Will show the results for the vigent day
#usage $ fastsarresults.sh 19
# Will show the results for the last day 19 from that month, you can use 03 or any other day
file=""
[ -z $1 ] || file="-f /var/log/sa/sa$1"
#Disk
diskresult=$(sar -d -p $file| sed '/^M.*:/d;/^A.*:/d'| awk '{print "Disk\n" $2 " on "$1 " %Usage " $10}' | egrep "([9][0-9],.*|[8][0-9],.*)"| sed 's/\t/;/g')
if [ -z "$diskresult" ]
then
diskresult=$(echo "No disk saturation above 80%"| sed 's/\t/;/g')
fi
#CPU
cpuresults=$(sar -u $file | sed '/^M.*:/d;/^A.*:/d'| awk '{print "CPU\n" "at "$1" CPU was below 50%: " $8 }' | egrep "[0-5][0-9],|: [0]+," | sed 's/\t/;/g')
if [ -z "$cpuresults" ]
then
cpuresults=$(echo "No CPU idle below 50%"| sed 's/\t/;/g')
fi
#MEM
memresult=$(sar -r $file | awk '
# Determine where each column is set
NR == 3 {
for(i = 3; i <= NF; i++) {
if( $i == "kbmemused" ) { kbmemused_col=i }
if( $i == "kbbuffers" ) { kbbuffers_col=i }
if( $i == "kbcached" ) { kbcached_col=i }
if( $i == "kbmemfree" ) { kbmemfree_col=i }
if( $i == "%commit" ) { commit_col=i }
}
}
# Pluck total memory usage from addition of a random line of "free + used"
NR == 4 {
memtotal = $3 + $4;
}
# For all lines that contain an actual usage log
NR >= 4 && $3 ~ /^[0-9]/ && $1 ~ /^[0-9]/ {
# determine real usage from "used - (buffers+cache)"
realusage = $kbmemused_col - ( $kbbuffers_col + $kbcached_col )
# percentage is simply dividing by total
{if (realusage > 0) {percentusage = (realusage / memtotal) * 100} else {percentusage=0}}
# print real usage / total (percentage)
printf "Memory\n%s %s: %dMB / %dMB = %d%%\n", $1, $2, realusage/1000, memtotal/1000, percentusage
}
' | egrep "([8][0-9]%$|[9][0-9]%$|[0-9][0-9][0-9]$%)"|sed 's/\t/;/g')
if [ -z "$memresult" ]
then
memresult=$(echo "No Memory usage above 80%" | sed 's/\t/;/g')
fi
paste <(echo -e "Memory\n$memresult") <(echo -e "CPU\n$cpuresults") <(echo -e "DISK\n$diskresult") | sed 's/\t/;/g'| sed 's/^;/;----;/g;s/;$/;----;/;s/;;/;----;/g' | sed 's/\(^;.*;.*\);$/\1/g;s/-$/-;/g;s/$/;/g;s/;;$/;/g' | sed 's/\(^No Memory usage above 80%;.*;No disk saturation above 80%;$\)/;\1/g;s/^Memory;CPU;DISK;$/;Memory;CPU;DISK;/g' | sed 's/\(^No Memory usage above 80%;at.*;.*;$\)/;\1/g' | column -s ';' -t
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment