[asterisk-dev] How to retrieve current time on hold?

Jeff Sherk Forerunner Ministries jeff at ForerunnerTV.com
Tue May 31 09:09:54 CDT 2011


Thanks Russell... I ended writing this script to read all the AMI events. Will just need 
to parse for state changes!

<?php
// AMI EVENT READER v0.1 by Jeff Sherk - May 2011
//
// This script will continuously read all the Asterisk AMI events and output them to your 
browser
//
// This FREE SCRIPT is offered AS IS with no guarantees and no warrantees. Use it at your 
own risk!

/////////////////////////////////////////////////
// NOTE: Required for this script to work, and also required is you want to use PHPAGI
/* MODIFY /etc/asterisk/manager.conf and add this (make sure user is asterisk 0664):
     [myamiclient]
     secret=********
     deny=0.0.0.0/0.0.0.0
     permit=127.0.0.1/255.255.255.0
     read = 
system,call,log,verbose,command,agent,user,config,command,dtmf,reporting,cdr,dialplan,originate
     write = 
system,call,log,verbose,command,agent,user,config,command,dtmf,reporting,cdr,dialplan,originate
/*

/////////////////////////////////////////////////
// NOTE: Only required if you want to use PHPAGI
/* MODIFY /etc/asterisk/phpagi.conf and add this (make sure user is asterisk 0777):
     [asmanager]
     server=127.0.0.1 ; server to connect to
     port=5038 ; default manager port
     username=myamiclient ; username for login
     secret=******** ; password for login
*/

//username and secret need to match the /etc/asterisk/manager.conf file
$username = 'myamiclient';
$secret = '********';

//Script should run forever, so prevent it from timing out
set_time_limit(0);

//Use fsockopen to connect the same way you would with Telnet
$fp = fsockopen("127.0.0.1", 5038, $errno, $errstr, 30);

//Unsuccessful connect
if (!$fp) {
     echo "$errstr ($errno)\n<br>";

//Successful connect
} else {

     //login
     fputs($fp,"Action: login\r\nUsername: ".$username."\r\nSecret: ".$secret."\r\n\r\n");
//TO DO: check if login was successful or not

     //LOOP FOREVER - continuously read data
     $line = '';
     while(1) {
         $read = fread($fp,1); //Read one byte at a time from the socket
         $line .= $read;

         //Check if we are at the end of a line
         if ("\n" == $read) {

             //Determine when we have reached a blank line which
             // signals the end of a single events info
             $event_separator = false;
             if ("\r\n" == $line) {
                 $event_separator = true;
             }
//TO DO: Add code that does something when we have an entire events info

             echo $line.'<br>'; //DEBUG ONLY: For screen display. Remove <br> for db storage
//TO DO: How do we redirect echo statement so we can just save info in db?

             flush($fp); //Flush the stdout to get it to display
             $line = '';

         } //end IF -> Check if we are at the end of a line
     } //end WHILE -> LOOP FOREVER

     fclose($fp); //Will never get here, but looks good to have it!
} //end ELSE -> Successful connect

?>

On 5/31/2011 1:27 AM, Russell Bryant wrote:
>
> ----- Original Message -----
>> I am interested in writing a script that can retrieve which extensions
>> are currently on
>> hold and how long they have been on hold for.
>>
>> I see that from the CLI I can use "core show hints" to actually find
>> which extensions have
>> State:Hold and I can also use "core show channels" to trace what/where
>> the channel/extension is connected.
>>
>> I cannot however find a current hold time for how long an
>> extension/channel has been on
>> hold. I searched the mysql db but did not find the information (or
>> just didn't know what I was looking for).
>>
>> What command and/or where should I be looking to find this
>> information? Maybe in a logfile somewhere?
> This information does not exist anywhere in Asterisk.  The only way to do it is to watch state changes from a manager interface application track the times yourself.
>



More information about the asterisk-dev mailing list