SpencerKu | 5e89efd | 2021-08-09 18:27:34 +0800 | [diff] [blame^] | 1 | #!/usr/bin/env python3 |
| 2 | |
| 3 | # Simple script to expose host serial console logs |
| 4 | # Search and get the log via redfish in every 5 seconds |
| 5 | |
| 6 | import argparse |
| 7 | import json |
| 8 | import logging |
| 9 | import os.path |
| 10 | import requests |
| 11 | import traceback |
| 12 | import time |
| 13 | from requests.auth import HTTPBasicAuth |
| 14 | |
| 15 | parser = argparse.ArgumentParser() |
| 16 | parser.add_argument("--host", help="Host to connect to", required=True) |
| 17 | parser.add_argument("--cert", help="File path to cert", required=True) |
| 18 | parser.add_argument("--username", help="Username to connect with", default="root") |
| 19 | parser.add_argument("--password", help="Password to use", default="0penBmc") |
| 20 | |
| 21 | args = parser.parse_args() |
| 22 | |
| 23 | |
| 24 | def requests_get(url): |
| 25 | try: |
| 26 | resp = requests.get( |
| 27 | url=url, |
| 28 | cert=args.cert, |
| 29 | verify=False, |
| 30 | headers={"Cache-Control": "no-cache"}, |
| 31 | timeout=5, |
| 32 | ) |
| 33 | data = resp.json() |
| 34 | |
| 35 | if resp.status_code != requests.codes.ok: |
| 36 | print "There occurs error when get request, status_code = " + str( |
| 37 | resp.status_code |
| 38 | ) + "\n" |
| 39 | print json.dumps(data, indent=4, sort_keys=True) |
| 40 | pass |
| 41 | |
| 42 | return data |
| 43 | |
| 44 | except Exception as err: |
| 45 | traceback.print_exc() |
| 46 | pass |
| 47 | |
| 48 | |
| 49 | def label_parser(url, label): |
| 50 | data = requests_get(url) |
| 51 | for key in sorted(data.keys()): |
| 52 | if key == label: |
| 53 | content = data[key] |
| 54 | break |
| 55 | return content |
| 56 | |
| 57 | |
| 58 | def main(): |
| 59 | logging.captureWarnings(True) |
| 60 | totalEntryUri = ( |
| 61 | "https://{}/redfish/v1/Systems/system/LogServices/HostLogger/Entries/".format( |
| 62 | args.host |
| 63 | ) |
| 64 | ) |
| 65 | id = 0 |
| 66 | entryCount = 0 |
| 67 | message = "" |
| 68 | |
| 69 | while 1: |
| 70 | entryCount = label_parser(totalEntryUri, "Members@odata.count") |
| 71 | if id == entryCount: |
| 72 | # entryCount equals to ID means there has no change during the interval, |
| 73 | # sleep 5 seconds for next search. |
| 74 | time.sleep(5) |
| 75 | elif id < entryCount: |
| 76 | # print new entries which created in this interval. |
| 77 | for i in range(id + 1, entryCount): |
| 78 | singleEntryUri = ( |
| 79 | "https://{}/redfish/v1/Systems/system/LogServices/" |
| 80 | "HostLogger/Entries/{}".format(args.host, i) |
| 81 | ) |
| 82 | message = label_parser(singleEntryUri, "Message") |
| 83 | # need to present all special characters, so use "repr" function |
| 84 | print (repr(message)) |
| 85 | id = entryCount |
| 86 | |
| 87 | |
| 88 | main() |