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 | |
Andrew Geissler | 39ee559 | 2017-11-09 16:05:48 -0600 | [diff] [blame] | 37 | def occ_all(self): |
| 38 | r = self.session.get(self.url + 'org/open_power/control/enumerate', |
| 39 | verify=False) |
| 40 | j = r.json() |
| 41 | if j['status'] != 'ok': |
| 42 | raise Exception("Failed to query occ sensors: \n" + r.text) |
| 43 | |
| 44 | sensors = j['data'] |
| 45 | #sensors = sorted(sensors,key=lambda x: x.split("/")[-1]) |
| 46 | |
| 47 | return sensors |
| 48 | |
Andrew Geissler | 000b50f | 2017-09-26 08:35:53 -0500 | [diff] [blame] | 49 | def get_sensor(self, sensor): |
| 50 | r = self.session.get(self.url + sensor, verify=False) |
| 51 | |
| 52 | j = r.json() |
| 53 | if j['status'] != 'ok': |
| 54 | raise Exception("Failed to get sensor " + sensor + ": \n" + r.text) |
| 55 | |
Andrew Geissler | f262229 | 2017-11-09 13:47:12 -0600 | [diff] [blame] | 56 | return j['data'] |
Andrew Geissler | 000b50f | 2017-09-26 08:35:53 -0500 | [diff] [blame] | 57 | |
| 58 | def do_list_all(args): |
| 59 | s = BMC(server=args.server) |
| 60 | for e in s.list_all(): |
Andrew Geissler | 058fd3d | 2017-11-09 14:09:40 -0600 | [diff] [blame] | 61 | if (args.value): |
| 62 | value = s.get_sensor(e) |
| 63 | print(e + ' : '+ str(value["Value"])) |
| 64 | else: |
| 65 | print(e) |
Andrew Geissler | 000b50f | 2017-09-26 08:35:53 -0500 | [diff] [blame] | 66 | |
Andrew Geissler | 39ee559 | 2017-11-09 16:05:48 -0600 | [diff] [blame] | 67 | def do_occ_all(args): |
| 68 | s = BMC(server=args.server) |
| 69 | print json.dumps(s.occ_all(), indent=4) |
| 70 | |
| 71 | |
Andrew Geissler | 000b50f | 2017-09-26 08:35:53 -0500 | [diff] [blame] | 72 | def do_view_sensor(args): |
| 73 | s = BMC(server=args.server) |
| 74 | print(s.get_sensor(args.sensor)) |
| 75 | print json.dumps(s.get_sensor(args.sensor), indent=4) |
| 76 | |
| 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 | |
| 83 | list_all_sensors = subparsers.add_parser('list', help='List all sensors on BMC') |
| 84 | list_all_sensors.set_defaults(func=do_list_all) |
Andrew Geissler | 058fd3d | 2017-11-09 14:09:40 -0600 | [diff] [blame] | 85 | list_all_sensors.add_argument( |
| 86 | '--value', |
| 87 | action='store_true', |
| 88 | default=False, |
| 89 | help='Provide current value of sensor') |
Andrew Geissler | 000b50f | 2017-09-26 08:35:53 -0500 | [diff] [blame] | 90 | |
| 91 | view_sensor = subparsers.add_parser( |
| 92 | 'view', help='View all data for an individual sensor') |
| 93 | view_sensor.add_argument('sensor', help='The sensor to view') |
| 94 | view_sensor.set_defaults(func=do_view_sensor) |
| 95 | |
Andrew Geissler | 39ee559 | 2017-11-09 16:05:48 -0600 | [diff] [blame] | 96 | # occ has some 'special' sensors not in the sensor namespace |
| 97 | occ_sensors = subparsers.add_parser('occ', help='List the special occ sensors') |
| 98 | occ_sensors.set_defaults(func=do_occ_all) |
| 99 | |
Andrew Geissler | 000b50f | 2017-09-26 08:35:53 -0500 | [diff] [blame] | 100 | args = parser.parse_args() |
| 101 | |
| 102 | if 'func' in args: |
| 103 | args.func(args) |
| 104 | else: |
| 105 | parser.print_help() |