Asterisk Outgoing CDR Logging To Mysql

asterisk

Trying to utilize the cdr logging (to mysql) using custom fields. The problem I am facing is only when an outbound call is placed, during inbound calls the custom field I am able to log no problem.

The reason I am having an issue is because the custom cdr field I need is a unique value for each user on the system.

sip.conf

 ...
 ...

 [sales_department](!)
 type=friend
 host=dynamic
 context=SalesAgents
 disallow=all
 allow=ulaw
 allow=alaw
 qualify=yes
 qualifyfreq=30


 ;; company sales agents:
 [11](sales_agent)
 secret=xxxxxx
 callerid="<...>"

 [12](sales_agent)
 secret=xxxxxx
 callerid="<...>"

 [13](sales_agent)
 secret=xxxxxx
 callerid="<...>"

 [14](sales_agent)
 secret=xxxxxx
 callerid="<...>"

extensions.conf

 [SalesAgents]
 include => Services

 ; Outbound calls
 exten=>_1NXXNXXXXXX,1,Dial(SIP/${EXTEN}@myprovider)


 ; Inbound calls
 exten=>100,1,NoOp()
 same => n,Set(CDR(agent_id)=11)
 same => n,CELGenUserEvent(Custom Event)
 same => n,Dial(${11_1},25)
 same => n,GotoIf($["${DIALSTATUS}" = "BUSY"]?busy:unavail)
 same => n(unavail),VoiceMail(11@asterisk)
 same => n,Hangup()
 same => n(busy),VoiceMail(11@asterisk)
 same => n,Hangup()

 exten=>101,1,NoOp()
 same => n,Set(CDR(agent_id)=12)
 same => n,CELGenUserEvent(Custom Event)
 same => n,Dial(${12_1},25)
 same => n,GotoIf($["${DIALSTATUS}" = "BUSY"]?busy:unavail)
 same => n(unavail),VoiceMail(12@asterisk)
 same => n,Hangup()
 same => n(busy),VoiceMail(12@asterisk)
 same => n,Hangup()

 ...
 ...

For the inbound section of the dialplan in the above example I am able to insert the custom cdr field (agent_id). But above it you can see for the Oubound section of the dialplan I have been stumped on how I would be able to tell the dialplan which agent_id is making the outbound call.

My Question: how to take the agent_id=[11] & agent_id=[12] and agent_id=[13] and agent_id=[14] etc and use that as a custom field for cdr on outbound calls?

Best Answer

@miken32's suggest is a good one.

Another way of doing this is to have the SIP peer definitions of each Agent define their agent ID in a channel variable:

; sip.conf
[agent_12]
type=peer
setvar=__AGENT_ID=agent_12

Any time a channel is created for this peer, it will automatically have the AGENT_ID variable set on it. Because we've prefaced that variable with inheritance, any child channels created from it - that is, channels it Dials - will also get the AGENT_ID set on it.

In your outbound dial, you could set it on the channel dialling like so:

 ; Outbound calls
 exten =>_1NXXNXXXXXX,1,NoOp()
 same  =>             n,Set(CDR(agent_id)=${AGENT_ID}
 same  =>             n,Dial(SIP/${EXTEN}@myprovider)

This will set the agent_id column in your CDR.

Note that if you need to set the value on the outbound channel that is created in the Dial, you could use a Pre-Dial handler.