blob: 41bbbefdefb32224c3c4ccae434aa188f88682f6 [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
Andrew Geissler4449be32017-09-29 15:03:18 -05008from warnings import catch_warnings
Andrew Geissler39541fd2017-08-19 08:31:07 -05009urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
10
Patrick Williams533c13a2017-03-27 15:28:51 -050011
Patrick Williamsb19c34c2017-03-27 14:52:28 -050012class BMC:
13 def __init__(self, server):
14 self.url = "https://{0}/".format(server)
15 self.session = requests.Session()
16 self.login()
17
18 def login(self):
19 r = self.session.post(self.url + 'login',
Patrick Williams533c13a2017-03-27 15:28:51 -050020 json={'data': ['root', '0penBmc']},
Patrick Williamsb19c34c2017-03-27 14:52:28 -050021 verify=False)
22 j = r.json()
23 if j['status'] != 'ok':
24 raise Exception("Failed to login: \n" + r.text)
25
Patrick Williamsec472d82017-03-27 15:15:43 -050026 def list_events(self):
27 r = self.session.get(self.url + 'xyz/openbmc_project/logging/entry/',
28 verify=False)
29 j = r.json()
Andrew Geissler4449be32017-09-29 15:03:18 -050030 if j['message'] == '404 Not Found':
31 print "No error logs on system\n"
32 return
Patrick Williamsec472d82017-03-27 15:15:43 -050033 if j['status'] != 'ok':
34 raise Exception("Failed to query entries: \n" + r.text)
35
36 events = j['data']
37 events.sort(key=lambda x: int(x.split("/")[-1]))
38
39 return events
Patrick Williamsb19c34c2017-03-27 14:52:28 -050040
Patrick Williams5e98b3a2017-03-27 15:28:35 -050041 def get_event(self, event):
42 r = self.session.get(self.url + event, verify=False)
43
44 j = r.json()
45 if j['status'] != 'ok':
46 raise Exception("Failed to get event " + event + ": \n" + r.text)
47
48 return j['data']
Andrew Geissler50a753c2017-08-19 09:00:16 -050049
50 def clear_event(self,event):
51 r = self.session.delete(self.url + event)
52 j = r.json()
53 if j['status'] != 'ok':
54 raise Exception("Failed to clear event " + event + ": \n" + r.text)
Patrick Williams5e98b3a2017-03-27 15:28:35 -050055
Andrew Geissler4370d992017-08-21 10:32:27 -050056 def list_dumps(self):
57 r = self.session.get(self.url + 'xyz/openbmc_project/dump/entry/',
58 verify=False)
59 j = r.json()
60 if j['status'] != 'ok':
61 raise Exception("Failed to list dumps: \n" + r.text)
62
63 dumps = j['data']
64 dumps.sort(key=lambda x: int(x.split("/")[-1]))
65
66 return dumps
67
68 def create_dump(self):
69 r = self.session.post(self.url + 'xyz/openbmc_project/dump/action/CreateDump')
70 j = r.json()
71 if j['status'] != 'ok':
72 raise Exception("Failed to create dump: \n" + r.text)
73
74 def get_dump(self,dump):
75 r = self.session.get(self.url + dump,
76 verify=False)
77 j = r.json()
78 if j['status'] != 'ok':
79 raise Exception("Failed to get dump " + event + ": \n" + r.text)
80
81 def clear_dump(self,dump):
82 r = self.session.delete(self.url + dump)
83 j = r.json()
84 if j['status'] != 'ok':
85 raise Exception("Failed to clear dump " + dump + ": \n" + r.text)
Patrick Williamsb19c34c2017-03-27 14:52:28 -050086
Patrick Williams9b05ea32017-03-27 15:22:31 -050087def do_list_events(args):
88 s = BMC(server=args.server)
Andrew Geissler4449be32017-09-29 15:03:18 -050089 try:
90 for e in s.list_events():
91 print(e)
92 except:
93 # ignore if we get nothing back
94 return;
Patrick Williams9b05ea32017-03-27 15:22:31 -050095
Patrick Williams5e98b3a2017-03-27 15:28:35 -050096
97def do_view_event(args):
98 s = BMC(server=args.server)
Andrew Geisslerd35538c2017-08-25 08:43:08 -050099 print json.dumps(s.get_event(args.event), indent=4)
Patrick Williams5e98b3a2017-03-27 15:28:35 -0500100
Andrew Geissler50a753c2017-08-19 09:00:16 -0500101def do_clear_event(args):
102 s = BMC(server=args.server)
103 s.clear_event(args.event)
Patrick Williams4296f4b2017-03-27 16:26:41 -0500104
Andrew Geissler4370d992017-08-21 10:32:27 -0500105def do_list_dumps(args):
106 s = BMC(server=args.server)
107 for e in s.list_dumps():
108 print(e)
109
110def do_create_dump(args):
111 s = BMC(server=args.server)
112 s.create_dump()
113
114def do_get_dump(args):
115 s = BMC(server=args.server)
116 s.get_dump(args.dump)
117
118def do_clear_dump(args):
119 s = BMC(server=args.server)
120 s.clear_dump(args.dump)
121
Patrick Williams7a09b672017-03-27 15:39:12 -0500122def do_get_esel(args):
123 s = BMC(server=args.server)
124 e = s.get_event(args.event)
125 if e['Message'] != 'org.open_power.Error.Host.Event' and\
126 e['Message'] != 'org.open_power.Error.Host.Event.Event':
127 raise Exception("Event is not from Host: " + e['Message'])
128 for d in e['AdditionalData']:
Patrick Williams842308b2017-03-27 15:49:38 -0500129 data = d.split("=")
130 tag = data.pop(0)
Patrick Williams7a09b672017-03-27 15:39:12 -0500131 if tag != 'ESEL':
132 continue
133 data = "=".join(data)
Patrick Williams6f5c3ea2017-03-27 16:23:52 -0500134 if args.binary:
135 data = data.split(" ")
136 if '' == data[-1]:
137 data.pop()
138 data = "".join(map(lambda x: chr(int(x, 16)), data))
Patrick Williams7a09b672017-03-27 15:39:12 -0500139 print(data)
140
Patrick Williams5e98b3a2017-03-27 15:28:35 -0500141
Patrick Williams9b05ea32017-03-27 15:22:31 -0500142parser = argparse.ArgumentParser()
143parser.add_argument('--server', help='hostname or IP of BMC', type=str,
144 required=True)
145
146subparsers = parser.add_subparsers()
Andrew Geissler50a753c2017-08-19 09:00:16 -0500147
Patrick Williams9b05ea32017-03-27 15:22:31 -0500148list_events = subparsers.add_parser('list', help='List all events on BMC')
149list_events.set_defaults(func=do_list_events)
Andrew Geissler50a753c2017-08-19 09:00:16 -0500150
Patrick Williams5e98b3a2017-03-27 15:28:35 -0500151view_event = subparsers.add_parser(
152 'view', help='View all data for an individual event')
153view_event.add_argument('event', help='The event to view')
154view_event.set_defaults(func=do_view_event)
Andrew Geissler50a753c2017-08-19 09:00:16 -0500155
Patrick Williams7a09b672017-03-27 15:39:12 -0500156get_esel = subparsers.add_parser(
157 'get-esel', help='Extract OpenPOWER eSEL data for an individual event')
158get_esel.add_argument('event', help='The event to get eSEL from')
Patrick Williams6f5c3ea2017-03-27 16:23:52 -0500159get_esel.add_argument('--binary', help='Print event in raw binary',
160 action='store_const', const=True)
Patrick Williams7a09b672017-03-27 15:39:12 -0500161get_esel.set_defaults(func=do_get_esel)
Patrick Williams9b05ea32017-03-27 15:22:31 -0500162
Andrew Geissler50a753c2017-08-19 09:00:16 -0500163clear_event = subparsers.add_parser(
164 'clear', help="Clear individual event")
165clear_event.add_argument('event', help="The event to clear")
166clear_event.set_defaults(func=do_clear_event)
167
Andrew Geissler4370d992017-08-21 10:32:27 -0500168list_dumps = subparsers.add_parser(
169 'list-dumps', help="List all dumps")
170list_dumps.set_defaults(func=do_list_dumps)
171
172create_dump = subparsers.add_parser(
173 'create-dump', help="Create a dump")
174create_dump.set_defaults(func=do_create_dump)
175
176get_dump = subparsers.add_parser(
177 'get-dump', help="Get a dump")
178get_dump.add_argument('dump', help="The dump to get")
179get_dump.set_defaults(func=do_get_dump)
180
181clear_dump = subparsers.add_parser(
182 'clear-dump', help="Clear individual dump")
183clear_dump.add_argument('dump', help="The dump to clear")
184clear_dump.set_defaults(func=do_clear_dump)
185
Patrick Williams9b05ea32017-03-27 15:22:31 -0500186args = parser.parse_args()
187
188if 'func' in args:
189 args.func(args)
190else:
191 parser.print_help()