Module: check_mk
Branch: master
Commit: d48a59853b31a3f5475447624f9b1fe7d8952875
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=d48a59853b31a3…
Author: Mathias Kettner <mk(a)mathias-kettner.de>
Date: Fri Oct 31 14:01:58 2014 +0100
Inventory sync now skips up-to-date files
---
doc/treasures/liveproxy/liveproxyd | 42 ++++++++++++++++++++++--------------
1 file changed, 26 insertions(+), 16 deletions(-)
diff --git a/doc/treasures/liveproxy/liveproxyd b/doc/treasures/liveproxy/liveproxyd
index 1581758..4f6d6f4 100755
--- a/doc/treasures/liveproxy/liveproxyd
+++ b/doc/treasures/liveproxy/liveproxyd
@@ -701,7 +701,7 @@ def dump_state():
#.
-# .--Inventory-----------------------------------------------------------.
+# .--HW/SW-Inventory-----------------------------------------------------.
# | ___ _ |
# | |_ _|_ ____ _____ _ __ | |_ ___ _ __ _ _ |
# | | || '_ \ \ / / _ \ '_ \| __/ _ \| '__| | | | |
@@ -723,8 +723,8 @@ def check_inventory_update(mk_inventory_last, sitename):
if mk_inventory_last <= sitestatus["last_inventory_update"]:
return # we are up-to-date
- log("Site %s: new HW/SW-Inventory data since last update %d secs ago" % (
- sitename, mk_inventory_last - sitestatus["last_inventory_update"]))
+ # log("Site %s: new HW/SW-Inventory data since last update %d secs ago" %
(
+ # sitename, mk_inventory_last - sitestatus["last_inventory_update"]))
# Inventory update goes in two steps:
# 1. Getting the list of all hosts that have new inventory
@@ -751,11 +751,10 @@ def check_inventory_update(mk_inventory_last, sitename):
if pid:
sitestatus["inventory_pid"] = pid
sitestatus["inventory_new_last"] = mk_inventory_last
- log("Created inventory sync helper with PID %d" % pid)
else:
try:
- do_inventory_update(channel, sitestatus["last_inventory_update"])
+ do_inventory_update(sitename, channel,
sitestatus["last_inventory_update"])
sys.exit(0)
except Exception, e:
log("Inventory update of %s failed: %s" % (sitename, e))
@@ -767,7 +766,6 @@ def collect_inventory_updates():
if sitestatus["inventory_pid"]:
pid, status = os.waitpid(sitestatus["inventory_pid"], os.WNOHANG)
if pid:
- log("Inventory update of %s finished %s" % (sitename, status))
sitestatus["inventory_pid"] = None
for channel in sitestatus["channels"]:
if channel["state"] == "inventory":
@@ -777,7 +775,7 @@ def collect_inventory_updates():
del sitestatus["inventory_new_last"]
-def do_inventory_update(channel, since):
+def do_inventory_update(sitename, channel, since):
# Close all filedescriptors that we do not need.
needed_fds = [
channel["socket"].fileno(),
@@ -793,7 +791,7 @@ def do_inventory_update(channel, since):
# Get list of all hosts that have more recent inventory data
query = "GET hosts\n" \
- "Columns: name\n" \
+ "Columns: name mk_inventory_last\n" \
"Filter: mk_inventory_last > %d\n" \
"KeepAlive: on\n" \
"ResponseHeader: fixed16\n\n" % \
@@ -802,10 +800,10 @@ def do_inventory_update(channel, since):
channel["socket"].send(query)
response = get_livestatus_response(channel["socket"])
- host_names = response.split()
+ entries = [ l.split(";") for l in response.split() ]
- for host_name in host_names:
- do_inventory_update_of(channel["socket"], host_name)
+ for host_name, timestamp in entries:
+ do_inventory_update_of(channel["socket"], sitename, host_name,
int(timestamp))
def get_livestatus_response(socket):
@@ -819,13 +817,23 @@ def get_livestatus_response(socket):
while(len(response) != length):
response += socket.recv(4096)
- if status != "200":
- raise Exception("Livestatus query failed: %s" % response)
+ if status != "200": raise Exception("Livestatus query failed: %s"
% response)
return response
-def do_inventory_update_of(socket, host_name):
+# Update the inventory data of a host *if* its timestamp differs from
+# the file that we already have
+def do_inventory_update_of(socket, sitename, host_name, timestamp):
+ path = opt_inventory_dir + "/" + host_name
+ try:
+ last_mtime = int(os.stat(path).st_mtime)
+ if last_mtime == timestamp:
+ log("Inventory of %s is uptodate" % host_name)
+ return
+ except:
+ pass
+
query = "GET hosts\n" \
"Columns: mk_inventory_gz\n" \
"Filter: name = %s\n" \
@@ -835,14 +843,16 @@ def do_inventory_update_of(socket, host_name):
socket.send(query)
response = get_livestatus_response(socket)
gzipped_invdata = response[:-1] # drop final linefeed
- path = opt_inventory_dir + "/" + host_name
file(path + ".gz", "w").write(gzipped_invdata)
+ os.utime(path + ".gz", (timestamp, timestamp))
unzipped_invdata = gzip.GzipFile(fileobj=StringIO.StringIO(gzipped_invdata)).read()
file(path, "w").write(unzipped_invdata)
+ os.utime(path, (timestamp, timestamp))
+ log("Site %s: new HW/SW-Inventory data for %s (%d bytes)" % (
+ sitename, host_name, len(gzipped_invdata)))
#.
-
# .--Daemon/main---------------------------------------------------------.
# | ____ __ _ |
# || _ \ __ _ ___ _ __ ___ ___ _ __ / / __ ___ __ _(_)_ __ |