1

I'm writing a bash script to run on CentOS to first grab the the lines for the start and end of an application session, then output if the duration is longer than an hour.

The timestamp format in the logfile is: 2017-01-03T00:00:15.529596-03:00

$i is the application session ID.


Here is what I have so far:

for i in $(  grep 'session-enter\|session-exit' logfile | awk '{ print $5}' ); do
                        echo ""
                        echo "*****   $i   *****"
        grep 'session-enter\|session-exit' logfile | grep $i

                start=$(grep session-enter logfile | grep $i | awk '{ print $1 }' | sed 's/-03:00//g')
                end=$(grep session-exit logfile | grep $i | awk '{ print $1 }' | sed 's/-03:00//g')

                epochStart=$(date -d "$start" +%s )
                epochEnd=$(date -d "$end" +%s )

                                duration=$( date -u -d "0 $epochEnd seconds - $epochStart seconds" +"%H:%M:%S" )

                        if [ "$epochStart"="" ] || [ "$epochEnd"="" ]
                                then
                                     echo Duration: $duration
                                else
                                     continue
                        fi


done

Any help on this is greatly appreciated.

  • can you post the sample input data and expected output. – Kamaraj Jan 5 '17 at 0:14
0

It looks like you already have the start time and end time stored as seconds since epoch. If that's true, you should be able to do:

duration=$(( $epochEnd - $epochStart ))
[ $duration -gt 3600 ] && echo "Duration: $duration"
  • $epochStart and $epochEnd aren't integers. – Mary Burak Jan 4 '17 at 23:16
  • 1
    why aren't they, @MaryBurak? Your script has: epochStart=$(date -d "$start" +%s ) which should return the number of seconds since the epoch to $start. – Jeff Schaller Jan 5 '17 at 12:08
0

Based on the assumption of below input data, i written this code..

pass the input file as argument for the script

input file

$ cat input
2017-01-03T00:00:15.529596-03:00 session-enter TEST1
......
.......
......
2017-01-03T00:00:37.529596-03:00 session-exit TEST1
.....
.....
2017-01-03T00:00:18.529596-03:00 session-enter TEST2
......
.......
......
2017-01-03T00:03:20.529596-03:00 session-exit TEST2
...........
..........
2017-01-03T00:00:18.529596-03:00 session-enter TEST3
......
.......
......
2017-01-03T02:03:20.529596-03:00 session-exit TEST3

extract.sh

#!/bin/bash

SESSION_LOG_FILE=${1}
SESSION_EXTRACT_FILE=/tmp/$$.txt

awk '/enter/{sname=$NF;start=$1;}/exit/ && $0~sname{end=$1;}start && end{print sname,start,end;start="";end="";}' "${SESSION_LOG_FILE}" > ${SESSION_EXTRACT_FILE}

while read SESSION_NAME SESSION_START SESSION_END
do
        echo "---------${SESSION_NAME}---------"
        echo "Start Time : ${SESSION_START}"
        echo "End   Time : ${SESSION_END}"
        START_EPOCH=${SESSION_START%-*}
        START_EPOCH=$(date -d "${START_EPOCH}" +%s)
        END_EPOCH=${SESSION_END%-*}
        END_EPOCH=$(date -d "${END_EPOCH}" +%s)
        DIFFERENCE=$((${END_EPOCH}-${START_EPOCH}))
        echo "DIFFERENCE in seconds : ${DIFFERENCE}"
done < "${SESSION_EXTRACT_FILE}"

output

$ ./extract.sh input
---------TEST1---------
Start Time : 2017-01-03T00:00:15.529596-03:00
End   Time : 2017-01-03T00:00:37.529596-03:00
DIFFERENCE in seconds : 22
---------TEST2---------
Start Time : 2017-01-03T00:00:18.529596-03:00
End   Time : 2017-01-03T00:03:20.529596-03:00
DIFFERENCE in seconds : 182
---------TEST3---------
Start Time : 2017-01-03T00:00:18.529596-03:00
End   Time : 2017-01-03T02:03:20.529596-03:00
DIFFERENCE in seconds : 7382

check if time seconds is greater than 3600

#!/bin/bash

SESSION_LOG_FILE=${1}
SESSION_EXTRACT_FILE=/tmp/$$.txt

awk '/enter/{sname=$NF;start=$1;}/exit/ && $0~sname{end=$1;}start && end{print sname,start,end;start="";end="";}' "${SESSION_LOG_FILE}" > ${SESSION_EXTRACT_FILE}

while read SESSION_NAME SESSION_START SESSION_END
do
        START_EPOCH=${SESSION_START%-*}
        START_EPOCH=$(date -d "${START_EPOCH}" +%s)
        END_EPOCH=${SESSION_END%-*}
        END_EPOCH=$(date -d "${END_EPOCH}" +%s)
        DIFFERENCE=$((${END_EPOCH}-${START_EPOCH}))
        if [ "${DIFFERENCE}" -gt "3600" ]
                then
                        echo "---------${SESSION_NAME}---------"
                        echo "Start Time : ${SESSION_START}"
                        echo "End   Time : ${SESSION_END}"
                        echo "DIFFERENCE in seconds : ${DIFFERENCE}"
                fi
done < "${SESSION_EXTRACT_FILE}"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.