send the status of a offshore server to identi.ca


/ Published in: Bash
Save to your folder(s)

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.


Copy this code and paste it in your HTML
  1. #!/bin/bash
  2.  
  3. # serverstatus 0.2
  4. # by Fabian A. Scherschel (fabsh) <[email protected]>
  5. # Licensed under GPLv3 or newer, see http://gplv3.fsf.org
  6. #
  7. # This command will check if a specified URL is up and send a
  8. # status notification to identi.ca
  9. # Improvements by Andreas Marschke <[email protected]>
  10.  
  11. #Options
  12. # --url= : URL You want to test
  13. # --admin= : Admin account to be notified about the change in an @reply
  14. # --account= : Account of the status script
  15. # --password= : the password of the notifier account
  16. # --globdns= : the IP Address of a known dns that should work
  17. # when the machine is connected to the network
  18.  
  19. # Call this command with the URL you wish to check as a parameter
  20.  
  21. if [ $(echo $@ | sed 's:\ :\n:g' | grep "help" | sed 's:\-\-help=::') ]; then
  22. echo "This command will check if a specified URL is up and send a "
  23. echo "status notification to identi.ca"
  24. echo "Options: "
  25. echo " --url= : URL You want to test"
  26. echo " --admin= : Admin account to be notified about the change in an @reply"
  27. echo " --account= : Account of the status script"
  28. echo " --password= : the password of the notifier account"
  29. echo " --globdns= : the IP Address of a known dns that should work"
  30. echo " when the machine is connected to the network"
  31. echo "Call this command with the URL you wish to check as a parameter."
  32. exit 0
  33. fi
  34.  
  35. URL=$(echo $@ | sed 's:\ :\n:g' | grep "url" | sed 's:\-\-url=::')
  36. adminAcc=$(echo $@ | sed 's:\ :\n:g' | grep "admin" | sed 's:\-\-admin=::')
  37. ACCOUNT=$(echo $@ | sed 's:\ :\n:g' | grep "account" | sed 's:\-\-account=::')
  38. PASSWORD=$(echo $@ | sed 's:\ :\n:g' | grep "password" | sed 's:\-\-password=::')
  39.  
  40. #this is a general DNS server that serves as a open dns server
  41. #it should be used to determine wether your server or the other one is disconnected
  42. #or actually down
  43. if [ $(echo $@ | sed 's:\ :\n:g' | grep "globdns") ]; then
  44. glob_dns=$(echo $@ | sed 's:\ :\n:g' | grep "globdns" | sed 's:\-\-globdns=::')
  45. else
  46. #add a default one if you are unsure if the one you choose when running the script is online
  47. glob_dns=208.67.220.220
  48. fi
  49.  
  50. #ping test for the server the script is running on
  51. pingtestdns=$( ping -c 3 $glob_dns | cut -d: -f2 | grep -o 'Unreachable' )
  52. pingtest=$( ping -c 3 $(echo $URL | sed 's+http://++g') | cut -d: -f2 | grep -o 'icmp')
  53. 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)
  54.  
  55. # Check for commandline parameters.
  56. if [ ! $URL ]; then
  57. echo Error: No URL provided!
  58. echo Pass the website URL as an argument.
  59. exit 1
  60. fi
  61. if [ ! $ACCOUNT ]; then
  62. echo Error: No Account provided!
  63. echo Pass the Account of the script as an argument.
  64. exit 1
  65. fi
  66.  
  67. lynx -connect_timeout=30 -dump -error_file=/tmp/x$$ $URL &> /dev/null
  68.  
  69. # If the error_file is neither created nor the STATUS is written to it, website should be down
  70. statuscode=(`awk '/STATUS/{print $2}' /tmp/x$$ 2>/dev/null`)
  71. if [ $? != 0 ]; then
  72. if [ $pingtestdns ]; then
  73. echo "At $(date): server not connected to the net.\n" >> /tmp/serverstatus.log
  74. else
  75. if [ ! $pingtest ]; then
  76. curl --silent -o /dev/null -u $ACCOUNT:$PASSWORD -d status="#alert @$adminAcc $URL is DOWN" http://identi.ca/api/statuses/update.xml
  77. fi
  78. fi
  79. fi
  80.  
  81. # If we get a STATUS of 200, website is up
  82. case ${statuscode[*]} in
  83. *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
  84. esac
  85.  
  86. # Remove temporary error_file
  87. if [ -f /tmp/x$$ ]; then
  88. rm -f /tmp/x$$
  89. fi

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.