CDR Parser
Sample shell script to parse CDR into view friendly format (same as GUI).
#!/bin/bash
# Minimal sanity check
[ $# -eq 0 ] && echo "I need one argument" && exit 1
[ $# -gt 3 ] && echo "I don't know what to do with more than 3 arguments" && exit 1
echo $string
if [ $# -eq 3 ] ; then
CDR=$3
else
# Check if cdr.tmp is local or full path is needed
[ -f cdr.tmp ] && CDR="cdr.tmp" || CDR="/CDR/cdr.tmp"
# delete /LOG/cdr_trace.log if it exists
[ -f /LOG/cdr_trace.log ] && \rm /LOG/cdr_trace.log
fi
if [ $# -eq 1 ] || [ "x$2" == "x" ]
then
string=$1
# grep string (command line argument) and save temporary result in /tmp/
nice -n 18 grep -i -m 2000 $string $CDR > /tmp/found$$
else
#we know it will be 2 or 3 by this point and their behavior is the same
string=$1
string2=$2
# grep string (command line argument) and save temporary result in /tmp/
nice -n 18 grep -i -m 2000 $string $CDR | grep -i $string2 > /tmp/found$$
fi
# looong loop to read interesting fields, print results after that.
# depends heavily on CDR specs, will work as long as fields are added
# at the end of cdr line.
while read -d\; first ; do
read -d\; CDR_Version
read -d\; x
read -d\; media
read -d\; session
read -d\; rel_cause
read -d\; start
read -d\; answer
read -d\; release
read -d\; x
read -d\; stack_release
read -d\; stack_release_code
read -d\; first_rel
read -d\; orig_tid
read -d\; orig_stack
read -d\; orig_ani
read -d\; orig_from
read -d\; orig_dnis
read -d\; orig_to
read -d\; orig_id
read -d\; orig_media_ip
read -d\; orig_media_udp
read -d\; orig_switched_ip
read -d\; orig_switched_udp
read -d\; orig_codec_list
read -d\; orig_pkt_in
read -d\; orig_pkt_out
read -d\; orig_byte_in
read -d\; orig_byte_out
read -d\; x
read -d\; x
read -d\; x
read -d\; dest_tid
read -d\; dest_stack
read -d\; dest_ani
read -d\; dest_from
read -d\; dest_dnis
read -d\; dest_to
read -d\; dest_id
read -d\; dest_media_ip
read -d\; dest_media_udp
read -d\; dest_switched_ip
read -d\; dest_switched_udp
read -d\; dest_codec_list
read -d\; dest_pkt_in
read -d\; dest_pkt_out
read -d\; dest_byte_in
read -d\; dest_byte_out
read -d\; x
read -d\; x
read -d\; x
read -d\; call_indication
read -d\; routed_digits
read -d\; duration
read -d\; post_delay
read -d\; ring
read -d\; duration_ms
read -d\; conf_id
read -d\; rpid_ani
minor=`echo $CDR_Version | cut -d\. -f2`
if [ $minor -ge 11 ] ; then
read -d\; route_entry
read -d\; route_table
fi
if [ $minor -ge 12 ] ; then
read -d\; lnp_dipped
read -d\; ingress_rn
read -d\; egress_rn
read -d\; cname_dipped
read -d\; dnc_dipped
fi
if [ $minor -ge 13 ] ; then
read -d\; orig_TID_alias
read -d\; term_TID_alias
fi
if [ $minor -ge 14 ] ; then
read -d\; ers_dipped
fi
if [ $minor -ge 15 ] ; then
read -d\; oli
fi
if [ $minor -ge 16 ] ; then
read -d\; route_match
read -d\; term_pdd
read -d\; pci
read -d\; jip
fi
if [ $minor -ge 17 ] ; then
read -d\; matched_ers_digits
read -d\; ers_route_table
read -d\; ers_route_sequence
read -d\; ers_jd
fi
# Read to end of record
read x
[ "$first_rel" = "O" ] && release_from="Origination"
[ "$first_rel" = "T" ] && release_from="Termination"
[ "$first_rel" = "N" ] && release_from="Internal"
[ "$first_rel" = "E" ] && release_from="ERS"
# print results closely follow web look
echo -n "Session $session, " >> /LOG/cdr_trace.log
echo "Release Cause (code) = $rel_cause" >> /LOG/cdr_trace.log
echo " $release_from Stack Cause = $stack_release_code, $stack_release" >> /LOG/cdr_trace.log
echo " Start = $start" >> /LOG/cdr_trace.log
echo " Answer = $answer" >> /LOG/cdr_trace.log
echo " Release = $release" >> /LOG/cdr_trace.log
echo " Duration = $duration" >> /LOG/cdr_trace.log
echo " Post Dial Delay (sec) = $post_delay" >> /LOG/cdr_trace.log
[ $minor -ge 16 ] && echo " Termination Post Dial Delay (msec) = $term_pdd" >> /LOG/cdr_trace.log
echo " Ring Time = $ring" >> /LOG/cdr_trace.log
echo " Media = $media" >> /LOG/cdr_trace.log
echo " " >> /LOG/cdr_trace.log
echo " Orig TID = $orig_tid, Proc_$orig_stack" >> /LOG/cdr_trace.log
echo " ANI = $orig_ani, From = $orig_from" >> /LOG/cdr_trace.log
echo " DNIS = $orig_dnis, To = $orig_to" >> /LOG/cdr_trace.log
[ "X$ingress_rn" = "X" ] && ingress_rn="N/A"
[ $minor -ge 12 ] && echo " RN = $ingress_rn" >> /LOG/cdr_trace.log
echo " Call Leg ID = $orig_id" >> /LOG/cdr_trace.log
echo " Media IP:UDP = $orig_media_ip:$orig_media_udp" >> /LOG/cdr_trace.log
echo " Switched IP:UDP = $orig_switched_ip:$orig_switched_udp" >> /LOG/cdr_trace.log
echo " Codec List = $orig_codec_list" >> /LOG/cdr_trace.log
echo " " >> /LOG/cdr_trace.log
echo " Term TID = $dest_tid, Proc_$dest_stack" >> /LOG/cdr_trace.log
echo " ANI = $dest_ani, From = $dest_from" >> /LOG/cdr_trace.log
echo " DNIS = $dest_dnis, To = $dest_to" >> /LOG/cdr_trace.log
[ "X$egress_rn" = "X" ] && egress_rn="N/A"
[ $minor -ge 12 ] && echo " RN = $egress_rn" >> /LOG/cdr_trace.log
echo " Call Leg ID = $dest_id" >> /LOG/cdr_trace.log
echo " Media IP:UDP = $dest_media_ip:$dest_media_udp" >> /LOG/cdr_trace.log
echo " Switched IP:UDP = $dest_switched_ip:$dest_switched_udp" >> /LOG/cdr_trace.log
echo " Codec List = $dest_codec_list" >> /LOG/cdr_trace.log
echo " " >> /LOG/cdr_trace.log
echo " Routed Digits = $routed_digits" >> /LOG/cdr_trace.log
if [ $minor -le 10 ] ; then
echo " Final Call Indication = $call_indication" >> /LOG/cdr_trace.log
elif [ $minor -ge 12 ] ; then
[ "X$route_entry" = "X" ] && route_entry="N/A"
echo -n " Route Selection = $route_entry, Final Call Indication = $call_indication" >> /LOG/cdr_trace.log
[ "X$oli" = "X" ] && oli="N/A"
[ $minor -ge 15 ] && echo " OLI = $oli" >> /LOG/cdr_trace.log || echo "" >> /LOG/cdr_trace.log
[ "$lnp_dipped" = "1" ] && lnp="Yes" || lnp="No"
[ "$cname_dipped" = "1" ] && cname="Yes" || cname="No"
[ "$ers_dipped" = "1" ] && ers="Yes" || ers="No"
echo " CNAM Dipped : $cname LNP Dipped : $lnp ERS Dipped : $ers" >> /LOG/cdr_trace.log
fi
if [ $minor -ge 17 ] ; then
echo " ERS Route Table : $ers_route_table , Sequence: $ers_route_sequence , Jurisdiction: $ers_jd " >> /LOG/cdr_trace.log
echo " Matched ERS Digits = $matched_ers_digits" >> /LOG/cdr_trace.log
fi
echo " Orig: Pkt In = $orig_pkt_in, Pkt Out = $orig_pkt_out,\
Byte In = $orig_byte_in, Byte Out = $orig_byte_out" >> /LOG/cdr_trace.log
echo " Term: Pkt In = $dest_pkt_in, Pkt Out = $dest_pkt_out,\
Byte In = $dest_byte_in, Byte Out = $dest_byte_out" >> /LOG/cdr_trace.log
echo " " >> /LOG/cdr_trace.log
echo " " >> /LOG/cdr_trace.log
done < /tmp/found$$
\rm /tmp/found$$