blob: e0493c6c969087899f927dc18a903e51ad85190c [file] [log] [blame]
Patrick Williamsb19c34c2017-03-27 14:52:28 -05001#!/bin/env python
2
Patrick Williams9b05ea32017-03-27 15:22:31 -05003import argparse
Patrick Williamsb19c34c2017-03-27 14:52:28 -05004import requests
Andrew Geisslerd35538c2017-08-25 08:43:08 -05005import json
Patrick Williamsb19c34c2017-03-27 14:52:28 -05006
Andrew Geissler39541fd2017-08-19 08:31:07 -05007import urllib3
8urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
9
Patrick Williams533c13a2017-03-27 15:28:51 -050010
Patrick Williamsb19c34c2017-03-27 14:52:28 -050011class BMC:
12 def __init__(self, server):
13 self.url = "https://{0}/".format(server)
14 self.session = requests.Session()
15 self.login()
16
17 def login(self):
18 r = self.session.post(self.url + 'login',
Patrick Williams533c13a2017-03-27 15:28:51 -050019 json={'data': ['root', '0penBmc']},
Patrick Williamsb19c34c2017-03-27 14:52:28 -050020 verify=False)
21 j = r.json()
22 if j['status'] != 'ok':
23 raise Exception("Failed to login: \n" + r.text)
24
Patrick Williamsec472d82017-03-27 15:15:43 -050025 def list_events(self):
26 r = self.session.get(self.url + 'xyz/openbmc_project/logging/entry/',
27 verify=False)
28 j = r.json()
29 if j['status'] != 'ok':
30 raise Exception("Failed to query entries: \n" + r.text)
31
32 events = j['data']
33 events.sort(key=lambda x: int(x.split("/")[-1]))
34
35 return events
Patrick Williamsb19c34c2017-03-27 14:52:28 -050036
Patrick Williams5e98b3a2017-03-27 15:28:35 -050037 def get_event(self, event):
38 r = self.session.get(self.url + event, verify=False)
39
40 j = r.json()
41 if j['status'] != 'ok':
42 raise Exception("Failed to get event " + event + ": \n" + r.text)
43
44 return j['data']
Andrew Geissler50a753c2017-08-19 09:00:16 -050045
46 def clear_event(self,event):
47 r = self.session.delete(self.url + event)
48 j = r.json()
49 if j['status'] != 'ok':
50 raise Exception("Failed to clear event " + event + ": \n" + r.text)
Patrick Williams5e98b3a2017-03-27 15:28:35 -050051
Patrick Williamsb19c34c2017-03-27 14:52:28 -050052
Patrick Williams9b05ea32017-03-27 15:22:31 -050053def do_list_events(args):
54 s = BMC(server=args.server)
55 for e in s.list_events():
56 print(e)
57
Patrick Williams5e98b3a2017-03-27 15:28:35 -050058
59def do_view_event(args):
60 s = BMC(server=args.server)
61 print(s.get_event(args.event))
Andrew Geisslerd35538c2017-08-25 08:43:08 -050062 print json.dumps(s.get_event(args.event), indent=4)
Patrick Williams5e98b3a2017-03-27 15:28:35 -050063
Andrew Geissler50a753c2017-08-19 09:00:16 -050064def do_clear_event(args):
65 s = BMC(server=args.server)
66 s.clear_event(args.event)
Patrick Williams4296f4b2017-03-27 16:26:41 -050067
Patrick Williams7a09b672017-03-27 15:39:12 -050068def do_get_esel(args):
69 s = BMC(server=args.server)
70 e = s.get_event(args.event)
71 if e['Message'] != 'org.open_power.Error.Host.Event' and\
72 e['Message'] != 'org.open_power.Error.Host.Event.Event':
73 raise Exception("Event is not from Host: " + e['Message'])
74 for d in e['AdditionalData']:
Patrick Williams842308b2017-03-27 15:49:38 -050075 data = d.split("=")
76 tag = data.pop(0)
Patrick Williams7a09b672017-03-27 15:39:12 -050077 if tag != 'ESEL':
78 continue
79 data = "=".join(data)
Patrick Williams6f5c3ea2017-03-27 16:23:52 -050080 if args.binary:
81 data = data.split(" ")
82 if '' == data[-1]:
83 data.pop()
84 data = "".join(map(lambda x: chr(int(x, 16)), data))
Patrick Williams7a09b672017-03-27 15:39:12 -050085 print(data)
86
Patrick Williams5e98b3a2017-03-27 15:28:35 -050087
Patrick Williams9b05ea32017-03-27 15:22:31 -050088parser = argparse.ArgumentParser()
89parser.add_argument('--server', help='hostname or IP of BMC', type=str,
90 required=True)
91
92subparsers = parser.add_subparsers()
Andrew Geissler50a753c2017-08-19 09:00:16 -050093
Patrick Williams9b05ea32017-03-27 15:22:31 -050094list_events = subparsers.add_parser('list', help='List all events on BMC')
95list_events.set_defaults(func=do_list_events)
Andrew Geissler50a753c2017-08-19 09:00:16 -050096
Patrick Williams5e98b3a2017-03-27 15:28:35 -050097view_event = subparsers.add_parser(
98 'view', help='View all data for an individual event')
99view_event.add_argument('event', help='The event to view')
100view_event.set_defaults(func=do_view_event)
Andrew Geissler50a753c2017-08-19 09:00:16 -0500101
Patrick Williams7a09b672017-03-27 15:39:12 -0500102get_esel = subparsers.add_parser(
103 'get-esel', help='Extract OpenPOWER eSEL data for an individual event')
104get_esel.add_argument('event', help='The event to get eSEL from')
Patrick Williams6f5c3ea2017-03-27 16:23:52 -0500105get_esel.add_argument('--binary', help='Print event in raw binary',
106 action='store_const', const=True)
Patrick Williams7a09b672017-03-27 15:39:12 -0500107get_esel.set_defaults(func=do_get_esel)
Patrick Williams9b05ea32017-03-27 15:22:31 -0500108
Andrew Geissler50a753c2017-08-19 09:00:16 -0500109clear_event = subparsers.add_parser(
110 'clear', help="Clear individual event")
111clear_event.add_argument('event', help="The event to clear")
112clear_event.set_defaults(func=do_clear_event)
113
Patrick Williams9b05ea32017-03-27 15:22:31 -0500114args = parser.parse_args()
115
116if 'func' in args:
117 args.func(args)
118else:
119 parser.print_help()