Friday 27 April 2012

Simple Monitoring of VPN links

    I use this script to check the status of non-critical vpn links, but you could use it for anything where a simple ping test is sufficient and you couldn't be bothered setting up SNMP.

Of course SNMP + Nagios is a better choice for mission critical monitoring than simply pinging something like I do here.

Here is the script;
#!/bin/bash

TIMEOUT=5  # Number of failed attempts before sending email
STATUSDIR=/tmp/vpn # Status files are written here
ADDRESS=$1  # The target address to be monitored
EMAIL=$2  # Optional email address to send alerts to

if [ ! -d $STATUSDIR ] ; then
 mkdir -p $STATUSDIR
fi

if [ ! -f $STATUSDIR/$ADDRESS ] ; then
 `echo "0" > $STATUSDIR/$ADDRESS`
fi

EXPECTEDCOUNT=`cat $STATUSDIR/$ADDRESS`

if ping -c 1 -w 5  "$ADDRESS" &>/dev/null ; then
 DOWNCOUNT=0
else
 DOWNCOUNT=$(($EXPECTEDCOUNT+1)) 
fi

echo "Expected count is :"$EXPECTEDCOUNT
echo "Down count is     :"$DOWNCOUNT

# Something has changed
if [ ! $EXPECTEDCOUNT = $DOWNCOUNT ] ; then
 if [ $DOWNCOUNT = 0 ] ; then
  STATUS="UP"
 else
  STATUS="DOWN"
 fi
 
 MSG="vpn-link: "$ADDRESS" is "$STATUS" (count="$DOWNCOUNT")" 
 logger $MSG

 # if the change was to 0 or TIMEOUT then trigger an email
 if [ $DOWNCOUNT = 0 -o $DOWNCOUNT = $TIMEOUT ] ; then
  
  # If the expected count has not reached the timeout setting then
  # we dont want to send email 
  EXPECTEDCOUNT=$(($EXPECTEDCOUNT+1))
  if [ $EXPECTEDCOUNT -ge $TIMEOUT -a -n "$EMAIL" ] ; then
   echo $ADDRESS" is "$STATUS": Sending email" 
   mail -s "$MSG" $EMAIL < /dev/null  > /dev/null
  fi
 fi
 echo $DOWNCOUNT > $STATUSDIR/$ADDRESS
fi
    To use it simply place the script in a convenient location such as /usr/sbin and create an entry in your system crontab like this;

*  *    * * *    brett    /usr/sbin/vpn-mon 192.168.1.2 brett@example.com  >> /dev/null 2>&1


    This will ping test to 192.168.1.2 as user "brett" every minute and send email alerts to brett@example.com

    By default the script will send an email after the test has failed 5 consecutive times. This can be changed by editing the script and changing the TIMEOUT variable.

Monday 23 April 2012

Have CRON Log To A Separate File

Sometimes you might want to have cron events logged to a file other than the standard syslog (/var/log/syslog)

This is how you do it.

Edit this file;

vi /etc/rsyslog.d/50-default.conf

Find the line starting with #cron.* and uncomment it.

This will cause all cron events to be logged to /var/log/cron.log (unless you changed  the path) however the same events will also continue to be logged to syslog.

In the same file, find the line that looks like this;

*.*;auth,authpriv.none  -/var/log/syslog

Alter the line so that it looks like this;

*.*;auth,authpriv.none;cron.none  -/var/log/syslog

Restart the logging service;

service rsyslog restart

Now cron will log to /var/log/cron.log but not to syslog


Tuesday 17 April 2012

Coding WTF

I got asked to help on a small PHP project that has already been started by another coder.

The very first thing I was confronted with was this;



if($variable == false)
{
}
else
{
      //do something
}




Ummm, WTF?

Wednesday 11 April 2012

TMDB Scraper Fails in XBMC

A recent update to Xbox Media Centre (XBMC) has (temporarily) broken the ability to scrape The Open Movie Database returning an error "Unable to connect to remote server"

A bug report has been lodged.

The bugged version is git20120229 compiled April 7 2012 and is currently in the XBMC PPA.

You can check your verison of XBMC on the System->System Info screen within XBMC.

The offending version is shown as;

"XBMC 11.0 Git:Unknown (Compiled : Apr 7 2012)"

You can wait for a fix to come down the pipeline, or if you are impatient (like me) you can revert to the older version.

You will need to download these files;


xbmc_11.0~git20120321.14feb09-0ubuntu1~ppa1~oneiric_all.deb 

xbmc-bin_11.0~git20120321.14feb09-0ubuntu1~ppa1~oneiric_amd64.deb 

 
Next, uninstall the bugged version;

sudo apt-get remove xbmc

Now, install the previous XBMC release from the files you downloaded;


sudo dpkg -i xbmc-bin_11.0~git20120321.14feb09-0ubuntu1~ppa1~oneiric_amd64.deb 

sudo dpkg -i xbmc_11.0~git20120321.14feb09-0ubuntu1~ppa1~oneiric_all.deb


Now, you should be able to scrape TMDB without connection issues.