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