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:


#!/bin/bash

Declaracao de variaveis

EXTEN=$1 #Numero de telefone recebido do Asterisk
ID=$2 #Uniqueid
LOG="/tmp/log"

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”)

AGT=${MSQL:7}

echo "$AGT << recebido " > $LOG

#IP Vanessa
agente[52]=“192.168.2.26”
#IP Leticia
agente[53]=“192.168.2.55”
#IP Aline
agente[54]=“192.168.2.28”
#IP Raquel
agente[55]=“192.168.2.24”
#IP Bianca
agente[56]=“192.168.2.22”
#IP Deise
agente[58]=“192.168.2.21”

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

#LOG
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”

end


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.
Thanks!

PATH?

@david55

path of what? the agi script?

the same for all the scripts:
/var/lib/asterisk/agi-bin/

permission of file: 755 (also tried the 777)

Thanks

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.
Thanks!

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.

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

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

astserver:/etc# echo $PATH
:/usr/bin/mysql:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin/usr/bin/usr/bin/mysql:/usr/bin/mysql:/var/lib/asterisk/agi-bin/:/var/lib/asterisk/agi-bin/shell.sh:/var/lib/asterisk/agi-bin/shell.sh

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?)

Thanks!!!

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

@david55, @orviljuarez
anyone?

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

@navaismo

Here’s the line where I call the AGI:

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

And here is the output of the AGI debug:

    -- Launched AGI Script /var/lib/asterisk/agi-bin/shell.sh
<DAHDI/32-1>AGI Tx >> agi_request: shell.sh
<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: 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: -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 shell.sh 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?

Hi,

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.

Stoyan

@navaismo
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: ./shell.sh 45063119 1314275772.839, the output is ok:

***Command output
asterisk:/var/lib/asterisk/agi-bin# ./shell.sh 45063119 1314275772.839
45063119 |nc -w 1 192.168.2.26 10629 --> this is the command I need to run
*** LOG FILE
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 shell.sh completed, returning 0

There should be:

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

Take a look at the script

Thanks

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 ?>

[/code]

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