Grep string from logs of last 1 hour on files of 2 different servers+calculate count
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Grep string from logs of last 1 hour on files of 2 different servers+calculate count
Hi,
I am trying to grep a particular string from the files of 2 different servers without copying and calculate the total count of its occurence on both files.
File structure is same on both servers and for reference as follows:
The "last 1 hour" (ie 60 minutes) makes it non trivial.
If you are happy to just have within the current hour you could do something like the following where I'm assuming you are searching for "TransactionStatus:Requested"
Also, I'm taking your request for "grep" literally... I'd do this in python ;-)
Code:
#!/bin/bash
t=0
for s in server1 server2 ; do
n=$(ssh $s "grep -cE ^\"$(date +'%d-%b-%Y %H).*TransactionStatus:Requested\" /var/log/foo.log")
echo "$s, $n occurrences"
t=$((t+n))
done
echo "Total $t occurrences"
(This can be copared with the file date as a simple ASCII string.)
This a General Hint For Living, that not only makes for flawless logic - such that you no longer have to do any tedious mucking about, worrying about the middle of February (if you need to work out something like, this):
Code:
user@server:~ $ date --date "next month"
Sun Oct 3 09:57:52 BST 2010
...or:
Code:
user@server:~ $ date --date "fortnight ago"
Fri Aug 20 09:58:13 BST 2010
...but it also makes for much more human-readable logic. It is relatively easy to see what someone who says:
Code:
date --date "last Friday"
Fri Aug 27 00:00:00 BST 2010
... is trying to achieve.
One only has to have encountered code written by someone who A) tried to work it all out in long hand, and B) had to worry about what happened in the middle of February, to see how non-human-readable, some code, can become.
Last edited by tracertong; 09-03-2010 at 06:00 AM.
Grep string from logs of last 1 hour on files of 2 different servers+calculate count
Hi,
@evo2 : I want this logic for last 15 minutes too.
Is there any idea for getting last 15 minutes logs and search for string count in last 15 minutes.
Quote:
Originally Posted by evo2
The "last 1 hour" (ie 60 minutes) makes it non trivial.
If you are happy to just have within the current hour you could do something like the following where I'm assuming you are searching for "TransactionStatus:Requested"
Also, I'm taking your request for "grep" literally... I'd do this in python ;-)
Code:
#!/bin/bash
t=0
for s in server1 server2 ; do
n=$(ssh $s "grep -cE ^\"$(date +'%d-%b-%Y %H).*TransactionStatus:Requested\" /var/log/foo.log")
echo "$s, $n occurrences"
t=$((t+n))
done
echo "Total $t occurrences"
Find the code below which i have made but getting error.
The code checks the last 15 minutes logs and search the string and get the total count.Can anybody correct the following code or provide me the optimized way:
Well, my own suggestion, was that the date, you have, is a standard, recognizable Unix time format - so you can really just extract that date, on the assumption that it will always be the first 20 characters of each line, and push it at the date command specifying the Unix Timestamp conversion:
Code:
date +"%s" --date "<feed-value>"
This obtains the Unix time stamp, as an int. Now do the same thing with "1 hour ago" (or "15 minutes ago", or whatever) and compare the two values: then do the extra work on whatever fits those criteria.
something like this:
Code:
#!/bin/bash
hourago=`date +"%s" --date "1 hour ago"`
IFS=$'\n'
for line in $(cat filename)
do
date=`echo ${line} | cut -c 1-20`
if [ `date +"%s" --date "$date"` -ge "$hourago" ]
then
echo "Your grep goes in here."
fi
done
The advantage of this, is that the filter can be changed to anything you want:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.