Displaying asterisk online status on a web page

Because the Infi office has consisted of multiple office spaces for some time now, problems began to arise when diverting calls to employees that were inadvertantly offline. Misdirecting calls leads to waiting customers.

As we would like to avoid this, we thought it might be a good idea to display the status of employees within the phone system (available / on the phone / offline) on the phone number page of our internal wiki site.

Our phone system consists of an asterisk PBX server. Asterisk PHP APIs are available, but they proved to be too slow to be usable in this case, so we decided to use the servers 'asterisk' command in combination with PHPs exec function:

sudo /usr/sbin/asterisk -r -x 'sip show peers' will show all known users, together with the IP address they used to login

Example output
Name/username Host Dyn Nat ACL Port Status
allard/allard D 49257 Unmonitored
bas (Unspecified) D 5060 Unmonitored
borre/borre D 23020 Unmonitored
corne/corne D 30260 Unmonitored
daniel/daniel D 14198 Unmonitored
freek/freek D 62058 Unmonitored
joram/joram D 19280 Unmonitored
jurgen/jurgen (Unspecified) D 0 Unmonitored
mark/mark (Unspecified) D 0 Unmonitored
patricia/patricia D 5060 Unmonitored
patton/patton 5060 OK (19 ms)
reinder/reinder D 58766 Unmonitored
roy/roy D 21852 Unmonitored
s132/s132 D 5060 Unmonitored
s140/s140 D 5060 Unmonitored
scato/scato D 50304 Unmonitored
wietse/wietse (Unspecified) D 5060 Unmonitored
xs4/0858774116 N 5060 OK (34 ms)
yorick/yorick (Unspecified) D 0 Unmonitored
19 sip peers [Monitored: 2 online, 0 offline Unmonitored: 14 online, 3 offline]

sudo /usr/sbin/asterisk -r -x 'core show channels' will show all current calls

Example output:
Channel Location State Application(Data)
SIP/patton-b5f05548 0612345678@incoming: Down AppDial((Outgoing Line))
SIP/borre-0a446518 0612345678@infi-140: Up Dial(SIP/0698765432@patton,60,
SIP/patton-0a379d60 s@incoming:2 Up Transferred Call(Local/000@inf
SIP/joram-b5f3d198 (None) Up AppDial((Outgoing Line))
Local/000@infi-000-0 000@infi-140:5 Up Dial(SIP/joram,,Tthk)
Local/000@infi-000-0 000@infi-140:1 Up Transferred Call(SIP/patton-0a
6 active channels
2 active calls
4317 calls processed

We recommend using sudo because it is much safer than granting the apache user root rights. Now, we use regular expressions to find the user information in the output, and, depending on what we find, we display an icon for the employees current status. Our wiki can then show the status of an employee by including:

Which looks like this:

Full source code:
?php function exec_get_output($command){ $output; exec($command,$output); return implode("\n",$output); } $peers = exec_get_output("/usr/sbin/asterisk -r -x 'sip show peers'"); //Find users preg_match_all("/([0-9a-z]*)(\/[0-9a-z]*)?[ ]*(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}|[0-9a-z\/]*[ ]*\(Unspecified\))/",$peers,$matches); $names = $matches[1]; $ipAddresses = $matches[3]; //We are returning a png image header('Content-Type: image/png'); if(($index = array_search($_GET['user'], $names)) === false){ //User was not found print file_get_contents('images/unknown.png'); }else if($ipAddresses[$index] == '(Unspecified)'){ //User is not logged in print file_get_contents('images/unavailable.png'); }else{ //Get current calls $channels = exec_get_output("/usr/sbin/asterisk -r -x 'core show channels'"); //Find requested user preg_match("/SIP\/" . $_GET['user'] . "*-[a-f0-9]*/",$channels,$matches); if($matches[0]){ //User is on the phone print file_get_contents('images/busy.png'); }else{ //User is available print file_get_contents('images/available.png'); } } ?

Icons used: http://code.google.com/p/twotiny/

Wil je iets waarmaken met Infi?

Wil jij een eigen webapplicatie of mobiele app waarmee jij het bij anderen maakt?

Waargemaakt door de nerds van Infi.
Nerds met liefde voor softwareontwikkeling en die kunnen communiceren. En heel belangrijk: wat we doen, doen we met veel lol!

Wij willen het fixen. Laat jij van je horen?

Voor wie heb je een vraag?