Return to Snippet

Revision: 20055
at November 5, 2009 11:11 by xxtjaxx


Initial Code
#!/bin/bash

# serverstatus 0.2
# by Fabian A. Scherschel (fabsh) <[email protected]>
# Licensed under GPLv3 or newer, see http://gplv3.fsf.org
#
# This command will check if a specified URL is up and send a 
# status notification to identi.ca
# Improvements by Andreas Marschke <[email protected]> 

#Options 
# --url=      :    URL You want to test
# --admin=    :    Admin account to be notified about the change in an @reply
# --account=  :    Account of the status script
# --password= :    the password of the notifier account
# --globdns=  :    the IP Address of a known dns that should work
#                  when the machine is connected to the network

# Call this command with the URL you wish to check as a parameter

if [ $(echo $@ | sed 's:\ :\n:g' | grep "help" | sed 's:\-\-help=::') ]; then
    echo "This command will check if a specified URL is up and send a "
    echo "status notification to identi.ca"
    echo "Options: "
    echo " --url=      :    URL You want to test"
    echo " --admin=    :    Admin account to be notified about the change in an @reply"
    echo " --account=  :    Account of the status script"
    echo " --password= :    the password of the notifier account"
    echo " --globdns=  :    the IP Address of a known dns that should work"
    echo "                 when the machine is connected to the network"
    echo "Call this command with the URL you wish to check as a parameter."
    exit 0
fi

URL=$(echo $@ | sed 's:\ :\n:g' | grep "url" | sed 's:\-\-url=::')
adminAcc=$(echo $@ | sed 's:\ :\n:g' | grep "admin" | sed 's:\-\-admin=::')
ACCOUNT=$(echo $@ | sed 's:\ :\n:g' | grep "account" | sed 's:\-\-account=::')
PASSWORD=$(echo $@ | sed 's:\ :\n:g' | grep "password" | sed 's:\-\-password=::')

#this is a general DNS server that serves as a open dns server 
#it should be used to determine wether your server or the other one is disconnected
#or actually down
if [ $(echo $@ | sed 's:\ :\n:g' | grep "globdns") ]; then 
    glob_dns=$(echo $@ | sed 's:\ :\n:g' | grep "globdns" | sed 's:\-\-globdns=::')
else
    #add a default one if you are unsure if the one you choose when running the script is online
    glob_dns=208.67.220.220
fi

#ping test for the server the script is running on  
pingtestdns=$( ping -c 3 $glob_dns | cut -d: -f2 | grep -o 'Unreachable' )
pingtest=$( ping -c 3 $(echo $URL | sed 's+http://++g') | cut -d: -f2 | grep -o 'icmp')
pingstat=$( ping -c 3 $(echo $URL | sed 's+http://++g') | cut -d: -f2 | grep 'time=[0-9][0-9]*\ ms' | cut -d" " -f4 | sed s/time=//g  | sed s:\ :\ ms\ :g)

# Check for commandline parameters.
if [ ! $URL ]; then
    echo Error: No URL provided!
    echo Pass the website URL as an argument.
    exit 1
fi
if [ ! $ACCOUNT ]; then 
    echo Error: No Account provided!
    echo Pass the Account of the script as an argument.
    exit 1
fi

lynx -connect_timeout=30 -dump -error_file=/tmp/x$$ $URL &> /dev/null

# If the error_file is neither created nor the STATUS is written to it, website should be down
statuscode=(`awk '/STATUS/{print $2}' /tmp/x$$ 2>/dev/null`)
if [ $? != 0 ]; then
    if [ $pingtestdns ]; then
	echo "At $(date): server not connected to the net.\n" >> /tmp/serverstatus.log
    else
	if [ ! $pingtest ]; then
            curl --silent -o /dev/null -u $ACCOUNT:$PASSWORD -d status="#alert @$adminAcc $URL is DOWN" http://identi.ca/api/statuses/update.xml
	fi
    fi
fi

# If we get a STATUS of 200, website is up
case ${statuscode[*]} in
        *200*) curl --silent -o /dev/null -u $ACCOUNT:$PASSWORD -d status="@$adminAcc $URL is up! stats:$(echo $pingstat)" http://identi.ca/api/statuses/update.xml
esac

# Remove temporary error_file
if [ -f /tmp/x$$ ]; then
    rm -f /tmp/x$$
fi

Initial URL


Initial Description
Originally written by Fabian Scherschel. Licensed under GPLv3.
I improved some parts of the usabillity.
If the server is online the identi.ca account will even send ping stats.

Initial Title
send the status of  a  offshore server to identi.ca

Initial Tags


Initial Language
Bash