diff options
-rw-r--r-- | rhnop-client/db.lua | 152 | ||||
-rwxr-xr-x | rhnop-client/nopcollectd | 19 | ||||
-rw-r--r-- | rhnop-client/nopcollectd.master.conf | 6 | ||||
-rw-r--r-- | rhnop-client/nopcollectd.standby.conf | 6 |
4 files changed, 93 insertions, 90 deletions
diff --git a/rhnop-client/db.lua b/rhnop-client/db.lua index ce3b15f..5a6a92c 100644 --- a/rhnop-client/db.lua +++ b/rhnop-client/db.lua @@ -21,103 +21,107 @@ require "luasql.mysql" --- CREATE DATABASE rhnop --- GRANT select,insert,update ON rhnop.* TO 'nopsyncd' IDENTIFIED BY '<password>'; --- GRANT select ON rhnop.* TO 'nopcollectd' IDENTIFIED BY '<password>'; --- USE rhnop --- CREATE TABLE IF NOT EXISTS now (timestamp BIGINT UNSIGNED PRIMARY KEY NOT NULL, cart INT NOT NULL, len INT, showtitle VARCHAR(255), title VARCHAR(255), artist VARCHAR(255), album VARCHAR(255), ismusic BOOLEAN); +-- for destination database +-- CREATE DATABASE nop +-- GRANT select,insert,update,delete ON nop.* TO 'nopcollectd' IDENTIFIED BY '<password>'; +-- GRANT select ON nop.* TO 'nop' IDENTIFIED BY '<password>'; +-- USE nop +-- CREATE TABLE IF NOT EXISTS master (timestamp BIGINT UNSIGNED PRIMARY KEY NOT NULL, cart INT NOT NULL, len INT, showtitle VARCHAR(255), title VARCHAR(255), artist VARCHAR(255), album VARCHAR(255), ismusic BOOLEAN); +-- CREATE TABLE IF NOT EXISTS standby (timestamp BIGINT UNSIGNED PRIMARY KEY NOT NULL, cart INT NOT NULL, len INT, showtitle VARCHAR(255), title VARCHAR(255), artist VARCHAR(255), album VARCHAR(255), ismusic BOOLEAN); local db = {} -function db:init(db, user, pwd, host, port, table) - self.table = table +function db.init(db, user, pwd, host, port, table) + local mydb = {} + + mydb.table = table local err - self.env, err = luasql.mysql() - if self.env == nil then + mydb.env, err = luasql.mysql() + if mydb.env == nil then return nil, err end - self.con, err = self.env:connect(db, user, pwd, host, port) - if self.con == nil then + mydb.con, err = mydb.env:connect(db, user, pwd, host, port) + if mydb.con == nil then return nil, err end - local ret, err = self.con:setautocommit(true) + local ret, err = mydb.con:setautocommit(true) if ret == nil then return nil, err end - return true -end - -function db:getLastEntry() - local cur, err = self.con:execute("SELECT MAX(timestamp) FROM " .. self.table) - if cur == nil then - return nil, err + function mydb:getLastEntry() + local cur, err = self.con:execute("SELECT MAX(timestamp) FROM " .. self.table) + if cur == nil then + return nil, err + end + + local timestamp = cur:fetch() + if timestamp == nil then timestamp = 0 end + return timestamp end - - local timestamp = cur:fetch() - if timestamp == nil then timestamp = 0 end - return timestamp -end - -function db:findMissingEntries(lasttimestamp) - local lastts = self.con:escape(lasttimestamp) - return = self.con:execute("SELECT * FROM " .. self.table .. " WHERE timestamp > '" .. lastts .. "')") -end - -function db:getNextMissingEntry(cur) - local data = {} - data = cur:fetch(data, "a") - if data == nil then - return nil, "that's all folks" + + function mydb:findMissingEntries(lasttimestamp) + local lastts = self.con:escape(lasttimestamp) + return self.con:execute("SELECT * FROM " .. self.table .. " WHERE timestamp > " .. lastts) end - return data -end - -function db: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 + + function mydb:getNextMissingEntry(cur) + local data = {} + data = cur:fetch(data, "a") + if data == nil then + return nil, "that's all folks" + end + return data end - - local data = {} - data = cur:fetch(data, "a") - if data == nil then - return nil, "nothing found" + + 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 + return nil, "nothing found" + end + return data end - return data -end - -function db:addEntry(data) - local timestamp = self.con:escape(data.timestamp) - local cart = self.con:escape(data.cart) - local len = self.con:escape(data.len) - local showtitle = self.con:escape(data.showtitle) - local title = self.con:escape(data.title) - 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 + + function mydb:addEntry(data) + local timestamp = self.con:escape(data.timestamp) + local cart = self.con:escape(data.cart) + local len = self.con:escape(data.len) + local showtitle = self.con:escape(data.showtitle) + local title = self.con:escape(data.title) + 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 - - return true -end - -function db:close() - if self.con then - self.con:close() + + function mydb:close() + if self.con then + self.con:close() + end + + if self.env then + self.env:close() + end end - if self.env then - self.env:close() - end + return mydb end return db diff --git a/rhnop-client/nopcollectd b/rhnop-client/nopcollectd index 0944323..ef3dcd4 100755 --- a/rhnop-client/nopcollectd +++ b/rhnop-client/nopcollectd @@ -26,6 +26,7 @@ -- require "socket" +db = require "db" if #arg < 1 then io.stderr:write("too few parameters\n") @@ -35,14 +36,11 @@ end conffile = "nopcollectd." .. arg[1] .. ".conf" conf = require "conf" -src_db = require "db" -assert(src_db:init(conf.src_db, conf.src_user, conf.src_pwd, conf.src_host, conf.src_port, conf.src.table)) - -dst_db = require "db" -assert(dst_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(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)) local sock = assert(socket.tcp()) -local ret, err = sock:connect(conf.host, conf.port) +local ret, err = sock:connect(conf.sync_host, conf.sync_port) if ret == nil then print(err) return 1 @@ -50,13 +48,14 @@ end print "connection established" local last = dst_db:getLastEntry() -cur = assert(src_db:findMissingEntries(last) +print("last timestamp was " .. last) +local cur = assert(src_db:findMissingEntries(last)) local cnt = 0 while true do - local data = srd_db:getNextMissingEntry(cur) + local data = src_db:getNextMissingEntry(cur) if data == nil then break end local ret, err = dst_db:addEntry(data) - if ret == nil do print(err) end + if ret == nil then print(err) end cnt = cnt + 1 end @@ -66,7 +65,7 @@ while true do local timestamp = sock:receive('*l') if timestamp == nil then break end data = assert(src_db:getEntry(timestamp)) - dst_db:addEntry(data) + assert(dst_db:addEntry(data)) end sock:close() diff --git a/rhnop-client/nopcollectd.master.conf b/rhnop-client/nopcollectd.master.conf index 8c353c1..5d61551 100644 --- a/rhnop-client/nopcollectd.master.conf +++ b/rhnop-client/nopcollectd.master.conf @@ -1,16 +1,16 @@ sync_host=airplay.helsinki.at sync_port=2345 -src_db=nop +src_db=rhnop src_table=now src_host=airplay.helsinki.at #src_port=3306 src_user=nopcollectd -src_pwd= +src_pwd=123456 dst_db=nop dst_table=master dst_host=127.0.0.1 #dst_port=3306 dst_user=nopcollectd -dst_pwd= +dst_pwd=123456 diff --git a/rhnop-client/nopcollectd.standby.conf b/rhnop-client/nopcollectd.standby.conf index 10e44fb..92ba1aa 100644 --- a/rhnop-client/nopcollectd.standby.conf +++ b/rhnop-client/nopcollectd.standby.conf @@ -1,16 +1,16 @@ sync_host=airplay2.helsinki.at sync_port=2345 -src_db=nop +src_db=rhnop src_table=now src_host=airplay2.helsinki.at #src_port=3306 src_user=nopcollectd -src_pwd= +src_pwd=123456 dst_db=nop dst_table=standby dst_host=127.0.0.1 #dst_port=3306 dst_user=nopcollectd -dst_pwd= +dst_pwd=123456 |