简单脚本实现:
#!/bin/bash DEVICES="sda sdb" #有几块硬盘就写几块 SMARTCTL=/usr/sbin/smartctl SMART_TMP_FILE="/opt/nagios/tmp/.smart" for DEV in $DEVICES do REL=`sudo $SMARTCTL -H /dev/$DEV |egrep -i ‘(Health status|test result)‘ |awk -F: ‘{print $2}‘ |awk ‘{print $1}‘` echo -n "${DEV}:${REL} " >> $SMART_TMP_FILE done >> /tmp/eeee.log 2>&1 egrep -i ‘(FAILED|err)‘ $SMART_TMP_FILE RETVAL=$? if [ $RETVAL -ne 0 ] then echo "OK - `cat $SMART_TMP_FILE`" rm -f $SMART_TMP_FILE exit 0 else echo "CRITICAL - `cat $SMART_TMP_FILE`" rm -f $SMART_TMP_FILE exit 2 fi
脚本很简单,不过要执行起来不容易,root用户执行此脚本,测试一切没问题,可是用nagios用户运行问题就来了
首先是nagios用户没有执行smartctl命令的权限:修改soduers文件
执行visudo
在root ALL=(ALL) ALL下面添加
nagios ALL=(root) NOPASSWD:/usr/sbin/smartctl *
##NOPASSWD可以在脚本执行实不用输入密码
只改了这个还不行,当用nagios用户执行时还是会报错:sudo: sorry, you must have a tty to run sudo
再改:
还是执行visudo命令(当然执行vi /etc/sudoers也完全可以)
找到Defaults requiretty,前面加‘#’注释掉就一切OK了!
原文:http://www.cnblogs.com/xuxyblog/p/3885039.html