#!/bin/bash
# This script will let you filter  a queue log file by start and end dates with an option filter string
#
# Example: ./filter_queuelog 2018-01-01 2018-01-31 /var/log/asterisk/queue_log
#
# It will print out only queue_log lines for juanuay 2018
#
#
# ./filter_queuelog 2018-03-03 2018-03-10 /var/log/asterisk/queue_log John
#
# It will print out queue_log lines for march 3 to 10 containing John
#


me=`basename "$0"`

if [[ $# -lt 3 ]] ; then
    echo "Usage: ./$me [START-DATE] [END-DATE] /var/log/asterisk/queue_log FILTERSTRING"
    exit 0
fi


INITIAL_TIMESTAMP=`date -d "$1 00:00:00" +%s`
FINAL_TIMESTAMP=`date -d "$2 23:59:59" +%s`

if [ ! -f $3 ]; then
   echo "File $3 not found! Specify a valid queue_log file"
   exit 0
fi

if [[ $# -eq  3 ]]; then
awk -F\| "\$1 >= $INITIAL_TIMESTAMP && \$1 <= $FINAL_TIMESTAMP {print}" $3 
else
awk -F\| "\$1 >= $INITIAL_TIMESTAMP && \$1 <= $FINAL_TIMESTAMP {print}" $3 | grep $4
fi

