1

The following script is generating the desired out put but not redirecting the result to /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

If I cat the output file /home/myuser/slavedelay.html it gives.

<html>
</html>

Execution result :

sh slave_delay.sh 
<tr><td>sdb7</td><td>OK</td>
2
  • Put the outout file name in a variable so you're sure you don't have a typo in it (I'm guessing you edited it before posting and that could be the issue). Commented Sep 1, 2012 at 9:21
  • Tried still the same issue.. documentroot=/home/myuser/slavedelay.html, everywhere the documentroot has replaced with file path Commented Sep 1, 2012 at 9:29

2 Answers 2

2

Each time through the loop you're emptying the output file because of the command

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

So the first iteration writes the <tr> row to the file, then the next iteration overwrites the file and doesn't write those lines because $i isn't ert7.

Change it to:

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
Sign up to request clarification or add additional context in comments.

Comments

0

Replace :

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

with:

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

You use = operator in test also.

2 Comments

I tried, But the same result only. In console it is resulting properly. But not redirecting to the output file(/home/myuser/slavedelay.html)
It is a logical error, echo "<html>" > /home/myuser/slavedelay.html is given with in the for loop. instead need to give it before the loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.