Asterisk AGI

@navaismo,
I tried with the code below and didn’t work. I will try your and tell the result:

[code]#!/usr/bin/php -q

<?php $no=preg_replace("#[^0-9]#","",$agi->request[agi_callerid]);//remove any non numeric characters /* connect to "asterisk" database in mysql; in this case only 1 table "clients" column telsearch contains concatenated mobile, home and work numbers e.g. 0121777887207986238123 script will search for caller number within this field */ $db = 'qstats'; $dbuser = 'root'; $dbpass = 'dbpass'; $dbhost = 'localhost'; mysql_connect($dbhost,$dbuser,$dbpass); mysql_select_db("$db"); //or die("could not open database"); $row=mysql_query("select qagent from dbtable where uniqueid=$argv[2] and qevent=4"); if (mysql_num_rows($row)==1&&strlen($no)>4){//if found number and number greater than 4 digits to avoid 3 digit internal extensions $row=mysql_fetch_array($row); if ($row[title]) $name .= $row[title]." "; if ($row[firstname]) $name .= $row[firstname]." "; if ($row[lastname]) $name .= $row[lastname]; } else $name=$agi->request[agi_callerid];//else set calleridname to callerid number #$agi->set_variable("cid", $name); $ip=array(52=>'192.168.2.2.26',53=>'192.168.2.55',54=>'192.168.2.28',55=>'192.168.2.24',56=>'192.168.2.22',58=>'192.168.2.21'); $url_string = 'echo "'.$argv[1].'" | nc -w 1 '.$ip[$name].' 10629'; echo $url_string; shell_exec($url_string); [/code] I will post the results. Thanks!!!

Take a look at the result, it seems it’s no an Asterisk problem:

-- Launched AGI Script /var/lib/asterisk/agi-bin/teste2.php
<DAHDI/32-1>AGI Tx >> agi_request: teste2.php
<DAHDI/32-1>AGI Tx >> agi_channel: DAHDI/32-1
<DAHDI/32-1>AGI Tx >> agi_language: br
<DAHDI/32-1>AGI Tx >> agi_type: DAHDI
<DAHDI/32-1>AGI Tx >> agi_uniqueid: 1314298266.7033
<DAHDI/32-1>AGI Tx >> agi_version: 1.6.2.13
<DAHDI/32-1>AGI Tx >> agi_callerid: 1133694500
<DAHDI/32-1>AGI Tx >> agi_calleridname: 1133694500
<DAHDI/32-1>AGI Tx >> agi_callingpres: 0
<DAHDI/32-1>AGI Tx >> agi_callingani2: 0
<DAHDI/32-1>AGI Tx >> agi_callington: 0
<DAHDI/32-1>AGI Tx >> agi_callingtns: 0
<DAHDI/32-1>AGI Tx >> agi_dnid: unknown
<DAHDI/32-1>AGI Tx >> agi_rdnis: 3119
<DAHDI/32-1>AGI Tx >> agi_context: from-pstn
<DAHDI/32-1>AGI Tx >> agi_extension: 3119
<DAHDI/32-1>AGI Tx >> agi_priority: 1
<DAHDI/32-1>AGI Tx >> agi_enhanced: 0.0
<DAHDI/32-1>AGI Tx >> agi_accountcode:
<DAHDI/32-1>AGI Tx >> agi_threadid: -1289540720
<DAHDI/32-1>AGI Tx >> agi_arg_1: 45063119
<DAHDI/32-1>AGI Tx >> agi_arg_2: 1314298266.7033
<DAHDI/32-1>AGI Tx >>
<DAHDI/32-1>AGI Rx << VERBOSE "" 1
 teste2.php,45063119,1314298266.7033:
<DAHDI/32-1>AGI Tx >> 200 result=1
<DAHDI/32-1>AGI Rx << HANGUP
<DAHDI/32-1>AGI Tx >> 200 result=1
<DAHDI/32-1>AGI Rx <<
<DAHDI/32-1>AGI Tx >> 510 Invalid or unknown command
-- <DAHDI/32-1>AGI Script teste2.php completed, returning -1

Change the Verbose($MSQL) for Verbose(“this is the result of query <<${MSQL}>>”,3);

and send your query with:

$link = mysql_connect($host,$user,$pass) or die(mysql_error()); $MSQL=mysql_query("SELECT qagent FROM dbtable WHERE uniqueid=$id AND event=4",$link) or die(mysql_error());

@navaismo, here’s the output of the PHP script.
It seems the problem still here.

-- Launched AGI Script /var/lib/asterisk/agi-bin/teste2.php <DAHDI/32-1>AGI Tx >> agi_request: teste2.php <DAHDI/32-1>AGI Tx >> agi_channel: DAHDI/32-1 <DAHDI/32-1>AGI Tx >> agi_language: br <DAHDI/32-1>AGI Tx >> agi_type: DAHDI <DAHDI/32-1>AGI Tx >> agi_uniqueid: 1314367753.11643 <DAHDI/32-1>AGI Tx >> agi_version: 1.6.2.13 <DAHDI/32-1>AGI Tx >> agi_callerid: 1173003084 <DAHDI/32-1>AGI Tx >> agi_calleridname: 1173003084 <DAHDI/32-1>AGI Tx >> agi_callingpres: 0 <DAHDI/32-1>AGI Tx >> agi_callingani2: 0 <DAHDI/32-1>AGI Tx >> agi_callington: 0 <DAHDI/32-1>AGI Tx >> agi_callingtns: 0 <DAHDI/32-1>AGI Tx >> agi_dnid: unknown <DAHDI/32-1>AGI Tx >> agi_rdnis: 3119 <DAHDI/32-1>AGI Tx >> agi_context: from-pstn <DAHDI/32-1>AGI Tx >> agi_extension: 3119 <DAHDI/32-1>AGI Tx >> agi_priority: 1 <DAHDI/32-1>AGI Tx >> agi_enhanced: 0.0 <DAHDI/32-1>AGI Tx >> agi_accountcode: <DAHDI/32-1>AGI Tx >> agi_threadid: -1276363888 <DAHDI/32-1>AGI Tx >> agi_arg_1: 45063119 <DAHDI/32-1>AGI Tx >> agi_arg_2: 1314367753.11643 <DAHDI/32-1>AGI Tx >> <DAHDI/32-1>AGI Rx << <DAHDI/32-1>AGI Tx >> 510 Invalid or unknown command <DAHDI/32-1>AGI Rx << ANSWER <DAHDI/32-1>AGI Tx >> 200 result=0 [b]<DAHDI/32-1>AGI Rx << VERBOSE "and the oscar goes to: " 3[/b] -- teste2.php,45063119,1314367753.11643: and the oscar goes to: <DAHDI/32-1>AGI Tx >> 200 result=1 <DAHDI/32-1>AGI Rx << HANGUP <DAHDI/32-1>AGI Tx >> 200 result=1 -- <DAHDI/32-1>AGI Script teste2.php completed, returning -1

Hi, the verbose is not showing the result of the query.

But THIS is the problem!!

My problem is that when I call a mysql query from Asterisk, it doesn’t work, shows nothing.
And when I run the script from out Asterisk enviorment it works fine.
The problem it’s here:
Asterisk -> Shell Script(AGI) -> Mysql

When its:
Shell Script - Mysql - there is no problem!!!

Yeah I understan but at least an “acces denied” or something else in the query result.

Do you have asteriskaddons? If yes try to make a connection with cmd MYSQL and check the result in the cli.

Hi, @navaismo

this app is ok.
I connected to the same database and ran a simple query and the value returned ok.
I think the problem isn’t Asterisk … :question:

So?

No Idea, if you have php-mysql with the PHP script must work.

Hi,

Actually the problem in your php script is the first line “#!/usr/bin/php -q”.

Check your PHP version and php file in bin folder and change accordingly in your script and try.

Regards,

Ketan

Guys,
I don’t think it is a problem with the scripts, both php and shell work when they are not running with Asterisk.

Maybe it’s a problem between Asterisk and these scripts.
Don’t you think?

Dude, check my last post - I think it’s the solution of your issue, if the query is working of course.

@SGM

man, where is this post??? I can’t find it …
Thanx!

@SGM

man, you were 50%, there was en error at the command…
I should really use:
echo “$EXTEN” |nc -w 1 ${agente[$AGT]} 10629

instead of
echo “$EXTEN |nc -w 1 ${agente[$AGT]} 10629”

But I still get null data from the database… :frowning:

Guys, I still have this f*** problem!
I wrote in PHP and happens the same thing, take a look at the script.
Remembering that if I run it from the shell enviorment, it runs ok.

Let’s remeber that the script it’s ok, because I can run it from the shell:

asterisk:/var/lib/asterisk/agi-bin#  ./teste2.php a 1314977992.32870

And the log:

asterisk:/var/lib/asterisk/agi-bin# cat /home/felipe/log_php
Id do agente: 56
Comando executado: echo a | nc -w 1 192.168.2.22 10629

And lets remember that the problem is when Asterisk calls the script. This is the output log

dbcpaulista:/var/lib/asterisk/agi-bin# cat /home/felipe/log_php
Id do agente:
Comando executado: echo 45063119 | nc -w 1  10629

updating … still have the problems… any idea?

Hi

Check out the logs, System logs, asterisk logs and make sure you are logging agi as well.

system hasnt got selinux enabled ??

Ian
www.cyber-cottage.co.uk
Twitter @cyberco

Hi @ianplain

[Sep  5 15:22:37] VERBOSE[30539] res_agi.c:     -- Launched AGI Script /var/lib/asterisk/agi-bin/teste2.php
[Sep  5 15:22:37] VERBOSE[30539] res_agi.c: <DAHDI/33-1>AGI Tx >> agi_request: teste2.php
[Sep  5 15:22:37] VERBOSE[30539] res_agi.c: <DAHDI/33-1>AGI Tx >> agi_channel: DAHDI/33-1
[Sep  5 15:22:37] VERBOSE[30539] res_agi.c: <DAHDI/33-1>AGI Tx >> agi_language: br
[Sep  5 15:22:37] VERBOSE[30539] res_agi.c: <DAHDI/33-1>AGI Tx >> agi_type: DAHDI
[Sep  5 15:22:37] VERBOSE[30539] res_agi.c: <DAHDI/33-1>AGI Tx >> agi_uniqueid: 1315246955.6487
[Sep  5 15:22:37] VERBOSE[30539] res_agi.c: <DAHDI/33-1>AGI Tx >> agi_version: 1.6.2.13
[Sep  5 15:22:37] VERBOSE[30539] res_agi.c: <DAHDI/33-1>AGI Tx >> agi_callerid: 1133694500
[Sep  5 15:22:37] VERBOSE[30539] res_agi.c: <DAHDI/33-1>AGI Tx >> agi_calleridname: 1133694500
[Sep  5 15:22:37] VERBOSE[30539] res_agi.c: <DAHDI/33-1>AGI Tx >> agi_callingpres: 0
[Sep  5 15:22:37] VERBOSE[30539] res_agi.c: <DAHDI/33-1>AGI Tx >> agi_callingani2: 0
[Sep  5 15:22:37] VERBOSE[30539] res_agi.c: <DAHDI/33-1>AGI Tx >> agi_callington: 0
[Sep  5 15:22:37] VERBOSE[30539] res_agi.c: <DAHDI/33-1>AGI Tx >> agi_callingtns: 0
[Sep  5 15:22:37] VERBOSE[30539] res_agi.c: <DAHDI/33-1>AGI Tx >> agi_dnid: unknown
[Sep  5 15:22:37] VERBOSE[30539] res_agi.c: <DAHDI/33-1>AGI Tx >> agi_rdnis: 3119
[Sep  5 15:22:37] VERBOSE[30539] res_agi.c: <DAHDI/33-1>AGI Tx >> agi_context: from-pstn
[Sep  5 15:22:37] VERBOSE[30539] res_agi.c: <DAHDI/33-1>AGI Tx >> agi_extension: 3119
[Sep  5 15:22:37] VERBOSE[30539] res_agi.c: <DAHDI/33-1>AGI Tx >> agi_priority: 1
[Sep  5 15:22:37] VERBOSE[30539] res_agi.c: <DAHDI/33-1>AGI Tx >> agi_enhanced: 0.0
[Sep  5 15:22:37] VERBOSE[30539] res_agi.c: <DAHDI/33-1>AGI Tx >> agi_accountcode:
[Sep  5 15:22:37] VERBOSE[30539] res_agi.c: <DAHDI/33-1>AGI Tx >> agi_threadid: -1270875248
[Sep  5 15:22:37] VERBOSE[30539] res_agi.c: <DAHDI/33-1>AGI Tx >> agi_arg_1: 45063119
[Sep  5 15:22:37] VERBOSE[30539] res_agi.c: <DAHDI/33-1>AGI Tx >> agi_arg_2: 1315246955.6487
[Sep  5 15:22:37] VERBOSE[30539] res_agi.c: <DAHDI/33-1>AGI Tx >>
[Sep  5 15:22:37] VERBOSE[30539] res_agi.c: <DAHDI/33-1>AGI Rx <<
[Sep  5 15:22:37] VERBOSE[30539] res_agi.c: <DAHDI/33-1>AGI Tx >> 510 Invalid or unknown command
[Sep  5 15:22:38] VERBOSE[3406] asterisk.c:     -- Remote UNIX connection
[Sep  5 15:22:38] VERBOSE[30539] res_agi.c: <DAHDI/33-1>AGI Rx <<
[Sep  5 15:22:38] VERBOSE[30539] res_agi.c: <DAHDI/33-1>AGI Tx >> 510 Invalid or unknown command
[Sep  5 15:22:38] VERBOSE[30539] res_agi.c: <DAHDI/33-1>AGI Rx << Notice: Undefined index:   in /var/lib/asterisk/agi-bin/teste2.php on line 54
[Sep  5 15:22:38] VERBOSE[30539] res_agi.c: <DAHDI/33-1>AGI Tx >> 510 Invalid or unknown command
[Sep  5 15:22:38] VERBOSE[30539] res_agi.c:     -- <DAHDI/33-1>AGI Script teste2.php completed, returning 0

Here’s the Asterisk output log for the Agi I called. The line 54, that the logs warns is this line:

 54 $url_string = 'echo "'.$exten.'" | nc -w 1 '.$agente[$vq].' 10629';
 55 shell_exec($url_string);

As you can see at the code, the $agente[$vq] should be the IP address fo the agent. But when it runs from Asterisk, it doesn’t work. There isn’t another information at the system logs, messages, fulls, etc.

Two things:
First, remove the empty line, after the #! line, as php outputs an empty line. That’s just to avoid the [Sep 5 15:22:37] VERBOSE[30539] res_agi.c: <DAHDI/33-1>AGI Tx >> 510 Invalid or unknown command.
Second: there’s obviously something wrong with either your query or the result, because PHP reports undefined index at line 54, which is for the $agente[$vq]. This error means, that the agente array doesn’t have an index of the value of $vq. Are you sure you’re connecting to MySQL server correctly? mysql_connect(), when connecting to “localhost”, uses either the php.ini configured socket, the .my.cnf configured socket for the user it’s running as, /etc/my.cnf or, as last resort, the build time configured/detected socket. I suspect you have an issue connecting to MySQL (might even be connecting to the wrong server, if you have multiple). On the other hand it doesn’t die at the mysql_select_db(), so this query has to be successful. The next thing would be some kind of an error with the query. Is this the real query you have posted? If so I think you should escape the “table” with backquotes, as it’s a keyword in MySQL. If not please post the real query and the asterisk command you’re using to invoke the AGI.