Macro to set callerid on DAHDI channels for outbound calls through sip trunk

We have 5 servers in a cluster with 96 DAHDI channels each and need to find a way to set the outbound callerid without having to write 96 individual config blocks in each of the chan_dahdi.conf files.

In addition to the sip trunk DID phone number each line also has a 5 digit number which we use internally.  We have a mysql database of the person's name, DID, 5 digit ext, and server they're on, but does not include their DAHDI channel.

My thought is to use a macro that runs an agi script to query the db and set the callerid, but I'm not sure how to write the macro.  If the caller is in the [homeoffice] context and the macro is in the [outboundsiptrunk] context, how do I extract the proper info needed to pass to the agi script?
LVL 28
FishMongerAsked:
Who is Participating?
 
grbladesConnect With a Mentor Commented:
agi_extension is basically the same as the $[EXTEN} variable in the dialplan so if you look at it in the context of an outgoing call it will contain the number dialed not the calling number.

If you dont set a callerid for an extension in asterisk then it will be set by whatever phone you are using. If you dont set a particular value in the phone then most default to using the authentication name.
0
 
FishMongerAuthor Commented:
>> the macro is in the [outboundsiptrunk] context

That's not worded well.  The macro is being called from within the [outboundsiptrunk] context.
0
 
grbladesCommented:
Here is an example that I use. To call it you just use the following command. It picks up the original caller by looking at the callerid of the extension and looks up the correct callerid number to use in a database.
${FOO} can be set to 'autowithold' to also look in the data to see whether callerid should be witheld.
$(FOO) set to 'withheld' will force the callerid to be witheld.
${FOO} set to 'force' followed by a number will force the callerid to be set to that number.
AGI(sm-setoutgoingcid.agi,${FOO})

<?php
include_once "ez_sql.php";

function writevar($variable, $value)
  {
  fwrite(STDOUT, "SET VARIABLE " .$variable ." " .$value ."\n");
  fflush(STDOUT);
  return fgets(STDIN);
  }

$agivars = array();
while (!feof(STDIN))
  {
   $agivar = trim(fgets(STDIN));
   if ($agivar === '')
     break;
   $agivar = explode(':', $agivar);
   $agivars[$agivar[0]] = trim($agivar[1]);
  }
extract($agivars);

// if withheld then withhold number
if ($agi_arg_1 == "withheld")
  writevar("CALLERPRES()","prohib");

$callerid="12345678";  \\ YOUR DEFAULT OUTGOING CALLERID
$qry="SELECT withhold, cid FROM autooutgoingcid WHERE extension='" .$agi_callerid ."'";

if ( $result = $db->get_row($qry))
  {
  if ( ($agi_arg_1 == "autowithheld")  && ($result->withhold == "yes") )
    writevar("CALLERPRES()","prohib");
  if (strlen($result->cid) > 3)
    $callerid = $result->cid;
  }

if (strncmp($agi_arg_1,"force",5) == 0)
  $callerid=substr($agi_arg_1,5);

writevar("CALLERID(num)",$callerid);
?>

Open in new window

0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
FishMongerAuthor Commented:
Thanks for the suggestions, but I'm not sure if that will do what I need.  I'm not in the office today, so will have to wait until tomorrow to run some tests.

Just so I know we're on the same page,  here's the basic config I'm thinking of,  but have not tested.

[homeoffice]

ignorepat => 9
include => outboundsiptrunk
; a few other includes

exten => 10180,1,Dial(DAHDI/45,,Tt)


[outboundsiptrunk]

exten => _9xxxxxxx,1,Macro(setcid,arg1)  ; what global var do I need to pass to have the macro then pass exten 10180 to the agi script?
exten => _9xxxxxxx,n,Dial(SIP/${EXTEN:1}@XO)


[macro-setcid]

exten => s,1,AGI(setcid.pl, ${ARG1})


***************

Since I'm a Perl guy, not PHP, my script would be something like the following.


#!/usr/bin/perl

use strict;
use warnings;
use Asterisk::AGI;

$|++;  # turn off buffering

my $agi    = Asterisk::AGI->new;
my %input  = $agi->ReadParse;
my $exten  = $agi->get_variable('callerid');
my $cid    = query_db($exten);

$agi->set_callerid($cid);

sub query_db {
    my $exten = shift;
    my $cid;
    
    # code that queries the db and assigns $cid
    
    return $cid;
}

Open in new window

0
 
grbladesCommented:

exten => _9xxxxxxx,1,Macro(setcid,arg1)  ; what global var do I need to pass to have the macro then pass exten 10180 to the agi script?

Assuming you have the phone 10180 is setup to have 10180 as its default callerid then this information will be sent to the agi script as part of the standard set of variables (agi_callerid in this specific case). See http://www.voip-info.org/wiki/view/Asterisk+AGI
0
 
FishMongerAuthor Commented:
We are not (or at least what to avoid) hard coding the callerid for individual extensions.  When we dial out through the sip trunk, the default callerid for any extension is the DID number that we use to authenticate with our provider.

So, if we don't hard code the callerid, would asterisk default to using the extension (10180 in this case) when it passes the vars to the agi script or would it set it to "unknown"?  I guess I'll test that tomorrow, but I assume it will be set to "unknown".

The agi_extension var would seem to be what I want, but my reading of the wiki would indicate otherwise.
0
 
FishMongerAuthor Commented:
Well, while I was having a day off they decided they could wait the extra day for me come back in ant write the agi script so they hard coded the callerid and several other parameters for all 480 extensions.  Personally I think that's a messy way of handling it, but it's not the first time where they did this kind of thing.

We just migrated our home office from Centrex lines to the SIP trunk and from the start (which has been a 6 month process) I brought up the callerid subject multiple times and was told not to worry about because we were going to use a single callerid for everyone.  Then on Saturday just before I went home they were in a panic because the CEO was upset because no-one had their proper callerid.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.