LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   A little scripting help.. (https://www.linuxquestions.org/questions/linux-newbie-8/a-little-scripting-help-948896/)

blsimpson 06-06-2012 06:03 PM

A little scripting help..
 
Hello all, been gathering a lot of info from here for the past couple of years, its been very invaluable.

My problem. We have an application that generates log files, on multiple machines, that I need to monitor on a daily basis. These machines also do not have access to the internet, so no email. The log files roll over to new ones when they reach a certain size, and have a time stamp of when they were created, but will have multiple days inside it. I need to monitor the last 24 hours for certain strings (ERROR and Exception). I figured out how multiple ways to pull these out and place them into another file, but I am having a problem just getting the last 24 hours.. Here is what I am using, which was taken from the internet from a couple different places and modified to fit what I need.


Code:

#!/bin/bash

#if there's no 'log' file
if [ ! -e "log-`date '+%Y-%m-%d'`.log" ] ; then
# let's search for the word 'ERROR' in our log and save the output to the newly created file named 'log' with a date stamp.
  grep ERROR sanjay.log > "log-`date '+%Y-%m-%d'`.log"
# if however the 'logs' file already exists..
else
# update our log*
  grep ERROR sanjay.log >> "log-`date '+%Y-%m-%d'`.log"
fi

Feel free to tell me I am doing it completely wrong :) Some examples of what the log looks like:

Code:

2012-06-06 17:30:04,675 INFO [org.springframework.integration.handler.LoggingHandler] - <JobExecution: id=15137, startTime=Wed Jun 06 17:30:04 EDT 2012, endTime=Wed Jun 06 17:30:04 EDT 2012, lastUpdated=Wed Jun 06 17:30:04 EDT 2012, status=COMPLETED, exitStatus=exitCode=COMPLETED;exitDescription=, job=[JobInstance: id=14931, JobParameters=[{INPUT_FILE_NAME=REVERSE_DRAFT5_FILE_2012-06-06-17-30-00, input.file.name=REVERSE_DRAFT5_FILE_2012-06-06-17-30-00}], Job=[reverseDraft5MessageProcessingJob]]>
job-requests
2012-06-06 17:30:04,675 INFO [com.xxxxxxx.xxxxxxx.batch.ap.draft5.ReverseDraft5JobProcessor] - <End of Reverse Draft5 Job at:2012-06-06-17-30-04>


chrism01 06-06-2012 06:13 PM

Seeing as you are familiar with the data cmd, use that to generate yesterday's date in the matching format to the log file, and then grep that eg (simple example)
Code:

x@x> cat t.t
hostname1 17/08/10
hostname1 18/08/10
hostname1 19/08/10
hostname1 20/08/10
hostname2 17/08/10
hostname2 18/08/10
hostname2 19/08/10
hostname2 20/08/10

x@x> d=17/08/10
x@x> echo $d
17/08/10

x@x> grep $d t.t
hostname1 17/08/10
hostname2 17/08/10

should give you the idea.
You may find these useful
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/

blsimpson 06-07-2012 12:11 PM

Quote:

Originally Posted by chrism01 (Post 4697298)
Seeing as you are familiar with the data cmd, use that to generate yesterday's date in the matching format to the log file, and then grep that eg (simple example)
Code:

x@x> cat t.t
hostname1 17/08/10
hostname1 18/08/10
hostname1 19/08/10
hostname1 20/08/10
hostname2 17/08/10
hostname2 18/08/10
hostname2 19/08/10
hostname2 20/08/10

x@x> d=17/08/10
x@x> echo $d
17/08/10

x@x> grep $d t.t
hostname1 17/08/10
hostname2 17/08/10

should give you the idea.
You may find these useful
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/

I am not sure I understand the first part of your post at all. Thanks for the links though, I will give those a read.

Anyone else have some insight?


All times are GMT -5. The time now is 03:07 AM.