Sponsored Content
Top Forums Shell Programming and Scripting Calculate response time from log Post 302499208 by BeefStu on Wednesday 23rd of February 2011 04:33:46 PM
Old 02-23-2011
Here is a good start for you but you will need to add code
to handle crossing mid-might. Hope this helps.
Code:
#!/bin/ksh
calc_time()
{
set -x
start_time=$1
end_time=$2
start_hh=$(echo $start_time | $AWK -F':' ' { print $1 } ')
start_mm=$(echo $start_time | $AWK -F':' ' { print $2 } ')
start_ss=$(echo $start_time | $AWK -F':' ' { print $3 } ')
 
end_hh=$(echo $end_time | $AWK -F':' ' { print $1 } ')
end_mm=$(echo $end_time | $AWK -F':' ' { print $2 } ')
end_ss=$(echo $end_time | $AWK -F':' ' { print $3 } ')
 
startsecs=`expr $start_hh \* 3600`
startsecs=`expr $startsecs + $start_mm \* 60`
startsecs=`expr $startsecs + $start_ss`
endsecs=`expr $end_hh \* 3600`
endsecs=`expr $endsecs + $end_mm \* 60`
endsecs=`expr $endsecs + $end_ss`
echo `expr $endsecs - $startsecs`
}
set -x
AWK=nawk
stime=`date +"%H:%M:%S"`
sleep 3
etime=`date +"%H:%M:%S"`
num_secs=$(calc_time $stime $etime )
echo "$stime $etime $num_secs"

---------- Post updated at 04:33 PM ---------- Previous update was at 04:19 PM ----------

Here is a solution in "C" if you like. It requires less tinkering but will
give you what you need.

Code:
#include <stdio.h>
#include <time.h>
static char *now();
static int csstrptime(char *, char *, struct tm * );
main()
{
char *format1="YYYYMMDDHHMMSS";
char *format2="YYYYMMDD";
struct tm tm1, tm2;
time_t secs1, secs2;
char *d1= now();
char *d2="20030118";
memset (&tm1, 0, sizeof tm1);
memset (&tm2, 0, sizeof tm2);
if (csstrptime(d1, format1, &tm1) != 0)
{
printf ("csstrptime failed. Date = %s format = %s", d1, format1 );
return (1);
}
if (csstrptime(d2, format2, &tm2) != 0)
{
printf ("csstrptime failed. Date = %s format = %s", d1, format2 );
return (1);
}
secs1 = mktime(&tm1);
secs2 = mktime(&tm2);
if (secs2 > secs1 )
printf ("Secs = %ld\n", secs2-secs1);
else
printf ("Secs = %ld\n", secs1-secs2);
}
 
 
 
/*****************************************************************************
**
** Name csstrptime()
**
** Purpose: This function converts the character string pointed to by buf to
** values, which are stored in the tm structure pointed to by tm,
** using the format specified by format.
**
** There is a system call strptime(), which works this emulates but
** there are known bugs in that function. For example, when parsing 
** the format'YYYYMMDDHHMMSS' on Solaris it does not work correctly.
** The format must be 'YYYY MM DD HH MM SS'.
**
** In addition, strptime() does not appear to be avialable on NT.
** Syntax:
** static int csstrptime(const char *buf, const char *format, struct tm *tm);
**
** Parameters:
** buf IN Date to converted
**
** format IN Format of DATE (YYYYMMDDHHMMSS or YYYYMMDD)
**
** tm OUT time structure
** int tm_sec; seconds after the minute - [0, 61]
** int tm_min; minutes after the hour - [0, 59]
** int tm_hour; hour since midnight - [0, 23]
** int tm_mday; day of the month - [1, 31] 
** int tm_mon; months since January - [0, 11]
** int tm_year; years since 1900 
** int tm_wday; days since Sunday - [0, 6]
** int tm_yday; days since January 1 - [0, 365]
** int tm_isdst; daylight savings time flag.
**
** Returns: Returns zero for success one if there is an error.
** 
*****************************************************************************/ 
 
static int
csstrptime(char *buf, char *format, struct tm *tm )
{
int year=0, month=0, day=0, hour=0, minute=0, second=0;
 
tm->tm_isdst = 0;
if (!strcmp(format, "YYYYMMDDHHMMSS") &&
(sscanf(buf, "%4d%2d%2d%2d%2d%2d",
&year, &month, &day, &hour, &minute, &second) == 6))
{
tm->tm_year = year - 1900;
tm->tm_mon = month - 1;
tm->tm_mday = day;
tm->tm_hour = hour;
tm->tm_min = minute;
tm->tm_sec = second;
}
else if (!strcmp(format, "YYYYMMDD") &&
(sscanf(buf, "%4d%2d%2d", &year, &month, &day) == 3))
{
tm->tm_year = year - 1900;
tm->tm_mon = month - 1;
tm->tm_mday = day;
/* Set the time to midnight since not time was given */
tm->tm_hour = 0;
tm->tm_min = 0;
tm->tm_sec = 0;
}
else
return (1);
return(0);
}
 
 
 
/*****************************************************************************
**
** Name now()
**
** Syntax: static char *now();
**
** Parameters: NONE
** Returns: Returns the the current date and time in YYYYMMDDHHMMSS format.
** 
*****************************************************************************/ 
/*
** This function returns the current date and time in YYYYMMDDHHMMSS format
*/
static char *
now()
{
static char current_time[20];
time_t clock;
 
clock = time((time_t)0);
(void)strftime(current_time, sizeof(current_time), "%Y%m%d%H%M%S", localtime(&clock));
return (current_time);
}


Last edited by Franklin52; 02-24-2011 at 03:29 AM.. Reason: Please use code tags, thank you
 

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

ls -l : response time slow

Hi all, If I give ls , it lists files in 1 second. It I give ls -l , it takes 8 seconds There are only 55 files in the directory. Any explanation? Thanks Wilson (4 Replies)
Discussion started by: geraldwilson
4 Replies

2. UNIX for Advanced & Expert Users

Response time under packet loss

I am experiencing a problem where under a dial condition I am experiencing packet loss, which is failrly normal, but the response to the packet loss is taking bewteen 6 and 10 seconds. Could someone please advise what the industry standard is on the response time under a packet loss senario. (1 Reply)
Discussion started by: shane
1 Replies

3. UNIX for Advanced & Expert Users

Response time & IO max

in HP-UX how i can measure the response time and how can i find the maximum IO (1 Reply)
Discussion started by: salhoub
1 Replies

4. 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

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 avg response time on hourly basis

Hi, I am trying to calculate avg response time on hourly basis from the log file which has millions of records. As of now I am trying with creating temp file which will have lines with unique id and start time and end time and after that another script will run on this temp file to... (7 Replies)
Discussion started by: random_thoughts
7 Replies

7. Shell Programming and Scripting

To check time stamp in log file and calculate.

Hi Friends, I have the following logfile. i want to make a script for calculate time by time2 - time1 1600266278|random|1|2014-09-19 02:08:56.024|2014-09-19 02:08:59.398|A|B|ROOM|Num0208559970111101788|1|dog|dos 1600266200|random|4|2014-09-19 02:08:06.572|2014-09-19... (2 Replies)
Discussion started by: ooilinlove
2 Replies

8. Shell Programming and Scripting

Script to check response time from nginx logs

Hi, My goal is to monitor the response time from the access logs of nginx server. I am using gawk to print the needed fields - 'response time' and 'name of the service' from nginx logs. Command: gawk '($6 ~ /cloudservice/) {print $10, $6}' access.log Output: 0.645 /nc/cloudservice... (6 Replies)
Discussion started by: nshah11
6 Replies

9. Shell Programming and Scripting

Calculate Time diff in milli milliseconds(Time format : HH:MM:SS,NNN)

Hi All, I have one file which contains time for request and response. I want to calculate time difference in milliseconds for each line. This file can contain 10K lines. Sample file with 4 lines. for first line. Request Time: 15:23:45,255 Response Time: 15:23:45,258 Time diff... (6 Replies)
Discussion started by: Raza Ali
6 Replies

10. Shell Programming and Scripting

Processes running response time

Hi All I have been asked to write scripts within our monitoring tool for a vast requirement set. One of the requirements is below: • Lowest, Highest & Average response times of the Documentum process threads serving client requests Essentially they want a view where we can see the... (4 Replies)
Discussion started by: simpsa27
4 Replies
All times are GMT -4. The time now is 01:38 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy