Create a dedicated host monitoring program
authorNikos Skalkotos <skalkoto@grnet.gr>
Mon, 23 Jul 2012 13:22:25 +0000 (16:22 +0300)
committerNikos Skalkotos <skalkoto@grnet.gr>
Mon, 23 Jul 2012 13:22:25 +0000 (16:22 +0300)
This program is used to output info and error messages from the host.

snf-image-host/Makefile.am
snf-image-host/common.sh.in
snf-image-host/host-monitor.py [moved from snf-image-host/stderr-monitor.py with 56% similarity]

index 0bc5d77..f474bb3 100644 (file)
@@ -12,7 +12,7 @@ variantsdir=${sysconfdir}/ganeti/snf-image/variants
 dist_os_SCRIPTS = ${srcdir}/create ${srcdir}/import ${srcdir}/export \
        ${srcdir}/rename ${srcdir}/verify ${srcdir}/pithcat \
        ${srcdir}/copy-monitor.py ${srcdir}/helper-monitor.py \
-       ${srcdir}/stderr-monitor.py
+       ${srcdir}/host-monitor.py
 
 dist_os_DATA = ${srcdir}/ganeti_api_version ${srcdir}/parameters.list \
                ${srcdir}/variants.list
index 293c4a0..653f1e7 100644 (file)
@@ -52,12 +52,7 @@ log_error() {
 log_info() {
     echo "$*" >&2
 
-    local type="$MSG_TYPE_INFO"
-    local msg="[\"$(sed 's/"/\\"/g' <<< "$@")\"]"
-
-    report="{\"type\":\"$type\","
-    report+="\"timestamp\":$(date +%s.%N),"
-    report+="\"messages\":$msg}"
+    local report="$(./host-monitor.py info <<< "$*")"
 
     eval "echo $(printf "%q" "$report") >&${MONITOR_FD}"
 }
@@ -70,24 +65,15 @@ close_fd() {
 
 report_error() {
     local error_file=$1
-
-    local type="$MSG_TYPE_ERROR"
-
+    local report=""
     if [ ${#ERROR_MSGS[@]} -gt 0 ]; then
-        report="{\"type\":\"$type\","
-        report+="\"timestamp\":$(date +%s.%N),"
-        local msg="["
+        local msg=""
         for err in "${ERROR_MSGS[@]}"; do
-            msg+="\"$(sed 's/"/\\"/g' <<< "$err")\","
+            msg+="$(echo "$err")"
         done
-        if [ ${#msg} -gt 1 ]; then
-            # remove last comma (,)
-            msg="${msg%?}"
-        fi
-        msg+="]"
-        report+="\"messages\":$msg}"
+        report="$(./host-monitor.py error <<< "$msg")"
     else
-        report=$(tail -10 "$error_file" | ./stderr-monitor.py "$type")
+        report=$(tail -10 "$error_file" | ./host-monitor.py stderr)
     fi
 
     eval "echo $(printf "%q" "$report") >&${MONITOR_FD}"
similarity index 56%
rename from snf-image-host/stderr-monitor.py
rename to snf-image-host/host-monitor.py
index 2144a99..903b5f8 100755 (executable)
@@ -1,6 +1,6 @@
 #!/usr/bin/env python
 
-# Copyright (C) 2012 GRNET S.A. 
+# Copyright (C) 2012 GRNET S.A.
 #
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 # 02110-1301, USA.
 
+"""Utility that generates monitoring messages for snf-image.
+
+This utility given a message type as option and the message body as input will
+print a monitoring message in stdout.
+"""
+
 import sys
 import os
 import json
 import time
 
+MSG_TYPE_ERROR = "image-error"
+MSG_TYPE_INFO = "image-info"
+
+PROTOCOL = {
+    "error": (MSG_TYPE_ERROR, "messages"),
+    "stderr": (MSG_TYPE_ERROR, "stderr"),
+    "info": (MSG_TYPE_INFO, "messages")
+}
+
 PROGNAME = os.path.basename(sys.argv[0])
 
 if __name__ == "__main__":
-    usage="Usage: %s <msg-type>\n" % PROGNAME
-    
+    usage = "Usage: %s <msg-type>\n" % PROGNAME
+
     if len(sys.argv) != 2:
         sys.stderr.write(usage)
         sys.exit(1)
 
+    msg_type = sys.argv[1]
+
+    if msg_type  not in PROTOCOL.keys():
+        sys.stderr.write("Unknown message type: %s\n" % msg_type)
+        sys.exit(1)
+
     msg = {}
-    msg['type'] =  sys.argv[1]
-    msg['stderr'] = sys.stdin.read()
-    msg['timestamp'] = time.time()
+    msg['type'] = PROTOCOL[msg_type][0]
+
+    lines = []
+    while True:
+        line = sys.stdin.readline()
 
+        if not line:
+            break
+
+        lines.append(line.strip())
+
+    msg[PROTOCOL[msg_type][1]] = lines
+    msg['timestamp'] = time.time()
     sys.stdout.write("%s\n" % json.dumps(msg))
 
     sys.exit(0)