mirror of
https://github.com/elastic/logstash.git
synced 2025-04-24 06:37:19 -04:00
initial commit for sqlite
This commit is contained in:
parent
ee0c542520
commit
e208181fec
3 changed files with 123 additions and 0 deletions
76
lib/logstash/inputs/sqlite-test.rb
Normal file
76
lib/logstash/inputs/sqlite-test.rb
Normal file
|
@ -0,0 +1,76 @@
|
|||
require "rubygems"
|
||||
require "sequel"
|
||||
require "jdbc/sqlite3"
|
||||
|
||||
public
|
||||
def init_placeholder_table(db)
|
||||
begin
|
||||
db.create_table :since_table do
|
||||
String :table
|
||||
Int :place
|
||||
end
|
||||
rescue
|
||||
p 'since tables already exists'
|
||||
end
|
||||
end
|
||||
|
||||
public
|
||||
def init_placeholder(db, table)
|
||||
p "init placeholder for #{table}"
|
||||
since = db[:since_table]
|
||||
since.insert(:table => table, :place => 1)
|
||||
end
|
||||
|
||||
public
|
||||
def get_placeholder(db, table)
|
||||
since = db[:since_table]
|
||||
x = since.where(:table => "#{table}")
|
||||
p x
|
||||
if x[:place].nil?
|
||||
p 'place is 0'
|
||||
init_placeholder(db, table)
|
||||
return 0
|
||||
else
|
||||
p "placeholder already exists"
|
||||
return x[:place][:place]
|
||||
end
|
||||
end
|
||||
|
||||
public
|
||||
def update_placeholder(db, table, place)
|
||||
since = db[:since_table]
|
||||
since.insert(:table => table, :place => place)
|
||||
end
|
||||
|
||||
public
|
||||
def get_all_tables(db)
|
||||
tables = db["SELECT * FROM sqlite_master WHERE type = 'table'"].map {|table| table[:name]}
|
||||
tables.delete_if { |table| table == 'since_table' }
|
||||
return tables
|
||||
end
|
||||
|
||||
public
|
||||
def get_n_rows_from_table(db, table, offset, limit)
|
||||
return db["SELECT * FROM #{table} WHERE ('id' > #{offset}) ORDER BY 'id' LIMIT #{limit}"].map { |row| row }
|
||||
end
|
||||
|
||||
@DB = Sequel.connect("jdbc:sqlite:/home/ec2-user/u2/log/log.db")
|
||||
|
||||
tables = get_all_tables(@DB)
|
||||
|
||||
#init table stuff
|
||||
tables.each{ |table|
|
||||
init_placeholder_table(@DB)
|
||||
last_place = get_placeholder(@DB, table)
|
||||
puts table
|
||||
#puts last_place
|
||||
}
|
||||
|
||||
#looped tabled stuff
|
||||
tables.each{ |table|
|
||||
offset = 10
|
||||
limit = 5
|
||||
#puts get_n_rows_from_table(@DB, table, offset, limit)
|
||||
#update_placeholder(@DB, table, offset+limit)
|
||||
}
|
||||
|
45
lib/logstash/inputs/sqlite.rb
Normal file
45
lib/logstash/inputs/sqlite.rb
Normal file
|
@ -0,0 +1,45 @@
|
|||
require "logstash/inputs/base"
|
||||
require "logstash/namespace"
|
||||
|
||||
require "sequel"
|
||||
require "jdbc/sqlite3"
|
||||
|
||||
class LogStash::Inputs::Sqlite < LogStash::Inputs::Base
|
||||
config_name "sqlite"
|
||||
plugin_status "beta"
|
||||
|
||||
config :dbfile, :validate => :string, :required => true
|
||||
config :exclude, :validate => :array
|
||||
config :stat_interval, :validate => :number, :default => 15
|
||||
|
||||
public
|
||||
def get_all_tables(db)
|
||||
@dataset = db["SELECT *
|
||||
FROM sqlite_master
|
||||
WHERE type = 'table'"]
|
||||
return @dataset.first
|
||||
end
|
||||
|
||||
public
|
||||
def register
|
||||
require "digest/md5"
|
||||
LogStash::Util::set_thread_name("input|sqlite|#{dbfile}")
|
||||
@logger.info("Registering sqlite input", :database => @dbfile)
|
||||
@logger.debug("connecting to sqlite db'#{@dbfile}'")
|
||||
@DB = Sequel.connect("jdbc:sqlite:#{dbfile}")
|
||||
@dataset = @DB["SELECT * FROM log"]
|
||||
end # def register
|
||||
|
||||
public
|
||||
def run(queue)
|
||||
begin
|
||||
@logger.debug("Tailing sqlite db'#{@dbfile}'")
|
||||
puts get_all_tables(@DB)
|
||||
loop do
|
||||
event_index = 0
|
||||
end # loop
|
||||
end # begin/rescue
|
||||
end #run
|
||||
|
||||
end # class Logtstash::Inputs::EventLog
|
||||
|
|
@ -68,6 +68,8 @@ Gem::Specification.new do |gem|
|
|||
gem.add_runtime_dependency "varnish-rb" #(MIT license)
|
||||
gem.add_runtime_dependency "mail" #(MIT license)
|
||||
gem.add_runtime_dependency "rbnacl" #(MIT license)
|
||||
gem.add_runtime_dependency "sequel" #(MIT license)
|
||||
gem.add_runtime_dependency "jdbc-sqlite3" #(MIT license)
|
||||
|
||||
if RUBY_PLATFORM == 'java'
|
||||
gem.platform = RUBY_PLATFORM
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue