blob: c54dc976a3c176a88072a006e2b13e38f6dc1a71 [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']
33 #sensors.sort(key=lambda x: int(x.split("/")[-1]))
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
44 return j['data']
45
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()