Asterisk AGI

Hi guys,

i think this is more a Linux issue than an Asterisk issu, but here we go.

I have an AGI script which connects to a mysql database, gets a certain value and runs a shell command.
Ok, when I run the shell script out of asterisk, it works pretty fine. But when Asterisk calls this script, it doesn’t connect to the database. Asterisk is running as ‘root’, so I can’t see where is the problem. I will post the code here:


Declaracao de variaveis

EXTEN=$1 #Numero de telefone recebido do Asterisk
ID=$2 #Uniqueid

Comando para extrair do banco do Id do agente que atendeu a chamada

MSQL=$(mysql -u root --password=“mypass” -e “select qagent from dbname.dbtable where uniqueid=$2 and event=4”)


echo "$AGT << recebido " > $LOG

#IP Vanessa
#IP Leticia
#IP Aline
#IP Raquel
#IP Bianca
#IP Deise

Enviar pop up de acordo com o array relacionado ao ID do agente:

echo "

Data: $(date +%d/%m/%Y)
Horario: $(date +%H:%M:%S)
Numero do cliente: $EXTEN
Uniqueid da chamada: $ID
Id do agente:$AGT
**************************************" >> $LOG

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


So, as I said before, Asterisk is running as root, but I still think it is a permission problem. But I can’t see where.
I wrote the same script in PHP, and I still have problems with the connection.



path of what? the agi script?

the same for all the scripts:

permission of file: 755 (also tried the 777)


The environment variable. It might not be set.

You’re talking 'bout these ones?
EXTEN=$1 #Numero de telefone recebido do Asterisk
ID=$2 #Uniqueid

should I use only the ‘$1…$n’ … instead of EXTEN and ID?

If so, I’ll try.

When you run a shell command interactively, the shell tries to prefix command names that don’t contain a “/” with each of the strings contained in the $PATH environment variable. I have a suspicion that, when Asterisk starts an AGI script, the PATH environment variable is empty, in which case, you must either set it in the script, or must include the full path name in all command names.

so, what exactlly should I do with these variables. I didn’t understand it very well.
Thank you

I included both mysql (/usr/sbin/mysql) and the script (/var/lib/asterisk/agi-bin/ in the $PATH and it is not working yet … Here’s the output of the $PATH:

astserver:/etc# echo $PATH

Change this

MSQL=$(mysql -u root --password="mypass" -e "select qagent from dbname.dbtable where uniqueid=$2 and event=4")

for this

MSQL=$(/usr/sbin/mysql -u root --password="mypass" -e "select qagent from dbname.dbtable where uniqueid=$2 and event=4")

Just Put the complete path to your mysql binary, i assume that is in /usr/sbin but for be sure check it with the command:

and tell us your results.

Hi @orviljuarez,

I had already changed this. In my Debian, mysql is at ‘/usr/bin’.
I included the full path into the script:

MSQL=$(/usr/bin/mysql -u root --password=“mypass*” -e “select qagent from dbname.dbtable where uniqueid=$2 and qevent=4”)

But it still not working.

Two things:

1)Taking a look at the mysql log, it seems that the connection and thequery is ok:

110823 9:33:07 21083 Connect root@localhost on
21083 Query select @@version_comment limit 1
21083 Query select qagent from dbname.dbtable where uniqueid=1314102782.10773 and qevent=4

2) I’m using Asterisk 1.4.32 (An update could help?)


Well, I tried to run this script on a and didn’t work. :frowning:

@david55, @orviljuarez

Enable agi debug with “agi set debug on” and show us the output n the asterisk cli.


Here’s the line where I call the AGI:

Executing [3119@from-pstn:1] Queue("DAHDI/32-1", "fila1,tTwW,,,180,",45063119,1314201512.4087"") in new stack

And here is the output of the AGI debug:

    -- Launched AGI Script /var/lib/asterisk/agi-bin/
<DAHDI/32-1>AGI Tx >> agi_request:
<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: 1314201512.4087
<DAHDI/32-1>AGI Tx >> agi_version:
<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: -1259992176
<DAHDI/32-1>AGI Tx >> agi_arg_1: 45063119
<DAHDI/32-1>AGI Tx >> agi_arg_2: 1314201512.4087
<DAHDI/32-1>AGI Tx >>
[u]<DAHDI/32-1>AGI Rx 45063119 <<  |nc -w 1  10629
<DAHDI/32-1>AGI Tx >> 510 Invalid or unknown command[/u]
-- <DAHDI/32-1>AGI Script completed, returning 0

Despite of the error line (510 Invalid command), it works fine - when I don’t use the mysql script this works fine.
The command, if the shell script worked fine should be like: 45063119 << nc -w 1 192.168.x.x 10629

So, the mysql query is not working? have your tried with PHP?


Regarding the error - are you sure you’re not trying to do echo "$EXTEN" |nc -w 1 ${agente[$AGT]} 10629?

About the MySQL output you could try something like “echo $MSQL >> /tmp/something.txt” after the “MSQL=…” line to log the output.


I tried with PHP and it’s not working too.

The mysql query it’s ok. When I run this script out from Asterisk enviorment it works pretty fine.

I have a log, take a look:

When I run from Asterisk:

1 << recebido 2 3 ************************************** 4 Data: 25/08/2011 5 Horario: 09:36:15 6 Numero do cliente: 45063119 7 Uniqueid da chamada: 1314275772.839 8 Id do agente: 9 **************************************

Line 1 should have the query result (a number) and it’s empty. Line 6 its the fist variable sent by Asterisk and the line 7 is the second variable. So Asterisk is sending the variables correctly.

When I run the commando from Linux: ./ 45063119 1314275772.839, the output is ok:

***Command output
asterisk:/var/lib/asterisk/agi-bin# ./ 45063119 1314275772.839
45063119 |nc -w 1 10629 --> this is the command I need to run
52 << recebido

Data: 25/08/2011
Horario: 09:40:10
Numero do cliente: 45063119
Uniqueid da chamada: 1314275772.839
Id do agente:52

Can you put an ECHO of the query result in your script? To see in the asterisk cli what exactly happens hen you send the query.

<DAHDI/32-1>AGI Tx >> <DAHDI/32-1>AGI Rx 45063119 << |nc -w 1 10629 <DAHDI/32-1>AGI Tx >> 510 Invalid or unknown command -- <DAHDI/32-1>AGI Script completed, returning 0

There should be:

<DAHDI/32-1>AGI Rx 45063119 << ‘query result’ |nc -w 1 10629

Take a look at the script


MM but when the query is performed what its the result? can you echo that?

try with this:

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

<?php set_time_limit(30); // Habilitamos el control de PHPAGI y reporte de errores require('phpagi/phpagi.php'); error_reporting(E_ALL); $host="localhost"; // Datos de conexion a la BD $user="root"; $pass="mypass"; $db="dbname"; $exten=$argv[1]; $id=$argv[2]; $agi = new AGI(); // Creamos el canal AGI y contestamos $agi->answer(); $link = mysql_connect($host,$user,$pass) or die(mysql_error()); //Conexion a la BD mysql_select_db($db, $link); $MSQL=mysql_query("SELECT qagent FROM dbtable WHERE uniqueid=$id AND event=4"); $agi->Verbose($MSQL); cual es el resultado del query? $agi->Hangup(); cuelga ?>


Requieres PHPAGI and only sends to the asterisk cli the result of the query