[asterisk-users] How does AMI work with events ?

Daniel Tryba daniel at tryba.nl
Tue Sep 6 11:59:45 CDT 2011


On Mon, Sep 05, 2011 at 02:41:50PM +0200, Jonas Kellens wrote:
> I read some information and examples on the net, but they all show how 
> you login to the AMI, give an action and receive a response. The end.
> I guess you just re-run the script every time you want the action to be 
> executed.
> 
> How then does this work when you use events ? If I want to use 
> "PeerStatus" to monitor the state of a SIP peer, how can I run a script 
> on change of peer status ?
[snip]
> That's it. I'm logged in and I have been subscribed to receive peer 
> status changes. Where do these peer changes appear ? I really don't get 
> that.

This is fairly basic stuff:
1-connect to AMI
2-authenticate
3-subscribe to events
4-listen for events
5-check if wanted event and do something with it, else
6-go back to listening

So in your script you are connected, just start listening (fgets()) and
check if the appropriate event arrived (keep reading lines till first
empty line). Parse the event and do something (mail in your case) when
required.

Examples of PeerStats Events:
http://www.voip-info.org/wiki/view/asterisk+manager+events#PeerStatusEvent

Creating a prototype daemon in PHP should be easy, but I experienced
weird hangups in PHP with longlived sockets.

Personally I monitor SIP/IAX peers by parsing:
asterisk -nrx "sip show peers" 
with Nagios/NRPE scripts.

-- 

   Daniel Tryba



More information about the asterisk-users mailing list