diff options
Diffstat (limited to 'src/nopsysstated')
-rwxr-xr-x | src/nopsysstated | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/src/nopsysstated b/src/nopsysstated new file mode 100755 index 0000000..f1a05f8 --- /dev/null +++ b/src/nopsysstated @@ -0,0 +1,71 @@ +#!/usr/bin/lua +-- +-- rhnop +-- +-- Copyright (C) 2011-2015 Christian Pointner <equinox@helsinki.at> +-- +-- 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 <http://www.gnu.org/licenses/>. +-- +-- +-- 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 |