Simple Script implementation:
#!/bin/bashdevices="SDA SDB"#有几块硬盘就写几块Smartctl=/usr/sbin/Smartctlsmart_tmp_file="/opt/nagios/tmp/.smart" forDEVinch$DEVICES DoREL=`sudo$SMARTCTL-H/dev/$DEV |Egrep-I.'(health status|test result)'|awk-F:'{print $}'|awk '{print $}'` Echo-N"${dev}:${rel}">>$SMART _tmp_file Done>>/tmp/eeee.log2>&1Egrep-I.'(Failed|err)'$SMART _tmp_fileretval=$?if[$RETVAL-ne0 ] Then Echo "OK-' cat $SMART _tmp_file '" RM-F $SMART _tmp_file exit0Else Echo "CRITICAL-' cat $SMART _tmp_file '" RM-F $SMART _tmp_file exit2fi
The script is simple, but it's not easy to execute, the root user executes the script, tests everything okay, but runs the problem with Nagios users.
First, the Nagios user does not have permission to execute the SMARTCTL command: Modify the Soduers file
Executive Visudo
Under Root all= (All), add the following
Nagios all= (Root) Nopasswd:/usr/sbin/smartctl *
# #NOPASSWD可以在脚本执行实不用输入密码
It's just not going to work, and when you do it with Nagios users will get an error:Sudo:sorry, you must has a TTY to run sudo
Further changes:
Or execute the visudo command (Of course execute vi/etc/sudoers is also perfectly possible)
Find Defaults requiretty, front Plus ' # ' comment off all OK!