blob: 415c584dc1069529046036338d61d1b92381d43a [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
9import os.path
10import requests
11import traceback
12import time
13from requests.auth import HTTPBasicAuth
14
15parser = argparse.ArgumentParser()
16parser.add_argument("--host", help="Host to connect to", required=True)
17parser.add_argument("--cert", help="File path to cert", required=True)
Patrick Williams13490142021-12-13 17:37:53 -060018parser.add_argument(
19 "--username", help="Username to connect with", default="root"
20)
SpencerKu5e89efd2021-08-09 18:27:34 +080021parser.add_argument("--password", help="Password to use", default="0penBmc")
22
23args = parser.parse_args()
24
25
26def 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 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
48 except Exception as err:
49 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 Williams13490142021-12-13 17:37:53 -060065 "https://{}/redfish/v1/Systems/system/"
66 + "LogServices/HostLogger/Entries/".format(args.host)
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()