1

以下脚本正在生成所需的输出,但未将结果重定向到 /home/myuser/slavedelay.html

#!/bin/bash

host=<ip>
echo $host
user=usr1
password=mypass
threshold=300
statusok=OK
statuscritical=CRITICAL

for i in ert7 ert9
do
echo "<html>" > /home/myuser/slavedelay.html

if [ "$i" == "ert7" ]; then
        slvdelay=`mysql  -u$user -p$password -h<ip> -S /backup/mysql/mysql.sock -e 'show slave status\G' | grep Seconds_Behind_Master | sed -e 's/ *Seconds_Behind_Master: //'`
         if [ $slvdelay -ge $threshold ]; then
                echo "<tr><td>$i</td><td>CRITICAL</td>"  >> /home/myuser/slavedelay.html
                echo "<tr><td>$i</td><td>CRITICAL</td>" 
        else
                echo "<tr><td>$i</td><td>OK</td>"  >> /home/myuser/slavedelay.html
                echo "<tr><td>$i</td><td>OK</td>"
         fi
fi
done
echo "</html>" >> /home/myuser/slavedelay.html

如果我 cat 输出文件 /home/myuser/slavedelay.html 它给出。

<html>
</html>

执行结果:

sh slave_delay.sh 
<tr><td>sdb7</td><td>OK</td>
4

2 回答 2

2

每次通过循环时,您都会因为命令清空输出文件

echo "<html>" > /home/myuser/slavedelay.html

所以第一次迭代将<tr>行写入文件,然后下一次迭代覆盖文件并且不写那些行,因为$iis not ert7

将其更改为:

for i in ert7 ert9
do
if [ "$i" == "ert7" ]; then
        echo "<html>" > /home/myuser/slavedelay.html
        slvdelay=`mysql  -u$user -p$password -h<ip> -S /backup/mysql/mysql.sock -e 'show slave status\G' | grep Seconds_Behind_Master | sed -e 's/ *Seconds_Behind_Master: //'`
         if [ $slvdelay -ge $threshold ]; then
                echo "<tr><td>$i</td><td>CRITICAL</td>"
        else
                echo "<tr><td>$i</td><td>OK</td>"
         fi | tee -a /home/myuser/slavedelay.html
        echo "</html>" >> /home/myuser/slavedelay.html
fi
done
于 2012-09-01T09:39:16.753 回答
0

代替 :

if [ "$i" == "ert7" ];

和:

if [ "$i" = "ert7" ];

=您也在测试中使用运算符。

于 2012-09-01T09:17:59.497 回答