From 99c07fae615a1f44a2eb24eb973700931017b248 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Tue, 5 Apr 2011 13:36:13 +0000 Subject: added nopsysstated diff --git a/rhnop-client/db.lua b/rhnop-client/db.lua index 5a6a92c..17e91f1 100644 --- a/rhnop-client/db.lua +++ b/rhnop-client/db.lua @@ -28,6 +28,8 @@ require "luasql.mysql" -- 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); +-- CREATE TABLE IF NOT EXISTS state (timestamp BIGINT UNSIGNED PRIMARY KEY NOT NULL, state VARCHAR(32)); +-- GRANT select,insert,update ON nop.state TO 'nopsysstated' IDENTIFIED BY ''; local db = {} @@ -111,6 +113,28 @@ function db.init(db, user, pwd, host, port, table) 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 + end + + function mydb:updateSysState(timestamp, state) + local t = self.con:escape(timestamp) + local s = self.con:escape(state) + + local cur, err = self.con:execute("INSERT INTO " .. self.table .. " VALUES('" .. t .. "', '" .. s .. "')") + if cur == nil then + return nil, err + end + return true + end + function mydb:close() if self.con then self.con:close() diff --git a/rhnop-client/nopsysstated b/rhnop-client/nopsysstated new file mode 100755 index 0000000..a47a0ce --- /dev/null +++ b/rhnop-client/nopsysstated @@ -0,0 +1,71 @@ +#!/usr/bin/lua +-- +-- rhnop +-- +-- Copyright (C) 2011 Christian Pointner +-- +-- This file is part of rhnop. +-- +-- rhnop is free software: you can redistribute it and/or modify +-- it under the terms of the GNU General Public License as published by +-- the Free Software Foundation, either version 3 of the License, or +-- any later version. +-- +-- rhnop is distributed in the hope that it will be useful, +-- but WITHOUT ANY WARRANTY; without even the implied warranty of +-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +-- GNU General Public License for more details. +-- +-- You should have received a copy of the GNU General Public License +-- along with rhnop. If not, see . +-- +-- +-- send now and next cart# and length to nopsyncd +-- this script should be called by the now/next macro cart +-- the arguments should be: now# nowlen next# nextlen +-- + +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 sock = assert(socket.tcp()) +local ret, err = sock:connect(conf.host, conf.port) +if ret == nil then + print(err) + return 1 +end +print "connection established" + +local current_state, err = db:getLastSysState() +if current_state == nil then + print(err) + return 1 +end +print("current state according to db is " .. current_state) + +while true do + local state = sock:receive('*l') + if state == nil then break end + if state ~= current_state then + -- reading timestamp (milliseconds since epoch) + local p = assert(io.popen("/bin/date --utc '+%s %N'" , 'r')) + local time = assert(p:read('*l')) + p:close() + local s, ns = assert(string.match(time, "([0-9]+) ([0-9]+)")) + local timestamp = s .. string.format("%06d", math.floor(ns/1000)) + + assert(db:updateSysState(timestamp, state)) + current_state = state + end +end + +sock:close() + +return 0 diff --git a/rhnop-client/nopsysstated.conf b/rhnop-client/nopsysstated.conf new file mode 100644 index 0000000..b9892c8 --- /dev/null +++ b/rhnop-client/nopsysstated.conf @@ -0,0 +1,9 @@ +host=rhctl.helsinki.at +port=2345 + +db_db=nop +db_table=state +db_host=127.0.0.1 +#db_port=3306 +db_user=nopsysstated +db_pwd=123456 -- cgit v0.10.2