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