blob: d73327a853d724a32f904fc8637866be1a4c9fbb [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")
21
22args = parser.parse_args()
23
24
25def 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 Williams13490142021-12-13 17:37:53 -060037 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))
SpencerKu5e89efd2021-08-09 18:27:34 +080043 pass
44
45 return data
46
Patrick Williamsddfc6752022-12-04 14:44:08 -060047 except Exception:
SpencerKu5e89efd2021-08-09 18:27:34 +080048 traceback.print_exc()
49 pass
50
51
52def 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
61def main():
62 logging.captureWarnings(True)
63 totalEntryUri = (
Patrick Williamsddfc6752022-12-04 14:44:08 -060064 f"https://{args.host}/redfish/v1/Systems/system/"
65 + "LogServices/HostLogger/Entries/"
SpencerKu5e89efd2021-08-09 18:27:34 +080066 )
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 Williams13490142021-12-13 17:37:53 -060074 # entryCount equals to ID means there has no change during the
75 # interval, sleep 5 seconds for next search.
SpencerKu5e89efd2021-08-09 18:27:34 +080076 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 Williams13490142021-12-13 17:37:53 -060085 # need to present all special characters, so use "repr"
86 # function
87 print(repr(message))
SpencerKu5e89efd2021-08-09 18:27:34 +080088 id = entryCount
89
90
91main()