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 | |
| 6 | class BMC: |
| 7 | def __init__(self, server): |
| 8 | self.url = "https://{0}/".format(server) |
| 9 | self.session = requests.Session() |
| 10 | self.login() |
| 11 | |
| 12 | def login(self): |
| 13 | r = self.session.post(self.url + 'login', |
| 14 | json={ 'data': [ 'root', '0penBmc']}, |
| 15 | verify=False) |
| 16 | j = r.json() |
| 17 | if j['status'] != 'ok': |
| 18 | raise Exception("Failed to login: \n" + r.text) |
| 19 | |
Patrick Williams | ec472d8 | 2017-03-27 15:15:43 -0500 | [diff] [blame] | 20 | def list_events(self): |
| 21 | r = self.session.get(self.url + 'xyz/openbmc_project/logging/entry/', |
| 22 | verify=False) |
| 23 | j = r.json() |
| 24 | if j['status'] != 'ok': |
| 25 | raise Exception("Failed to query entries: \n" + r.text) |
| 26 | |
| 27 | events = j['data'] |
| 28 | events.sort(key=lambda x: int(x.split("/")[-1])) |
| 29 | |
| 30 | return events |
Patrick Williams | b19c34c | 2017-03-27 14:52:28 -0500 | [diff] [blame] | 31 | |
Patrick Williams | 5e98b3a | 2017-03-27 15:28:35 -0500 | [diff] [blame^] | 32 | def get_event(self, event): |
| 33 | r = self.session.get(self.url + event, verify=False) |
| 34 | |
| 35 | j = r.json() |
| 36 | if j['status'] != 'ok': |
| 37 | raise Exception("Failed to get event " + event + ": \n" + r.text) |
| 38 | |
| 39 | return j['data'] |
| 40 | |
Patrick Williams | b19c34c | 2017-03-27 14:52:28 -0500 | [diff] [blame] | 41 | |
Patrick Williams | 9b05ea3 | 2017-03-27 15:22:31 -0500 | [diff] [blame] | 42 | def do_list_events(args): |
| 43 | s = BMC(server=args.server) |
| 44 | for e in s.list_events(): |
| 45 | print(e) |
| 46 | |
Patrick Williams | 5e98b3a | 2017-03-27 15:28:35 -0500 | [diff] [blame^] | 47 | |
| 48 | def do_view_event(args): |
| 49 | s = BMC(server=args.server) |
| 50 | print(s.get_event(args.event)) |
| 51 | |
| 52 | |
Patrick Williams | 9b05ea3 | 2017-03-27 15:22:31 -0500 | [diff] [blame] | 53 | parser = argparse.ArgumentParser() |
| 54 | parser.add_argument('--server', help='hostname or IP of BMC', type=str, |
| 55 | required=True) |
| 56 | |
| 57 | subparsers = parser.add_subparsers() |
| 58 | list_events = subparsers.add_parser('list', help='List all events on BMC') |
| 59 | list_events.set_defaults(func=do_list_events) |
Patrick Williams | 5e98b3a | 2017-03-27 15:28:35 -0500 | [diff] [blame^] | 60 | view_event = subparsers.add_parser( |
| 61 | 'view', help='View all data for an individual event') |
| 62 | view_event.add_argument('event', help='The event to view') |
| 63 | view_event.set_defaults(func=do_view_event) |
Patrick Williams | 9b05ea3 | 2017-03-27 15:22:31 -0500 | [diff] [blame] | 64 | |
| 65 | args = parser.parse_args() |
| 66 | |
| 67 | if 'func' in args: |
| 68 | args.func(args) |
| 69 | else: |
| 70 | parser.print_help() |