[omd-commits] OMD Git: omd: omd update: Added update log ~/var/log/update.log

git version control git at mathias-kettner.de
Mon Apr 15 16:25:05 CEST 2013


Module:   omd
Branch:   master
Commit:   7fa34fc8b8d46e9be6c87faa96a943226e9b6084
URL:      http://omdistro.org/projects/omd/repository/revisions/7fa34fc8b8d46e9be6c87faa96a943226e9b6084

Author:   Lars Michelsen <lm at mathias-kettner.de>
Date:     Mon Apr 15 16:25:02 2013 +0200
Commiter: Lars Michelsen <lm at mathias-kettner.de>
Date:     Mon Apr 15 16:25:02 2013 +0200

omd update: Added update log ~/var/log/update.log

---

 Changelog                                |    1 +
 packages/omd/omd                         |   53 ++++++++++++++++++++++++++++--
 packages/omd/skel/etc/logrotate.d/update |    7 ++++
 3 files changed, 59 insertions(+), 2 deletions(-)

diff --git a/Changelog b/Changelog
index c8f7743..cd1f0bb 100644
--- a/Changelog
+++ b/Changelog
@@ -15,6 +15,7 @@ This file documents the revision history for the Open Monitoring Distribution
             - new option --no-autostart, -A for omd create (set AUTOSTART
               to off right at creation, useful for test sites!)
             - update: Ensure execution of the target versions hooks
+            - update: Added update log ~/var/log/update.log
           - PNP4Nagios: Updated to version 0.6.21
              - Added INIT hooks to npcd and pnp_gearman_worker scripts
           - Support Ubuntu 12.10 (Quantal)
diff --git a/packages/omd/omd b/packages/omd/omd
index 1b54ca2..2df19b7 100644
--- a/packages/omd/omd
+++ b/packages/omd/omd
@@ -90,6 +90,50 @@ opt_force = False
 
 g_hooks = None
 
+# Is used to duplicate output from stdout/stderr to a logfiles. This
+# is e.g. used during "omd update" to have a chance to analyze errors
+# during past updates
+class Log(object):
+    def __init__(self, fd, logfile):
+        self.log = open(logfile, 'a')
+        self.fd  = fd
+
+        if self.fd == 1:
+            self.orig  = sys.stdout
+            sys.stdout = self
+        else:
+            self.orig  = sys.stderr
+            sys.stderr = self
+
+        self.color_replace = re.compile("\033\[\d{1,2}m", re.UNICODE)
+
+    def __del__(self):
+        if self.fd == 1:
+            sys.stdout = self.orig
+        else:
+            sys.stderr = self.orig
+        self.log.close()
+
+    def write(self, data):
+        self.orig.write(data)
+        self.log.write(self.color_replace.sub('', data))
+
+    def flush(self):
+        self.log.flush()
+        self.orig.flush()
+
+g_stdout_log = None
+g_stderr_log = None
+def start_logging(logfile):
+    global g_stdout_log, g_stderr_log
+    g_stdout_log = Log(1, logfile)
+    g_stderr_log = Log(2, logfile)
+
+def stop_logging():
+    global g_stdout_log, g_stderr_log
+    g_stdout_log = None
+    g_stderr_log = None
+
 def show_success(exit_code):
     if exit_code == True or exit_code == 0:
         ok()
@@ -2666,8 +2710,10 @@ def main_update(args, options={}):
             "Update!", "Abort"):
         bail_out("Aborted.")
 
-    sys.stdout.write("Updating site '%s' from version %s to %s...\n\n" %
-            (g_sitename, from_version, to_version))
+    start_logging(g_sitedir + '/var/log/update.log')
+
+    sys.stdout.write("%s - Updating site '%s' from version %s to %s...\n\n" %
+            (time.strftime('%Y-%m-%d %H:%M:%S'), g_sitename, from_version, to_version))
 
     # Now apply changes of skeleton files. This can be done
     # in two ways:
@@ -2699,6 +2745,9 @@ def main_update(args, options={}):
     # the hooks of the target version are executed
     refresh_hooks(True)
 
+    sys.stdout.write('Finished update.\n\n')
+    stop_logging()
+
 def main_umount(args, options = {}):
     global g_sitename
     global g_sitedir
diff --git a/packages/omd/skel/etc/logrotate.d/update b/packages/omd/skel/etc/logrotate.d/update
new file mode 100644
index 0000000..f761877
--- /dev/null
+++ b/packages/omd/skel/etc/logrotate.d/update
@@ -0,0 +1,7 @@
+###ROOT###/var/log/update.log {
+        missingok
+        rotate 7
+        compress
+        delaycompress
+        create 640 ###SITE### ###SITE### 
+}



More information about the omd-commits mailing list