Skip to content

Instantly share code, notes, and snippets.

@davidlee
Created March 18, 2009 05:28
Show Gist options
  • Select an option

  • Save davidlee/80961 to your computer and use it in GitHub Desktop.

Select an option

Save davidlee/80961 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
require 'ostruct'
require 'rubygems'
require 'sequel'
DB = Sequel.sqlite('syslogdb')
DB.create_table :syslogs do
primary_key :id
column :time, :timestamp
column :host, :string
column :service, :string
column :pid, :integer
column :message, :text
end
syslog = '/var/log/syslog'
lines = File.read(syslog).split "\n"
entries = DB[:syslogs]
rows = lines.map do |line|
fields = line.split(' ', 6)
h = {}
h[:time] = Time.local( Time.now.year,
fields[0],
fields[1],
*fields[2].split( ':' ) )
h[:host] = fields[3]
h[:service] = fields[4].split(/\[|\]/)[0]
h[:pid] = fields[4].split(/\[|\]/)[1]
h[:message] = fields[5]
entries << h
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment