Sunday 1 June 2008

Asterisk AGI

Despite using FastAGI on Asterisk very successfully for a while now, I needed today to use good old normal AGI. The application was simple - I needed to check the dialled number against a flat text file and then choose how to route it.

Simple in principle, and the Perl code was a breeze. However, it needed to retrieve a variable from the dial plan at the beginning and then set another at the end. Could I get that variable's value? Could I buggery. I scoured the forums and Google for information but all anyone had to say on the matter was "don't use AGI->Verbose() with a \n before get_ or set_variable" which was of absolutely no use to me. I couldn't actually retrieve any variable with agi->get_variable().

A few hours of trial and error later, and my script is running like a dream. What did I do? Well, it goes like this ...
  1. My kit : Centos5.1 server on a quad core running Asterisk 1.4.18.1.

  2. Install the Asterisk::AGI module by typing :
    perl -MCPAN -e 'install Asterisk::AGI'

  3. Start your AGI program like this :
    #!/usr/bin/perl
    use strict; # Not essential, but always adviseable.
    use Asterisk::AGI;
    my $agi = new Asterisk::AGI;
    my %dummyvar = $agi->ReadParse();

  4. That last line is the key. Without that there was no way any calls to $agi->get_variable() were going to work. Why? Who knows, certainly no one on the forums seemed to. What it does is load the hash dummyvar with all the environment & call information passed when the agi call was made. You don't need to do anything with it.
Why did I need to resort to standard AGI when I've stated I'm using FastAGI already and very successfully (it's handling over 1,000 simultaneous call flows across many machines)? Well, for some reason my FastAGI application was, despite appearing to close it's process down once completed, not returning control to the dial plan for up to 20 seconds. It was fine if I sent a hang up, but I didn't want to hang up. Anyway, that's an ongoing investigation for me. Any advice gratefully accepted ...

Wednesday 9 January 2008

I wandered lonely in a crowd ...

My recent attempts at pretending to be a grown up have caused me to stumble away from my nice, secure (if somewhat insecure) PC-running-Windows environment and headlong into the bewildering, seemingly drug induced confusion that is Linux.

The reason for this is that my income is primarily from telephony based products and a recent contract delivered me to the world of Asterisk, and any serious commercial application of Asterisk tends to force you to use Linux.

I must admit that, despite my initial hostility I have become completely won over. Yes, it's quirky. No, I STILL can't get my sound card to recognise a microphone. Yes, it's a bit like learning Mandarin. No, I have no idea what my main hard drive is called. But, despite all of this, I now have Ubuntu 7.10 as my main operating system with a laptop running Vista for critical emergencies only (and for running RemoteAdmin for access to machines that run nothing else).

The purpose of this blog is not, however, to convert people to Linux, but to offer up little snippets of advice or code that may well help others who feel a bit lost realise that they are not alone in what seems to be a crowd of people who all know where they're going.

I will only be posting sporadically on subjects such as Asterisk, Freeswitch, Perl, PHP & various Linux utilities such as GREP, AWK, SED, etc.

I hope these posts help someone, somewhere.