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 |
Andrew Geissler | 4449be3 | 2017-09-29 15:03:18 -0500 | [diff] [blame^] | 8 | from warnings import catch_warnings |
Andrew Geissler | 39541fd | 2017-08-19 08:31:07 -0500 | [diff] [blame] | 9 | urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) |
| 10 | |
Patrick Williams | 533c13a | 2017-03-27 15:28:51 -0500 | [diff] [blame] | 11 | |
Patrick Williams | b19c34c | 2017-03-27 14:52:28 -0500 | [diff] [blame] | 12 | class 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 Williams | 533c13a | 2017-03-27 15:28:51 -0500 | [diff] [blame] | 20 | json={'data': ['root', '0penBmc']}, |
Patrick Williams | b19c34c | 2017-03-27 14:52:28 -0500 | [diff] [blame] | 21 | verify=False) |
| 22 | j = r.json() |
| 23 | if j['status'] != 'ok': |
| 24 | raise Exception("Failed to login: \n" + r.text) |
| 25 | |
Patrick Williams | ec472d8 | 2017-03-27 15:15:43 -0500 | [diff] [blame] | 26 | def list_events(self): |
| 27 | r = self.session.get(self.url + 'xyz/openbmc_project/logging/entry/', |
| 28 | verify=False) |
| 29 | j = r.json() |
Andrew Geissler | 4449be3 | 2017-09-29 15:03:18 -0500 | [diff] [blame^] | 30 | if j['message'] == '404 Not Found': |
| 31 | print "No error logs on system\n" |
| 32 | return |
Patrick Williams | ec472d8 | 2017-03-27 15:15:43 -0500 | [diff] [blame] | 33 | 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 Williams | b19c34c | 2017-03-27 14:52:28 -0500 | [diff] [blame] | 40 | |
Patrick Williams | 5e98b3a | 2017-03-27 15:28:35 -0500 | [diff] [blame] | 41 | 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 Geissler | 50a753c | 2017-08-19 09:00:16 -0500 | [diff] [blame] | 49 | |
| 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 Williams | 5e98b3a | 2017-03-27 15:28:35 -0500 | [diff] [blame] | 55 | |
Andrew Geissler | 4370d99 | 2017-08-21 10:32:27 -0500 | [diff] [blame] | 56 | 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 Williams | b19c34c | 2017-03-27 14:52:28 -0500 | [diff] [blame] | 86 | |
Patrick Williams | 9b05ea3 | 2017-03-27 15:22:31 -0500 | [diff] [blame] | 87 | def do_list_events(args): |
| 88 | s = BMC(server=args.server) |
Andrew Geissler | 4449be3 | 2017-09-29 15:03:18 -0500 | [diff] [blame^] | 89 | try: |
| 90 | for e in s.list_events(): |
| 91 | print(e) |
| 92 | except: |
| 93 | # ignore if we get nothing back |
| 94 | return; |
Patrick Williams | 9b05ea3 | 2017-03-27 15:22:31 -0500 | [diff] [blame] | 95 | |
Patrick Williams | 5e98b3a | 2017-03-27 15:28:35 -0500 | [diff] [blame] | 96 | |
| 97 | def do_view_event(args): |
| 98 | s = BMC(server=args.server) |
Andrew Geissler | d35538c | 2017-08-25 08:43:08 -0500 | [diff] [blame] | 99 | print json.dumps(s.get_event(args.event), indent=4) |
Patrick Williams | 5e98b3a | 2017-03-27 15:28:35 -0500 | [diff] [blame] | 100 | |
Andrew Geissler | 50a753c | 2017-08-19 09:00:16 -0500 | [diff] [blame] | 101 | def do_clear_event(args): |
| 102 | s = BMC(server=args.server) |
| 103 | s.clear_event(args.event) |
Patrick Williams | 4296f4b | 2017-03-27 16:26:41 -0500 | [diff] [blame] | 104 | |
Andrew Geissler | 4370d99 | 2017-08-21 10:32:27 -0500 | [diff] [blame] | 105 | def do_list_dumps(args): |
| 106 | s = BMC(server=args.server) |
| 107 | for e in s.list_dumps(): |
| 108 | print(e) |
| 109 | |
| 110 | def do_create_dump(args): |
| 111 | s = BMC(server=args.server) |
| 112 | s.create_dump() |
| 113 | |
| 114 | def do_get_dump(args): |
| 115 | s = BMC(server=args.server) |
| 116 | s.get_dump(args.dump) |
| 117 | |
| 118 | def do_clear_dump(args): |
| 119 | s = BMC(server=args.server) |
| 120 | s.clear_dump(args.dump) |
| 121 | |
Patrick Williams | 7a09b67 | 2017-03-27 15:39:12 -0500 | [diff] [blame] | 122 | def 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 Williams | 842308b | 2017-03-27 15:49:38 -0500 | [diff] [blame] | 129 | data = d.split("=") |
| 130 | tag = data.pop(0) |
Patrick Williams | 7a09b67 | 2017-03-27 15:39:12 -0500 | [diff] [blame] | 131 | if tag != 'ESEL': |
| 132 | continue |
| 133 | data = "=".join(data) |
Patrick Williams | 6f5c3ea | 2017-03-27 16:23:52 -0500 | [diff] [blame] | 134 | 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 Williams | 7a09b67 | 2017-03-27 15:39:12 -0500 | [diff] [blame] | 139 | print(data) |
| 140 | |
Patrick Williams | 5e98b3a | 2017-03-27 15:28:35 -0500 | [diff] [blame] | 141 | |
Patrick Williams | 9b05ea3 | 2017-03-27 15:22:31 -0500 | [diff] [blame] | 142 | parser = argparse.ArgumentParser() |
| 143 | parser.add_argument('--server', help='hostname or IP of BMC', type=str, |
| 144 | required=True) |
| 145 | |
| 146 | subparsers = parser.add_subparsers() |
Andrew Geissler | 50a753c | 2017-08-19 09:00:16 -0500 | [diff] [blame] | 147 | |
Patrick Williams | 9b05ea3 | 2017-03-27 15:22:31 -0500 | [diff] [blame] | 148 | list_events = subparsers.add_parser('list', help='List all events on BMC') |
| 149 | list_events.set_defaults(func=do_list_events) |
Andrew Geissler | 50a753c | 2017-08-19 09:00:16 -0500 | [diff] [blame] | 150 | |
Patrick Williams | 5e98b3a | 2017-03-27 15:28:35 -0500 | [diff] [blame] | 151 | view_event = subparsers.add_parser( |
| 152 | 'view', help='View all data for an individual event') |
| 153 | view_event.add_argument('event', help='The event to view') |
| 154 | view_event.set_defaults(func=do_view_event) |
Andrew Geissler | 50a753c | 2017-08-19 09:00:16 -0500 | [diff] [blame] | 155 | |
Patrick Williams | 7a09b67 | 2017-03-27 15:39:12 -0500 | [diff] [blame] | 156 | get_esel = subparsers.add_parser( |
| 157 | 'get-esel', help='Extract OpenPOWER eSEL data for an individual event') |
| 158 | get_esel.add_argument('event', help='The event to get eSEL from') |
Patrick Williams | 6f5c3ea | 2017-03-27 16:23:52 -0500 | [diff] [blame] | 159 | get_esel.add_argument('--binary', help='Print event in raw binary', |
| 160 | action='store_const', const=True) |
Patrick Williams | 7a09b67 | 2017-03-27 15:39:12 -0500 | [diff] [blame] | 161 | get_esel.set_defaults(func=do_get_esel) |
Patrick Williams | 9b05ea3 | 2017-03-27 15:22:31 -0500 | [diff] [blame] | 162 | |
Andrew Geissler | 50a753c | 2017-08-19 09:00:16 -0500 | [diff] [blame] | 163 | clear_event = subparsers.add_parser( |
| 164 | 'clear', help="Clear individual event") |
| 165 | clear_event.add_argument('event', help="The event to clear") |
| 166 | clear_event.set_defaults(func=do_clear_event) |
| 167 | |
Andrew Geissler | 4370d99 | 2017-08-21 10:32:27 -0500 | [diff] [blame] | 168 | list_dumps = subparsers.add_parser( |
| 169 | 'list-dumps', help="List all dumps") |
| 170 | list_dumps.set_defaults(func=do_list_dumps) |
| 171 | |
| 172 | create_dump = subparsers.add_parser( |
| 173 | 'create-dump', help="Create a dump") |
| 174 | create_dump.set_defaults(func=do_create_dump) |
| 175 | |
| 176 | get_dump = subparsers.add_parser( |
| 177 | 'get-dump', help="Get a dump") |
| 178 | get_dump.add_argument('dump', help="The dump to get") |
| 179 | get_dump.set_defaults(func=do_get_dump) |
| 180 | |
| 181 | clear_dump = subparsers.add_parser( |
| 182 | 'clear-dump', help="Clear individual dump") |
| 183 | clear_dump.add_argument('dump', help="The dump to clear") |
| 184 | clear_dump.set_defaults(func=do_clear_dump) |
| 185 | |
Patrick Williams | 9b05ea3 | 2017-03-27 15:22:31 -0500 | [diff] [blame] | 186 | args = parser.parse_args() |
| 187 | |
| 188 | if 'func' in args: |
| 189 | args.func(args) |
| 190 | else: |
| 191 | parser.print_help() |