-
Notifications
You must be signed in to change notification settings - Fork 92
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sample for enabling/disabling hardware sensors. #71
Open
Dreadth
wants to merge
2
commits into
solarwinds:master
Choose a base branch
from
Dreadth:hardware_sensor_sample
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import orionsdk | ||
|
||
server = 'orion_server' | ||
username = 'username' | ||
password = 'password' | ||
|
||
# The structure of the payload required for the verbs (Enable|Disable)Sensors is not straight-forward. | ||
# Below is what the payload should look like: | ||
|
||
# payload_template = [ | ||
# {'HardwareInfoID': int, 'HardwareCategoryStatusID': int, 'UniqueName': str}, | ||
# ] | ||
# Notice that you can supply multiple sensors in the payload. | ||
|
||
# This script shows how to create a payload to disable/enable every sensor - | ||
# on a particular node by supplying the node's IP address. | ||
# This could be re-written to target specific/individual sensors. | ||
|
||
|
||
def main(): | ||
ip_address = '192.168.1.1' # Change this value to the node you want to target. | ||
payload = collect_sensor_data(ip_address=ip_address) | ||
change_sensor_state(payload, enabled=True) # Change the enabled bool if you want to disable sensors. | ||
|
||
|
||
def sensor_payload(hardware_info_id: int, hardware_category_status_id: int, unique_name: str): | ||
data = { | ||
'HardwareInfoID': hardware_info_id, | ||
'HardwareCategoryStatusID': hardware_category_status_id, | ||
'UniqueName': unique_name | ||
} | ||
return data | ||
|
||
|
||
def change_sensor_state(payload, enabled=True): | ||
# Defaults to the sensor being enabled unless enabled flag is set to False. | ||
if enabled: | ||
verb = 'EnableSensors' | ||
else: | ||
verb = 'DisableSensors' | ||
client = orionsdk.SwisClient(server, username, password) | ||
client.invoke('Orion.HardwareHealth.HardwareItemBase', verb, payload) | ||
|
||
|
||
def collect_sensor_data(ip_address): | ||
|
||
# Hardware Sensors are tied to the node via the Node ID. | ||
# Collect the NodeID by supplying the IP address of the node. | ||
# This query assumes that we will only get one result back (targeting index 0 of the results) | ||
# You could target another index value or restructure this code if you need to accommodate multiple results. | ||
client = orionsdk.SwisClient(server, username, password) | ||
node_id = client.query( | ||
"SELECT NodeID, IPAddress " | ||
"FROM Orion.Nodes " | ||
f"WHERE IPAddress = '{ip_address}' " | ||
)['results'][0]['NodeID'] | ||
|
||
# Collect all the Hardware Sensors on that Node. | ||
hardware_sensors = client.query( | ||
"SELECT HardwareInfoID, HardwareCategoryStatusID, UniqueName " | ||
"FROM Orion.HardwareHealth.HardwareItem " | ||
f"WHERE HardwareInfoID = '{node_id}' " | ||
)['results'] | ||
|
||
sensor_list = [] | ||
# Loop through the results and add every sensor to a list to generate the payload | ||
for sensor in hardware_sensors: | ||
hardware_info_id = sensor['HardwareInfoID'] | ||
hardware_category_status_id = sensor['HardwareCategoryStatusID'] | ||
unique_name = sensor['UniqueName'] | ||
sensor_list.append(sensor_payload(hardware_info_id, hardware_category_status_id, unique_name)) | ||
|
||
return sensor_list | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be useful to add a comment to explain that this sample assumes this query will return exactly one matching record.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good Suggestion. Added a comment to clarify.