LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-27-2013, 01:21 PM   #1
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
counting 503 errors in a log for the last hour...


I need to acquire the total number of 503 errors in a log file for the last hour.

I used this to get some particulars from the log file:
grep "HTTP/1.1\" 503" /mnt/varnish/varnish-access.log | tail -1
Code:
174.76.140.74 - - [27/Nov/2013:08:48:51 -0800] "GET http://secondary-schools.domain.in/rx/admin.emp.735.js HTTP/1.1" 503 435 "http://secondary-schools.domain.in/" "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36
yes, that IS a domain.in (sanitized of course).

What I have worked out are the dates, as I planned to use bash to do this, but I think I'm using the wrong tool for the job.
Code:
date '+%d/%b/%Y:%I:%M:%S'
date '+%d/%b/%Y:%I:%M:%S' --date="1 hour ago"
"'now' minus 1 hour" is the requirement and the ‘hour’ and the ‘count' part should be "configurable" (allow the client, or us to readily adjust these 2 options)

I can't use grep with a from/to range (can I?) and sed is only good for replace in a from/to format, so I concluded that awk (or perl) must be the choice and I know neither.

I'd appreciate any direction, or pointers to a working solution.

Thank you for your time.
 
Old 11-27-2013, 01:50 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
How about
Code:
logwatch --output stdout --numeric --service http --detail Medium --range 'since 1 hours ago for those hours'|grep 503 -A1
 
1 members found this post helpful.
Old 11-27-2013, 01:54 PM   #3
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374

Original Poster
Blog Entries: 37

Rep: Reputation: Disabled
Not installed, and I'm not sure I can.
But thanks!
 
Old 11-27-2013, 02:24 PM   #4
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by Habitual View Post
I need to acquire the total number of 503 errors in a log file for the last hour.
This task is simplified if your one-hour "window" may be defined as falling on neat hourly bounds. For example, 14:00:00 to 14:59:59 but not 14:17:05 to 15:17:04.

Daniel B. Martin
 
Old 11-27-2013, 02:28 PM   #5
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374

Original Poster
Blog Entries: 37

Rep: Reputation: Disabled
Daniel:

I asked that very thing too!
"in the last hour" I would grep and count from 0700 - 0800
otherwise
grep and count as "'now' - 1 hour" and he replied the latter.

Thanks.
 
Old 11-29-2013, 10:43 AM   #6
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374

Original Poster
Blog Entries: 37

Rep: Reputation: Disabled
Quote:
Originally Posted by unSpawn View Post
How about
Code:
logwatch --output stdout --numeric --service http --detail Medium --range 'since 1 hours ago for those hours'|grep 503 -A1
Thanks UnSpawn, I installed this from source (oneric whined about broken deps) on 1 of the clients' hosts.
I am reading up on it now.
 
Old 12-02-2013, 08:59 AM   #7
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374

Original Poster
Blog Entries: 37

Rep: Reputation: Disabled
https://www.linuxquestions.org/quest...er-4175486549/
 
Old 12-02-2013, 11:15 AM   #8
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374

Original Poster
Blog Entries: 37

Rep: Reputation: Disabled
Code:
#!/bin/bash
THISHOUR=$(date '+%I:%M')
LASTHOUR=$(date '+%I:%M' --date="1 hour ago")
sed -n "/$LASTHOUR/,/$THISHOUR/p" /mnt/varnish/varnish-access.log | grep "HTTP/1.1\" 503"
#EOF
real 0m11.342s
vs
logwatch --service varnish --range today --detail medium --print and > 15m = no brainer!

Thanks unSpawn.
You were just the motivation I needed.
 
Old 12-02-2013, 01:37 PM   #9
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Thanks for the feedback!
 
Old 12-02-2013, 01:44 PM   #10
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374

Original Poster
Blog Entries: 37

Rep: Reputation: Disabled
easy-peasy,
mac-n-cheesy!
 
Old 12-04-2013, 10:08 AM   #11
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374

Original Poster
Blog Entries: 37

Rep: Reputation: Disabled
Well, it was a great exercise in bash date|time skills.
But I knew there was a varnish-related tool to do the job and there is:
Code:
varnishtop -1 -i TxStatus | grep 503
Well, the client payed for the 'experience'.
 
Old 12-05-2013, 03:22 PM   #12
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374

Original Poster
Blog Entries: 37

Rep: Reputation: Disabled
Re-worked it to include the UTC timestamp. I hate TZ conversions!

Code:
#!/bin/bash
# JJ of cirrhus9.com
# Wed Dec  4 16:21:39 EST
# cron: * * * * * /etc/zabbix/count_varnish_503s.sh
# Edited: 12/05/2013 03:28:08 PM EST
# Added UTCTIME	for 'tracking' 
# varnish-access.log in this format
# append /tmp/503s.out for better tracking.
UTCTIME=$(TZ=UTC date '+%d/%b/%Y:%H:%M:%S' --date="8 hours ago")
THISMINUTE=$(date '+%I:%M')
LASTMINUTE=$(date '+%I:%M' --date="1 minute ago")
COUNT=$(sed -n "/$LASTMINUTE/,/$THISMINUTE/p" /mnt/varnish/varnish-access.log | grep "HTTP/1.1\\" 503" | wc -l)
echo "$UTCTIME = $COUNT" >> /tmp/503s.out
#EOF

The UTC Time manipulation was necessary to make the output timestamp equivalent to the format used in the varnish-access.log

Code:
i.e.:
tail -1 /mnt/varnish/varnish-access.log | awk '{print $4}'
[05/Dec/2013:12:40:43

cat /tmp/503s.out
05/Dec/2013:12:32:01 = 0
05/Dec/2013:12:33:01 = 0
05/Dec/2013:12:34:01 = 0
05/Dec/2013:12:35:01 = 0
05/Dec/2013:12:36:01 = 0
05/Dec/2013:12:37:01 = 0
05/Dec/2013:12:38:01 = 0
05/Dec/2013:12:39:01 = 0
05/Dec/2013:12:40:01 = 0
http://www.bournetoraiseshell.com/ar...31202122038302

Last edited by Habitual; 12-05-2013 at 03:24 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
rsync - how to suppress counting files during --process to log output file ? masuch Linux - Newbie 1 09-16-2012 03:20 PM
(Bash) Redirect all output from script to all.log and copy of errors to err.log hmsdefender Programming 5 03-05-2010 01:52 PM
Cron job issue - every hour works, but specific hour fails lunarleviathan Linux - Newbie 6 11-20-2009 12:19 AM
FX5600 Geforce - FC9 - black screen, no (EE) errors in /var/log/Xorg.0.log?? boyd98 Linux - Hardware 1 06-17-2008 08:56 PM
change clock from 24 hour to 12 hour in suse 9.2/KDE 3.3 jmlumpkin Linux - Newbie 1 01-22-2005 11:45 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:02 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration