summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Pointner <equinox@helsinki.at>2015-05-08 18:17:06 (GMT)
committerChristian Pointner <equinox@helsinki.at>2015-05-08 18:17:06 (GMT)
commit9407d2d102b3759f37d1cc45027cf27c39994303 (patch)
treea4a913eba6ea8fe37c804944517f835e173f2460
parent3ff60e5490c8b782dbe103b7b60754468c6e5c9c (diff)
conf refactoring for nopsysstated and nopcollectd
-rw-r--r--src/db.lua22
-rwxr-xr-xsrc/nopcollectd14
-rwxr-xr-xsrc/nopsysstated10
3 files changed, 23 insertions, 23 deletions
diff --git a/src/db.lua b/src/db.lua
index d606593..d244bad 100644
--- a/src/db.lua
+++ b/src/db.lua
@@ -69,17 +69,17 @@ function db.init(db, user, pwd, host, port, table)
if cur == nil then
return nil, err
end
-
+
local timestamp = cur:fetch()
if timestamp == nil then timestamp = 0 end
return timestamp
end
-
+
function mydb:findMissingEntries(lasttimestamp)
local lastts = self.con:escape(lasttimestamp)
return self.con:execute("SELECT * FROM " .. self.table .. " WHERE timestamp > " .. lastts)
end
-
+
function mydb:getNextMissingEntry(cur)
local data = {}
data = cur:fetch(data, "a")
@@ -88,14 +88,14 @@ function db.init(db, user, pwd, host, port, table)
end
return data
end
-
+
function mydb:getEntry(timestamp)
local ts = self.con:escape(timestamp)
local cur, err = self.con:execute("SELECT * FROM " .. self.table .. " WHERE timestamp = " .. ts)
if cur == nil then
return nil, err
end
-
+
local data = {}
data = cur:fetch(data, "a")
if data == nil then
@@ -103,7 +103,7 @@ function db.init(db, user, pwd, host, port, table)
end
return data
end
-
+
function mydb:addEntry(data)
local timestamp = self.con:escape(data.timestamp)
local cart = self.con:escape(data.cart)
@@ -113,21 +113,21 @@ function db.init(db, user, pwd, host, port, table)
local artist = self.con:escape(data.artist)
local album = self.con:escape(data.album)
local ismusic = self.con:escape(data.ismusic)
-
+
local cur, err = self.con:execute("REPLACE INTO " .. self.table .. " VALUES('" .. timestamp .. "', '" .. cart .. "', '" .. len .. "', '" .. showtitle .. "', '" .. title .. "', '" .. artist .."', '" .. album .. "', '" .. ismusic .. "')")
if cur == nil then
return nil, err
end
-
+
return true
end
-
+
function mydb:getLastSysState()
local cur, err = self.con:execute("SELECT state FROM " .. self.table .. " WHERE timestamp = (SELECT MAX(timestamp) FROM " .. self.table .. ")")
if cur == nil then
return nil, err
end
-
+
local state = cur:fetch()
if state == nil then state = "unknown" end
return state
@@ -148,7 +148,7 @@ function db.init(db, user, pwd, host, port, table)
if self.con then
self.con:close()
end
-
+
if self.env then
self.env:close()
end
diff --git a/src/nopcollectd b/src/nopcollectd
index 0f7c66e..933fb81 100755
--- a/src/nopcollectd
+++ b/src/nopcollectd
@@ -2,7 +2,7 @@
--
-- rhnop
--
--- Copyright (C) 2011-2014 Christian Pointner <equinox@helsinki.at>
+-- Copyright (C) 2011-2015 Christian Pointner <equinox@helsinki.at>
--
-- This file is part of rhnop.
--
@@ -25,24 +25,24 @@
-- the arguments should be: now# nowlen next# nextlen
--
-require "socket"
+socket = require "socket"
package.path = package.path .. ";_rhnoplibdir_/?.lua"
db = require "db"
+conf = require "conf"
if #arg < 1 then
io.stderr:write("too few parameters\n")
os.exit(1)
end
-conffile = "nopcollectd." .. arg[1] .. ".conf"
-conf = require "conf"
+local cnf = conf.load("nopcollectd." .. arg[1] .. ".conf")
-src_db = assert(db.init(conf.src_db, conf.src_user, conf.src_pwd, conf.src_host, conf.src_port, conf.src_table))
-dst_db = assert(db.init(conf.dst_db, conf.dst_user, conf.dst_pwd, conf.dst_host, conf.dst_port, conf.dst_table))
+src_db = assert(db.init(cnf.src_db, cnf.src_user, cnf.src_pwd, cnf.src_host, cnf.src_port, cnf.src_table))
+dst_db = assert(db.init(cnf.dst_db, cnf.dst_user, cnf.dst_pwd, cnf.dst_host, cnf.dst_port, cnf.dst_table))
local sock = assert(socket.tcp())
-local ret, err = sock:connect(conf.sync_host, conf.sync_port)
+local ret, err = sock:connect(cnf.sync_host, cnf.sync_port)
if ret == nil then
print(err)
return 1
diff --git a/src/nopsysstated b/src/nopsysstated
index f1a05f8..dc43c99 100755
--- a/src/nopsysstated
+++ b/src/nopsysstated
@@ -25,18 +25,18 @@
-- the arguments should be: now# nowlen next# nextlen
--
-require "socket"
+socket = require "socket"
package.path = package.path .. ";_rhnoplibdir_/?.lua"
db = require "db"
-
-conffile = "nopsysstated.conf"
conf = require "conf"
-db = assert(db.init(conf.db_db, conf.db_user, conf.db_pwd, conf.db_host, conf.db_port, conf.db_table))
+local cnf = conf.load("nopsysstated.conf")
+
+db = assert(db.init(cnf.db_db, cnf.db_user, cnf.db_pwd, cnf.db_host, cnf.db_port, cnf.db_table))
local sock = assert(socket.tcp())
-local ret, err = sock:connect(conf.host, conf.port)
+local ret, err = sock:connect(cnf.host, cnf.port)
if ret == nil then
print(err)
return 1