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.


Monday, 26 March 2012

On Adolescent Coders

Warning: Rant mode engaged

It appears to me that the Open Source Software (OSS) mantle has been passed on to a gaggle of post adolescent coders with some sort of group ADD disability.

Or something.

It started with the total fiasco that was KDE4, and all the idiocy that was apparently introduced on its release.

I didn't use KDE though, so although I was aware of all the angst, it didn't directly affect me.

Now we have Gnome 3, or more accurately, Gnome Shell, which is part of the new Gnome 3.

What an abomination. You can understand why Canonical balked at presenting their users with the steaming pile that is Gnome Shell, and it is almost understandable that they decided to promote their Unity shell from the netbook remix (where it belonged, and was actually quite useful in that context),  but in the age of HD, multi-monitor setups Unity too is totally underwhelming.

Alternatives such as Mate and Cinnamon are worthwhile projects which will hopefully mature to become usable desktops but right now the state of user interfaces in the OSS sphere is totally unsatisfactory (and yes, I have tried XFCE and KDE4 and I don't like them either. I like my menu bar at the top, my task bar at the bottom and the freedom to put icons wherever the hell I please, thank you.


However, as much as it may seem otherwise, this rant is not about the sorry state of OSS desktops at all.

It is about the state of OSS video players. Specifically, VLC, Totem and XBMC.

You see, back in the old days you would get an AVI file, or maybe an MPG file, and you could open that file in one of the afore-mentioned players and life was good.

However, if the movie was in a language you were not familiar with, you had a problem. To solve that problem some bright spark came up with the idea of having a subtitles file (.srt), which was OK, if a bit cumbersome.

Or, occasionally some mentally challenged dingbat would hard code subtitles into the actual video stream and not actually mention that fact when presenting the resulting file to the general public.

I've done my fair share of shouting spittle laden expletives at people who do that I assure you.

It would be better if somehow the subtitles and the audio/video could all be encapsulated in one file, right?

Clearly this is a good idea.

While you are at it, maybe we could include separate audio tracks for other languages. You could even put in the commentary track! Just like a DVD!

So the ever resourceful guys in OSS land went to work, and gave us "containers" (initially MKV but now also the venerable AVI files) that held all this extra data in one convenient file.

Life, you would think, is good.

But no.

Having spent all this time very cleverly making all this stuff work it seems the adolescents at the wheel have decided that we must all have the results of their handiwork shoved in our faces every time we watch a movie.

This is why, when you play a movie these days, a movie that has a whole lot of nice  subtitles conveniently bundled up into the container file, it plays with the subtitles ON by default.

Oh, I can hear you now. "Well, if it such a problem for you, then you can just turn them off!"

If it were only that simple.

Yes, you can turn them off every time you start a movie and it is not that hard. It is ctrl+t if I recall.

However, I don't want to have to do that every time I watch a movie, and if I am on my media centre I don't want to have to find where my keyboard is under a cushion on the couch (or where ever) just so I can turn the goddamn subtitles off.

Again!

I just want to watch the goddam movie.

Without subtitles.

I also wouldn't mind so much if it was a simple thing to turn them off globally, although I would still contend that having them on by default is borderline retarded and only required because the ADD sufferers who implemented the subtitle functionality wanted to make damn sure you got to experience the wonder of their coding prowess, but given a method to easily and permanently turn off subtitles I could forgive the ADD sufferers and give them their 15 seconds of fame.

After all, they earned it by contributing to the OSS pool, right?

Unfortunately,  the reality is somewhat different. The sad fact is that it is far from easy to turn subtitles off by default in VLC and to a lesser degree XBMC. I have not tried it with Totem because I don't use it much, but I have noticed it behaves this way as well.

Try doing a google search for "permanently disable subtitles vlc" and you will find vast numbers of people asking how to do it with a number of proposed, convoluted, obfuscated solutions, none of which seem to work on the latest version of VLC.

I spent half an hour on trying to do that without any success.

I've found it is easier to download mkvtoolnix  and actually remove the subtitle track than it is to fucking disable it permanently in VLC.

In Xbox Media Centre things are slightly better. You can't (apparently) disable subtitles globally in XBMC via the front end, but it is, at least, possible to go and edit a configuration file in a text editor to turn them off.

How very user friendly.

The fact is there is absolutely no reason to have subtitles on by default, certainly not when the default language is set to English. Sure, if my OS was configured to use Spanish by default then you could make a case to set subtitles to on, on the assumption that I wanted to watch the typical idiocy inducing rubbish that pours out of Hollywood with only an English soundtrack.

But I am not Spanish.

The unfortunate truth here is that this is the ADD adolescents telling users "we spent all this time making this shit work, and you are damn well going to use it, whether you like it or not."

It it weren't for the god-awful mess that MS appear to be making with Windows 8 it would almost be enough to push me back to the dark side I swear.

Wednesday, 21 March 2012

PHP: Create variable names from XML element names

I needed to convert an xml file into an array. You can use an element name to name a variable using this example.

        $string = "<postcode>5253</postcode>";
        $xml = new SimpleXMLElement($string);

        $varname = $xml->getName();

        ${$varname} = trim($xml);

        echo($varname." is ".${$varname}."\n");
        echo($varname." is ".$postcode."\n");
 
Output:
 
postcode is 5253
postcode is 5253 

Thursday, 8 March 2012

Remove Unwanted Audio Tracks From AVI Files

If you have downloaded videos from certain sources lately, you may have noticed that it is now possible to create a video container (AVI,MKV) that includes multiple audio channels just like on a DVD.

This is a great thing because it allows people of different languages to use the same video file. Alternately it allows the directors commentary to be included.

That said, I am an English speaker, and I have never had any interest in directors commentaries so all these extra audio tracks represent unwanted data in my movie library.

Also, some files default to playing the commentary or the non English track in some players which is also mildly annoying.

So, in shuch circumstances you can use ffmpeg to remove the unnecessary tracks from an AVI file (I have not tried it for MKV, I will update this page if I do.

Things you need to install are vlc and avconv (avconv is the replacement for ffmpg which is now deprecated)

sudo apt-get install vlc libav-tools

Note: On RedHat based distributions you must install libav. ie:
yum install libav
You can see what audio tracks are available, and select them by opening the video file in vlc and looking in Audio>Audio Track.

Once you have determined which track you want to keep, you can run the file through avconv to strip the unwanted tracks. In this example I use the second map parameter to keep track 2 (ie lose track 1);

avconv -i sourcefile.avi -map 0:0 -map 0:2 -acodec copy -vcodec copy outfile.avi

And that's it, happy Linuxing (is that a word?)