show-log-for-call (Source)

#!/bin/bash
##---------------------------------------------------------------------------##
#   Program: show-log-for-call
#   Author:  Brian <genius@groupbcl.ca> :)
#   Date:    January 2020
#
#   Extracts lines for a given call from the /var/log/asterisk/full and
#   shortens them for display by removing the date, 'VERBOSE[#####]',
#   'Executing', and 'in new stack'. Output is to stdout.
#
#   With no parameters, determines the last call in the log and outputs its
#   log lines.
#
#   A parameter such as '1a' is expanded to 'C-0000001a' to display the
#   lines for that call.
#
#   A second parameter can be specified to indicate the log file to search:
#   'yda' will search yesterday's log, and '20200101' will search the file
#   /var/log/asterisk/full-20200101.
##---------------------------------------------------------------------------##
function die { echo "$1" >&2; exit 1; }

# If no call number was passed, get the last call from the most current log
if [ ! "$1" ]
then
    CALL_NUM="$(egrep "C-[0-9a-f]{8}" /var/log/asterisk/full | tail -n 1 |
        sed 's/^.*\[\(C-[0-9a-f]\+\)\].*/\1/')"
fi

# Figure out a log file name
#   - if '$2' is all, concat full-* and full
#   - Nothing: use /var/log/asterisk/full
#   - Try file named in $2
#   - else try /var/log/asterisk/$2
#   - else if 'yda' try /var/log/asterisk/full-YYYMMDD (where YYYMMDD is yesterday)
#   - else try /var/log/asterisk/full-$2
[ "$2" ] && LOG_FN=$2
if [ "$2" == 'all' ]
then
    LOG_FN="/var/tmp/asterisk-all.log"
    cat /var/log/asterisk/full-* /var/log/asterisk/full >$LOG_FN
    chown asterisk: $LOG_FN
fi
[ "$LOG_FN" ] || LOG_FN='/var/log/asterisk/full'
if [ ! -f $LOG_FN -a "${LOG_FN:0:1}" != '/' ]
then
    if [ -f "/var/log-asterisk/$LOG_FN" ]
    then
        LOG_FN="/var/log-asterisk/$LOG_FN"
    elif [ $LOG_FN == 'yda' ]
    then
        YESTERDAY="$(date +%Y%m%d --date 'yesterday')"
        LOG_FN="/var/log/asterisk/full-$YESTERDAY"
    else
        LOG_FN="/var/log/asterisk/full-$LOG_FN"
    fi
fi
[ -f $LOG_FN ] || die "Could not find log file $LOG_FN"

# If $1 is 'summary', display a call summary
if [ "$1" == 'summary' ]
then
    awk -v heading_sw=1 'match($0,/(C-[0-9a-f]{8})/, a) && a[1] != curr_call {
        if (curr_call) {
            if (heading_sw)
                print "  Call      Date       Time       DID                 CNUM CNAM"
            printf("%s  %s  %12s %12s %s\n", curr_call, date_time, from_did, cnum, cnam)
            heading_sw = 0
        }
        curr_call = a[1]    
        date_time = substr($1,2) " " substr($2, 1, length($2)-1)
        from_did = ""
        cnam = ""
    }
    match($0, /__FROM_DID=([0-9]+)/, a) { from_did = a[1] }
    match($0, /CALLERID\(number\)=(.*)"/, a) { cnum = a[1] }
    match($0, /whitelist\.agi: CID name=\047([^\047]+)/, a) { cnam = a[1] }
    END {
        if (curr_call)
            printf("%s  %s  %12s %12s %s\n", curr_call, date_time, from_did, cnum, cnam)
    }
    ' $LOG_FN | LANG=C sort -u
    exit
fi

# Reformat $1 as a call number, if needed
if [ ! "$CALL_NUM" ]
then
    ZEROS='00000000'
    L="$((8 - ${#1}))"
    CALL_NUM="C-${ZEROS:0:$L}$1"
fi

grep -q $CALL_NUM $LOG_FN || die "Could not find $CALL_NUM in $LOG_FN"

echo "Log for call $CALL_NUM; log file $LOG_FN"
grep $CALL_NUM $LOG_FN | egrep -v '(Goto|If.*0\?)' | sed 's/ in new stack//' | awk '{
    print substr($2,0,8) " " gensub(/^([^:]*: )?(Executing )?/, "", 1, substr($0, 50))
}'

# vim: tabstop=4