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 |
| 8 | urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) |
| 9 | |
Patrick Williams | 533c13a | 2017-03-27 15:28:51 -0500 | [diff] [blame] | 10 | |
Patrick Williams | b19c34c | 2017-03-27 14:52:28 -0500 | [diff] [blame] | 11 | class 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 Williams | 533c13a | 2017-03-27 15:28:51 -0500 | [diff] [blame] | 19 | json={'data': ['root', '0penBmc']}, |
Patrick Williams | b19c34c | 2017-03-27 14:52:28 -0500 | [diff] [blame] | 20 | verify=False) |
| 21 | j = r.json() |
| 22 | if j['status'] != 'ok': |
| 23 | raise Exception("Failed to login: \n" + r.text) |
| 24 | |
Patrick Williams | ec472d8 | 2017-03-27 15:15:43 -0500 | [diff] [blame] | 25 | 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 Williams | b19c34c | 2017-03-27 14:52:28 -0500 | [diff] [blame] | 36 | |
Patrick Williams | 5e98b3a | 2017-03-27 15:28:35 -0500 | [diff] [blame] | 37 | 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 Geissler | 50a753c | 2017-08-19 09:00:16 -0500 | [diff] [blame] | 45 | |
| 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 Williams | 5e98b3a | 2017-03-27 15:28:35 -0500 | [diff] [blame] | 51 | |
Andrew Geissler | 4370d99 | 2017-08-21 10:32:27 -0500 | [diff] [blame^] | 52 | 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 Williams | b19c34c | 2017-03-27 14:52:28 -0500 | [diff] [blame] | 82 | |
Patrick Williams | 9b05ea3 | 2017-03-27 15:22:31 -0500 | [diff] [blame] | 83 | def do_list_events(args): |
| 84 | s = BMC(server=args.server) |
| 85 | for e in s.list_events(): |
| 86 | print(e) |
| 87 | |
Patrick Williams | 5e98b3a | 2017-03-27 15:28:35 -0500 | [diff] [blame] | 88 | |
| 89 | def do_view_event(args): |
| 90 | s = BMC(server=args.server) |
| 91 | print(s.get_event(args.event)) |
Andrew Geissler | d35538c | 2017-08-25 08:43:08 -0500 | [diff] [blame] | 92 | print json.dumps(s.get_event(args.event), indent=4) |
Patrick Williams | 5e98b3a | 2017-03-27 15:28:35 -0500 | [diff] [blame] | 93 | |
Andrew Geissler | 50a753c | 2017-08-19 09:00:16 -0500 | [diff] [blame] | 94 | def do_clear_event(args): |
| 95 | s = BMC(server=args.server) |
| 96 | s.clear_event(args.event) |
Patrick Williams | 4296f4b | 2017-03-27 16:26:41 -0500 | [diff] [blame] | 97 | |
Andrew Geissler | 4370d99 | 2017-08-21 10:32:27 -0500 | [diff] [blame^] | 98 | def do_list_dumps(args): |
| 99 | s = BMC(server=args.server) |
| 100 | for e in s.list_dumps(): |
| 101 | print(e) |
| 102 | |
| 103 | def do_create_dump(args): |
| 104 | s = BMC(server=args.server) |
| 105 | s.create_dump() |
| 106 | |
| 107 | def do_get_dump(args): |
| 108 | s = BMC(server=args.server) |
| 109 | s.get_dump(args.dump) |
| 110 | |
| 111 | def do_clear_dump(args): |
| 112 | s = BMC(server=args.server) |
| 113 | s.clear_dump(args.dump) |
| 114 | |
Patrick Williams | 7a09b67 | 2017-03-27 15:39:12 -0500 | [diff] [blame] | 115 | def 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 Williams | 842308b | 2017-03-27 15:49:38 -0500 | [diff] [blame] | 122 | data = d.split("=") |
| 123 | tag = data.pop(0) |
Patrick Williams | 7a09b67 | 2017-03-27 15:39:12 -0500 | [diff] [blame] | 124 | if tag != 'ESEL': |
| 125 | continue |
| 126 | data = "=".join(data) |
Patrick Williams | 6f5c3ea | 2017-03-27 16:23:52 -0500 | [diff] [blame] | 127 | 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 Williams | 7a09b67 | 2017-03-27 15:39:12 -0500 | [diff] [blame] | 132 | print(data) |
| 133 | |
Patrick Williams | 5e98b3a | 2017-03-27 15:28:35 -0500 | [diff] [blame] | 134 | |
Patrick Williams | 9b05ea3 | 2017-03-27 15:22:31 -0500 | [diff] [blame] | 135 | parser = argparse.ArgumentParser() |
| 136 | parser.add_argument('--server', help='hostname or IP of BMC', type=str, |
| 137 | required=True) |
| 138 | |
| 139 | subparsers = parser.add_subparsers() |
Andrew Geissler | 50a753c | 2017-08-19 09:00:16 -0500 | [diff] [blame] | 140 | |
Patrick Williams | 9b05ea3 | 2017-03-27 15:22:31 -0500 | [diff] [blame] | 141 | list_events = subparsers.add_parser('list', help='List all events on BMC') |
| 142 | list_events.set_defaults(func=do_list_events) |
Andrew Geissler | 50a753c | 2017-08-19 09:00:16 -0500 | [diff] [blame] | 143 | |
Patrick Williams | 5e98b3a | 2017-03-27 15:28:35 -0500 | [diff] [blame] | 144 | view_event = subparsers.add_parser( |
| 145 | 'view', help='View all data for an individual event') |
| 146 | view_event.add_argument('event', help='The event to view') |
| 147 | view_event.set_defaults(func=do_view_event) |
Andrew Geissler | 50a753c | 2017-08-19 09:00:16 -0500 | [diff] [blame] | 148 | |
Patrick Williams | 7a09b67 | 2017-03-27 15:39:12 -0500 | [diff] [blame] | 149 | get_esel = subparsers.add_parser( |
| 150 | 'get-esel', help='Extract OpenPOWER eSEL data for an individual event') |
| 151 | get_esel.add_argument('event', help='The event to get eSEL from') |
Patrick Williams | 6f5c3ea | 2017-03-27 16:23:52 -0500 | [diff] [blame] | 152 | get_esel.add_argument('--binary', help='Print event in raw binary', |
| 153 | action='store_const', const=True) |
Patrick Williams | 7a09b67 | 2017-03-27 15:39:12 -0500 | [diff] [blame] | 154 | get_esel.set_defaults(func=do_get_esel) |
Patrick Williams | 9b05ea3 | 2017-03-27 15:22:31 -0500 | [diff] [blame] | 155 | |
Andrew Geissler | 50a753c | 2017-08-19 09:00:16 -0500 | [diff] [blame] | 156 | clear_event = subparsers.add_parser( |
| 157 | 'clear', help="Clear individual event") |
| 158 | clear_event.add_argument('event', help="The event to clear") |
| 159 | clear_event.set_defaults(func=do_clear_event) |
| 160 | |
Andrew Geissler | 4370d99 | 2017-08-21 10:32:27 -0500 | [diff] [blame^] | 161 | list_dumps = subparsers.add_parser( |
| 162 | 'list-dumps', help="List all dumps") |
| 163 | list_dumps.set_defaults(func=do_list_dumps) |
| 164 | |
| 165 | create_dump = subparsers.add_parser( |
| 166 | 'create-dump', help="Create a dump") |
| 167 | create_dump.set_defaults(func=do_create_dump) |
| 168 | |
| 169 | get_dump = subparsers.add_parser( |
| 170 | 'get-dump', help="Get a dump") |
| 171 | get_dump.add_argument('dump', help="The dump to get") |
| 172 | get_dump.set_defaults(func=do_get_dump) |
| 173 | |
| 174 | clear_dump = subparsers.add_parser( |
| 175 | 'clear-dump', help="Clear individual dump") |
| 176 | clear_dump.add_argument('dump', help="The dump to clear") |
| 177 | clear_dump.set_defaults(func=do_clear_dump) |
| 178 | |
Patrick Williams | 9b05ea3 | 2017-03-27 15:22:31 -0500 | [diff] [blame] | 179 | args = parser.parse_args() |
| 180 | |
| 181 | if 'func' in args: |
| 182 | args.func(args) |
| 183 | else: |
| 184 | parser.print_help() |