blob: 251622fa62109a6e98e0534df8f4639715e2256c [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
Andrew Geissler4370d992017-08-21 10:32:27 -050052 def list_dumps(self):
53 r = self.session.get(self.url + 'xyz/openbmc_project/dump/entry/',
54 verify=False)
55 j = r.json()
56 if j['status'] != 'ok':
57 raise Exception("Failed to list dumps: \n" + r.text)
58
59 dumps = j['data']
60 dumps.sort(key=lambda x: int(x.split("/")[-1]))
61
62 return dumps
63
64 def create_dump(self):
65 r = self.session.post(self.url + 'xyz/openbmc_project/dump/action/CreateDump')
66 j = r.json()
67 if j['status'] != 'ok':
68 raise Exception("Failed to create dump: \n" + r.text)
69
70 def get_dump(self,dump):
71 r = self.session.get(self.url + dump,
72 verify=False)
73 j = r.json()
74 if j['status'] != 'ok':
75 raise Exception("Failed to get dump " + event + ": \n" + r.text)
76
77 def clear_dump(self,dump):
78 r = self.session.delete(self.url + dump)
79 j = r.json()
80 if j['status'] != 'ok':
81 raise Exception("Failed to clear dump " + dump + ": \n" + r.text)
Patrick Williamsb19c34c2017-03-27 14:52:28 -050082
Patrick Williams9b05ea32017-03-27 15:22:31 -050083def do_list_events(args):
84 s = BMC(server=args.server)
85 for e in s.list_events():
86 print(e)
87
Patrick Williams5e98b3a2017-03-27 15:28:35 -050088
89def do_view_event(args):
90 s = BMC(server=args.server)
91 print(s.get_event(args.event))
Andrew Geisslerd35538c2017-08-25 08:43:08 -050092 print json.dumps(s.get_event(args.event), indent=4)
Patrick Williams5e98b3a2017-03-27 15:28:35 -050093
Andrew Geissler50a753c2017-08-19 09:00:16 -050094def do_clear_event(args):
95 s = BMC(server=args.server)
96 s.clear_event(args.event)
Patrick Williams4296f4b2017-03-27 16:26:41 -050097
Andrew Geissler4370d992017-08-21 10:32:27 -050098def do_list_dumps(args):
99 s = BMC(server=args.server)
100 for e in s.list_dumps():
101 print(e)
102
103def do_create_dump(args):
104 s = BMC(server=args.server)
105 s.create_dump()
106
107def do_get_dump(args):
108 s = BMC(server=args.server)
109 s.get_dump(args.dump)
110
111def do_clear_dump(args):
112 s = BMC(server=args.server)
113 s.clear_dump(args.dump)
114
Patrick Williams7a09b672017-03-27 15:39:12 -0500115def do_get_esel(args):
116 s = BMC(server=args.server)
117 e = s.get_event(args.event)
118 if e['Message'] != 'org.open_power.Error.Host.Event' and\
119 e['Message'] != 'org.open_power.Error.Host.Event.Event':
120 raise Exception("Event is not from Host: " + e['Message'])
121 for d in e['AdditionalData']:
Patrick Williams842308b2017-03-27 15:49:38 -0500122 data = d.split("=")
123 tag = data.pop(0)
Patrick Williams7a09b672017-03-27 15:39:12 -0500124 if tag != 'ESEL':
125 continue
126 data = "=".join(data)
Patrick Williams6f5c3ea2017-03-27 16:23:52 -0500127 if args.binary:
128 data = data.split(" ")
129 if '' == data[-1]:
130 data.pop()
131 data = "".join(map(lambda x: chr(int(x, 16)), data))
Patrick Williams7a09b672017-03-27 15:39:12 -0500132 print(data)
133
Patrick Williams5e98b3a2017-03-27 15:28:35 -0500134
Patrick Williams9b05ea32017-03-27 15:22:31 -0500135parser = argparse.ArgumentParser()
136parser.add_argument('--server', help='hostname or IP of BMC', type=str,
137 required=True)
138
139subparsers = parser.add_subparsers()
Andrew Geissler50a753c2017-08-19 09:00:16 -0500140
Patrick Williams9b05ea32017-03-27 15:22:31 -0500141list_events = subparsers.add_parser('list', help='List all events on BMC')
142list_events.set_defaults(func=do_list_events)
Andrew Geissler50a753c2017-08-19 09:00:16 -0500143
Patrick Williams5e98b3a2017-03-27 15:28:35 -0500144view_event = subparsers.add_parser(
145 'view', help='View all data for an individual event')
146view_event.add_argument('event', help='The event to view')
147view_event.set_defaults(func=do_view_event)
Andrew Geissler50a753c2017-08-19 09:00:16 -0500148
Patrick Williams7a09b672017-03-27 15:39:12 -0500149get_esel = subparsers.add_parser(
150 'get-esel', help='Extract OpenPOWER eSEL data for an individual event')
151get_esel.add_argument('event', help='The event to get eSEL from')
Patrick Williams6f5c3ea2017-03-27 16:23:52 -0500152get_esel.add_argument('--binary', help='Print event in raw binary',
153 action='store_const', const=True)
Patrick Williams7a09b672017-03-27 15:39:12 -0500154get_esel.set_defaults(func=do_get_esel)
Patrick Williams9b05ea32017-03-27 15:22:31 -0500155
Andrew Geissler50a753c2017-08-19 09:00:16 -0500156clear_event = subparsers.add_parser(
157 'clear', help="Clear individual event")
158clear_event.add_argument('event', help="The event to clear")
159clear_event.set_defaults(func=do_clear_event)
160
Andrew Geissler4370d992017-08-21 10:32:27 -0500161list_dumps = subparsers.add_parser(
162 'list-dumps', help="List all dumps")
163list_dumps.set_defaults(func=do_list_dumps)
164
165create_dump = subparsers.add_parser(
166 'create-dump', help="Create a dump")
167create_dump.set_defaults(func=do_create_dump)
168
169get_dump = subparsers.add_parser(
170 'get-dump', help="Get a dump")
171get_dump.add_argument('dump', help="The dump to get")
172get_dump.set_defaults(func=do_get_dump)
173
174clear_dump = subparsers.add_parser(
175 'clear-dump', help="Clear individual dump")
176clear_dump.add_argument('dump', help="The dump to clear")
177clear_dump.set_defaults(func=do_clear_dump)
178
Patrick Williams9b05ea32017-03-27 15:22:31 -0500179args = parser.parse_args()
180
181if 'func' in args:
182 args.func(args)
183else:
184 parser.print_help()