xref: /openbmc/bmcweb/scripts/hostlogger_test.py (revision 1b8b02a4)
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")
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:
37            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))
43            pass
44
45        return data
46
47    except Exception:
48        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 = (
64        f"https://{args.host}/redfish/v1/Systems/system/"
65        + "LogServices/HostLogger/Entries/"
66    )
67    id = 0
68    entryCount = 0
69    message = ""
70
71    while 1:
72        entryCount = label_parser(totalEntryUri, "Members@odata.count")
73        if id == entryCount:
74            # entryCount equals to ID means there has no change during the
75            # interval, sleep 5 seconds for next search.
76            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")
85                # need to present all special characters, so use "repr"
86                # function
87                print(repr(message))
88            id = entryCount
89
90
91main()
92