主题:一键查看服务器使用率
cpu vmstat suyu wa
memery free
disk df -h /dev
tcp连接数 netstat
cpu(){
used=$(vmstat |awk '{if(NR==3)print $13+$14}')
iowait=$(vmstat |awk '{if(NR==3)print $16}')
echo "cpu - used : ${used}%, iowait : ${iowait}%"
}
memery(){
total=$(free -m|awk '{if(NR==2)printf "%.1f",$2/1024}')
used=$(free -m|awk '{if(NR==2)printf "%.1f",$3/1024}')
available=$(free -m|awk '{if(NR==2)printf "%.1f",$NF/1024}')
echo "memery - total: ${total}G,used: ${used}G, ${available}G"
}
disk(){
fs=$(df -h|awk '/^/dev/{print $1}')
for p in $fs;do
mounted=$(df -h|awk -v p=$p '$1==p{print $NF}')
used=$(df -h|awk -v p=$p '$1==p{print $3}')
size=$(df -h|awk -v p=$p '$1==p{print $2}')
echo "disk - mounted: $mounted, size: $size, used: $used"
done
}
tcp_status(){
summary=$(netstat -anplt|awk '{a[$6]++}END{for (i in a)printf i ":"a[i]" "}')
echo "tcp link - $summary"
}
小结:
1.printf "%.1f",$x 打印小数点后1位
2.awk的用法
3.cpu 用户和内核态(us sy)的使用率不超过60% iowait(wa)不超过10%