summaryrefslogtreecommitdiff
path: root/lib/oxidized/source/sql.rb
diff options
context:
space:
mode:
authorMarcus Stoegbauer <ms@man-da.de>2014-07-20 08:48:50 +0200
committerMarcus Stoegbauer <ms@man-da.de>2014-07-20 08:48:50 +0200
commitc3b006a449bcf3f6d9afb4de31499c865748748a (patch)
treebad7bba600e301d60d59115f2380403a65228bb0 /lib/oxidized/source/sql.rb
parentcee5eaf2d57f9e9c591a2d506a35d0e338264e56 (diff)
Added support for custom SQL databases. Works as expected for sqlite, except config option "file" got renamed to "database", added authentication options for other SQL adapters and an optional query argument to overwrite the generic query.
config snipplet: sql: adapter: mysql2 user: sqluser password: sekrit host: mysql.local database: inventory table: devices query: SELECT `fqhn` AS `name`, lower(`vendor`) AS `model` FROM `devices` INNER JOIN `devtypes` ON (`devtypes`.`id` = `devices`.`model_id`) WHERE (`aktiv` = 'J') map: name: name model: model
Diffstat (limited to 'lib/oxidized/source/sql.rb')
-rw-r--r--lib/oxidized/source/sql.rb28
1 files changed, 19 insertions, 9 deletions
diff --git a/lib/oxidized/source/sql.rb b/lib/oxidized/source/sql.rb
index c744e31..c65d335 100644
--- a/lib/oxidized/source/sql.rb
+++ b/lib/oxidized/source/sql.rb
@@ -23,18 +23,28 @@ class SQL < Source
end
end
+ def connect
+ #begin
+ #require @cfg.adapter
+ #rescue LoadError
+ #raise OxidizedError, "@{cfg.adapter} not found: install gem"
+ #end
+ Sequel.connect(:adapter => @cfg.adapter,
+ :host => @cfg.host,
+ :user => @cfg.user,
+ :password => @cfg.password,
+ :database => @cfg.database)
+ end
+
def load
nodes = []
- db = case @cfg.adapter
- when 'sqlite'
- begin
- require 'sqlite3'
- rescue LoadError
- raise OxidizedError, 'sqlite3 not found: sudo apt install libsqlite3-dev; sudo gem install sqlite3'
- end
- Sequel.sqlite @cfg.file
+ db = connect
+ if @cfg.query?
+ query = db[@cfg.table.to_sym].with_sql(@cfg.query)
+ else
+ query = db[@cfg.table.to_sym]
end
- db[@cfg.table.to_sym].each do |node|
+ query.each do |node|
# map node parameters
keys = {}
@cfg.map.each { |key, sql_column| keys[key.to_sym] = node[sql_column.to_sym] }