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 |
| 5 | |
Patrick Williams | 533c13a | 2017-03-27 15:28:51 -0500 | [diff] [blame] | 6 | |
Patrick Williams | b19c34c | 2017-03-27 14:52:28 -0500 | [diff] [blame] | 7 | class BMC: |
| 8 | def __init__(self, server): |
| 9 | self.url = "https://{0}/".format(server) |
| 10 | self.session = requests.Session() |
| 11 | self.login() |
| 12 | |
| 13 | def login(self): |
| 14 | r = self.session.post(self.url + 'login', |
Patrick Williams | 533c13a | 2017-03-27 15:28:51 -0500 | [diff] [blame] | 15 | json={'data': ['root', '0penBmc']}, |
Patrick Williams | b19c34c | 2017-03-27 14:52:28 -0500 | [diff] [blame] | 16 | verify=False) |
| 17 | j = r.json() |
| 18 | if j['status'] != 'ok': |
| 19 | raise Exception("Failed to login: \n" + r.text) |
| 20 | |
Patrick Williams | ec472d8 | 2017-03-27 15:15:43 -0500 | [diff] [blame] | 21 | def list_events(self): |
| 22 | r = self.session.get(self.url + 'xyz/openbmc_project/logging/entry/', |
| 23 | verify=False) |
| 24 | j = r.json() |
| 25 | if j['status'] != 'ok': |
| 26 | raise Exception("Failed to query entries: \n" + r.text) |
| 27 | |
| 28 | events = j['data'] |
| 29 | events.sort(key=lambda x: int(x.split("/")[-1])) |
| 30 | |
| 31 | return events |
Patrick Williams | b19c34c | 2017-03-27 14:52:28 -0500 | [diff] [blame] | 32 | |
Patrick Williams | 5e98b3a | 2017-03-27 15:28:35 -0500 | [diff] [blame] | 33 | def get_event(self, event): |
| 34 | r = self.session.get(self.url + event, verify=False) |
| 35 | |
| 36 | j = r.json() |
| 37 | if j['status'] != 'ok': |
| 38 | raise Exception("Failed to get event " + event + ": \n" + r.text) |
| 39 | |
| 40 | return j['data'] |
| 41 | |
Patrick Williams | b19c34c | 2017-03-27 14:52:28 -0500 | [diff] [blame] | 42 | |
Patrick Williams | 9b05ea3 | 2017-03-27 15:22:31 -0500 | [diff] [blame] | 43 | def do_list_events(args): |
| 44 | s = BMC(server=args.server) |
| 45 | for e in s.list_events(): |
| 46 | print(e) |
| 47 | |
Patrick Williams | 5e98b3a | 2017-03-27 15:28:35 -0500 | [diff] [blame] | 48 | |
| 49 | def do_view_event(args): |
| 50 | s = BMC(server=args.server) |
| 51 | print(s.get_event(args.event)) |
| 52 | |
Patrick Williams | 4296f4b | 2017-03-27 16:26:41 -0500 | [diff] [blame^] | 53 | |
Patrick Williams | 7a09b67 | 2017-03-27 15:39:12 -0500 | [diff] [blame] | 54 | def do_get_esel(args): |
| 55 | s = BMC(server=args.server) |
| 56 | e = s.get_event(args.event) |
| 57 | if e['Message'] != 'org.open_power.Error.Host.Event' and\ |
| 58 | e['Message'] != 'org.open_power.Error.Host.Event.Event': |
| 59 | raise Exception("Event is not from Host: " + e['Message']) |
| 60 | for d in e['AdditionalData']: |
Patrick Williams | 842308b | 2017-03-27 15:49:38 -0500 | [diff] [blame] | 61 | data = d.split("=") |
| 62 | tag = data.pop(0) |
Patrick Williams | 7a09b67 | 2017-03-27 15:39:12 -0500 | [diff] [blame] | 63 | if tag != 'ESEL': |
| 64 | continue |
| 65 | data = "=".join(data) |
Patrick Williams | 6f5c3ea | 2017-03-27 16:23:52 -0500 | [diff] [blame] | 66 | if args.binary: |
| 67 | data = data.split(" ") |
| 68 | if '' == data[-1]: |
| 69 | data.pop() |
| 70 | data = "".join(map(lambda x: chr(int(x, 16)), data)) |
Patrick Williams | 7a09b67 | 2017-03-27 15:39:12 -0500 | [diff] [blame] | 71 | print(data) |
| 72 | |
Patrick Williams | 5e98b3a | 2017-03-27 15:28:35 -0500 | [diff] [blame] | 73 | |
Patrick Williams | 9b05ea3 | 2017-03-27 15:22:31 -0500 | [diff] [blame] | 74 | parser = argparse.ArgumentParser() |
| 75 | parser.add_argument('--server', help='hostname or IP of BMC', type=str, |
| 76 | required=True) |
| 77 | |
| 78 | subparsers = parser.add_subparsers() |
| 79 | list_events = subparsers.add_parser('list', help='List all events on BMC') |
| 80 | list_events.set_defaults(func=do_list_events) |
Patrick Williams | 5e98b3a | 2017-03-27 15:28:35 -0500 | [diff] [blame] | 81 | view_event = subparsers.add_parser( |
| 82 | 'view', help='View all data for an individual event') |
| 83 | view_event.add_argument('event', help='The event to view') |
| 84 | view_event.set_defaults(func=do_view_event) |
Patrick Williams | 7a09b67 | 2017-03-27 15:39:12 -0500 | [diff] [blame] | 85 | get_esel = subparsers.add_parser( |
| 86 | 'get-esel', help='Extract OpenPOWER eSEL data for an individual event') |
| 87 | get_esel.add_argument('event', help='The event to get eSEL from') |
Patrick Williams | 6f5c3ea | 2017-03-27 16:23:52 -0500 | [diff] [blame] | 88 | get_esel.add_argument('--binary', help='Print event in raw binary', |
| 89 | action='store_const', const=True) |
Patrick Williams | 7a09b67 | 2017-03-27 15:39:12 -0500 | [diff] [blame] | 90 | get_esel.set_defaults(func=do_get_esel) |
Patrick Williams | 9b05ea3 | 2017-03-27 15:22:31 -0500 | [diff] [blame] | 91 | |
| 92 | args = parser.parse_args() |
| 93 | |
| 94 | if 'func' in args: |
| 95 | args.func(args) |
| 96 | else: |
| 97 | parser.print_help() |