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