Created
December 2, 2010 20:24
-
-
Save gorsuch/725992 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# | |
# simple rrd trending of http access logs | |
# Usage example: | |
# trends.rb /var/log/messages | |
require "rubygems" | |
require "eventmachine" | |
require "eventmachine-tail" | |
require "rrd" | |
class Reader < EventMachine::FileTail | |
def initialize(path, startpos=-1) | |
super(path, startpos) | |
puts "Tailing #{path}" | |
@buffer = BufferedTokenizer.new | |
end | |
def receive_data(data) | |
@buffer.extract(data).each do |line| | |
@@counter += 1 | |
end | |
end | |
end | |
def init_rrd | |
rrd = RRD::Base.new('trends.rrd') | |
if !File.exists?('trends.rrd') | |
rrd.create :start => Time.now - 10.seconds, :step => 5.minutes do | |
datasource "requests", :type => :gauge, :heartbeat => 10.minutes, :min => 0, :max => :unlimited | |
archive :average, :every => 10.minutes, :during => 1.year | |
end | |
end | |
rrd | |
end | |
def update_rrd(time, value) | |
rrd = init_rrd | |
rrd.update time, value | |
end | |
def graph | |
RRD.graph 'trends.png', :title => 'Test', :width => 800, :height => 250, :color => ["FONT#000000", "BACK#FFFFFF"] do | |
area 'trends.rrd', :requests => :average, :color => "#0066cc", :label => 'requests' | |
end | |
end | |
def main(args) | |
if args.length == 0 | |
puts "Usage: #{$0} <path> [path2] [...]" | |
return 1 | |
end | |
@@counter = 0 | |
EventMachine.run do | |
EM.add_periodic_timer(300) do | |
time = Time.now | |
puts "#{time} #{@@counter}" | |
update_rrd time, @@counter | |
graph | |
@@counter = 0 | |
end | |
args.each do |path| | |
EventMachine::file_tail(path, Reader) | |
end | |
end | |
end # def main | |
exit(main(ARGV)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment