blob: d138b3381b8e0c1adaa1757297e881e71533a09c [file] [log] [blame]
Andrew Geissler000b50f2017-09-26 08:35:53 -05001#!/bin/env python
2
3import argparse
4import requests
5import json
6
7import urllib3
8urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
9
10
11class 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 Geisslerf2622292017-11-09 13:47:12 -060033 sensors = sorted(sensors,key=lambda x: x.split("/")[-1])
Andrew Geissler000b50f2017-09-26 08:35:53 -050034
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 Geisslerf2622292017-11-09 13:47:12 -060044 return j['data']
Andrew Geissler000b50f2017-09-26 08:35:53 -050045
46def do_list_all(args):
47 s = BMC(server=args.server)
48 for e in s.list_all():
49 print(e)
50
51def 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
56parser = argparse.ArgumentParser()
57parser.add_argument('--server', help='hostname or IP of BMC', type=str,
58 required=True)
59
60subparsers = parser.add_subparsers()
61
62list_all_sensors = subparsers.add_parser('list', help='List all sensors on BMC')
63list_all_sensors.set_defaults(func=do_list_all)
64
65view_sensor = subparsers.add_parser(
66 'view', help='View all data for an individual sensor')
67view_sensor.add_argument('sensor', help='The sensor to view')
68view_sensor.set_defaults(func=do_view_sensor)
69
70args = parser.parse_args()
71
72if 'func' in args:
73 args.func(args)
74else:
75 parser.print_help()