利用Script监控Linux,处理df命令折行

日常运维工作中,我们需要写很多script,根据script的输出,进行判断、给出报警等。

在Linux系统中,使用df命令时,存在一个问题,输出会出现折行(wrap):

[root@rems2 tgt]# df -i
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/mapper/vg_rems2-lv_root
3276800 289823 2986977 9% /
tmpfs 1005929 1 1005928 1% /dev/shm
/dev/sda1 128016 51 127965 1% /boot
/dev/mapper/vg_rems2-lv_home
26714112 4788 26709324 1% /home

如上所示,/ 文件系统的信息被折成了两行,这给我们的判断处理带来了问题。为解决此问题,我们可以使用 -P 参数。

[root@rems2 tgt]# df -Pi
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/mapper/vg_rems2-lv_root 3276800 289823 2986977 9% /
tmpfs 1005929 1 1005928 1% /dev/shm
/dev/sda1 128016 51 127965 1% /boot
/dev/mapper/vg_rems2-lv_home 26714112 4788 26709324 1% /home

[root@rems2 tgt]# df -Pkv
Filesystem 1024-blocks Used Available Capacity Mounted on
/dev/mapper/vg_rems2-lv_root 51475068 16828936 32008308 35% /
tmpfs 4023716 0 4023716 0% /dev/shm
/dev/sda1 487652 148246 309710 33% /boot
/dev/mapper/vg_rems2-lv_home 420526720 424616 398717504 1% /home

-P 表示POSIX风格。

参考文档:https://support.symantec.com/en_US/article.HOWTO5953.html

Leave Comment