xref: /openbmc/bmcweb/scripts/hostlogger_test.py (revision 5e89efd9)
1*5e89efd9SSpencerKu#!/usr/bin/env python3
2*5e89efd9SSpencerKu
3*5e89efd9SSpencerKu# Simple script to expose host serial console logs
4*5e89efd9SSpencerKu# Search and get the log via redfish in every 5 seconds
5*5e89efd9SSpencerKu
6*5e89efd9SSpencerKuimport argparse
7*5e89efd9SSpencerKuimport json
8*5e89efd9SSpencerKuimport logging
9*5e89efd9SSpencerKuimport os.path
10*5e89efd9SSpencerKuimport requests
11*5e89efd9SSpencerKuimport traceback
12*5e89efd9SSpencerKuimport time
13*5e89efd9SSpencerKufrom requests.auth import HTTPBasicAuth
14*5e89efd9SSpencerKu
15*5e89efd9SSpencerKuparser = argparse.ArgumentParser()
16*5e89efd9SSpencerKuparser.add_argument("--host", help="Host to connect to", required=True)
17*5e89efd9SSpencerKuparser.add_argument("--cert", help="File path to cert", required=True)
18*5e89efd9SSpencerKuparser.add_argument("--username", help="Username to connect with", default="root")
19*5e89efd9SSpencerKuparser.add_argument("--password", help="Password to use", default="0penBmc")
20*5e89efd9SSpencerKu
21*5e89efd9SSpencerKuargs = parser.parse_args()
22*5e89efd9SSpencerKu
23*5e89efd9SSpencerKu
24*5e89efd9SSpencerKudef requests_get(url):
25*5e89efd9SSpencerKu    try:
26*5e89efd9SSpencerKu        resp = requests.get(
27*5e89efd9SSpencerKu            url=url,
28*5e89efd9SSpencerKu            cert=args.cert,
29*5e89efd9SSpencerKu            verify=False,
30*5e89efd9SSpencerKu            headers={"Cache-Control": "no-cache"},
31*5e89efd9SSpencerKu            timeout=5,
32*5e89efd9SSpencerKu        )
33*5e89efd9SSpencerKu        data = resp.json()
34*5e89efd9SSpencerKu
35*5e89efd9SSpencerKu        if resp.status_code != requests.codes.ok:
36*5e89efd9SSpencerKu            print "There occurs error when get request, status_code = " + str(
37*5e89efd9SSpencerKu                resp.status_code
38*5e89efd9SSpencerKu            ) + "\n"
39*5e89efd9SSpencerKu            print json.dumps(data, indent=4, sort_keys=True)
40*5e89efd9SSpencerKu            pass
41*5e89efd9SSpencerKu
42*5e89efd9SSpencerKu        return data
43*5e89efd9SSpencerKu
44*5e89efd9SSpencerKu    except Exception as err:
45*5e89efd9SSpencerKu        traceback.print_exc()
46*5e89efd9SSpencerKu        pass
47*5e89efd9SSpencerKu
48*5e89efd9SSpencerKu
49*5e89efd9SSpencerKudef label_parser(url, label):
50*5e89efd9SSpencerKu    data = requests_get(url)
51*5e89efd9SSpencerKu    for key in sorted(data.keys()):
52*5e89efd9SSpencerKu        if key == label:
53*5e89efd9SSpencerKu            content = data[key]
54*5e89efd9SSpencerKu            break
55*5e89efd9SSpencerKu    return content
56*5e89efd9SSpencerKu
57*5e89efd9SSpencerKu
58*5e89efd9SSpencerKudef main():
59*5e89efd9SSpencerKu    logging.captureWarnings(True)
60*5e89efd9SSpencerKu    totalEntryUri = (
61*5e89efd9SSpencerKu        "https://{}/redfish/v1/Systems/system/LogServices/HostLogger/Entries/".format(
62*5e89efd9SSpencerKu            args.host
63*5e89efd9SSpencerKu        )
64*5e89efd9SSpencerKu    )
65*5e89efd9SSpencerKu    id = 0
66*5e89efd9SSpencerKu    entryCount = 0
67*5e89efd9SSpencerKu    message = ""
68*5e89efd9SSpencerKu
69*5e89efd9SSpencerKu    while 1:
70*5e89efd9SSpencerKu        entryCount = label_parser(totalEntryUri, "Members@odata.count")
71*5e89efd9SSpencerKu        if id == entryCount:
72*5e89efd9SSpencerKu            # entryCount equals to ID means there has no change during the interval,
73*5e89efd9SSpencerKu            # sleep 5 seconds for next search.
74*5e89efd9SSpencerKu            time.sleep(5)
75*5e89efd9SSpencerKu        elif id < entryCount:
76*5e89efd9SSpencerKu            # print new entries which created in this interval.
77*5e89efd9SSpencerKu            for i in range(id + 1, entryCount):
78*5e89efd9SSpencerKu                singleEntryUri = (
79*5e89efd9SSpencerKu                    "https://{}/redfish/v1/Systems/system/LogServices/"
80*5e89efd9SSpencerKu                    "HostLogger/Entries/{}".format(args.host, i)
81*5e89efd9SSpencerKu                )
82*5e89efd9SSpencerKu                message = label_parser(singleEntryUri, "Message")
83*5e89efd9SSpencerKu                # need to present all special characters, so use "repr" function
84*5e89efd9SSpencerKu                print (repr(message))
85*5e89efd9SSpencerKu            id = entryCount
86*5e89efd9SSpencerKu
87*5e89efd9SSpencerKu
88*5e89efd9SSpencerKumain()
89