Redfish: Script to Expose Host Log Entries

Simple script to expose host serial console logs,
Search and get the log via redfish in every 5 seconds

option:
--host : Host to connect to
--cert : File path to cert
--username : Username to connect with
--password : Password to use

Signed-off-by: SpencerKu <Spencer.Ku@quantatw.com>
Change-Id: Ia95735e2f03a4361ee876774b5906df18ab013b8
diff --git a/scripts/hostlogger_test.py b/scripts/hostlogger_test.py
new file mode 100755
index 0000000..6080b34
--- /dev/null
+++ b/scripts/hostlogger_test.py
@@ -0,0 +1,88 @@
+#!/usr/bin/env python3
+
+# Simple script to expose host serial console logs
+# Search and get the log via redfish in every 5 seconds
+
+import argparse
+import json
+import logging
+import os.path
+import requests
+import traceback
+import time
+from requests.auth import HTTPBasicAuth
+
+parser = argparse.ArgumentParser()
+parser.add_argument("--host", help="Host to connect to", required=True)
+parser.add_argument("--cert", help="File path to cert", required=True)
+parser.add_argument("--username", help="Username to connect with", default="root")
+parser.add_argument("--password", help="Password to use", default="0penBmc")
+
+args = parser.parse_args()
+
+
+def requests_get(url):
+    try:
+        resp = requests.get(
+            url=url,
+            cert=args.cert,
+            verify=False,
+            headers={"Cache-Control": "no-cache"},
+            timeout=5,
+        )
+        data = resp.json()
+
+        if resp.status_code != requests.codes.ok:
+            print "There occurs error when get request, status_code = " + str(
+                resp.status_code
+            ) + "\n"
+            print json.dumps(data, indent=4, sort_keys=True)
+            pass
+
+        return data
+
+    except Exception as err:
+        traceback.print_exc()
+        pass
+
+
+def label_parser(url, label):
+    data = requests_get(url)
+    for key in sorted(data.keys()):
+        if key == label:
+            content = data[key]
+            break
+    return content
+
+
+def main():
+    logging.captureWarnings(True)
+    totalEntryUri = (
+        "https://{}/redfish/v1/Systems/system/LogServices/HostLogger/Entries/".format(
+            args.host
+        )
+    )
+    id = 0
+    entryCount = 0
+    message = ""
+
+    while 1:
+        entryCount = label_parser(totalEntryUri, "Members@odata.count")
+        if id == entryCount:
+            # entryCount equals to ID means there has no change during the interval,
+            # sleep 5 seconds for next search.
+            time.sleep(5)
+        elif id < entryCount:
+            # print new entries which created in this interval.
+            for i in range(id + 1, entryCount):
+                singleEntryUri = (
+                    "https://{}/redfish/v1/Systems/system/LogServices/"
+                    "HostLogger/Entries/{}".format(args.host, i)
+                )
+                message = label_parser(singleEntryUri, "Message")
+                # need to present all special characters, so use "repr" function
+                print (repr(message))
+            id = entryCount
+
+
+main()