Cristian Livadaru's blog

... think again ...

Use Adhearsion to Check Asterisk

If you have a business depending on asterisk you need to be sure that everything is running. You could use Nagios or Monit to check if asterisk is running, but is it also working? What about your AGI Server? Is it doing what it’s supposed to do?

The best way to find out if this works would be to call and see if the call is accepted and if you get something back from your AGI server and you can do this with automatically with Adhearsion!

What you will need is a wav file containing a DTMF sequence. You can create one with Adobe Audition for example (aka Cooledit before Adobe took over). The Trial version is enough to create the DTMF. Use a 300ms pause at least the other settings didn’t really work for me. Also don’t forget to set your Asterisk server to dtmfmod=inband.

I created a new adhearsion project and used the Simon game as start of and modified it to look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
methods_for :dialplan do
  def check_call
    CheckCall.new(self).start
  end
end

class CheckCall

  def initialize(call)
    @call = call
    @number='12345'
  end

  def start
    collect_attempt
    verify_attempt
  end

  def collect_attempt
    @attempt = @call.input @number.length,
      :timeout => 10.seconds
  end

  def verify_attempt
    if attempt_correct?
      # do something here
    else
      # do something else
    end
  end

  def attempt_correct?
    @attempt == @number
  end

end

What this does is check if the called server sent the DTMF sequence 12345 and if not it would do something.

You cold for example use growl and/or prowl for notification or use some SMS service for notification like lcx.at (I know, shameless self promotion).

Also don’t forget to activate drb in your adhersion project, do this in startup.rb

1
2
3
4
config.enable_asterisk
config.asterisk.enable_ami :host => "localhost", :username => "amiuser", :password => "amipassowrd", :events => true

config.enable_drb :host => "0.0.0.0", :deny => "0.0.0.0", :allow => ["127.0.0.1", "213.123.118.123"]

Ok now all we need is the script that triggers the call. This could look like this:

1
2
3
4
5
6
7
8
9
require 'drb'
@adhearsion = DRbObject.new_with_uri "druby://localhost:9050"
result = @adhearsion.originate({ :channel   => "SIP/....",
                                 :context   => "checkcall",
                                 :exten   => '4000',
                                 :priority  => 1,
                                 :callerid  => "43123456789",
                                 :async => 'true',
                                 :variable  => "call_launcher=true|sleep_time=3600" })

So, this is it. I know this isn’t really a copy & paste howto, some things are missing but I don’t want to repeat things that can be read in the adhearsion documentation.
Oh, what I forgot to mention, I use a separate server which runs Asterisk to do the checks, the checks are NOT done form the same server I am checking, but I think this should be obvious.
If you need a VPS with this Setup, or need someone to verify your VoIP Servers, feel free to contact me!