xref: /openbmc/bmcweb/scripts/hostlogger_test.py (revision 3cb42ba0)
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
6import argparse
7import json
8import logging
9import time
10import traceback
11
12import requests
13
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)
17parser.add_argument(
18    "--username", help="Username to connect with", default="root"
19)
20parser.add_argument("--password", help="Password to use", default="0penBmc")
21parser.add_argument("--verify", default=True, help="Verify TLS certificates")
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=args.verify,
32            headers={"Cache-Control": "no-cache"},
33            timeout=5,
34        )
35        data = resp.json()
36
37        if resp.status_code != requests.codes.ok:
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))
44            pass
45
46        return data
47
48    except Exception:
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 = (
65        f"https://{args.host}/redfish/v1/Systems/system/"
66        + "LogServices/HostLogger/Entries/"
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:
75            # entryCount equals to ID means there has no change during the
76            # interval, sleep 5 seconds for next search.
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")
86                # need to present all special characters, so use "repr"
87                # function
88                print(repr(message))
89            id = entryCount
90
91
92main()
93