|
From: Jeff L. <je...@je...> - 2016-03-22 18:14:39
|
Hello,
I started what I hoped to be a simple hack this morning and found pyst.
I would like to use the asterisk AMI to pull a list of SIP peers and
their status. I am doing this today on a number of our customer
asterisk instances using bourne shell and "asterisk -x "sip show
peers'", then parsing the output, but this is turning out to be quite an
intensive act to perform on the instance, thus the desire to use AMI.
After a few hours of surfing and playing around, I have ended up with this:
--------------
root@jlpenha:/usr/local/bin# more pypeers
#!/usr/bin/python
from asterisk import manager
import signal
ip = "127.0.0.1"
user = "XXX"
passwd = "XXX"
peers = []
def handleEvent(event, ami):
print ("Received event: %s" % event.name)
if event.name == 'PeerlistComplete':
printResults()
ami.close()
#print ("Received event Header: %s" % event.headers)
#add to peers[]
def eventTimeout(signum, frame):
print "Timeout!"
ami.close()
def printResults():
print peers
ami = manager.Manager()
ami.connect(ip)
ami.login(user, passwd)
ami.register_event('PeerEntry', handleEvent)
ami.register_event('PeerlistComplete', handleEvent)
signal.signal(signal.SIGALRM, eventTimeout) # Sippeers tends to hang?
signal.alarm(5)
response = ami.send_action({'Action' : 'Sippeers'})
ami.message_loop()
print response
---------------
In testing I first noticed that about 30% of the time the script hangs,
thus I implemented the signal timeout. It seems to hang because I never
get the PeerlistComplete event. Is this a problem with asterisk not
sending it or some kind of bug in pyst?
I next noticed that I rarely get the same number of peers:
--------
root@jlpenha:/usr/local/bin# ./pypeers | wc -l
27
root@jlpenha:/usr/local/bin# ./pypeers | wc -l
68
root@jlpenha:/usr/local/bin# ./pypeers | wc -l
71
root@jlpenha:/usr/local/bin# ./pypeers | wc -l
67
root@jlpenha:/usr/local/bin# ./pypeers | wc -l
64
-------
This can't possibly be so difficult. Surely someone has done what I am
trying to do, though I haven't found any examples with lots of googling
today. Can anyone chime in?
Thanks!
j
|