【发布时间】:2017-01-24 17:33:48
【问题描述】:
我在这篇文章中写了一个小 bash 脚本:How to search for a string in a text file and perform a specific action based on the result
我注意到,当我运行脚本并检查日志时,一切似乎都在工作,但是当我查看 Nagios UI 时,我的文本文件中列出的几乎一半的服务器没有禁用它们的通知。脚本的修改版本如下:
host=/Users/bob/wsus.txt
password="P@assw0rd123"
while read -r host; do
region=$(echo "$host" | cut -f1 -d-)
if [[ $region == *sea1* ]]
then
echo "Disabling host notifications for: $host"
curl -vs -o /dev/null -d "cmd_mod=2&cmd_typ=25&host=$host&btnSubmit=Commit" https://nagios.$region.blah.com/nagios/cgi-bin/cmd.cgi" -u "bob:$password" -k 2>&1
else
echo "Disabling host notifications for: $host"
curl -vs -o /dev/null -d "cmd_mod=2&cmd_typ=25&host=$host&btnSubmit=Commit" https://nagios.$region.blah02.com/nagios/cgi-bin/cmd.cgi" -u "bob:$password" -k 2>&1
fi
done < wsus.txt >> /Users/bob/disable.log 2>&1
如果我手动对有问题的服务器运行命令,它会在 Nagios UI 中被禁用,所以我有点困惑。仅供参考,我也不太精通 Bash,所以这是我尝试将这个过程自动化一点的尝试。
【问题讨论】:
-
您能否从日志中确定问题是由“while”循环未读取丢失服务器的行引起的,还是因为调用了命令但失败了?
-
将主机变量重命名为
hostsfile=/Users/bob/wsus.txt,确保该文件存在,然后将完成的行更改为done < $hostsfile >> /Users/bob/disable.log 2>&1。应该这样做。 -
@NagiosSupport 成功了,谢谢!我还注意到之前的一些“故障”服务器在我的文本文件中具有错误的 FQDN...
标签: bash while-loop nagios