Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7686879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:34:44+00:00 2026-05-31T19:34:44+00:00

I have an apache log I am looking to filter on HTTP status codes.

  • 0

I have an apache log I am looking to filter on HTTP status codes. Ive done the math manually since my ruby program isn’t returning the right numbers. I also plan to use this same code to possibly output requency of vitsits by IP and URLs visited but not if I cant get my code working.

heres what i got

class Numeric
  def percent_of(n)
    self.to_f / n.to_f * 100.0
  end
end

stat_hash = Hash.new(0)
url_hash = Hash.new(0)
ip_hash = Hash.new(0)
#lineArray= Array.new()
file = File.open("./test_log", 'r')

total = 0

#load hash
file.each_line do |line|
    total += 1
    lnarr = line.chomp.split #Split is messed up needs to split to array first i think then hash from array  similar to Lab 10
   #Array needs to split to {IP,Date/time, URL, Status, size}
   #http://httpd.apache.org/docs/1.3/logs.html
    stat_hash[lnarr[-2]] += 1
    url_hash[lnarr[-4]] += 1
    ip_hash[lnarr[0]] +=1
end

for i in 0..stat_hash.length-1 do
    percent = stat_hash.percent_of(total) #current equation will not work. Hash does populate with the http status do but math
#does not output any average. returns undefined method but method is defined at top.
    status = stat_hash[i]
end

puts total
#puts (stat_hash[i]/total)
stat_hash.sort.each { |status| puts "#{status}:"+ percent}

The test log I am running against can be found here:
http://dl.dropbox.com/u/71927/test_log

I’ve done it manually and am expecting

200:90%
301:8%
401:1%
404:1%

but I am getting

200: 97%
301: 1%
304: 8%
403: 2%

which is simply their number of time they occur. if you add them up its 108 and there are 108 lines in the log file with status codes in them.

EDIT: As a follow up to this question percentages to round up as I need them to using the ceiling function of the float data type with ‘.ceil’ and got my data to parse specific data by writing in command line options with the optparser

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T19:34:44+00:00Added an answer on May 31, 2026 at 7:34 pm

    I’m not sure if you copied/pasted correctly, but I don’t see how you’re successfully accessing the percent variable outside of your for loop.

    I’d drop the percent_of function and simplify the end using the (more Ruby-esque) map function like this:

    stat_hash.map do |code, num_times|
      puts "#{code}: #{(num_times*100.0)/total}%"
    end
    

    I’d also recommend using something more descriptive than total (such as total_occurences or similar). I couldn’t access your test_log file, but I ran your code with my change against a dummy log I had and it produced accurate results.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In the Apache log configuration it is possible to specify that the HTTP auth
I have all my Apache access log files as access.log, access.log.1 access.log.1.gz etc... What
I have a java app that uses log4j. Config: log4j.rootLogger=info, file log4j.appender.file=org.apache.log4j.DailyRollingFileAppender log4j.appender.file.File=${user.home}/logs/app.log log4j.appender.file.layout=org.apache.log4j.PatternLayout
I have apache 2.2 and tomcat 5.5 running on a Windows XP machine. Which
I have Apache running on a public-facing Debian server, and am a bit worried
I have Apache with mod_rewrite, and whenever I enter a URI with an accented
I have apache web server installed as frontend and I have j2ee SAP Netweaver
I have Apache 2 running on a VPS server (running Debian). I recently changed
I have Apache/SVN running on Windows Server 2003 with authentication via LDAP/Active Directory and
I have Apache installed with the XAMPP package, and I'm working locally. I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.