jeisenbath / ansible-collection-solarwinds-orion

An Ansible collection for managing nodes in Solarwinds Orion
11 stars 3 forks source link

Hardware health monitoring #19

Closed Andyjb8 closed 1 hour ago

Andyjb8 commented 5 months ago

The orion_node_poller_info in your recent update works. However hardware health poller isn't showing up for some reason.

NodeID=1194"
    },
    "pollers": [
        {
            "Enabled": false,
            "PollerType": "N.Status.SNMP.Native"
        },
        {
            "Enabled": false,
            "PollerType": "N.ResponseTime.SNMP.Native"
        },
        {
            "Enabled": true,
            "PollerType": "N.Status.ICMP.Native"
        },
        {
            "Enabled": true,
            "PollerType": "N.ResponseTime.ICMP.Native"
        },
        {
            "Enabled": true,
            "PollerType": "N.Details.SNMP.Generic"
        },
        {
            "Enabled": true,
            "PollerType": "N.Uptime.SNMP.Generic"
        },
        {
            "Enabled": true,
            "PollerType": "N.Routing.SNMP.Ipv4CidrRoutingTable"
        },
        {
            "Enabled": true,
            "PollerType": "N.Topology_Layer3.SNMP.ipNetToMedia"
        },
        {
            "Enabled": true,
            "PollerType": "N.Memory.SNMP.NetSnmpReal"
        },
        {
            "Enabled": true,
            "PollerType": "N.CPU.SNMP.NetSnmpReal"
        }
    ]
}

This issue is one I really want to figure out as we need the ability to add hardware health monitoring with our automation of node monitoring. I mentioned this in issue 12 but I thought I would just open a new issue specific to this.

image

I found these, but I wasn't sure how to make it work. https://thwack.solarwinds.com/products/the-orion-platform/f/orion-sdk/43027/adding-hardware-health-details/273716 https://github.com/solarwinds/OrionSDK/wiki/Hardware-Health

jeisenbath commented 5 months ago

Looks like HardwareHealth is it's own entity, not a poller, from those posts. This would need to be a new module then, so I'll mark this as an enhancement request.

Andyjb8 commented 5 months ago

I do not know python that well, but I was able to create a module with chatgpt and I was successfully able to enable the hardware health sensor.

#!/usr/bin/python
# -*- coding: utf-8 -*-

from ansible.module_utils.basic import AnsibleModule
try:
    from orionsdk import SwisClient
    HAS_ORIONSDK = True
except ImportError:
    HAS_ORIONSDK = False

DOCUMENTATION = r'''
---
module: orion_enable_hardware_health
short_description: Enable hardware health polling on a node in Solarwinds Orion
description:
    - This module enables hardware health polling on a node in Solarwinds Orion.
author: "Your Name (@Andyjb8)"
requirements:
    - orionsdk
options:
    hostname:
        description:
            - The Orion server hostname.
        required: True
        type: str
    username:
        description:
            - The Orion username.
        required: True
        type: str
    password:
        description:
            - The Orion password.
        required: True
        type: str
    node_name: 
        description:
            - The Caption  in Orion.
        required: True if node_id not specified
        type: str
    node_id: 
        description:
            - The node_id in Orion.
        required: True if node_name not specified
        type: str
    polling_method:
        description:
            - The polling method to be used for hardware health.
        required: True
        type: int
'''

EXAMPLES = r'''
---
- name: Enable hardware health polling on Cisco node
  orion_enable_hardware_health:
    hostname: "server"
    username: "admin"
    password: "pass"
    node_name: "{{ inventory_hostname }}"
    polling_method: 9  # SNMP Cisco as an example
  delegate_to: localhost
  when: "juniper' in group_names"

- name: Enable hardware health polling on Juniper node
  orion_enable_hardware_health:
    hostname: "server"
    username: "admin"
    password: "pass"
    node_name: "{{ inventory_hostname }}"
    polling_method: 10  # SNMP Juniper as an example
  delegate_to: localhost
  when: "cisco' in group_names"
'''

RETURN = r'''
# Default return values
'''

def main():
    module_args = dict(
        hostname=dict(type='str', required=True),
        username=dict(type='str', required=True),
        password=dict(type='str', required=True, no_log=True),
        node_name=dict(type='str', required=False),  # Now accept node_name
        node_id=dict(type='str', required=False),  # Make node_id optional
        polling_method=dict(type='int', required=True),
    )

    module = AnsibleModule(
        argument_spec=module_args,
        required_one_of=[('node_name', 'node_id')],  # Require at least one
        supports_check_mode=True
    )

    if not HAS_ORIONSDK:
        module.fail_json(msg="The orionsdk module is required")

    hostname = module.params['hostname']
    username = module.params['username']
    password = module.params['password']
    node_name = module.params['node_name']
    node_id = module.params['node_id']
    polling_method = module.params['polling_method']

    swis = SwisClient(hostname, username, password)

    # Resolve node_name to node_id if node_name is provided
    if node_name:
        try:
            results = swis.query(f"SELECT NodeID FROM Orion.Nodes WHERE Caption='{node_name}'")
            node_id = "N:" + str(results['results'][0]['NodeID'])
        except Exception as e:
            module.fail_json(msg=f"Failed to resolve node name to ID: {e}")

    try:
        swis.invoke('Orion.HardwareHealth.HardwareInfoBase', 'EnableHardwareHealth', node_id, polling_method)
        module.exit_json(changed=True)
    except Exception as e:
        module.fail_json(msg=str(e))

if __name__ == '__main__':
    main()
jeisenbath commented 5 months ago

Cool, for practicality sake, probably would need to map the 'polling method' params to the set list of options, so you can pass the options as plain text instead of their IDs. The important bit there is the swis,invoke() params.

Any new modules/changes are basically frozen until I finish testing and merge the updates to work with orionsdk 0.4.0, I'll look at this as a new module next then on the roadmap.

jeisenbath commented 3 months ago

Getting back to this, do you want to take a crack at a PR for this module for the next release, or have me do it? If I do it, could I also get you to validate an "absent" state call (guessing if its possible there's a verb like 'DisableHardwareHealth')

jeisenbath commented 1 hour ago

Released collection 2.1.0 #41 with the new module we put together, thanks!