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 | |
| 53 | |
Patrick Williams | 9b05ea3 | 2017-03-27 15:22:31 -0500 | [diff] [blame] | 54 | parser = argparse.ArgumentParser() |
| 55 | parser.add_argument('--server', help='hostname or IP of BMC', type=str, |
| 56 | required=True) |
| 57 | |
| 58 | subparsers = parser.add_subparsers() |
| 59 | list_events = subparsers.add_parser('list', help='List all events on BMC') |
| 60 | list_events.set_defaults(func=do_list_events) |
Patrick Williams | 5e98b3a | 2017-03-27 15:28:35 -0500 | [diff] [blame] | 61 | view_event = subparsers.add_parser( |
| 62 | 'view', help='View all data for an individual event') |
| 63 | view_event.add_argument('event', help='The event to view') |
| 64 | view_event.set_defaults(func=do_view_event) |
Patrick Williams | 9b05ea3 | 2017-03-27 15:22:31 -0500 | [diff] [blame] | 65 | |
| 66 | args = parser.parse_args() |
| 67 | |
| 68 | if 'func' in args: |
| 69 | args.func(args) |
| 70 | else: |
| 71 | parser.print_help() |