blob: 6661ca7c756dd8e5498235fc84c5ef0a73a2ef45 [file] [log] [blame]
SpencerKu5e89efd2021-08-09 18:27:34 +08001#!/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
6import argparse
7import json
8import logging
SpencerKu5e89efd2021-08-09 18:27:34 +08009import time
Patrick Williamsddfc6752022-12-04 14:44:08 -060010import traceback
11
12import requests
SpencerKu5e89efd2021-08-09 18:27:34 +080013
14parser = argparse.ArgumentParser()
15parser.add_argument("--host", help="Host to connect to", required=True)
16parser.add_argument("--cert", help="File path to cert", required=True)
Patrick Williams13490142021-12-13 17:37:53 -060017parser.add_argument(
18 "--username", help="Username to connect with", default="root"
19)
SpencerKu5e89efd2021-08-09 18:27:34 +080020parser.add_argument("--password", help="Password to use", default="0penBmc")
Ed Tanousbaddb592024-05-15 19:10:15 -070021parser.add_argument("--verify", default=True, help="Verify TLS certificates")
SpencerKu5e89efd2021-08-09 18:27:34 +080022
23args = parser.parse_args()
24
25
26def requests_get(url):
27 try:
28 resp = requests.get(
29 url=url,
30 cert=args.cert,
Ed Tanousbaddb592024-05-15 19:10:15 -070031 verify=args.verify,
SpencerKu5e89efd2021-08-09 18:27:34 +080032 headers={"Cache-Control": "no-cache"},
33 timeout=5,
34 )
35 data = resp.json()
36
37 if resp.status_code != requests.codes.ok:
Patrick Williams13490142021-12-13 17:37:53 -060038 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))
SpencerKu5e89efd2021-08-09 18:27:34 +080044 pass
45
46 return data
47
Patrick Williamsddfc6752022-12-04 14:44:08 -060048 except Exception:
SpencerKu5e89efd2021-08-09 18:27:34 +080049 traceback.print_exc()
50 pass
51
52
53def 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
62def main():
63 logging.captureWarnings(True)
64 totalEntryUri = (
Patrick Williamsddfc6752022-12-04 14:44:08 -060065 f"https://{args.host}/redfish/v1/Systems/system/"
66 + "LogServices/HostLogger/Entries/"
SpencerKu5e89efd2021-08-09 18:27:34 +080067 )
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 Williams13490142021-12-13 17:37:53 -060075 # entryCount equals to ID means there has no change during the
76 # interval, sleep 5 seconds for next search.
SpencerKu5e89efd2021-08-09 18:27:34 +080077 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 Williams13490142021-12-13 17:37:53 -060086 # need to present all special characters, so use "repr"
87 # function
88 print(repr(message))
SpencerKu5e89efd2021-08-09 18:27:34 +080089 id = entryCount
90
91
92main()