#! /bin/bash INPUT_FILE= LOG_FILE=/var/log/m2_cdr_parser.log CDR_FILE=/tmp/m2_cdr_parser/m2_cdr_file.txt CDR_LINES_FILE=/tmp/m2_cdr_parser/m2_cdr_lines.txt CDR_DIR=/tmp/m2_cdr_parser/ CDR_OUT_FILE=/tmp/m2_cdr_parser/cdr_out_file.sql SKIPPED_CDRS=/tmp/m2_cdr_parser/skipped_cdrs.txt TOTAL_CDRS=0 PROCCESSED_CDRS=0 FILTER="This CDR will not be logged to DB because Disconnect Code [200]" log() { echo "$1" echo "$1" > $LOG_FILE } # Remove old tmp files rm -fr $CDR_FILE &> /dev/null rm -fr $CDR_LINES_FILE &> /dev/null rm -fr $CDR_DIR &> /dev/null rm -fr $CDR_OUT_FILE &> /dev/null rm -fr $SKIPPED_CDRS &> /dev/null mkdir $CDR_DIR log "Starting M2 CDR Parser script" log "Total $# files" for file in $@; do # File exists? if [ ! -e $file ]; then log "File $file not found" exit 0 fi done for file in $@; do if [ "$FILTER" == "" ]; then # Get all CDRs grep -F "M2 Core" $file &> $CDR_LINES_FILE else # Get filtered CDRs grep -F "$FILTER" $file &> $CDR_LINES_FILE fi # Count how many CDRs found LOCAL_TOTAL_CDRS=$(cat $CDR_LINES_FILE | wc -l) ((TOTAL_CDRS=TOTAL_CDRS+LOCAL_TOTAL_CDRS)) done log "Found $TOTAL_CDRS CDRs" if [ "$TOTAL_CDRS" == "0" ]; then log "Nothing to do..." exit 0 fi for file in $@; do log "Proccessing file: $file" if [ "$FILTER" == "" ]; then # Get all CDRs grep -F "M2 Core" $file &> $CDR_LINES_FILE else # Get filtered CDRs grep -F "$FILTER" $file &> $CDR_LINES_FILE fi # Count how many CDRs found LOCAL_TOTAL_CDRS=$(cat $CDR_LINES_FILE | wc -l) if [ "$LOCAL_TOTAL_CDRS" == "0" ]; then continue fi log "Parsing CDRs for data" while IFS= read -r line do rm -fr $CDR_FILE &> /dev/null ((PROCCESSED_CDRS=PROCCESSED_CDRS+1)) # Get uniqueid UNIQUEID=$(echo $line | grep -Po "\[NOTICE\] \[.*\] " | awk '{print $2}' | tail -c +2 | head -c -2) if [ "$UNIQUEID" == "" ]; then log "[$PROCCESSED_CDRS/$TOTAL_CDRS] Uniqueid not found, skipping call: $line" echo "$line" >> $SKIPPED_CDRS continue fi # Get full CDR log into tmp file grep -F "$UNIQUEID" $file >> $CDR_FILE # Check if we have beginning of CDR USER_ID=$(cat $CDR_FILE | grep -Po "User .* balance/min-max" | tail -n 1 | tail -c +7 | head -c -18 | grep -Po "\d+") if [ "$USER_ID" == "" ]; then rm -fr $CDR_FILE &> /dev/null # Check in other files for local_file in $@; do grep -F "$UNIQUEID" $local_file >> $CDR_FILE done # Check if we have beginning of CDR USER_ID=$(cat $CDR_FILE | grep -Po "User .* balance/min-max" | tail -n 1 | tail -c +7 | head -c -18 | grep -Po "\d+") if [ "$USER_ID" == "" ]; then log "[$PROCCESSED_CDRS/$TOTAL_CDRS] USER_ID not found, skipping call: $UNIQUEID" echo "$UNIQUEID" >> $SKIPPED_CDRS continue fi fi # Get CDR fields CALLDATE=$(grep -F "Received data" $CDR_FILE | grep -Po "calldate: .*, uniqueid" | tail -n 1 | tail -c +11 | head -c -11) DATE=$(grep -F "Received data" $CDR_FILE | grep -Po "calldate: .*, uniqueid" | tail -n 1 | tail -c +11 | head -c -20) CLID=$(grep -F "Received data" $CDR_FILE | grep -Po "callerid: .*, calldate" | tail -n 1 | tail -c +11 | head -c -11) SRC=$(grep -F "Received data" $CDR_FILE | grep -Po "src: .*, dst" | tail -n 1 | tail -c +6 | head -c -6) DST=$(grep -F "Received data" $CDR_FILE | grep -Po "dst: .*, callerid" | tail -n 1 | tail -c +6 | head -c -11) REAL_DURATION=$(cat $CDR_FILE | grep -Po "Real Duration: .*, Real" | tail -n 1 | tail -c +16 | head -c -7) DURATION=$(cat $CDR_FILE | grep -Po ", Duration: .*, Billsec" | tail -n 1 | tail -c +13 | head -c -10) BILLSEC=$(cat $CDR_FILE | grep -Po ", Billsec: .*" | tail -n 1 | tail -c +12 | head -c -2) DISPOSITION=$(cat $CDR_FILE | grep -Po " cd->dialstatus .* q850_code" | tail -n 1 | tail -c +18 | head -c -12) REAL_BILLSEC=$(cat $CDR_FILE | grep -Po "Real Billsec: .*, Duration" | tail -n 1 | tail -c +15 | head -c -11) SRC_DEVICE_ID=$(cat $CDR_FILE | grep -Po "OP .*, tech_prefix" | tail -n 1 | tail -c +5 | head -c -15 | grep -Po "\d+") DST_DEVICE_ID=$(cat $CDR_FILE | grep -Po "TP .* q850_reason" | tail -n 1 | tail -c +5 | head -c -14) PROVIDER_ID=$(cat $CDR_FILE | grep "TP \[$DST_DEVICE_ID\], host:" | tail -n 1 | grep -Po "user_id: .*, tariff_id" | tail -c +10 | head -c -12) TERMIANTOR_IP=$(cat $CDR_FILE | grep "TP \[$DST_DEVICE_ID\], host:" | tail -n 1 | grep -Po ", host: .*, port" | tail -c +9 | head -c -7) PREFIX=$(cat $CDR_FILE | grep -Po ", prefix\[.*\]:" | tail -n 1 | tail -c +10 | head -c -3) SERVER_ID=$(cat $CDR_FILE | grep -Po ", server_id: .* \(" | tail -n 1 | tail -c +14 | head -c -3) HANGUPCAUSE=$(cat $CDR_FILE | grep -Po "cd->hangupcause \[.*\]" | tail -n 1 | tail -c +18 | head -c -2) ORIGINATOR_IP=$(cat $CDR_FILE | grep "Received data" | tail -n 1 | grep -Po "host: .*, port" | tail -n 1 | tail -c +7 | head -c -7) PDD=$(cat $CDR_FILE | grep -Po ", pdd: .*, op codec" | tail -n 1 | tail -c +8 | head -c -11) TERMINATED_BY=$(cat $CDR_FILE | grep -Po "Terminated by .*" | tail -n 1 | tail -c +15 | head -c -2) ANSWER_TIME=$(cat $CDR_FILE | grep -Po ", answer time: .*, end time" | tail -n 1 | tail -c +16 | head -c -11) END_TIME=$(cat $CDR_FILE | grep -Po ", end time: .*, billsec" | tail -n 1 | tail -c +13 | head -c -10) SRC_USER_ID=$USER_ID DST_USER_ID=$PROVIDER_ID USER_BILLSEC=$BILLSEC PROVIDER_BILLSEC=$BILLSEC DST_DEVICE_ID=$(echo "$DST_DEVICE_ID" | grep -Po "\d+") LOCALIZED_DST=$(cat $CDR_FILE | grep "Dst before transformation \[.*\], after \[.*\]" | grep -v "TP" | tail -n 1 | grep -Po ", after \[.*\]" | tail -n 1 | tail -c +10 | head -c -2) if [ "$LOCALIZED_DST" == "" ]; then LOCALIZED_DST=$(cat $CDR_FILE | grep -Po " stripped dst \[.*\]" | tail -n 1 | tail -c +16 | head -c -2) if [ "$LOCALIZED_DST" == "" ]; then LOCALIZED_DST=$DST fi fi if [ "$DISPOSITION" == "ANSWERED" ]; then USER_RATE=$(cat $CDR_FILE | grep "Prices after convert to def\.curr: .*" | tail -n 1 | grep -Po "OP rate \[.*\] price" | tail -n 1 | tail -c +10 | head -c -8) USER_PRICE=$(cat $CDR_FILE | grep "Prices after convert to def\.curr: .*" | tail -n 1 | grep -Po "OP rate .*" | grep -Po "price \[.*\]" | tail -n 1 | tail -c +8 | head -c -2) PROVIDER_RATE=$(cat $CDR_FILE | grep "Prices after convert to def\.curr: .*" | tail -n 1 | grep -Po "TP rate \[[0-9\.]+" | tail -c +10) PROVIDER_PRICE=$(cat $CDR_FILE | grep "Prices after convert to def\.curr: .*" | tail -n 1 | grep -Po "TP rate .*, OP" | grep -Po "price \[.*\]" | tail -n 1 | tail -c +8 | head -c -2) fi if [ "$CALLDATE" == "" ]; then CALLDATE="0000-00-00 00:00:00" fi if [ "$DATE" == "" ]; then CALLDATE="0000-00-00" fi if [ "$REAL_DURATION" == "" ]; then REAL_DURATION="0" fi if [ "$BILLSEC" == "" ]; then BILLSEC="0" fi if [ "$SERVER_ID" == "" ]; then SERVER_ID="1" fi if [ "$REAL_BILLSEC" == "" ]; then REAL_BILLSEC="0" fi if [ "$PDD" == "" ]; then PDD="0" fi if [ "$USER_RATE" == "" ]; then USER_RATE="0" fi if [ "$USER_PRICE" == "" ]; then USER_PRICE="0" fi if [ "$PROVIDER_RATE" == "" ]; then PROVIDER_RATE="0" fi if [ "$PROVIDER_PRICE" == "" ]; then PROVIDER_PRICE="0" fi if [ "$HANGUPCAUSE" == "" ]; then HANGUPCAUSE="0" fi if [ "$PROVIDER_ID" == "" ]; then PROVIDER_ID="0" DST_USER_ID="0" fi if [ "$DST_DEVICE_ID" == "" ]; then DST_DEVICE_ID="0" fi echo "INSERT INTO calls (calldate,date,uniqueid,clid,src,dst,localized_dst,duration,real_duration,billsec,real_billsec,disposition,user_rate,user_price,provider_rate,provider_price,src_device_id,dst_device_id,provider_id,user_id,prefix,server_id,hangupcause,originator_ip,terminator_ip,pdd,terminated_by,src_user_id,dst_user_id,channel) VALUE ('$CALLDATE','$DATE','$UNIQUEID','$CLID','$SRC','$DST','$LOCALIZED_DST','$DURATION','$REAL_DURATION','$BILLSEC','$REAL_BILLSEC','$DISPOSITION','$USER_RATE','$USER_PRICE','$PROVIDER_RATE','$PROVIDER_PRICE','$SRC_DEVICE_ID','$DST_DEVICE_ID','$PROVIDER_ID','$USER_ID','$PREFIX','$SERVER_ID','$HANGUPCAUSE','$ORIGINATOR_IP','$TERMIANTOR_IP','$PDD','$TERMINATED_BY','$SRC_USER_ID','$DST_USER_ID','RESTORED');" >> $CDR_OUT_FILE log "[$PROCCESSED_CDRS/$TOTAL_CDRS] Date: [$CALLDATE], Src: [$SRC], Dst: [$DST], Billsec: [$BILLSEC], Disp: [$DISPOSITION], User price: [$USER_PRICE], Prov price: [$PROVIDER_PRICE], OP id: [$SRC_DEVICE_ID], TP id: [$DST_DEVICE_ID], [$USER_ID], HGC: [$HANGUPCAUSE]" done < $CDR_LINES_FILE done log "Output CDR file is located in $CDR_OUT_FILE"