cpu和内存的使用率统计
统计cpu和内存一个月的平均使用率:
#!/bin/bash totalcpu_rate=0.0 totalmem_rate=0.0 num_days=$(ls -l /var/log/sa/sa[0-3]* | wc -l) echo $num_days for file in /var/log/sa/sa[0-3]* do cpu_average=$(sar -f $file | grep Average | tail -n1 | awk '{printf("%.2f\n", (100-$NF))}') mem_average=$(sar -r -f $file | grep Average | tail -1 | awk '{printf("%.2f\n", $4)}') totalcpu_rate=$(echo "${cpu_average} + ${totalcpu_rate}" | bc) totalmem_rate=$(echo "${mem_average} + ${totalmem_rate}" | bc) done cpu_average=$(echo "scale=5; $totalcpu_rate/$num_days" | bc) mem_average=$(echo "scale=5; $totalmem_rate/$num_days" | bc) printf "%.2f%%, %.2f%%\n" $cpu_average $mem_average >> /tmp/hostinfo.csv
统计cpu和内存一个月的使用峰值:
#!/bin/bash cpu_min=100 mem_max=0 for file in /var/log/sa/sa[0-3]* do _day_cpu_min=$(sar -f $file | awk 'BEGIN{min=100} {if(($NF ~ /^[1-9]/) && $NF< min) {min=$NF} fi} END{printf("%.2f\n", min)}') if [ $(echo "${_day_cpu_min} < ${cpu_min}" | bc) ]; then cpu_min=${_day_cpu_min} fi _day_mem_max=$(sar -r -f $file | awk 'BEGIN{max=0} {if((NF==12) && ($5 ~ /^[1-9]/) && $5>max) {max=$5} fi} END{printf("%.2f", max)}') if [ $(echo "${_day_mem_max} > ${mem_max}" | bc) ]; then mem_max=${_day_mem_max} fi done cpu_max=$(echo "100-${cpu_min}" | bc) echo ${cpu_max} echo ${mem_max} printf "%.2f%%, %.2f%%\n" $cpu_max $mem_max >> /tmp/hostinfo.csv