blob: e6798371fd78971250be013a7975f4a65d33dbe4 [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
Patrick Williams533c13a2017-03-27 15:28:51 -05008
Patrick Williamsb19c34c2017-03-27 14:52:28 -05009class BMC:
10 def __init__(self, server):
11 self.url = "https://{0}/".format(server)
12 self.session = requests.Session()
13 self.login()
14
15 def login(self):
16 r = self.session.post(self.url + 'login',
Patrick Williams533c13a2017-03-27 15:28:51 -050017 json={'data': ['root', '0penBmc']},
Patrick Williamsb19c34c2017-03-27 14:52:28 -050018 verify=False)
19 j = r.json()
20 if j['status'] != 'ok':
21 raise Exception("Failed to login: \n" + r.text)
22
Patrick Williamsec472d82017-03-27 15:15:43 -050023 def list_events(self):
24 r = self.session.get(self.url + 'xyz/openbmc_project/logging/entry/',
25 verify=False)
26 j = r.json()
Andrew Geissler4449be32017-09-29 15:03:18 -050027 if j['message'] == '404 Not Found':
28 print "No error logs on system\n"
29 return
Patrick Williamsec472d82017-03-27 15:15:43 -050030 if j['status'] != 'ok':
31 raise Exception("Failed to query entries: \n" + r.text)
32
33 events = j['data']
34 events.sort(key=lambda x: int(x.split("/")[-1]))
35
36 return events
Patrick Williamsb19c34c2017-03-27 14:52:28 -050037
Patrick Williams5e98b3a2017-03-27 15:28:35 -050038 def get_event(self, event):
39 r = self.session.get(self.url + event, verify=False)
40
41 j = r.json()
42 if j['status'] != 'ok':
43 raise Exception("Failed to get event " + event + ": \n" + r.text)
44
45 return j['data']
Nagaraju Gorugantic1a00af2018-11-07 00:52:11 -060046
Andrew Geissler50a753c2017-08-19 09:00:16 -050047 def clear_event(self,event):
48 r = self.session.delete(self.url + event)
49 j = r.json()
50 if j['status'] != 'ok':
51 raise Exception("Failed to clear event " + event + ": \n" + r.text)
Patrick Williams5e98b3a2017-03-27 15:28:35 -050052
Andrew Geissler96916f62017-10-03 13:57:26 -050053 def clear_all_events(self):
54 r = self.session.post(self.url + 'xyz/openbmc_project/logging/action/deleteall',
55 headers={'Content-Type': 'application/json'},
56 data='{"data":[]}',
57 verify=False)
58 j = r.json()
59 if j['status'] != 'ok':
60 raise Exception("Failed to clear all events\n" + r.text)
61
Andrew Geissler4370d992017-08-21 10:32:27 -050062 def list_dumps(self):
63 r = self.session.get(self.url + 'xyz/openbmc_project/dump/entry/',
64 verify=False)
65 j = r.json()
Andrew Geissler377306d2017-12-18 20:27:44 -060066 if j['message'] == '404 Not Found':
67 print "No dumps on system\n"
Andrew Geisslerda72d3f2018-02-07 12:53:47 -060068 return []
Andrew Geissler4370d992017-08-21 10:32:27 -050069 if j['status'] != 'ok':
70 raise Exception("Failed to list dumps: \n" + r.text)
71
72 dumps = j['data']
73 dumps.sort(key=lambda x: int(x.split("/")[-1]))
74
75 return dumps
Nagaraju Gorugantic1a00af2018-11-07 00:52:11 -060076
Andrew Geissler4370d992017-08-21 10:32:27 -050077 def create_dump(self):
Andrew Geisslerdf4d2912017-12-18 20:35:28 -060078 r = self.session.post(self.url + 'xyz/openbmc_project/dump/action/CreateDump',
79 headers={'Content-Type': 'application/json'},
Andrew Geisslerda72d3f2018-02-07 12:53:47 -060080 data='{"data":[]}')
Andrew Geissler4370d992017-08-21 10:32:27 -050081 j = r.json()
82 if j['status'] != 'ok':
83 raise Exception("Failed to create dump: \n" + r.text)
Nagaraju Gorugantic1a00af2018-11-07 00:52:11 -060084
Andrew Geissler4370d992017-08-21 10:32:27 -050085 def get_dump(self,dump):
Andrew Geissleraec68eb2017-12-18 21:45:13 -060086 r = self.session.get(self.url + "/download/dump/" + dump,
Andrew Geissler4370d992017-08-21 10:32:27 -050087 verify=False)
Andrew Geissleraec68eb2017-12-18 21:45:13 -060088 with open("dump" + dump + ".tar.gz","wb") as dump:
89 dump.write(r.content)
Andrew Geissler4370d992017-08-21 10:32:27 -050090
91 def clear_dump(self,dump):
92 r = self.session.delete(self.url + dump)
93 j = r.json()
94 if j['status'] != 'ok':
Nagaraju Gorugantic1a00af2018-11-07 00:52:11 -060095 raise Exception("Failed to clear dump " + dump + ": \n" + r.text)
Patrick Williamsb19c34c2017-03-27 14:52:28 -050096
Patrick Williams9b05ea32017-03-27 15:22:31 -050097def do_list_events(args):
98 s = BMC(server=args.server)
Andrew Geissler4449be32017-09-29 15:03:18 -050099 try:
100 for e in s.list_events():
101 print(e)
102 except:
103 # ignore if we get nothing back
104 return;
Patrick Williams9b05ea32017-03-27 15:22:31 -0500105
Patrick Williams5e98b3a2017-03-27 15:28:35 -0500106
107def do_view_event(args):
108 s = BMC(server=args.server)
Andrew Geisslerd35538c2017-08-25 08:43:08 -0500109 print json.dumps(s.get_event(args.event), indent=4)
Patrick Williams5e98b3a2017-03-27 15:28:35 -0500110
Andrew Geissler50a753c2017-08-19 09:00:16 -0500111def do_clear_event(args):
112 s = BMC(server=args.server)
113 s.clear_event(args.event)
Patrick Williams4296f4b2017-03-27 16:26:41 -0500114
Andrew Geissler96916f62017-10-03 13:57:26 -0500115def do_clear_all_events(args):
116 s = BMC(server=args.server)
117 s.clear_all_events()
118
Andrew Geissler4370d992017-08-21 10:32:27 -0500119def do_list_dumps(args):
120 s = BMC(server=args.server)
Andrew Geisslerda72d3f2018-02-07 12:53:47 -0600121 for e in s.list_dumps():
122 print(e)
Andrew Geissler4370d992017-08-21 10:32:27 -0500123
124def do_create_dump(args):
125 s = BMC(server=args.server)
126 s.create_dump()
Nagaraju Gorugantic1a00af2018-11-07 00:52:11 -0600127
Andrew Geissler4370d992017-08-21 10:32:27 -0500128def do_get_dump(args):
129 s = BMC(server=args.server)
130 s.get_dump(args.dump)
131
132def do_clear_dump(args):
133 s = BMC(server=args.server)
134 s.clear_dump(args.dump)
135
Patrick Williams7a09b672017-03-27 15:39:12 -0500136def do_get_esel(args):
137 s = BMC(server=args.server)
138 e = s.get_event(args.event)
139 if e['Message'] != 'org.open_power.Error.Host.Event' and\
140 e['Message'] != 'org.open_power.Error.Host.Event.Event':
141 raise Exception("Event is not from Host: " + e['Message'])
142 for d in e['AdditionalData']:
Patrick Williams842308b2017-03-27 15:49:38 -0500143 data = d.split("=")
144 tag = data.pop(0)
Patrick Williams7a09b672017-03-27 15:39:12 -0500145 if tag != 'ESEL':
146 continue
147 data = "=".join(data)
Patrick Williams6f5c3ea2017-03-27 16:23:52 -0500148 if args.binary:
149 data = data.split(" ")
150 if '' == data[-1]:
151 data.pop()
152 data = "".join(map(lambda x: chr(int(x, 16)), data))
Patrick Williams7a09b672017-03-27 15:39:12 -0500153 print(data)
154
Patrick Williams5e98b3a2017-03-27 15:28:35 -0500155
Patrick Williams9b05ea32017-03-27 15:22:31 -0500156parser = argparse.ArgumentParser()
157parser.add_argument('--server', help='hostname or IP of BMC', type=str,
158 required=True)
Andrew Geisslerda72d3f2018-02-07 12:53:47 -0600159parser.add_argument('--suppress-insecure-warnings', '-I', action="store_true",
160 default=False)
Patrick Williams9b05ea32017-03-27 15:22:31 -0500161
162subparsers = parser.add_subparsers()
Andrew Geissler50a753c2017-08-19 09:00:16 -0500163
Patrick Williams9b05ea32017-03-27 15:22:31 -0500164list_events = subparsers.add_parser('list', help='List all events on BMC')
165list_events.set_defaults(func=do_list_events)
Andrew Geissler50a753c2017-08-19 09:00:16 -0500166
Patrick Williams5e98b3a2017-03-27 15:28:35 -0500167view_event = subparsers.add_parser(
168 'view', help='View all data for an individual event')
169view_event.add_argument('event', help='The event to view')
170view_event.set_defaults(func=do_view_event)
Andrew Geissler50a753c2017-08-19 09:00:16 -0500171
Patrick Williams7a09b672017-03-27 15:39:12 -0500172get_esel = subparsers.add_parser(
173 'get-esel', help='Extract OpenPOWER eSEL data for an individual event')
174get_esel.add_argument('event', help='The event to get eSEL from')
Patrick Williams6f5c3ea2017-03-27 16:23:52 -0500175get_esel.add_argument('--binary', help='Print event in raw binary',
176 action='store_const', const=True)
Patrick Williams7a09b672017-03-27 15:39:12 -0500177get_esel.set_defaults(func=do_get_esel)
Patrick Williams9b05ea32017-03-27 15:22:31 -0500178
Andrew Geissler50a753c2017-08-19 09:00:16 -0500179clear_event = subparsers.add_parser(
180 'clear', help="Clear individual event")
181clear_event.add_argument('event', help="The event to clear")
182clear_event.set_defaults(func=do_clear_event)
183
Andrew Geissler96916f62017-10-03 13:57:26 -0500184clear_all_events = subparsers.add_parser(
185 'clear-all', help="Clear all event")
186clear_all_events.set_defaults(func=do_clear_all_events)
187
Andrew Geissler4370d992017-08-21 10:32:27 -0500188list_dumps = subparsers.add_parser(
189 'list-dumps', help="List all dumps")
190list_dumps.set_defaults(func=do_list_dumps)
191
192create_dump = subparsers.add_parser(
193 'create-dump', help="Create a dump")
194create_dump.set_defaults(func=do_create_dump)
195
196get_dump = subparsers.add_parser(
197 'get-dump', help="Get a dump")
198get_dump.add_argument('dump', help="The dump to get")
199get_dump.set_defaults(func=do_get_dump)
200
201clear_dump = subparsers.add_parser(
202 'clear-dump', help="Clear individual dump")
203clear_dump.add_argument('dump', help="The dump to clear")
204clear_dump.set_defaults(func=do_clear_dump)
205
Patrick Williams9b05ea32017-03-27 15:22:31 -0500206args = parser.parse_args()
207
Andrew Geisslerda72d3f2018-02-07 12:53:47 -0600208if args.suppress_insecure_warnings:
209 from requests.packages.urllib3.exceptions import InsecureRequestWarning
210 requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
211
Patrick Williams9b05ea32017-03-27 15:22:31 -0500212if 'func' in args:
213 args.func(args)
214else:
215 parser.print_help()