Archive

Posts Tagged ‘server’

tweeting server status

March 11th, 2011 No comments

Since I’ve discovered twidge I find Twitter very useful, as my Server tweets me it’s status.
Therefore I wrote the following simple shell-script:

#!/bin/bash
#CC-BY-SA 11th of March 2011 by Lx
time=`date +"%d/%b/%Y:%H:%M:%S %z"`
if [ ! -e "~/log/$1_status.log" ]; then touch ~/log/$1_status.log; fi
lastlog_on=`tail -n 1 ~/log/$1_status.log | grep ONLINE`
lastlog_off=`tail -n 1 ~/log/$1_status.log | grep OFFLINE`
control=`ping -c 3 -W 1 google.com | grep "[1-3][[:space:]]\(packets received\|received\)"`
if [ -n $1 ]; then
 echo "Sending pings to "$1
 pong=`ping -c 3 -W 1 $1 | grep "[1-3][[:space:]]\(packets received\|received\)"`
 echo "Ping result: "$pong
 if [ -n "$pong" ]; then
  echo "Host "$1" is online"
  if [ -z "$lastlog_on" ]; then
   echo $time" "$1" status ONLINE" >> ~/log/$1_status.log
   echo "Host "$1" is online" | twidge dmsend twitteraccount
  fi
 elf [ -n "$control" ]; then
  echo "Host "$1" is offline"
  if [ -z "$lastlog_off" ]; then
   echo $time" "$1" status OFFLINE" >> ~/log/$1_status.log
   echo "Host "$1" is offline" | twidge dmsend twitteraccount
  fi
 else
  echo "Network down"
 fi
elif [ -z $1 ]; then
 echo "Usage: $arg0 hostname|ip"
fi
exit;

Then I use a cronjob accordingly:

*/5 * * * * sh ~/path/to/shellscript.sh foo.bar.baz > /dev/null 2>&1

You can easily also tweet the server uptime, load or whatever 😎

uptime | twidge update twitteraccount

Have phun!