Chinaunix

标题: 巡检脚本问题~~ [打印本页]

作者: where27    时间: 2009-08-14 09:58
标题: 巡检脚本问题~~
日常巡检的脚本
以检查磁盘使用为例
[root@localhost]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda2             3.8G  3.4G  0.4G  89% /
/dev/sda3              14G  164M   13G   2% /home
/dev/sda1              46M   11M   34M  24% /boot
tmpfs                 312M     0  312M   0% /dev/shm
要求:
使用率在80%以下正常
80%~90% 输出“提示 挂载目录如 /  使用超过80%”
90%以上   输出“报警 挂载目录如 / 使用超过90%”
如果所有都正常
输出“磁盘使用情况正常”

大侠们给个解答吧
代码越简单越好
谢谢!
作者: waker    时间: 2009-08-14 10:12
df -h|awk '$5+0>90{print $6,"xxoo";next}
$5+0>80{print $6,"ooxx"}'

有时间看看置顶的教程
作者: where27    时间: 2009-08-14 10:14
标题: 回复 #2 waker 的帖子

我会的
谢谢了
作者: cookis    时间: 2009-08-14 10:27

  1. df -h | awk '$1 !~ /^Filesystem/ {if (int($5) > 80 && int($5) < 90) print $6 " is out of 80%"; else if (int($5) > 90) print $6 " is out of 90%"; else normal++;} END{if (normal + 1 == NR) print "alls are normal!"}'
复制代码

作者: Minsic    时间: 2009-08-14 10:33
和4楼类似,稍微修改了下,为了防止文件系统名过长分行,df加了-P选项

  1. df -Ph|awk -F"[ %]+" 'NR>1{s=$5+0;if(s>=80&&s<90){print "提示 挂载目录"$NF"使用超过80";a++}if(s>=90){print "报警 挂载目录"$NF"使用超过90";a++}}END{if(!a)print "磁盘使用情况正常"}'
复制代码

作者: where27    时间: 2009-08-14 10:33
标题: 回复 #4 cookis 的帖子
cookis
能解释一下
else normal++;} END{if (normal + 1 == NR) print "alls are normal!"}'
后面这些么

而且我运行了一下好像不灵
没有输出
作者: cookis    时间: 2009-08-14 10:37
标题: 回复 #6 where27 的帖子
我这儿运动正常啊,

else normal++;} END{if (normal + 1 == NR) print "alls are normal!"}'

记录一下正常的个数,END 这个关键字是表示在所有的 行都处理完后,执行END后边的语句  normal + 1 是算上标题行
作者: where27    时间: 2009-08-14 10:41
标题: 回复 #7 cookis 的帖子
en
没问题了
厉害!
作者: where27    时间: 2009-08-14 10:42
标题: 回复 #5 Minsic 的帖子
你这个也很好使
谢谢了
作者: alenbull    时间: 2009-08-14 12:53
为何不装个nagios
作者: blackold    时间: 2009-08-14 12:57

作者: 小木虫子    时间: 2009-08-14 13:03
原帖由 where27 于 2009-8-14 09:58 发表
日常巡检的脚本
以检查磁盘使用为例
[root@localhost]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda2             3.8G  3.4G  0.4G  89% /
/dev/sda3              14G  164 ...



df -h |awk '{if ( $5 < "80%" ) print "正常",$6};{if ( $5 > "80%" && $5 < "90%" ) print "使用超过80%",$6};{if ( $5 > "90%" ) print "使用超过90%",$6}' | tail -n +2




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2