| |||||||||||
| |||||||||||
Ruby script / CGI - demo reporting current log file
Ruby on the Web example from a Well House Consultants training course
More on Ruby on the Web [link]
Source code: brightred Module: R114
#!/usr/bin/env ruby
# Ruby script / CGI - demo reporting current log file # Graham Ellis / Version 1.0 / See http://www.wellho.net/course/ruby.html # This example was written very early in a course - before we had looked # at many of the ways of making code shorter and more maintainable. We are # showing principles, and there are shorter and better ways of doing some # of this, but we have to start somewhere! print "Content-type: text/plain\n\n" print "Greetings webby ruby world\n" print "If it's Tuesday it must be ruby\n" print "\nServer visits by category so far today ...\n\n" logger = File.new "/home/wellho/logs/access_log","r" counter = 0 terrible = "" n200 = 0; n300 = 0; n400 = 0; n500 = 0 while hit = logger.gets # print hit parts = hit.split(" ") codeno = parts[8].to_i if codeno >= 500 terrible = terrible + hit n500 = n500 + 1 elsif codeno >= 400 n400 = n400 + 1 elsif codeno >= 300 n300 = n300 + 1 else n200 = n200 + 1 end counter = counter + 1 end puts "There are #{counter} log lines of which #{n200} were code 200s" puts "There are #{counter} log lines of which #{n300} were code 300s" puts "There are #{counter} log lines of which #{n400} were code 400s" puts "There are #{counter} log lines of which #{n500} were code 500s" if n500 > 0 print "\nThere were server issues as follows\n#{terrible}" end __END__ The program finishes as __END__ so the rest is comments ... or in this case an example output: -bash-3.2$ ruby brightred Content-type: text/plain Greetings webby ruby world If it's Tuesday it must be ruby Server visits by category so far today ... There are 11489 log lines of which 10182 were code 200s There are 11489 log lines of which 165 were code 300s There are 11489 log lines of which 1140 were code 400s There are 11489 log lines of which 2 were code 500s There were server issues as follows 79.138.204.49 - - [13/Feb/2013:05:25:12 +0000] "CONNECT apis.google.com:443 HTTP/1.0" 500 666 "-" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)" 64.124.57.5 - - [13/Feb/2013:06:22:23 +0000] "GET / HTTP/1.0" 500 666 "-" "Wget/1.11.4 Red Hat modified" -bash-3.2$ Learn about this subject
This module and example are covered on the following public courses:
* Learning to program in Ruby * Ruby Programming Also available on on site courses for larger groups Books covering this topic
Yes. We have over 700 books in our library. Books
covering Ruby are listed here and when you've selected a
relevant book we'll link you on to Amazon to order.
Other Examples
This example comes from our "Ruby on the Web" training module. You'll find a description of the topic and some
other closely related examples on the "Ruby on the Web" module index page.
Full description of the source code
You can learn more about this example on the training courses listed on this page,
on which you'll be given a full set of training notes.
Many other training modules are available for download (for limited use) from our download centre under an Open Training Notes License. Other resources
• Our Solutions centre provides a number of longer technical articles.
• Our Opentalk forum archive provides a question and answer centre. • The Horse's mouth provides a daily tip or thought. • Further resources are available via the resources centre. • All of these resources can be searched through through our search engine • And there's a global index here. Web site author
Purpose of this website
This is a sample program, class demonstration or answer from a
training course. It's main purpose
is to provide an after-course service to customers who have attended our
public private or
on site courses, but the examples are made
generally available under conditions described below.
Conditions of use
Past attendees on our training courses are welcome to use individual
examples in the course of their programming, but must check
the examples they use to ensure that they are suitable for their
job. Remember that some of our examples show you how not to do
things - check in your notes. Well House Consultants take no responsibility
for the suitability of these example programs to customer's needs.
This program is copyright Well House Consultants Ltd. You are forbidden from using it for running your own training courses without our prior written permission. See our page on courseware provision for more details. Any of our images within this code may NOT be reused on a public URL without our prior permission. For Bona Fide personal use, we will often grant you permission provided that you provide a link back. Commercial use on a website will incur a license fee for each image used - details on request. |
| ||||||||||
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho PAGE: http://www.wellho.net/resources/ex.php • PAGE BUILT: Sun Oct 11 14:50:09 2020 • BUILD SYSTEM: JelliaJamb |