Andrew Geissler | 000b50f | 2017-09-26 08:35:53 -0500 | [diff] [blame] | 1 | #!/bin/env python |
| 2 | |
| 3 | import argparse |
| 4 | import requests |
| 5 | import json |
| 6 | |
| 7 | import urllib3 |
| 8 | urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) |
| 9 | |
| 10 | |
| 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', |
| 19 | json={'data': ['root', '0penBmc']}, |
| 20 | verify=False) |
| 21 | j = r.json() |
| 22 | if j['status'] != 'ok': |
| 23 | raise Exception("Failed to login: \n" + r.text) |
| 24 | |
| 25 | def list_all(self): |
| 26 | r = self.session.get(self.url + 'xyz/openbmc_project/sensors/enumerate', |
| 27 | verify=False) |
| 28 | j = r.json() |
| 29 | if j['status'] != 'ok': |
| 30 | raise Exception("Failed to query sensors: \n" + r.text) |
| 31 | |
| 32 | sensors = j['data'] |
Andrew Geissler | f262229 | 2017-11-09 13:47:12 -0600 | [diff] [blame^] | 33 | sensors = sorted(sensors,key=lambda x: x.split("/")[-1]) |
Andrew Geissler | 000b50f | 2017-09-26 08:35:53 -0500 | [diff] [blame] | 34 | |
| 35 | return sensors |
| 36 | |
| 37 | def get_sensor(self, sensor): |
| 38 | r = self.session.get(self.url + sensor, verify=False) |
| 39 | |
| 40 | j = r.json() |
| 41 | if j['status'] != 'ok': |
| 42 | raise Exception("Failed to get sensor " + sensor + ": \n" + r.text) |
| 43 | |
Andrew Geissler | f262229 | 2017-11-09 13:47:12 -0600 | [diff] [blame^] | 44 | return j['data'] |
Andrew Geissler | 000b50f | 2017-09-26 08:35:53 -0500 | [diff] [blame] | 45 | |
| 46 | def do_list_all(args): |
| 47 | s = BMC(server=args.server) |
| 48 | for e in s.list_all(): |
| 49 | print(e) |
| 50 | |
| 51 | def do_view_sensor(args): |
| 52 | s = BMC(server=args.server) |
| 53 | print(s.get_sensor(args.sensor)) |
| 54 | print json.dumps(s.get_sensor(args.sensor), indent=4) |
| 55 | |
| 56 | parser = argparse.ArgumentParser() |
| 57 | parser.add_argument('--server', help='hostname or IP of BMC', type=str, |
| 58 | required=True) |
| 59 | |
| 60 | subparsers = parser.add_subparsers() |
| 61 | |
| 62 | list_all_sensors = subparsers.add_parser('list', help='List all sensors on BMC') |
| 63 | list_all_sensors.set_defaults(func=do_list_all) |
| 64 | |
| 65 | view_sensor = subparsers.add_parser( |
| 66 | 'view', help='View all data for an individual sensor') |
| 67 | view_sensor.add_argument('sensor', help='The sensor to view') |
| 68 | view_sensor.set_defaults(func=do_view_sensor) |
| 69 | |
| 70 | args = parser.parse_args() |
| 71 | |
| 72 | if 'func' in args: |
| 73 | args.func(args) |
| 74 | else: |
| 75 | parser.print_help() |