code-dumps/sh/sar-analysis.sh

18 lines
820 B
Bash
Raw Normal View History

2024-10-24 23:09:21 +08:00
#!/usr/bin/env bash
SARDIR=/var/log/sysstat
if [ -d /var/log/sa ]; then
SARDIR=/var/log/sa
fi
# echo "CPUUsed MEMUsed SWAPUsed IOWait"
for f in $(ls $SARDIR/sa[0123]*); do
# SARDate=$(stat $f | grep Modify | awk '{print $2}')
CPUUsed=$(sar -f $f | tail -1 | awk '{print 100-$NF}')
MEMUsed=$(sar -r -f $f | tail -1 | grep -Eo '[0-9]+\.[0-9]+' | head -1)
SWAPUsed=$(sar -S -f $f | tail -1 | awk '{print $4}')
IOWait=$(sar -f $f | tail -1 | awk '{print $6}')
echo "$CPUUsed $MEMUsed $SWAPUsed $IOWait" >> /tmp/sar-report.txt
done
cat /tmp/sar-report.txt | awk '{for (i=1;i<=NF;i++){a[i]+=$i;}} END {for (i=1;i<=NF;i++){printf "%.2f", a[i]/NR; printf "\t"};printf "\n"}' | awk '{ if ($1 > 50) print "high cpu",$1}; {if ($3 > 20) print "high swap", $3}; {if ($4 > 10) print "high iowait", $4}'
# rm -f /tmp/sar-report.txt