pseudo-realtime reporting of agent status in asterisk

Once you have a few agents on your asterisk box, and especially if they are distributed throughout the building, you quickly find yourself in need of a way of easily seeing who is or isn’t logged in. This helps with agent tracking, but it’s also useful for those times that you’re debugging your dialing stategies. So we’re going to write a Python script that will run constantly and update a database with all of the agents’ names and their current status. I’ve personally been running this for months with no issues as far as CPU load or instability in the Asterisk system.

First of all, you need to enable the manager interface. Look in /etc/asterisk/manager.conf. You’ll need something to the effect of…

[general]
enabled = yes
port = 5038
bindaddr = 0.0.0.0

[joe]
secret = schmoe
read = system,call,log,verbose,command,agent,user
write = system,call,log,verbose,command,agent,user

So Asterisk will be listening on the localhost address, on port 5038. Our user ‘joe’ is set up with the password ‘schmoe’. Now we need to connect to the server using Python. Here’s the script:
[python]
import socket, time, MySQLdb

def main():
db = MySQLdb.connect(host=”dbaddress”, user=”dbuser”, passwd=”dbpass”, db=”dbname”)
sql = db.cursor()
agents = {}
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((“localhost”, 5038))
while 1:
try:
s.send(“Action: Login\r\n”+”UserName: joe\r\n”+”Secret: schmoe\r\n\r\n”+”Action: Agents\r\n\r\n”)
time.sleep(1)
data = s.recv(30000)
for line in data.split(“\r\n”):
if line[:5] == ‘Event’:
if line[7:] == ‘AgentsComplete’:
break
if line[:5] == ‘Agent’:
agent = line[7:]
if line[:4] == ‘Name’:
name = line[6:]
if line[:6] == ‘Status’:
if vars().has_key(‘agent’) and vars().has_key(‘name’):
if not agents.has_key(agent):
agents[agent] = “”
if line[8:] <> agents[agent]:
agents[agent] = line[8:]
print name.ljust(15), agent.ljust(10), line[8:].ljust(20), time.ctime()
sql.execute(‘replace into ACD (agent, status, name) values (“‘+agent+'”,”‘+line[8:]+'”,”‘+name+'”)’)
s.send(“Action: Login\r\n”+”UserName: manager\r\n”+”Secret: manager\r\n\r\n”+”Action: Queues\r\n\r\n”)
time.sleep(1)
data = s.recv(30000)
for line in data.split(“\r\n”):
if line[:16] == “4 has”:
calls = int(line[line.find(“has “)+4:line.find(” calls”)])
if calls > 0:
print calls
sql.execute(‘replace into ACD (agent, status, name) values (“CALLS”,”‘+str(calls)+'”,”CALLS”)’)
except:
s.connect((“localhost”, 5038))

s.close()
[/python]
As you can see, I’m using sockets to connect to the manager interface. You can actually open up the interactive interface to Python and type these commands individually if you’d like to see the output that you’ll receive directly on the socket. Suffice it to say, the FOR statement starting on line 14 checks each line, looking for specific attributes that give us various info. In my case, I wanted to get the Agent ID, their Name, and their status. Once I have that information, I just insert it into a table that I created called ACD.

Something else that I should note is the FOR statement on line 33. This chunk writes a line to the same MySQL table with the total number of unanswered calls in the queue. This number allows me to know if customers are waiting unnecessarily long to get to an agent. You could obviously extend something like this to include timestamps, etc… allowing you to know average wait times and whatnot.

That’s basically it! At this point, you should install the script on your asterisk box as a daemon. Otherwise, you’ll need to keep a live SSH connection open specifically for this. Keep in mind, any time the Asterisk server dies for any reason, so does the manager interface, and your script. You’ll need to either restart it manually, or let your daemon restart it.

If you can think of ways to improve this script… I’m sure there are many ways… please comment below!

2 Responses to “pseudo-realtime reporting of agent status in asterisk”

  1. juan david abreo Says:

    Great job! thanks, does the script works with dinamic agents?? tks

  2. kimberly Says:

    Couldn’t you change agents to members? I could be wrong but it makes sense that is what comes up when you are adding dynamic agents to the queue.

Leave a Reply