blob: 4b6aa92ba701b98a21adb88292b5147a1c906e9e [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
Andrew Geissler39ee5592017-11-09 16:05:48 -060037 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 Geissler000b50f2017-09-26 08:35:53 -050049 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 Geisslerf2622292017-11-09 13:47:12 -060056 return j['data']
Andrew Geissler000b50f2017-09-26 08:35:53 -050057
58def do_list_all(args):
59 s = BMC(server=args.server)
60 for e in s.list_all():
Andrew Geissler058fd3d2017-11-09 14:09:40 -060061 if (args.value):
62 value = s.get_sensor(e)
63 print(e + ' : '+ str(value["Value"]))
64 else:
65 print(e)
Andrew Geissler000b50f2017-09-26 08:35:53 -050066
Andrew Geissler39ee5592017-11-09 16:05:48 -060067def do_occ_all(args):
68 s = BMC(server=args.server)
69 print json.dumps(s.occ_all(), indent=4)
70
71
Andrew Geissler000b50f2017-09-26 08:35:53 -050072def 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
77parser = argparse.ArgumentParser()
78parser.add_argument('--server', help='hostname or IP of BMC', type=str,
79 required=True)
80
81subparsers = parser.add_subparsers()
82
83list_all_sensors = subparsers.add_parser('list', help='List all sensors on BMC')
84list_all_sensors.set_defaults(func=do_list_all)
Andrew Geissler058fd3d2017-11-09 14:09:40 -060085list_all_sensors.add_argument(
86 '--value',
87 action='store_true',
88 default=False,
89 help='Provide current value of sensor')
Andrew Geissler000b50f2017-09-26 08:35:53 -050090
91view_sensor = subparsers.add_parser(
92 'view', help='View all data for an individual sensor')
93view_sensor.add_argument('sensor', help='The sensor to view')
94view_sensor.set_defaults(func=do_view_sensor)
95
Andrew Geissler39ee5592017-11-09 16:05:48 -060096# occ has some 'special' sensors not in the sensor namespace
97occ_sensors = subparsers.add_parser('occ', help='List the special occ sensors')
98occ_sensors.set_defaults(func=do_occ_all)
99
Andrew Geissler000b50f2017-09-26 08:35:53 -0500100args = parser.parse_args()
101
102if 'func' in args:
103 args.func(args)
104else:
105 parser.print_help()