Skip to content

Commit 849a1e4

Browse files
caberoscaberos
authored andcommitted
Add sensor data to hardware
1 parent f6a8d6c commit 849a1e4

7 files changed

Lines changed: 122 additions & 4 deletions

File tree

SoftLayer/CLI/hardware/sensor.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
"""Hardware internal Sensor ."""
2+
# :license: MIT, see LICENSE for more details.
3+
4+
import SoftLayer
5+
from SoftLayer.CLI import click
6+
from SoftLayer.CLI import environment
7+
from SoftLayer.CLI import formatting
8+
9+
10+
@click.command()
11+
@click.argument('identifier')
12+
@click.option('--discrete', is_flag=True, default=False, help='Show discrete units associated hardware sensor')
13+
@environment.pass_env
14+
def cli(env, identifier, discrete):
15+
"""Retrieve a server’s hardware state via its internal sensors."""
16+
mgr = SoftLayer.HardwareManager(env.client)
17+
sensors = mgr.get_sensors(identifier)
18+
19+
temperature_table = formatting.Table(["sensor", "status", "Reading", "min", "Max"],
20+
title='temperature Degree c')
21+
22+
volts_table = formatting.Table(["sensor", "status", "Reading", "min", "Max"],
23+
title='volts')
24+
25+
watts_table = formatting.Table(["sensor", "status", "Reading"],
26+
title='Watts')
27+
28+
rpm_table = formatting.Table(["sensor", "status", "Reading", "min"],
29+
title='RPM')
30+
31+
discrete_table = formatting.Table(["sensor", "status", "Reading"],
32+
title='discrete')
33+
34+
for sensor in sensors:
35+
if sensor.get('sensorUnits') == 'degrees C':
36+
temperature_table.add_row([sensor.get('sensorId'),
37+
sensor.get('status'),
38+
sensor.get('sensorReading'),
39+
sensor.get('upperNonCritical'),
40+
sensor.get('upperCritical')])
41+
42+
if sensor.get('sensorUnits') == 'volts':
43+
volts_table.add_row([sensor.get('sensorId'),
44+
sensor.get('status'),
45+
sensor.get('sensorReading'),
46+
sensor.get('lowerNonCritical'),
47+
sensor.get('lowerCritical')])
48+
49+
if sensor.get('sensorUnits') == 'Watts':
50+
watts_table.add_row([sensor.get('sensorId'),
51+
sensor.get('status'),
52+
sensor.get('sensorReading')])
53+
54+
if sensor.get('sensorUnits') == 'RPM':
55+
rpm_table.add_row([sensor.get('sensorId'),
56+
sensor.get('status'),
57+
sensor.get('sensorReading'),
58+
sensor.get('lowerCritical')])
59+
60+
if sensor.get('sensorUnits') == 'discrete':
61+
discrete_table.add_row([sensor.get('sensorId'),
62+
sensor.get('status'),
63+
sensor.get('sensorReading')])
64+
env.fout(temperature_table)
65+
env.fout(rpm_table)
66+
env.fout(volts_table)
67+
env.fout(watts_table)
68+
if discrete:
69+
env.fout(discrete_table)

SoftLayer/CLI/routes.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@
272272
('hardware:dns-sync', 'SoftLayer.CLI.hardware.dns:cli'),
273273
('hardware:storage', 'SoftLayer.CLI.hardware.storage:cli'),
274274
('hardware:upgrade', 'SoftLayer.CLI.hardware.upgrade:cli'),
275+
('hardware:sensor', 'SoftLayer.CLI.hardware.sensor:cli'),
275276

276277
('securitygroup', 'SoftLayer.CLI.securitygroup'),
277278
('securitygroup:list', 'SoftLayer.CLI.securitygroup.list:cli'),

SoftLayer/fixtures/SoftLayer_Hardware.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,31 @@
5959
}
6060

6161
allowAccessToNetworkStorageList = True
62+
63+
getSensorData = [
64+
{
65+
"sensorId": "Ambient 1 Temperature",
66+
"sensorReading": "25.000",
67+
"sensorUnits": "degrees C",
68+
"status": "ok",
69+
"upperCritical": "43.000",
70+
"upperNonCritical": "41.000",
71+
"upperNonRecoverable": "46.000"
72+
},
73+
{
74+
"lowerCritical": "3500.000",
75+
"sensorId": "Fan 1 Tach",
76+
"sensorReading": "6580.000",
77+
"sensorUnits": "RPM",
78+
"status": "ok"
79+
}, {
80+
"sensorId": "IPMI Watchdog",
81+
"sensorReading": "0x0",
82+
"sensorUnits": "discrete",
83+
"status": "0x0080"
84+
}, {
85+
"sensorId": "Avg Power",
86+
"sensorReading": "70.000",
87+
"sensorUnits": "Watts",
88+
"status": "ok"
89+
}]

SoftLayer/managers/hardware.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,6 +1098,10 @@ def get_components(self, hardware_id, mask=None, filter_component=None):
10981098
return self.client.call('Hardware_Server', 'getComponents',
10991099
mask=mask, filter=filter_component, id=hardware_id)
11001100

1101+
def get_sensors(self, hardware_id):
1102+
"""Returns Hardware sensor data"""
1103+
return self.client.call('Hardware', 'getSensorData', id=hardware_id)
1104+
11011105

11021106
def _get_bandwidth_key(items, hourly=True, no_public=False, location=None):
11031107
"""Picks a valid Bandwidth Item, returns the KeyName"""

docs/cli/hardware.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,7 @@ This function updates the firmware of a server. If already at the latest version
127127
.. click:: SoftLayer.CLI.hardware.upgrade:cli
128128
:prog: hardware upgrade
129129
:show-nested:
130+
131+
.. click:: SoftLayer.CLI.hardware.sensor:cli
132+
:prog: hardware sensor
133+
:show-nested:

tests/CLI/modules/server_tests.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -996,3 +996,11 @@ def test_upgrade(self, confirm_mock):
996996
def test_components(self):
997997
result = self.run_command(['hardware', 'detail', '100', '--components'])
998998
self.assert_no_fail(result)
999+
1000+
def test_sensor(self):
1001+
result = self.run_command(['hardware', 'sensor', '100'])
1002+
self.assert_no_fail(result)
1003+
1004+
def test_sensor_discrete(self):
1005+
result = self.run_command(['hardware', 'sensor', '100', '--discrete'])
1006+
self.assert_no_fail(result)

tests/managers/hardware_tests.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -557,10 +557,10 @@ def test_edit(self):
557557
self.assert_called_with('SoftLayer_Hardware_Server',
558558
'editObject',
559559
args=({
560-
'hostname': 'new-host',
561-
'domain': 'new.sftlyr.ws',
562-
'notes': 'random notes',
563-
},),
560+
'hostname': 'new-host',
561+
'domain': 'new.sftlyr.ws',
562+
'notes': 'random notes',
563+
},),
564564
identifier=100)
565565

566566
def test_rescue(self):
@@ -948,6 +948,10 @@ def test_get_components(self):
948948
self.assertEqual(result[0]['hardwareComponentModel']['name'], 'IMM2 - Onboard')
949949
self.assertEqual(result[0]['hardwareComponentModel']['firmwares'][0]['version'], '5.60')
950950

951+
def test_sensor(self):
952+
self.hardware.get_sensors(100)
953+
self.assert_called_with('SoftLayer_Hardware', 'getSensorData')
954+
951955

952956
class HardwareHelperTests(testing.TestCase):
953957

0 commit comments

Comments
 (0)