Retrieve logs for previous 4 hours


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Retrieve logs for previous 4 hours
# 1  
Old 01-17-2011
Retrieve logs for previous 4 hours

Hi,

I am in the process of configuring a script, and i intend it to retrieve logs for previous four hours, and then scan for predefined errors.

I am kind of stuck on the log retrieval part where the script will run early morning like 1 AM or 2 AM, the command as posted below will give me negative numbers, and may not give me previous day's date.

Code:
Log_From_Time=`date '+%d\/%b\/%Y:%H' | awk -F: '{printf("%s:%02d\n",$1,$2-4)}'`

P.S : The extra slash i put on date is on purpose, and i would like that way.

Please help me how to accomplish this.

Thanks, John.
# 2  
Old 01-17-2011
If you are running on a linux based OS, date supports relative times, as in:
Code:

trogdor $ date
Mon Jan 17 15:08:38 EST 2011
trogdor $ date -d '4 hours ago' '+%d/%b/%Y:%H'
17/Jan/2011:11

If your version of date does not support the "-d" option, you could select a timezone that is four hours before yours. For instance, I am in the "America/New York" timezone, so if I:
Code:
trogdor $  Log_From_Time=`TZ=America/Anchorage date '+%d/%b/%Y:%H'`
trogdor $ echo $Log_From_Time
17/Jan/2011:11

But it is frightening how non-portable that is.

Last edited by m.d.ludwig; 01-17-2011 at 07:11 PM..
This User Gave Thanks to m.d.ludwig For This Post:
# 3  
Old 01-17-2011
If you have a C compiler available I've written a little C program that prints a time with secs adjustment (positive or negative) you can also specify the output format but it defaults to %d/%b/%Y:%H
dateadj.c
Code:
#include <time.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
   long long adj = 0;
   char res[1024], fmt[1024] = "%d/%b/%Y:%H";
   time_t secs_now;
   struct tm *time_now = (struct tm *)malloc(sizeof(struct tm));
   if (argc > 1) adj=atoll(argv[argc-1]);
   if (argc > 2) strcpy(fmt, argv[1]);
   if (argc > 1 && *argv[argc-1] == '@') secs_now=atoll(argv[argc-1]+1);
   else secs_now = time(NULL)+adj;
   *time_now = *localtime(&secs_now);
   strftime(res, 1024, fmt, time_now);
   free(time_now);
   puts(res);
   return 0;
}

Compile with cc -o dateadj dateadj.c, or replace cc with gcc if you have it.

Some usage examples:
Code:
$ ./dateadj   
18/Jan/2011:08
$ ./dateadj $((-3600 * 4))
18/Jan/2011:04
$ ./dateadj '%s' 0
1295302878
$ ./dateadj @1295302878
18/Jan/2011:08


Last edited by Chubler_XL; 01-17-2011 at 06:29 PM..
# 4  
Old 01-18-2011
Try this,


Code:
#!/usr/bin/perl
@abbr = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec );
($year,$month,$day,$hr)=(localtime(time-4 * 60 * 60))[5,4,3,2];
$year+=1900;
$month++;
print "$day\\/$abbr[$month]\\/$year:$hr\n";

# 5  
Old 01-18-2011
Hi Ludwig,

Does the time difference between Anchorage and New York remains same across the year? or does it differ?

My servers are located in ET. So i could use the suggested steps.

Thanks.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

If I ran perl script again,old logs should move with today date and new logs should generate.

Appreciate help for the below issue. Im using below code.....I dont want to attach the logs when I ran the perl twice...I just want to take backup with today date and generate new logs...What I need to do for the below scirpt.............. 1)if logs exist it should move the logs with extention... (1 Reply)
Discussion started by: Sanjeev G
1 Replies

2. Shell Programming and Scripting

Remove previous line if next & previous lines have same 4th character.

I want to remove commands having no output. In below text file. bash-3.2$ cat abc_do_it.txt grpg10so>show trunk group all status grpg11so>show trunk group all status grpg12so>show trunk group all status GCPKNYAIGT73IMO 1440 1345 0 0 94 0 0 INSERVICE 93% 0%... (4 Replies)
Discussion started by: Raza Ali
4 Replies

3. UNIX for Dummies Questions & Answers

Removing the logs of previous date

Hi folks, Please advise there is directory named logs and which there are several logs are there with the name appended by date , I can take out the latest one through cd /var/log ls -ltr but please suggest the command through which I can remove the logs of all the previous dats , the... (2 Replies)
Discussion started by: SankalpS
2 Replies

4. Shell Programming and Scripting

Retrieve logs generated in last 10 mins from a log file using 'grep' command

HI All, I have a log file where the logs will be in the format as given below: 2011-05-25 02:32:51 INFO PROCESS STARTING 2011-05-25 02:32:52 INFO PROCESS STARTED . . . I want to retrieve only the logs which are less than 5 mins older than current time using grep... (3 Replies)
Discussion started by: rvhg16
3 Replies

5. Shell Programming and Scripting

Command to clear logs for every 6 hours in solaris

Hi Folks, I need to remove log files for six hours on Solaris. before i used to do for every 24 hours below is the code for 1 day older log files, now i tried using -mmin +360 but it says command not found. Can someone please help me out!!! part of the code: LOG_FILE=`find /home/Logdir... (1 Reply)
Discussion started by: Sendhil.Kumaran
1 Replies

6. Shell Programming and Scripting

Retrieve logs for last 4 hours

Our log file looks like this: 2010-11-18 00:57:01,698 ERROR Shipment Error Log:Error Details - Account Id:3962, PO:2710015, Line:2, File:221112901, Version:V1, Desc:Order cannot not be shipped as there are no line items in New state 2010-11-18 14:59:39,960 ERROR... (11 Replies)
Discussion started by: Deepthz
11 Replies

7. Shell Programming and Scripting

how to list files between last 6 hours to 3 hours

Hi Frens, I want to list some files from a directory, which contains "DONE" in their name, i am receiving files every minute. In this i want to list all the files which are newer than 6 hours but older than 3 hours, of current time i dont want my list to contain the latest files which are ... (4 Replies)
Discussion started by: Prat007
4 Replies

8. Shell Programming and Scripting

Grep yesterday logs from weblogic logs

Hi, I am trying to write a script which would go search and get the info from the logs based on yesterday timestamp and write yesterday logs in new file. The log file format is as follows: """"""""""""""""""""""""""... (3 Replies)
Discussion started by: harish.parker
3 Replies

9. UNIX for Dummies Questions & Answers

Delete logs every 3 hours

Hi, I want to setup a cronjob that will delete logs every 2 hours. I have script that delete logs per day. but logging is too big and i want to run a conjob that will delete every 2 hours. this is my current command but it deletes on a per day basis. find . -name "*.log*" -o -name... (3 Replies)
Discussion started by: tungaw2004
3 Replies

10. Shell Programming and Scripting

search and retrieve previous line in file

I've got a file with the following layout: #STMP FSgroup filename /filesysname1 filestatus 2 #STMP FSstatus filename /filesysname1 ratio 30 #STMP FSgroup filename ... (2 Replies)
Discussion started by: paulsew
2 Replies
Login or Register to Ask a Question