Linux下硬盘使用率详解及shell脚本实现
linux下使用df -TPh查看硬盘使用率:
sdua:/ # df -TPh Filesystem Type Size Used Avail Use% Mounted on /dev/vda3 ext3 2.0G 974M 940M 51% / udev tmpfs 16G 500K 16G 1% /dev tmpfs tmpfs 32G 9.4G 23G 30% /dev/shm /dev/vda2 ext3 1008M 56M 902M 6% /boot /dev/vda9 ext3 20G 173M 19G 1% /core /dev/vda8 ext3 28G 2.5G 24G 10% /home /dev/vda10 ext3 21G 6.1G 14G 31% /opt /dev/vda6 ext3 4.0G 186M 3.6G 5% /var tmpfs tmpfs 512M 620K 512M 1% /tmp shm tmpfs 32G 9.4G 23G 30% /dev/shm /dev/mapper/vgdb-lv_bakData ext3 11G 326M 9.8G 4% /home/oracle/rmcbak /dev/mapper/vgdb-lvarchive ext3 11G 156M 9.9G 2% /home/oracle/archive
去掉Filesystem行,tmpfs行(tmpfs是一种基于内存的文件系统) ,获取硬盘名和使用率:
sdua:/ # df -TPh | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $6 " " $1 }' 51% /dev/vda3 6% /dev/vda2 1% /dev/vda9 10% /dev/vda8 31% /dev/vda10 5% /dev/vda6 4% /dev/mapper/vgdb-lv_bakData 2% /dev/mapper/vgdb-lvarchive
具体实现的shell脚本如下:
#!/bin/bash # set alert level 90% is default ALERT=50 df -TPh | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $6 " " $1 }' | while read output; do usep=$(echo $output | awk '{ print $1}' | cut -d '%' -f 1 ) partition=$(echo $output | awk '{ print $2 }' ) if [ $usep -ge $ALERT ]; then echo "Running out of space $partition ($usep%) on $(date +%Y-%m-%d)" fi done exit 0
运行效果超过50%使用率:
sdua:/ # ./disk.sh Running out of space /dev/vda3 (51%) on 2015-09-09 sdua:/ #
我可能什么都想要