Patrick Williams | b19c34c | 2017-03-27 14:52:28 -0500 | [diff] [blame] | 1 | #!/bin/env python |
| 2 | |
Patrick Williams | 9b05ea3 | 2017-03-27 15:22:31 -0500 | [diff] [blame] | 3 | import argparse |
Patrick Williams | b19c34c | 2017-03-27 14:52:28 -0500 | [diff] [blame] | 4 | import requests |
Andrew Geissler | d35538c | 2017-08-25 08:43:08 -0500 | [diff] [blame] | 5 | import json |
Patrick Williams | b19c34c | 2017-03-27 14:52:28 -0500 | [diff] [blame] | 6 | |
Andrew Geissler | 39541fd | 2017-08-19 08:31:07 -0500 | [diff] [blame] | 7 | import urllib3 |
Patrick Williams | 533c13a | 2017-03-27 15:28:51 -0500 | [diff] [blame] | 8 | |
Patrick Williams | b19c34c | 2017-03-27 14:52:28 -0500 | [diff] [blame] | 9 | class 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 Williams | 533c13a | 2017-03-27 15:28:51 -0500 | [diff] [blame] | 17 | json={'data': ['root', '0penBmc']}, |
Patrick Williams | b19c34c | 2017-03-27 14:52:28 -0500 | [diff] [blame] | 18 | verify=False) |
| 19 | j = r.json() |
| 20 | if j['status'] != 'ok': |
| 21 | raise Exception("Failed to login: \n" + r.text) |
| 22 | |
Patrick Williams | ec472d8 | 2017-03-27 15:15:43 -0500 | [diff] [blame] | 23 | def list_events(self): |
| 24 | r = self.session.get(self.url + 'xyz/openbmc_project/logging/entry/', |
| 25 | verify=False) |
| 26 | j = r.json() |
Andrew Geissler | 4449be3 | 2017-09-29 15:03:18 -0500 | [diff] [blame] | 27 | if j['message'] == '404 Not Found': |
| 28 | print "No error logs on system\n" |
| 29 | return |
Patrick Williams | ec472d8 | 2017-03-27 15:15:43 -0500 | [diff] [blame] | 30 | 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 Williams | b19c34c | 2017-03-27 14:52:28 -0500 | [diff] [blame] | 37 | |
Patrick Williams | 5e98b3a | 2017-03-27 15:28:35 -0500 | [diff] [blame] | 38 | 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 Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 46 | |
Andrew Geissler | 50a753c | 2017-08-19 09:00:16 -0500 | [diff] [blame] | 47 | 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 Williams | 5e98b3a | 2017-03-27 15:28:35 -0500 | [diff] [blame] | 52 | |
Andrew Geissler | 96916f6 | 2017-10-03 13:57:26 -0500 | [diff] [blame] | 53 | 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 Geissler | 4370d99 | 2017-08-21 10:32:27 -0500 | [diff] [blame] | 62 | def list_dumps(self): |
| 63 | r = self.session.get(self.url + 'xyz/openbmc_project/dump/entry/', |
| 64 | verify=False) |
| 65 | j = r.json() |
Andrew Geissler | 377306d | 2017-12-18 20:27:44 -0600 | [diff] [blame] | 66 | if j['message'] == '404 Not Found': |
| 67 | print "No dumps on system\n" |
Andrew Geissler | da72d3f | 2018-02-07 12:53:47 -0600 | [diff] [blame] | 68 | return [] |
Andrew Geissler | 4370d99 | 2017-08-21 10:32:27 -0500 | [diff] [blame] | 69 | 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 Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 76 | |
Andrew Geissler | 4370d99 | 2017-08-21 10:32:27 -0500 | [diff] [blame] | 77 | def create_dump(self): |
Andrew Geissler | df4d291 | 2017-12-18 20:35:28 -0600 | [diff] [blame] | 78 | r = self.session.post(self.url + 'xyz/openbmc_project/dump/action/CreateDump', |
| 79 | headers={'Content-Type': 'application/json'}, |
Andrew Geissler | da72d3f | 2018-02-07 12:53:47 -0600 | [diff] [blame] | 80 | data='{"data":[]}') |
Andrew Geissler | 4370d99 | 2017-08-21 10:32:27 -0500 | [diff] [blame] | 81 | j = r.json() |
| 82 | if j['status'] != 'ok': |
| 83 | raise Exception("Failed to create dump: \n" + r.text) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 84 | |
Andrew Geissler | 4370d99 | 2017-08-21 10:32:27 -0500 | [diff] [blame] | 85 | def get_dump(self,dump): |
Andrew Geissler | aec68eb | 2017-12-18 21:45:13 -0600 | [diff] [blame] | 86 | r = self.session.get(self.url + "/download/dump/" + dump, |
Andrew Geissler | 4370d99 | 2017-08-21 10:32:27 -0500 | [diff] [blame] | 87 | verify=False) |
Andrew Geissler | aec68eb | 2017-12-18 21:45:13 -0600 | [diff] [blame] | 88 | with open("dump" + dump + ".tar.gz","wb") as dump: |
| 89 | dump.write(r.content) |
Andrew Geissler | 4370d99 | 2017-08-21 10:32:27 -0500 | [diff] [blame] | 90 | |
| 91 | def clear_dump(self,dump): |
| 92 | r = self.session.delete(self.url + dump) |
| 93 | j = r.json() |
| 94 | if j['status'] != 'ok': |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 95 | raise Exception("Failed to clear dump " + dump + ": \n" + r.text) |
Patrick Williams | b19c34c | 2017-03-27 14:52:28 -0500 | [diff] [blame] | 96 | |
Patrick Williams | 9b05ea3 | 2017-03-27 15:22:31 -0500 | [diff] [blame] | 97 | def do_list_events(args): |
| 98 | s = BMC(server=args.server) |
Andrew Geissler | 4449be3 | 2017-09-29 15:03:18 -0500 | [diff] [blame] | 99 | try: |
| 100 | for e in s.list_events(): |
| 101 | print(e) |
| 102 | except: |
| 103 | # ignore if we get nothing back |
| 104 | return; |
Patrick Williams | 9b05ea3 | 2017-03-27 15:22:31 -0500 | [diff] [blame] | 105 | |
Patrick Williams | 5e98b3a | 2017-03-27 15:28:35 -0500 | [diff] [blame] | 106 | |
| 107 | def do_view_event(args): |
| 108 | s = BMC(server=args.server) |
Andrew Geissler | d35538c | 2017-08-25 08:43:08 -0500 | [diff] [blame] | 109 | print json.dumps(s.get_event(args.event), indent=4) |
Patrick Williams | 5e98b3a | 2017-03-27 15:28:35 -0500 | [diff] [blame] | 110 | |
Andrew Geissler | 50a753c | 2017-08-19 09:00:16 -0500 | [diff] [blame] | 111 | def do_clear_event(args): |
| 112 | s = BMC(server=args.server) |
| 113 | s.clear_event(args.event) |
Patrick Williams | 4296f4b | 2017-03-27 16:26:41 -0500 | [diff] [blame] | 114 | |
Andrew Geissler | 96916f6 | 2017-10-03 13:57:26 -0500 | [diff] [blame] | 115 | def do_clear_all_events(args): |
| 116 | s = BMC(server=args.server) |
| 117 | s.clear_all_events() |
| 118 | |
Andrew Geissler | 4370d99 | 2017-08-21 10:32:27 -0500 | [diff] [blame] | 119 | def do_list_dumps(args): |
| 120 | s = BMC(server=args.server) |
Andrew Geissler | da72d3f | 2018-02-07 12:53:47 -0600 | [diff] [blame] | 121 | for e in s.list_dumps(): |
| 122 | print(e) |
Andrew Geissler | 4370d99 | 2017-08-21 10:32:27 -0500 | [diff] [blame] | 123 | |
| 124 | def do_create_dump(args): |
| 125 | s = BMC(server=args.server) |
| 126 | s.create_dump() |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 127 | |
Andrew Geissler | 4370d99 | 2017-08-21 10:32:27 -0500 | [diff] [blame] | 128 | def do_get_dump(args): |
| 129 | s = BMC(server=args.server) |
| 130 | s.get_dump(args.dump) |
| 131 | |
| 132 | def do_clear_dump(args): |
| 133 | s = BMC(server=args.server) |
| 134 | s.clear_dump(args.dump) |
| 135 | |
Patrick Williams | 7a09b67 | 2017-03-27 15:39:12 -0500 | [diff] [blame] | 136 | def 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 Williams | 842308b | 2017-03-27 15:49:38 -0500 | [diff] [blame] | 143 | data = d.split("=") |
| 144 | tag = data.pop(0) |
Patrick Williams | 7a09b67 | 2017-03-27 15:39:12 -0500 | [diff] [blame] | 145 | if tag != 'ESEL': |
| 146 | continue |
| 147 | data = "=".join(data) |
Patrick Williams | 6f5c3ea | 2017-03-27 16:23:52 -0500 | [diff] [blame] | 148 | 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 Williams | 7a09b67 | 2017-03-27 15:39:12 -0500 | [diff] [blame] | 153 | print(data) |
| 154 | |
Patrick Williams | 5e98b3a | 2017-03-27 15:28:35 -0500 | [diff] [blame] | 155 | |
Patrick Williams | 9b05ea3 | 2017-03-27 15:22:31 -0500 | [diff] [blame] | 156 | parser = argparse.ArgumentParser() |
| 157 | parser.add_argument('--server', help='hostname or IP of BMC', type=str, |
| 158 | required=True) |
Andrew Geissler | da72d3f | 2018-02-07 12:53:47 -0600 | [diff] [blame] | 159 | parser.add_argument('--suppress-insecure-warnings', '-I', action="store_true", |
| 160 | default=False) |
Patrick Williams | 9b05ea3 | 2017-03-27 15:22:31 -0500 | [diff] [blame] | 161 | |
| 162 | subparsers = parser.add_subparsers() |
Andrew Geissler | 50a753c | 2017-08-19 09:00:16 -0500 | [diff] [blame] | 163 | |
Patrick Williams | 9b05ea3 | 2017-03-27 15:22:31 -0500 | [diff] [blame] | 164 | list_events = subparsers.add_parser('list', help='List all events on BMC') |
| 165 | list_events.set_defaults(func=do_list_events) |
Andrew Geissler | 50a753c | 2017-08-19 09:00:16 -0500 | [diff] [blame] | 166 | |
Patrick Williams | 5e98b3a | 2017-03-27 15:28:35 -0500 | [diff] [blame] | 167 | view_event = subparsers.add_parser( |
| 168 | 'view', help='View all data for an individual event') |
| 169 | view_event.add_argument('event', help='The event to view') |
| 170 | view_event.set_defaults(func=do_view_event) |
Andrew Geissler | 50a753c | 2017-08-19 09:00:16 -0500 | [diff] [blame] | 171 | |
Patrick Williams | 7a09b67 | 2017-03-27 15:39:12 -0500 | [diff] [blame] | 172 | get_esel = subparsers.add_parser( |
| 173 | 'get-esel', help='Extract OpenPOWER eSEL data for an individual event') |
| 174 | get_esel.add_argument('event', help='The event to get eSEL from') |
Patrick Williams | 6f5c3ea | 2017-03-27 16:23:52 -0500 | [diff] [blame] | 175 | get_esel.add_argument('--binary', help='Print event in raw binary', |
| 176 | action='store_const', const=True) |
Patrick Williams | 7a09b67 | 2017-03-27 15:39:12 -0500 | [diff] [blame] | 177 | get_esel.set_defaults(func=do_get_esel) |
Patrick Williams | 9b05ea3 | 2017-03-27 15:22:31 -0500 | [diff] [blame] | 178 | |
Andrew Geissler | 50a753c | 2017-08-19 09:00:16 -0500 | [diff] [blame] | 179 | clear_event = subparsers.add_parser( |
| 180 | 'clear', help="Clear individual event") |
| 181 | clear_event.add_argument('event', help="The event to clear") |
| 182 | clear_event.set_defaults(func=do_clear_event) |
| 183 | |
Andrew Geissler | 96916f6 | 2017-10-03 13:57:26 -0500 | [diff] [blame] | 184 | clear_all_events = subparsers.add_parser( |
| 185 | 'clear-all', help="Clear all event") |
| 186 | clear_all_events.set_defaults(func=do_clear_all_events) |
| 187 | |
Andrew Geissler | 4370d99 | 2017-08-21 10:32:27 -0500 | [diff] [blame] | 188 | list_dumps = subparsers.add_parser( |
| 189 | 'list-dumps', help="List all dumps") |
| 190 | list_dumps.set_defaults(func=do_list_dumps) |
| 191 | |
| 192 | create_dump = subparsers.add_parser( |
| 193 | 'create-dump', help="Create a dump") |
| 194 | create_dump.set_defaults(func=do_create_dump) |
| 195 | |
| 196 | get_dump = subparsers.add_parser( |
| 197 | 'get-dump', help="Get a dump") |
| 198 | get_dump.add_argument('dump', help="The dump to get") |
| 199 | get_dump.set_defaults(func=do_get_dump) |
| 200 | |
| 201 | clear_dump = subparsers.add_parser( |
| 202 | 'clear-dump', help="Clear individual dump") |
| 203 | clear_dump.add_argument('dump', help="The dump to clear") |
| 204 | clear_dump.set_defaults(func=do_clear_dump) |
| 205 | |
Patrick Williams | 9b05ea3 | 2017-03-27 15:22:31 -0500 | [diff] [blame] | 206 | args = parser.parse_args() |
| 207 | |
Andrew Geissler | da72d3f | 2018-02-07 12:53:47 -0600 | [diff] [blame] | 208 | if args.suppress_insecure_warnings: |
| 209 | from requests.packages.urllib3.exceptions import InsecureRequestWarning |
| 210 | requests.packages.urllib3.disable_warnings(InsecureRequestWarning) |
| 211 | |
Patrick Williams | 9b05ea3 | 2017-03-27 15:22:31 -0500 | [diff] [blame] | 212 | if 'func' in args: |
| 213 | args.func(args) |
| 214 | else: |
| 215 | parser.print_help() |