Register
It is currently Fri Nov 21, 2014 8:46 pm

Status change


All times are UTC - 6 hours


Post new topic Reply to topic  [ 37 posts ]  Go to page 1, 2  Next
Author Message
 PostPosted: Sat Apr 24, 2010 2:03 pm   

Joined: Fri May 16, 2008 4:58 am
Posts: 94
I need to do simple status monitoring script that will get status of a server from a webpage and then post/email it (not decided yet)

so something like

curl http://www.domain.com/status.html
grep Offline status.htm > /dev/null

if....

But I can't work out how to manage the status changes. The status is either online or offline but I only need to be informed if the status is different to the previous one ie If goes online I get a message saying it's online but I won't get another message till it changes to offline so I don't get bombarded with updates every time the script runs.


Top
 Profile  
 PostPosted: Sat Apr 24, 2010 2:30 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 567
something like
Code:
ServerStatus=

if curl -o - http://www.domain.com/status.html | grep Offline status.htm &>/dev/null
then if [ "$ServerStatus" = Online ]
     then sed 's/^ServerStatus=.*/ServerStatus=Offline/' $0
          mail -s'ServerStatus changed' ...
     fi
fi
I didn't test, it's just an idea.


Top
 Profile  
 PostPosted: Sun Apr 25, 2010 2:54 am   

Joined: Fri May 16, 2008 4:58 am
Posts: 94
I've tried it and I thought i'd get it to post the status update to twitter, the code seems to work apart from the bit that actually posts the update?

Code:
ServerStatus=

if curl -s -u user:password http://IP-Address/status.htm | grep Off &>/dev/null
then if [ "$ServerStatus" = On ]
     then sed 's/^ServerStatus=.*/ServerStatus=Off/' $0
curl -u username:password -d status="Server Offline" http://twitter.com/statuses/update.xml
     fi
fi

sleep 1
done


Top
 Profile  
 PostPosted: Thu Apr 29, 2010 2:38 pm   

Joined: Fri May 16, 2008 4:58 am
Posts: 94
anyone?


Top
 Profile  
 PostPosted: Mon May 03, 2010 10:51 am   

Joined: Fri May 16, 2008 4:58 am
Posts: 94
I still can't figure it out it's dring me mad ha


Top
 Profile  
 PostPosted: Mon May 03, 2010 8:53 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 567
may be ServerStatus should not be empty when you launch the script the first time


Top
 Profile  
 PostPosted: Fri May 07, 2010 1:30 pm   

Joined: Fri May 16, 2008 4:58 am
Posts: 94
Doh! how could I miss that, cheers Watael

When I run script using bash -x and server is online I get

Code:
+ true
+ ServerStatus=On
+ curl -s -u user:password http://IP-Address/status.htm
+ grep Off
+ sleep 1


When I take the server offline I get

Code:
+ '[' On = On ']'
+ sed 's/^SrverStatus=.*/ServerStatus=Off/' script
#!/bin/bash

## Web Get Parameters
user="user"
password="password"
url="IP-Address/status.htm"

## Other Parameters

while true
do

ServerStatus=Off

if curl -s -u admin:password http://IP-Address/status.htm | grep Off &>/dev/null
then if [ "$ServerStatus" = On ]
     then sed 's/^ServerStatus=.*/ServerStatus=Off/' $0
curl -u username:password -d status="Server Off" http://twitter.com/statuses/update.xml
     fi
fi


sleep 1
done
+ curl -u username:password -d status="Server Off" http://twitter.com/statuses/update.xml


It then updates the twitter page with Server Off however it keeps trying to post the same status again and again which gets denied as it's a duplicate entry I only want it to update if status is changed.

When I put the server back online I get

Code:
+ true
+ ServerStatus=On
+ curl -s -u user:password http://IP-Address/status.htm
+ grep Off
+ sleep 1


But it does not update twitter with server online


Top
 Profile  
 PostPosted: Fri May 07, 2010 6:09 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 567
if you're using GNU/sed add the -i option
otherwise you have to pass by a temporary file, that you'll have to copy to your scriptname, and remove.


Top
 Profile  
 PostPosted: Sun May 23, 2010 9:22 am   

Joined: Fri May 16, 2008 4:58 am
Posts: 94
I tried doing

Code:
then sed -i 's/^ServerStatus=.*/ServerStatus=Off/' $0


and take the server offline I still get the same result

Code:
<?xml version="1.0" encoding="UTF-8"?>
<hash>
  <request>/statuses/update.xml</request>
  <error>Status is a duplicate.</error>
</hash>
<?xml version="1.0" encoding="UTF-8"?>
<hash>
  <request>/statuses/update.xml</request>
  <error>Status is a duplicate.</error>
</hash>


and when I put it online I get no output


Top
 Profile  
 PostPosted: Sun May 23, 2010 10:15 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 567
ah, you're using an infinite while loop, thus the script is not being re-read.

You could use an extra file where you'll check/modify known-status.
Code:
#!/bin/bash
##

## Web Get Parameters
user="user"
password="password"
url="IP-Address/status.htm"
StatusFile=/tmp/TwitterStatus #contains only 0/1: 0=Off 1=On
## Other Parameters

while true
do ServerStatus="$(<$StatusFile)"
   if curl -s -u admin:password http://IP-Address/status.htm | grep Off &>/dev/null && ((ServerStatus))
   then echo "0" > $StatusFile
        curl -u username:password -d status="Server Off" http://twitter.com/statuses/update.xml
   else [ -s $StatusFile ] && echo "1" > $StatusFile
   fi
   sleep 1
done


Top
 Profile  
 PostPosted: Sun May 23, 2010 1:50 pm   

Joined: Fri May 16, 2008 4:58 am
Posts: 94
I see where you coming from with the tmp file but it's not working, if I tail TwitterStatus while running the script there's no output and twitter does not get updated at all.

Code:
bash -x script shows

when offline

+ ServerStatus=
+ curl -s -u admin:password http://IP-Address/status.htm
+ grep Off
+ (( ServerStatus ))
+ '[' -s /tmp/TwitterStatus ']'
+ sleep 1
+ true

when online

+ ServerStatus=
+ curl -s -u admin:password http://IP-Address/status.htm
+ grep Off
+ '[' -s /tmp/TwitterStatus ']'
+ sleep 1
+ true


Top
 Profile  
 PostPosted: Sun May 23, 2010 2:34 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 567
get rid of the -s test, and just echo 1 >$StatusFile
because -s tests if file exists, but it doesn't exist at first run


Top
 Profile  
 PostPosted: Mon May 24, 2010 1:15 am   

Joined: Fri May 16, 2008 4:58 am
Posts: 94
Getting very close now, tmp file seems to update ok now, twitter updates when server taken offline but keeps trying to post duplicate I only want twitter updated if the status changes.

Also need to update twitter when online


Top
 Profile  
 PostPosted: Wed May 26, 2010 4:43 am   

Joined: Fri May 16, 2008 4:58 am
Posts: 94
Ideally i'd like to get this working in the next couple of days, if someone can help me via msn i'll happily donate $5 to them.

PM me for address


Top
 Profile  
 PostPosted: Sat May 29, 2010 6:52 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 567
let's try another appoach:
Code:
#!/bin/bash

## Web Get Parameters
user="user"
password="password"
url="IP-Address/status.htm"
## Other Parameters
statusFile=/tmp/StatusFile
>$statusFile       

while :
do actualStatus="$(lynx --dump -auth $user:$password "$url")"
   previousStatus="$(<$statusFile)"
   if [ "$actualStatus" != "$previousStatus" ]
   then curl -u username:password -d status="$actualStatus" http://twitter.com/statuses/update.xml
   else echo "$actualStatus" > $statusFile
   fi
   sleep 1s
done


Top
 Profile  
 PostPosted: Sun May 30, 2010 6:04 am   
User avatar

Joined: Tue Apr 27, 2010 2:28 pm
Posts: 172
Location: Czech Republic
Watael wrote:
Code:
...
   if [ "$actualStatus" != "$previousStatus" ]
   then ...
   else echo "$actualStatus" > $statusFile
   fi

Is not the echo unnecessary? The actual status is the same as the previous one, so why saving it?


Top
 Profile  
 PostPosted: Sun May 30, 2010 7:18 am   

Joined: Fri May 16, 2008 4:58 am
Posts: 94
the status.htm file contains more text than just on line/off line so i'm getting

Code:
http://twitter.com/statuses/update.xml
<?xml version="1.0" encoding="UTF-8"?>
<hash>
  <request>/statuses/update.xml</request>
  <error>Status is over 140 characters.</error>
</hash>


as twitter only accepts 140 characters, where do I need to put in a grep for off line/on line?


Top
 Profile  
 PostPosted: Sun May 30, 2010 9:31 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 567
Quote:
Is not the echo unnecessary? The actual status is the same as the previous one, so why saving it?
right! but echo is needed, not the else
Code:
   if [ "$actualStatus" != "$previousStatus" ]
   then curl -u username:password -d status="$actualStatus" http://twitter.com/statuses/update.xml
        echo "$actualStatus" > $statusFile
   fi

Quote:
the status.htm file contains more text than just on line/off line
can you paste lynx result?


Top
 Profile  
 PostPosted: Sun May 30, 2010 11:59 am   

Joined: Fri May 16, 2008 4:58 am
Posts: 94
Code:
   Server Status
            ETH0

                    Server Name    : HOME
                    Server Status  : Off line

                     [BUTTON]


Top
 Profile  
 PostPosted: Sun May 30, 2010 1:04 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 567
well, I did slight changes, it seems to work
Code:
#!/bin/bash
#set -x

## Web Get Parameters
user="user"
password="password"
url="IP-Address/status.htm"
## Other Parameters
statusFile=/tmp/StatusFile
>$statusFile

while :
do actualStatus="$(sed -n '/Status[[:blank:]]*:/s/.*\(O[^[:blank:]]*\).*/\1/p' <(lynx --dump -auth $user:$password "$url"))"
   previousStatus="$(<$statusFile)"
   if [ "$actualStatus" != "$previousStatus" ]
   then curl -u username:password -d status="Server $actualStatus" \
http://twitter.com/statuses/update.xml
        echo "$actualStatus" > $statusFile
   fi
   sleep 1s
done


Last edited by Watael on Sun May 30, 2010 1:47 pm, edited 2 times in total.

Top
 Profile  
 PostPosted: Sun May 30, 2010 1:43 pm   

Joined: Fri May 16, 2008 4:58 am
Posts: 94
I've edited parameters and now at last hurdle I think, server was off line and I put it on line

Code:
curl: (6) Couldn't resolve host 'Off"'
<?xml version="1.0" encoding="UTF-8"?>
<hash>
  <request>/statuses/update.xml</request>
  <error>Status is a duplicate.</error>
</hash>
curl: (6) Couldn't resolve host 'On"'
<?xml version="1.0" encoding="UTF-8"?>
<hash>
  <request>/statuses/update.xml</request>
  <error>Status is a duplicate.</error>
</hash>


Code:
#!/bin/bash
#set -x

## Web Get Parameters
user="user"
password="password"
url="http://IP/Status.htm"
StatusFile=/tmp/StatusFile
## Twitter Parameters
t_user=
t_passw=


>$StatusFile

while :
do actualStatus="$(sed -n '/Status[[:blank:]]*:/s/.*\(O[^[:blank:]]*\).*/\1/p' <(lynx --dump -auth $user:$password "$url"))"
   previousStatus="$(<$StatusFile)"
   if [ "$actualStatus" != "$previousStatus" ]
   then curl -u t_user:t_passw -d status=\"Server $actualStatus\" http://twitter.com/statuses/update.xml
        echo "$actualStatus" > $StatusFile
   fi
   sleep 1s
done


Last edited by BrianUK on Sun May 30, 2010 1:47 pm, edited 1 time in total.

Top
 Profile  
 PostPosted: Sun May 30, 2010 1:46 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 567
uncomment '#set -x' line, and please show output.


Last edited by Watael on Sun May 30, 2010 1:52 pm, edited 2 times in total.

Top
 Profile  
 PostPosted: Sun May 30, 2010 1:52 pm   

Joined: Fri May 16, 2008 4:58 am
Posts: 94
Code:
++ sed -n '/Status[[:blank:]]*:/s/.*\(O[^[:blank:]]*\).*/\1/p' /dev/fd/63
+++ lynx --dump -auth admin:password http://IP/Status.htm
+ actualStatus=Off
+ previousStatus=
+ '[' Off '!=' '' ']'
+ curl -u user:passw -d 'status="Server' 'Off"' http://twitter.com/statuses/update.xml
curl: (6) Couldn't resolve host 'Off"'
<?xml version="1.0" encoding="UTF-8"?>
<hash>
  <request>/statuses/update.xml</request>
  <error>Status is a duplicate.</error>
</hash>
+ echo Off
+ sleep 1s
+ :
++ sed -n '/Status[[:blank:]]*:/s/.*\(O[^[:blank:]]*\).*/\1/p' /dev/fd/63
+++ lynx --dump -auth user:password http://IP/Status.htm
+ actualStatus=Off
+ previousStatus=Off
+ '[' Off '!=' Off ']'
+ sleep 1s
+ :
++ sed -n '/Status[[:blank:]]*:/s/.*\(O[^[:blank:]]*\).*/\1/p' /dev/fd/63
+++ lynx --dump -auth user:password http://IP/Status.htm
+ actualStatus=Off
+ previousStatus=Off
+ '[' Off '!=' Off ']'
+ sleep 1s
+ :


+ echo On
+ sleep 1s
+ :
++ sed -n '/Status[[:blank:]]*:/s/.*\(O[^[:blank:]]*\).*/\1/p' /dev/fd/63
+++ lynx --dump -auth user:password http://IP/Status.htm
+ actualStatus=On
+ previousStatus=On
+ '[' On '!=' On ']'
+ sleep 1s
+ :
++ sed -n '/Status[[:blank:]]*:/s/.*\(O[^[:blank:]]*\).*/\1/p' /dev/fd/63
+++ lynx --dump -auth user:password http://IP/Status.htm
+ actualStatus=On
+ previousStatus=On
+ '[' On '!=' On ']'
+ sleep 1s
+ :
++ sed -n '/Status[[:blank:]]*:/s/.*\(O[^[:blank:]]*\).*/\1/p' /dev/fd/63
+++ lynx --dump -auth user:password http://IP/Status.htm
+ actualStatus=On
+ previousStatus=On
+ '[' On '!=' On ']'
+ sleep 1s
+ :
++ sed -n '/Status[[:blank:]]*:/s/.*\(O[^[:blank:]]*\).*/\1/p' /dev/fd/63
+++ lynx --dump -auth user:password http://IP/Status.htm
+ actualStatus=Off
+ previousStatus=On
+ '[' Off '!=' On ']'
+ curl -u user:passw -d 'status="Server' 'Off"' http://twitter.com/statuses/update.xml
curl: (6) Couldn't resolve host 'Off"'
<?xml version="1.0" encoding="UTF-8"?>
<hash>
<request>/statuses/update.xml</request>
<error>Status is a duplicate.</error>
</hash>
+ echo Off
+ sleep 1s
+ :
[/code]


Top
 Profile  
 PostPosted: Sun May 30, 2010 1:53 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 567
I forgot to delete \s after -d status :(


Top
 Profile  
 PostPosted: Sun May 30, 2010 1:54 pm   

Joined: Fri May 16, 2008 4:58 am
Posts: 94
So which bit do I remove?

status=\"Server $actualStatus\" ?


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 37 posts ]  Go to page 1, 2  Next

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP