Formatting output so I can calculate time difference between two stamps


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Formatting output so I can calculate time difference between two stamps
# 1  
Old 04-28-2010
Formatting output so I can calculate time difference between two stamps

I know there have been a million questions regarding calculating time stamps, and with enough googling, I think I'm almost there (I'm going to use the changing the times into seconds and subtracting solution). My problem is that I'm not sure how to format my log file to get the info I need. Below is the script that's generating the report, bare with me because it's a little big

Code:
######################################################
# Program:      tearTime 
# Date Created: 27 April 2010 
# Developer:    XXX (Digital Sys. Admin)
# Description:  Shows last N number of sessions that have been torn down.
######################################################


USAGE ()                        #USAGE FUNCTION
{
        printf "USAGE: $(basename $0) <The number of sessions back you wish to look>
This script will go back through however many sessions you specify and show you any that have been torn down\n"
}

spinx ()                        #Function to animate loading screen
    {
    if [[ "$x" == "" ]];then
        x="|"   
    elif [[ "$x" == "|" ]];then
        echo -ne "$x" && echo -ne "\r" && x="/"  
    elif [[ "$x" == "/" ]];then
        echo -ne "$x" && echo -ne "\r" && x="-" 
    elif [[ "$x" == "-" ]];then
        echo -ne "$x" && echo -ne "\r" && x="\\"
    elif [[ "$x" == "\\" ]];then
        echo -ne "$x" && echo -ne "\r" && x="|" 
    fi
    }

if [[ -z $1 ]];then             #Check that a value has been specified
        USAGE   
        exit 1  
fi
    
OUTFILE=/tmp/$(basename $0).$$  #specifiy outfile
REPORTFILE=$OUTFILE.report      #specifiy report file


###########################
#   Begin Processing      #
###########################

tac /usr/local/twc/log/twc.log|egrep -i "Successfully set up session"|awk -F\' '{print $2}'|head -$1|
while read SESSION
    do egrep $SESSION /usr/local/twc/log/twc.log|egrep "Successfully set up|releaseSession" |nl >> $OUTFILE
    echo -ne "Please wait, A report of the last $1 sessions is being generated   "
    spinx
    echo -ne "\r"


    done  
echo

############################################################################
#Check if any of the sessions have been torn down, if so format for report #
############################################################################
    
TEARDOWN=$(grep -w 2 -B1 $OUTFILE |awk '{if ($1 == 1) print "Setup\t \t "$5,$14; else if ($1 == 2) print "Teardown\t "$5,$14"\n"}')

if [[ -n $TEARDOWN ]];then              #If there are any teardowns spit out the info 
        RAW=$(grep -w 2 -B1 $OUTFILE|grep -v "\-\-"|wc -l)
        COUNT=$(( $RAW / 2 ))
        printf "Of the last $1 Sessions to setup, $COUNT have already torn down\n"|tee $REPORTFILE
        printf "ACTION\t\tTIME\t\tSESSION\n"|tee -a $REPORTFILE
        echo "$TEARDOWN"|tee -a $REPORTFILE
        printf "This info has been saved to $REPORTFILE\n"      #Otherwise, do nothing
else
        printf "Of the last $1 Sessions to setup, all of them are still running\n"
fi


rm -rf $OUTFILE         #Cleanup

root@bms-nycnm-srv02:/usr/local/twc/scripts# ./tearTime.experimental 100
Please wait, A report of the last 100 sessions is being generated   -
Of the last 100 Sessions to setup, 14 have already torn down
ACTION          TIME            SESSION
Setup            01:11:36 '0021be0630be00000888'
Teardown         01:11:52 '0021be0630be00000888'

Setup            01:11:30 '001ac31ef2ae00000a7b'
Teardown         01:11:36 '001ac31ef2ae00000a7b'

Basically what the script is doing is going through a log file, getting session ID's, then looking for both the start time and the end time for each session and dumps it into a tmp file. Then it goes through and looks at the tmp file, and looks to see if there is both a start and an end time, and if so, shows the start time, the end time and the session ID. What I'd love to do is calculate the differences between the two times, but I'm not sure how to get there. The script would have to know that each "Setup" and each "Teardown" is a pair, and then work on just that pair. I was thinking maybe I could find a way to put each pair on one line and then do a while read x y timeA a b timeB ... and then compare like that, and then format them back the way I want, but that seems really sloppy, and a lot of overhead. So, does anyone have any ideas? In case it matters, this is what the raw tmp file looks like before I go through in the end and format it:

Code:
root@bms-nycnm-srv02:/usr/local/twc/scripts# tail -20 /tmp/tearTime.experimental.3935
     1  2010/04/28 05:10:51.061 GMT(04/28 01:10:51 -0400) INFO       SESSIONGW  N2BBSessionGateway_impl.msg_handler_SvrAddResCnf(): Successfully set up session '0021be48c05d0000043a'
     2  2010/04/28 05:10:55.731 GMT(04/28 01:10:55 -0400) INFO       SESSIONGW  N2BBSessionGateway_impl.releaseSession(): Sending SvrRlsRsp for session '0021be48c05d0000043a'
     1  2010/04/28 05:10:50.040 GMT(04/28 01:10:50 -0400) INFO       SESSIONGW  N2BBSessionGateway_impl.msg_handler_SvrAddResCnf(): Successfully set up session '001bd76a07f600000686'
     1  2010/04/28 05:10:49.717 GMT(04/28 01:10:49 -0400) INFO       SESSIONGW  N2BBSessionGateway_impl.msg_handler_SvrAddResCnf(): Successfully set up session '00e03647e7e424f249e7'
     2  2010/04/28 05:11:17.490 GMT(04/28 01:11:17 -0400) INFO       SESSIONGW  N2BBSessionGateway_impl.releaseSession(): Sending SvrRlsRsp for session '00e03647e7e424f249e7'
     1  2010/04/28 05:10:49.714 GMT(04/28 01:10:49 -0400) INFO       SESSIONGW  N2BBSessionGateway_impl.msg_handler_SvrAddResCnf(): Successfully set up session '000a73dcefa42676cdb0'
     1  2010/04/28 05:10:49.541 GMT(04/28 01:10:49 -0400) INFO       SESSIONGW  N2BBSessionGateway_impl.msg_handler_SvrAddResCnf(): Successfully set up session '00e03665a22e0405081d'
     1  2010/04/28 05:10:49.043 GMT(04/28 01:10:49 -0400) INFO       SESSIONGW  N2BBSessionGateway_impl.msg_handler_SvrAddResCnf(): Successfully set up session '001bd78472040000037f'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Calculate time difference between two lines

i grepped the time stamp in a file as given below now i need to calculate time difference file data: 18:29:10 22:15:50 (5 Replies)
Discussion started by: vivekn
5 Replies

2. Shell Programming and Scripting

Calculate time difference

I have time in a file in HH:MM:SS format as it contents(its not the file creation time). i need this to be converted to epoch time or time since 1970. The time is written into that file by a script, which i cannot modify. Im using AIX machine $ cat abc.txt 10:29:34 (2 Replies)
Discussion started by: gpk_newbie
2 Replies

3. Shell Programming and Scripting

Time difference between two time stamps

Hi Friends, I have 2 varaibles which contain START=`date '+ %m/%d/%y %H:%M:%S'` END=`date '+ %m/%d/%y %H:%M:%S'` i want the time difference between the two variables in Seconds. Plz help. (2 Replies)
Discussion started by: i150371485
2 Replies

4. Shell Programming and Scripting

Calculate time stamp difference

Hi All, I am new to shell scripting.I have to write a shell script for the problem statement: "A file is updated continously. If it is not updated for a day then an error message needs to pop up." So the script needs to read the last modified time of that file and current system time .If... (4 Replies)
Discussion started by: bharath.phatak
4 Replies

5. Shell Programming and Scripting

Calculate age of a file | calculate time difference

Hello, I'm trying to create a shell script (#!/bin/sh) which should tell me the age of a file in minutes... I have a process, which delivers me all 15 minutes a new file and I want to have a monitoring script, which sends me an email, if the present file is older than 20 minutes. To do... (10 Replies)
Discussion started by: worm
10 Replies

6. Shell Programming and Scripting

Calculate the Time stamp difference

hi, I have a log file which gives time stamps hh:mm:ss.sssss format in which hh- hours , mm -minutes ss.sssss - seconds.microseconds I need to calculate the time diff between sent time stamp and received time stamp .... could any one please help me.. i am tryin to write a script but... (2 Replies)
Discussion started by: firestar
2 Replies

7. Shell Programming and Scripting

How to calculate time difference between start and end time of a process!

Hello All, I have a problem calculating the time difference between start and end timings...! the timings are given by 24hr format.. Start Date : 08/05/10 12:55 End Date : 08/09/10 06:50 above values are in mm/dd/yy hh:mm format. Now the thing is, 7th(08/07/10) and... (16 Replies)
Discussion started by: smarty86
16 Replies

8. Shell Programming and Scripting

How to calculate the time difference.

Hi All, I've written a script which reads all the systems backup information and saves it in a log file. ssh -l ora${sid} ${primaryhost} "tail -1 /oracle/$ORACLE_SID/sapbackup/back$ORACLE_SID.log" | awk '{print $3,$4,$5,$6}' >> ${RESULTFILE} The output comes as below: 2008-09-30 06.00.01... (2 Replies)
Discussion started by: suri.tyson
2 Replies

9. Shell Programming and Scripting

How to calculate the time difference...

Hi All, I've written a script which reads all the systems backup information and saves it in a log file. ssh -l ora${sid} ${primaryhost} "tail -2 /oracle/$ORACLE_SID/sapbackup/back$ORACLE_SID.log" |head -1 | awk '{print echo "PREVIOUS:-- Start Date&Time: " $3,$4,echo "|| End Date&Time:... (1 Reply)
Discussion started by: suri.tyson
1 Replies

10. Shell Programming and Scripting

How to calculate this time difference

Hi, Please help me in calculating the time difference between below mentioned timestamps. a=07/17/2007 02:20:00 AM MST b=07/17/2007 02:07:46 AM MST Thanks (2 Replies)
Discussion started by: Prat007
2 Replies
Login or Register to Ask a Question