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") |
Ed Tanous | baddb59 | 2024-05-15 19:10:15 -0700 | [diff] [blame] | 21 | parser.add_argument("--verify", default=True, help="Verify TLS certificates") |
SpencerKu | 5e89efd | 2021-08-09 18:27:34 +0800 | [diff] [blame] | 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, |
Ed Tanous | baddb59 | 2024-05-15 19:10:15 -0700 | [diff] [blame] | 31 | verify=args.verify, |
SpencerKu | 5e89efd | 2021-08-09 18:27:34 +0800 | [diff] [blame] | 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 | |
Patrick Williams | ddfc675 | 2022-12-04 14:44:08 -0600 | [diff] [blame] | 48 | except Exception: |
SpencerKu | 5e89efd | 2021-08-09 18:27:34 +0800 | [diff] [blame] | 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 | ddfc675 | 2022-12-04 14:44:08 -0600 | [diff] [blame] | 65 | f"https://{args.host}/redfish/v1/Systems/system/" |
| 66 | + "LogServices/HostLogger/Entries/" |
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() |