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