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 | |
Patrick Williams | b19c34c | 2017-03-27 14:52:28 -0500 | [diff] [blame] | 52 | |
Patrick Williams | 9b05ea3 | 2017-03-27 15:22:31 -0500 | [diff] [blame] | 53 | def do_list_events(args): |
| 54 | s = BMC(server=args.server) |
| 55 | for e in s.list_events(): |
| 56 | print(e) |
| 57 | |
Patrick Williams | 5e98b3a | 2017-03-27 15:28:35 -0500 | [diff] [blame] | 58 | |
| 59 | def do_view_event(args): |
| 60 | s = BMC(server=args.server) |
| 61 | print(s.get_event(args.event)) |
Andrew Geissler | d35538c | 2017-08-25 08:43:08 -0500 | [diff] [blame^] | 62 | print json.dumps(s.get_event(args.event), indent=4) |
Patrick Williams | 5e98b3a | 2017-03-27 15:28:35 -0500 | [diff] [blame] | 63 | |
Andrew Geissler | 50a753c | 2017-08-19 09:00:16 -0500 | [diff] [blame] | 64 | def do_clear_event(args): |
| 65 | s = BMC(server=args.server) |
| 66 | s.clear_event(args.event) |
Patrick Williams | 4296f4b | 2017-03-27 16:26:41 -0500 | [diff] [blame] | 67 | |
Patrick Williams | 7a09b67 | 2017-03-27 15:39:12 -0500 | [diff] [blame] | 68 | def do_get_esel(args): |
| 69 | s = BMC(server=args.server) |
| 70 | e = s.get_event(args.event) |
| 71 | if e['Message'] != 'org.open_power.Error.Host.Event' and\ |
| 72 | e['Message'] != 'org.open_power.Error.Host.Event.Event': |
| 73 | raise Exception("Event is not from Host: " + e['Message']) |
| 74 | for d in e['AdditionalData']: |
Patrick Williams | 842308b | 2017-03-27 15:49:38 -0500 | [diff] [blame] | 75 | data = d.split("=") |
| 76 | tag = data.pop(0) |
Patrick Williams | 7a09b67 | 2017-03-27 15:39:12 -0500 | [diff] [blame] | 77 | if tag != 'ESEL': |
| 78 | continue |
| 79 | data = "=".join(data) |
Patrick Williams | 6f5c3ea | 2017-03-27 16:23:52 -0500 | [diff] [blame] | 80 | if args.binary: |
| 81 | data = data.split(" ") |
| 82 | if '' == data[-1]: |
| 83 | data.pop() |
| 84 | data = "".join(map(lambda x: chr(int(x, 16)), data)) |
Patrick Williams | 7a09b67 | 2017-03-27 15:39:12 -0500 | [diff] [blame] | 85 | print(data) |
| 86 | |
Patrick Williams | 5e98b3a | 2017-03-27 15:28:35 -0500 | [diff] [blame] | 87 | |
Patrick Williams | 9b05ea3 | 2017-03-27 15:22:31 -0500 | [diff] [blame] | 88 | parser = argparse.ArgumentParser() |
| 89 | parser.add_argument('--server', help='hostname or IP of BMC', type=str, |
| 90 | required=True) |
| 91 | |
| 92 | subparsers = parser.add_subparsers() |
Andrew Geissler | 50a753c | 2017-08-19 09:00:16 -0500 | [diff] [blame] | 93 | |
Patrick Williams | 9b05ea3 | 2017-03-27 15:22:31 -0500 | [diff] [blame] | 94 | list_events = subparsers.add_parser('list', help='List all events on BMC') |
| 95 | list_events.set_defaults(func=do_list_events) |
Andrew Geissler | 50a753c | 2017-08-19 09:00:16 -0500 | [diff] [blame] | 96 | |
Patrick Williams | 5e98b3a | 2017-03-27 15:28:35 -0500 | [diff] [blame] | 97 | view_event = subparsers.add_parser( |
| 98 | 'view', help='View all data for an individual event') |
| 99 | view_event.add_argument('event', help='The event to view') |
| 100 | view_event.set_defaults(func=do_view_event) |
Andrew Geissler | 50a753c | 2017-08-19 09:00:16 -0500 | [diff] [blame] | 101 | |
Patrick Williams | 7a09b67 | 2017-03-27 15:39:12 -0500 | [diff] [blame] | 102 | get_esel = subparsers.add_parser( |
| 103 | 'get-esel', help='Extract OpenPOWER eSEL data for an individual event') |
| 104 | get_esel.add_argument('event', help='The event to get eSEL from') |
Patrick Williams | 6f5c3ea | 2017-03-27 16:23:52 -0500 | [diff] [blame] | 105 | get_esel.add_argument('--binary', help='Print event in raw binary', |
| 106 | action='store_const', const=True) |
Patrick Williams | 7a09b67 | 2017-03-27 15:39:12 -0500 | [diff] [blame] | 107 | get_esel.set_defaults(func=do_get_esel) |
Patrick Williams | 9b05ea3 | 2017-03-27 15:22:31 -0500 | [diff] [blame] | 108 | |
Andrew Geissler | 50a753c | 2017-08-19 09:00:16 -0500 | [diff] [blame] | 109 | clear_event = subparsers.add_parser( |
| 110 | 'clear', help="Clear individual event") |
| 111 | clear_event.add_argument('event', help="The event to clear") |
| 112 | clear_event.set_defaults(func=do_clear_event) |
| 113 | |
Patrick Williams | 9b05ea3 | 2017-03-27 15:22:31 -0500 | [diff] [blame] | 114 | args = parser.parse_args() |
| 115 | |
| 116 | if 'func' in args: |
| 117 | args.func(args) |
| 118 | else: |
| 119 | parser.print_help() |