| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2018, Christian Kotte <[email protected]> |
| 5 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 7 |
|
| 8 |
from __future__ import absolute_import, division, print_function |
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
|
| 12 |
DOCUMENTATION = r''' |
| 13 |
--- |
| 14 |
module: vmware_host_snmp |
| 15 |
short_description: Configures SNMP on an ESXi host system |
| 16 |
description: |
| 17 |
- This module can be used to configure the embedded SNMP agent on an ESXi host. |
| 18 |
author: |
| 19 |
- Christian Kotte (@ckotte) |
| 20 |
- Alexander Nikitin (@ihumster) |
| 21 |
notes: |
| 22 |
- You need to reset the agent (to factory defaults) if you want to clear all community strings, trap targets, or filters |
| 23 |
- SNMP v3 configuration isn't implemented yet |
| 24 |
options: |
| 25 |
cluster_name: |
| 26 |
description: |
| 27 |
- Name of cluster. |
| 28 |
- All host system from given cluster used to manage SNMP agent. |
| 29 |
- Required parameter, if O(esxi_hostname) is not set. |
| 30 |
type: str |
| 31 |
version_added: '3.11.0' |
| 32 |
esxi_hostname: |
| 33 |
description: |
| 34 |
- List of ESXi hostname to manage SNMP agent. |
| 35 |
- Required parameter, if O(cluster_name) is not set. |
| 36 |
type: list |
| 37 |
elements: str |
| 38 |
version_added: '3.11.0' |
| 39 |
state: |
| 40 |
description: |
| 41 |
- Enable, disable, or reset the SNMP agent. |
| 42 |
type: str |
| 43 |
choices: [ disabled, enabled, reset ] |
| 44 |
default: disabled |
| 45 |
community: |
| 46 |
description: |
| 47 |
- List of SNMP community strings. |
| 48 |
type: list |
| 49 |
default: [] |
| 50 |
elements: str |
| 51 |
snmp_port: |
| 52 |
description: |
| 53 |
- Port used by the SNMP agent. |
| 54 |
type: int |
| 55 |
default: 161 |
| 56 |
trap_targets: |
| 57 |
description: |
| 58 |
- A list of trap targets. |
| 59 |
- You need to use C(hostname), C(port), and C(community) for each trap target. |
| 60 |
default: [] |
| 61 |
type: list |
| 62 |
elements: dict |
| 63 |
trap_filter: |
| 64 |
description: |
| 65 |
- A list of trap oids for traps not to be sent by agent, |
| 66 |
e.g. [ 1.3.6.1.4.1.6876.4.1.1.0, 1.3.6.1.4.1.6876.4.1.1.1 ] |
| 67 |
- Use value V(reset) to clear settings. |
| 68 |
type: list |
| 69 |
elements: str |
| 70 |
send_trap: |
| 71 |
description: |
| 72 |
- Send a test trap to validate the configuration. |
| 73 |
type: bool |
| 74 |
default: false |
| 75 |
hw_source: |
| 76 |
description: |
| 77 |
- Source hardware events from IPMI sensors or CIM Indications. |
| 78 |
- The embedded SNMP agent receives hardware events either from IPMI sensors V(sensors) or CIM indications V(indications). |
| 79 |
type: str |
| 80 |
choices: [ indications, sensors ] |
| 81 |
default: indications |
| 82 |
log_level: |
| 83 |
description: |
| 84 |
- Syslog logging level. |
| 85 |
type: str |
| 86 |
choices: [ debug, info, warning, error ] |
| 87 |
default: info |
| 88 |
sys_contact: |
| 89 |
description: |
| 90 |
- System contact who manages the system. |
| 91 |
type: str |
| 92 |
sys_location: |
| 93 |
description: |
| 94 |
- System location. |
| 95 |
type: str |
| 96 |
extends_documentation_fragment: |
| 97 |
- vmware.vmware.base_options |
| 98 |
|
| 99 |
''' |
| 100 |
|
| 101 |
EXAMPLES = r''' |
| 102 |
- name: Enable and configure SNMP community on standalone ESXi host |
| 103 |
community.vmware.vmware_host_snmp: |
| 104 |
hostname: '{{ esxi_hostname }}' |
| 105 |
username: '{{ esxi_username }}' |
| 106 |
password: '{{ esxi_password }}' |
| 107 |
community: [ test ] |
| 108 |
state: enabled |
| 109 |
delegate_to: localhost |
| 110 |
|
| 111 |
- name: Configure SNMP traps and filters on cluster |
| 112 |
community.vmware.vmware_host_snmp: |
| 113 |
hostname: '{{ vcenter_hostname }}' |
| 114 |
username: '{{ vcenter_username }}' |
| 115 |
password: '{{ vcenter_password }}' |
| 116 |
cluster_name: '{{ cluster_name }}' |
| 117 |
community: [ test ] |
| 118 |
trap_targets: |
| 119 |
- hostname: 192.168.1.100 |
| 120 |
port: 162 |
| 121 |
community: test123 |
| 122 |
- hostname: 192.168.1.101 |
| 123 |
port: 162 |
| 124 |
community: test1234 |
| 125 |
trap_filter: |
| 126 |
- 1.3.6.1.4.1.6876.4.1.1.0 |
| 127 |
- 1.3.6.1.4.1.6876.4.1.1.1 |
| 128 |
state: enabled |
| 129 |
delegate_to: localhost |
| 130 |
|
| 131 |
- name: Enable and configure SNMP system contact and location on simple ESXi host in vCenter |
| 132 |
community.vmware.vmware_host_snmp: |
| 133 |
hostname: '{{ vcenter_hostname }}' |
| 134 |
username: '{{ vcenter_username }}' |
| 135 |
password: '{{ vcenter_password }}' |
| 136 |
esxi_hostname: '{{ esxi_hostname }}' |
| 137 |
sys_contact: "[email protected]" |
| 138 |
sys_location: "Austin, USA" |
| 139 |
state: enabled |
| 140 |
delegate_to: localhost |
| 141 |
|
| 142 |
- name: Disable SNMP on standalone ESXi host |
| 143 |
community.vmware.vmware_host_snmp: |
| 144 |
hostname: '{{ esxi_hostname }}' |
| 145 |
username: '{{ esxi_username }}' |
| 146 |
password: '{{ esxi_password }}' |
| 147 |
state: disabled |
| 148 |
delegate_to: localhost |
| 149 |
''' |
| 150 |
|
| 151 |
RETURN = r''' |
| 152 |
results: |
| 153 |
description: metadata about host system's SNMP configuration |
| 154 |
returned: always |
| 155 |
type: dict |
| 156 |
sample: { |
| 157 |
"changed": true, |
| 158 |
"esx01.example.local": { |
| 159 |
"changed": true, |
| 160 |
"community": [ |
| 161 |
"test" |
| 162 |
], |
| 163 |
"community_previous": [], |
| 164 |
"hw_source": "indications", |
| 165 |
"log_level": "info", |
| 166 |
"log_level_previous": "warning", |
| 167 |
"msg": "SNMP state, community list, log level, sys contact, and sys location changed", |
| 168 |
"port": 161, |
| 169 |
"state": "enabled", |
| 170 |
"state_previous": "disabled", |
| 171 |
"sys_contact_previous": "", |
| 172 |
"sys_location_previous": "", |
| 173 |
"trap_filter": null, |
| 174 |
"trap_targets": [] |
| 175 |
}, |
| 176 |
"failed": false |
| 177 |
} |
| 178 |
''' |
| 179 |
|
| 180 |
try: |
| 181 |
from pyVmomi import vim |
| 182 |
except ImportError: |
| 183 |
pass |
| 184 |
|
| 185 |
from ansible.module_utils.basic import AnsibleModule |
| 186 |
from ansible_collections.community.vmware.plugins.module_utils.vmware import PyVmomi, find_obj |
| 187 |
from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec |
| 188 |
from ansible.module_utils.common.text.converters import to_native |
| 189 |
|
| 190 |
|
| 191 |
class VmwareHostSnmp(PyVmomi): |
| 192 |
"""Manage SNMP configuration for an ESXi host system""" |
| 193 |
|
| 194 |
def __init__(self, module): |
| 195 |
super(VmwareHostSnmp, self).__init__(module) |
| 196 |
self.results = {"changed": False} |
| 197 |
self.changed = False |
| 198 |
self.hosts = None |
| 199 |
|
| 200 |
if self.is_vcenter(): |
| 201 |
esxi_hostname = self.params['esxi_hostname'] |
| 202 |
cluster_name = self.params['cluster_name'] |
| 203 |
if all([esxi_hostname, cluster_name]): |
| 204 |
self.module.fail_json( |
| 205 |
msg="Only one of parameters [cluster_name, esxi_hostname] is required" |
| 206 |
) |
| 207 |
else: |
| 208 |
if cluster_name: |
| 209 |
self.hosts = self.get_all_host_objs(cluster_name=cluster_name) |
| 210 |
if self.hosts is None: |
| 211 |
self.module.fail_json(msg="Failed to find host system in cluster %s." % cluster_name) |
| 212 |
if esxi_hostname: |
| 213 |
self.hosts = self.get_all_host_objs(esxi_host_name=esxi_hostname) |
| 214 |
if self.hosts is None: |
| 215 |
self.module.fail_json(msg="Failed to find host system.") |
| 216 |
else: |
| 217 |
host = find_obj(self.content, [vim.HostSystem], None) |
| 218 |
self.hosts = list() |
| 219 |
self.hosts.append(host) |
| 220 |
|
| 221 |
def ensure(self): |
| 222 |
"""Manage SNMP configuration for an ESXi host system""" |
| 223 |
# results = dict(changed=False, result=dict()) |
| 224 |
snmp_state = self.params.get('state') |
| 225 |
snmp_port = self.params.get('snmp_port') |
| 226 |
community = self.params.get('community') |
| 227 |
desired_trap_targets = self.params.get("trap_targets") |
| 228 |
hw_source = self.params.get("hw_source") |
| 229 |
log_level = self.params.get("log_level") |
| 230 |
send_trap = self.params.get("send_trap") |
| 231 |
trap_filter = self.params.get("trap_filter") |
| 232 |
sys_contact = self.params.get("sys_contact") |
| 233 |
sys_location = self.params.get("sys_location") |
| 234 |
event_filter = None |
| 235 |
if trap_filter: |
| 236 |
event_filter = ';'.join(trap_filter) |
| 237 |
reset_hint = None |
| 238 |
|
| 239 |
for host in self.hosts: |
| 240 |
changed = False |
| 241 |
changed_list = [] |
| 242 |
self.results[host.name] = dict(changed=False, msg='') |
| 243 |
snmp_system = host.configManager.snmpSystem |
| 244 |
if snmp_system: |
| 245 |
if snmp_system.configuration: |
| 246 |
snmp_config_spec = snmp_system.configuration |
| 247 |
else: |
| 248 |
self.module.fail_json(msg="SNMP agent configuration isn't supported on the ESXi host") |
| 249 |
else: |
| 250 |
self.module.fail_json(msg="SNMP system isn't available on the ESXi host") |
| 251 |
|
| 252 |
# Check state |
| 253 |
self.results[host.name]['state'] = snmp_state |
| 254 |
if snmp_state == 'reset': |
| 255 |
changed = True |
| 256 |
# Get previous config |
| 257 |
if snmp_config_spec.enabled: |
| 258 |
self.results[host.name]['state_previous'] = 'enabled' |
| 259 |
else: |
| 260 |
self.results[host.name]['state_previous'] = 'disabled' |
| 261 |
self.results[host.name]['port_previous'] = snmp_config_spec.port |
| 262 |
self.results[host.name]['community_previous'] = snmp_config_spec.readOnlyCommunities |
| 263 |
self.results[host.name]['trap_targets_previous'] = self.get_previous_targets(snmp_config_spec.trapTargets) |
| 264 |
for option in snmp_config_spec.option: |
| 265 |
if option.key == 'EnvEventSource' and option.value != hw_source: |
| 266 |
self.results[host.name]['hw_source_previous'] = option.value |
| 267 |
if option.key == 'loglevel' and option.value != hw_source: |
| 268 |
self.results[host.name]['log_level_previous'] = option.value |
| 269 |
if option.key == 'EventFilter' and option.value != hw_source: |
| 270 |
self.results[host.name]['trap_filter_previous'] = option.value.split(';') |
| 271 |
if option.key == 'syscontact' and option.value != hw_source: |
| 272 |
self.results[host.name]['syscontact_previous'] = option.value |
| 273 |
if option.key == 'syslocation' and option.value != hw_source: |
| 274 |
self.results[host.name]['syslocation_previous'] = option.value |
| 275 |
# Build factory default config |
| 276 |
destination = vim.host.SnmpSystem.SnmpConfigSpec.Destination() |
| 277 |
destination.hostName = "" |
| 278 |
destination.port = 0 |
| 279 |
destination.community = "" |
| 280 |
options = [] |
| 281 |
options.append(self.create_option('EnvEventSource', 'indications')) |
| 282 |
options.append(self.create_option('EventFilter', 'reset')) |
| 283 |
snmp_config_spec = vim.host.SnmpSystem.SnmpConfigSpec() |
| 284 |
# Looks like this value is causing the reset |
| 285 |
snmp_config_spec.readOnlyCommunities = [""] |
| 286 |
snmp_config_spec.trapTargets = [destination] |
| 287 |
snmp_config_spec.port = 161 |
| 288 |
snmp_config_spec.enabled = False |
| 289 |
snmp_config_spec.option = options |
| 290 |
else: |
| 291 |
if snmp_state == 'enabled' and not snmp_config_spec.enabled: |
| 292 |
changed = True |
| 293 |
changed_list.append("state") |
| 294 |
self.results[host.name]['state_previous'] = 'disabled' |
| 295 |
snmp_config_spec.enabled = True |
| 296 |
elif snmp_state == 'disabled' and snmp_config_spec.enabled: |
| 297 |
changed = True |
| 298 |
changed_list.append("state") |
| 299 |
self.results[host.name]['state_previous'] = 'enabled' |
| 300 |
snmp_config_spec.enabled = False |
| 301 |
|
| 302 |
# Check port |
| 303 |
self.results[host.name]['port'] = snmp_port |
| 304 |
if snmp_config_spec.port != snmp_port: |
| 305 |
changed = True |
| 306 |
changed_list.append("port") |
| 307 |
self.results[host.name]['port_previous'] = snmp_config_spec.port |
| 308 |
snmp_config_spec.port = snmp_port |
| 309 |
|
| 310 |
# Check read-only community strings |
| 311 |
self.results[host.name]['community'] = community |
| 312 |
if snmp_config_spec.readOnlyCommunities != community: |
| 313 |
changed = True |
| 314 |
changed_list.append("community list") |
| 315 |
self.results[host.name]['community_previous'] = snmp_config_spec.readOnlyCommunities |
| 316 |
if community: |
| 317 |
snmp_config_spec.readOnlyCommunities = community |
| 318 |
else: |
| 319 |
# Doesn't work. Need to reset config instead |
| 320 |
# snmp_config_spec.readOnlyCommunities = [] |
| 321 |
reset_hint = True |
| 322 |
|
| 323 |
# Check trap targets |
| 324 |
self.results[host.name]['trap_targets'] = desired_trap_targets |
| 325 |
if snmp_config_spec.trapTargets: |
| 326 |
if desired_trap_targets: |
| 327 |
temp_desired_targets = [] |
| 328 |
# Loop through desired targets |
| 329 |
for target in desired_trap_targets: |
| 330 |
dest_hostname, dest_port, dest_community = self.check_if_options_are_valid(target) |
| 331 |
trap_target_found = False |
| 332 |
for trap_target in snmp_config_spec.trapTargets: |
| 333 |
if trap_target.hostName == dest_hostname: |
| 334 |
if trap_target.port != dest_port or trap_target.community != dest_community: |
| 335 |
changed = True |
| 336 |
changed_list.append("trap target '%s'" % dest_hostname) |
| 337 |
trap_target_found = True |
| 338 |
break |
| 339 |
if not trap_target_found: |
| 340 |
changed = True |
| 341 |
changed_list.append("trap target '%s'" % dest_hostname) |
| 342 |
# Build destination and add to temp target list |
| 343 |
destination = self.build_destination(dest_hostname, dest_port, dest_community) |
| 344 |
temp_desired_targets.append(destination) |
| 345 |
# Loop through existing targets to find targets that need to be deleted |
| 346 |
for trap_target in snmp_config_spec.trapTargets: |
| 347 |
target_found = False |
| 348 |
for target in desired_trap_targets: |
| 349 |
if trap_target.hostName == target.get('hostname'): |
| 350 |
target_found = True |
| 351 |
break |
| 352 |
if not target_found: |
| 353 |
changed = True |
| 354 |
changed_list.append("trap target '%s'" % trap_target.hostName) |
| 355 |
# Configure trap targets if something has changed |
| 356 |
if changed: |
| 357 |
self.results[host.name]['trap_targets_previous'] = self.get_previous_targets(snmp_config_spec.trapTargets) |
| 358 |
snmp_config_spec.trapTargets = temp_desired_targets |
| 359 |
else: |
| 360 |
changed = True |
| 361 |
changed_list.append("trap targets") |
| 362 |
self.results[host.name]['trap_targets_previous'] = self.get_previous_targets(snmp_config_spec.trapTargets) |
| 363 |
# Doesn't work. Need to reset config instead |
| 364 |
# snmp_config_spec.trapTargets = [] |
| 365 |
reset_hint = True |
| 366 |
else: |
| 367 |
if desired_trap_targets: |
| 368 |
changed = True |
| 369 |
changed_list.append("trap targets") |
| 370 |
self.results[host.name]['trap_targets_previous'] = None |
| 371 |
desired_targets = [] |
| 372 |
for target in desired_trap_targets: |
| 373 |
dest_hostname, dest_port, dest_community = self.check_if_options_are_valid(target) |
| 374 |
destination = self.build_destination(dest_hostname, dest_port, dest_community) |
| 375 |
desired_targets.append(destination) |
| 376 |
snmp_config_spec.trapTargets = desired_targets |
| 377 |
|
| 378 |
# Check options |
| 379 |
self.results[host.name]['hw_source'] = hw_source |
| 380 |
self.results[host.name]['log_level'] = log_level |
| 381 |
self.results[host.name]['trap_filter'] = trap_filter |
| 382 |
event_filter_found = False |
| 383 |
sys_contact_found = False |
| 384 |
sys_location_found = False |
| 385 |
if snmp_config_spec.option: |
| 386 |
for option in snmp_config_spec.option: |
| 387 |
if option.key == 'EnvEventSource' and option.value != hw_source: |
| 388 |
changed = True |
| 389 |
changed_list.append("HW source") |
| 390 |
self.results[host.name]['hw_source_previous'] = option.value |
| 391 |
option.value = hw_source |
| 392 |
if option.key == 'loglevel' and option.value != log_level: |
| 393 |
changed = True |
| 394 |
changed_list.append("log level") |
| 395 |
self.results[host.name]['log_level_previous'] = option.value |
| 396 |
option.value = log_level |
| 397 |
if option.key == 'EventFilter': |
| 398 |
event_filter_found = True |
| 399 |
if event_filter and option.value != event_filter: |
| 400 |
changed = True |
| 401 |
changed_list.append("trap filter") |
| 402 |
self.results[host.name]['trap_filter_previous'] = option.value.split(';') |
| 403 |
option.value = event_filter |
| 404 |
if option.key == 'syscontact': |
| 405 |
sys_contact_found = True |
| 406 |
if sys_contact is not None and option.value != sys_contact: |
| 407 |
changed = True |
| 408 |
changed_list.append("sys contact") |
| 409 |
self.results[host.name]['sys_contact_previous'] = option.value |
| 410 |
option.value = sys_contact |
| 411 |
if option.key == 'syslocation': |
| 412 |
sys_location_found = True |
| 413 |
if sys_location is not None and option.value != sys_location: |
| 414 |
changed = True |
| 415 |
changed_list.append("sys location") |
| 416 |
self.results[host.name]['sys_location_previous'] = option.value |
| 417 |
option.value = sys_location |
| 418 |
if trap_filter and not event_filter_found: |
| 419 |
changed = True |
| 420 |
changed_list.append("trap filter") |
| 421 |
self.results[host.name]['trap_filter_previous'] = [] |
| 422 |
snmp_config_spec.option.append(self.create_option('EventFilter', event_filter)) |
| 423 |
elif not trap_filter and event_filter_found: |
| 424 |
changed = True |
| 425 |
changed_list.append("trap filter") |
| 426 |
# options = [] |
| 427 |
for option in snmp_config_spec.option: |
| 428 |
if option.key == 'EventFilter': |
| 429 |
self.results[host.name]['trap_filter_previous'] = option.value.split(';') |
| 430 |
# else: |
| 431 |
# options.append(option) |
| 432 |
# Doesn't work. Need to reset config instead |
| 433 |
# snmp_config_spec.option = options |
| 434 |
reset_hint = True |
| 435 |
if sys_contact and not sys_contact_found: |
| 436 |
changed = True |
| 437 |
changed_list.append("sys contact") |
| 438 |
self.results[host.name]['sys_contact_previous'] = '' |
| 439 |
snmp_config_spec.option.append(self.create_option('syscontact', sys_contact)) |
| 440 |
if sys_location and not sys_location_found: |
| 441 |
changed = True |
| 442 |
changed_list.append("sys location") |
| 443 |
self.results[host.name]['sys_location_previous'] = '' |
| 444 |
snmp_config_spec.option.append(self.create_option('syslocation', sys_location)) |
| 445 |
if changed: |
| 446 |
if snmp_state == 'reset': |
| 447 |
if self.module.check_mode: |
| 448 |
message = "SNMP agent would be reset to factory defaults" |
| 449 |
else: |
| 450 |
message = "SNMP agent config reset to factory defaults" |
| 451 |
else: |
| 452 |
if self.module.check_mode: |
| 453 |
changed_suffix = ' would be changed' |
| 454 |
else: |
| 455 |
changed_suffix = ' changed' |
| 456 |
if len(changed_list) > 2: |
| 457 |
message = ', '.join(changed_list[:-1]) + ', and ' + str(changed_list[-1]) |
| 458 |
elif len(changed_list) == 2: |
| 459 |
message = ' and '.join(changed_list) |
| 460 |
elif len(changed_list) == 1: |
| 461 |
message = changed_list[0] |
| 462 |
message = "SNMP " + message + changed_suffix |
| 463 |
if reset_hint: |
| 464 |
message += ". Agent reset required!" |
| 465 |
if not self.module.check_mode: |
| 466 |
try: |
| 467 |
snmp_system.ReconfigureSnmpAgent(snmp_config_spec) |
| 468 |
except vim.fault.NotFound as not_found: |
| 469 |
self.module.fail_json( |
| 470 |
msg="Not found : %s" % to_native(not_found) |
| 471 |
) |
| 472 |
except vim.fault.InsufficientResourcesFault as insufficient_resources: |
| 473 |
self.module.fail_json( |
| 474 |
msg="Insufficient resources : %s" % to_native(insufficient_resources) |
| 475 |
) |
| 476 |
else: |
| 477 |
message = "SNMP already configured properly" |
| 478 |
if not snmp_state == 'reset' and send_trap and desired_trap_targets: |
| 479 |
# Check if there was a change before |
| 480 |
if changed: |
| 481 |
message += " and " |
| 482 |
else: |
| 483 |
message += ", but " |
| 484 |
changed = True |
| 485 |
if self.module.check_mode: |
| 486 |
message = message + "a test trap would be sent" |
| 487 |
else: |
| 488 |
try: |
| 489 |
snmp_system.SendTestNotification() |
| 490 |
message = message + "a test trap was sent" |
| 491 |
except vim.fault.NotFound as not_found: |
| 492 |
self.module.fail_json( |
| 493 |
msg="Error during trap test : Not found : %s" % to_native(not_found) |
| 494 |
) |
| 495 |
except vim.fault.InsufficientResourcesFault as insufficient_resources: |
| 496 |
self.module.fail_json( |
| 497 |
msg="Error during trap test : Insufficient resources : %s" % to_native(insufficient_resources) |
| 498 |
) |
| 499 |
self.changed = any([self.changed, changed]) |
| 500 |
self.results[host.name]['changed'] = changed |
| 501 |
self.results[host.name]['msg'] = message |
| 502 |
self.results['changed'] = self.changed |
| 503 |
self.module.exit_json(**self.results) |
| 504 |
|
| 505 |
@staticmethod |
| 506 |
def create_option(key, value): |
| 507 |
"""Create option""" |
| 508 |
option = vim.KeyValue() |
| 509 |
option.key = key |
| 510 |
option.value = value |
| 511 |
return option |
| 512 |
|
| 513 |
@staticmethod |
| 514 |
def get_previous_targets(trap_targets): |
| 515 |
"""Get target entries from trap targets object""" |
| 516 |
previous_targets = [] |
| 517 |
for target in trap_targets: |
| 518 |
temp = dict() |
| 519 |
temp['hostname'] = target.hostName |
| 520 |
temp['port'] = target.port |
| 521 |
temp['community'] = target.community |
| 522 |
previous_targets.append(temp) |
| 523 |
return previous_targets |
| 524 |
|
| 525 |
@staticmethod |
| 526 |
def build_destination(dest_hostname, dest_port, dest_community): |
| 527 |
"""Build destination spec""" |
| 528 |
destination = vim.host.SnmpSystem.SnmpConfigSpec.Destination() |
| 529 |
destination.hostName = dest_hostname |
| 530 |
destination.port = dest_port |
| 531 |
destination.community = dest_community |
| 532 |
return destination |
| 533 |
|
| 534 |
def check_if_options_are_valid(self, target): |
| 535 |
"""Check if options are valid""" |
| 536 |
dest_hostname = target.get('hostname', None) |
| 537 |
if dest_hostname is None: |
| 538 |
self.module.fail_json( |
| 539 |
msg="Please specify hostname for the trap target as it's a required parameter" |
| 540 |
) |
| 541 |
dest_port = target.get('port', None) |
| 542 |
if dest_port is None: |
| 543 |
self.module.fail_json( |
| 544 |
msg="Please specify port for the trap target as it's a required parameter" |
| 545 |
) |
| 546 |
dest_community = target.get('community', None) |
| 547 |
if dest_community is None: |
| 548 |
self.module.fail_json( |
| 549 |
msg="Please specify community for the trap target as it's a required parameter" |
| 550 |
) |
| 551 |
return dest_hostname, dest_port, dest_community |
| 552 |
|
| 553 |
|
| 554 |
def main(): |
| 555 |
"""Main""" |
| 556 |
argument_spec = base_argument_spec() |
| 557 |
argument_spec.update( |
| 558 |
cluster_name=dict(type='str', required=False), |
| 559 |
esxi_hostname=dict(type='list', elements='str', required=False), |
| 560 |
state=dict(type='str', default='disabled', choices=['enabled', 'disabled', 'reset']), |
| 561 |
snmp_port=dict(type='int', default=161), |
| 562 |
community=dict(type='list', default=[], elements='str'), |
| 563 |
trap_targets=dict(type='list', default=list(), elements='dict'), |
| 564 |
trap_filter=dict(type='list', elements='str'), |
| 565 |
hw_source=dict(type='str', default='indications', choices=['indications', 'sensors']), |
| 566 |
log_level=dict(type='str', default='info', choices=['debug', 'info', 'warning', 'error']), |
| 567 |
send_trap=dict(type='bool', default=False), |
| 568 |
sys_contact=dict(type='str'), |
| 569 |
sys_location=dict(type='str') |
| 570 |
) |
| 571 |
|
| 572 |
module = AnsibleModule( |
| 573 |
argument_spec=argument_spec, |
| 574 |
supports_check_mode=True, |
| 575 |
) |
| 576 |
|
| 577 |
host_snmp = VmwareHostSnmp(module) |
| 578 |
host_snmp.ensure() |
| 579 |
|
| 580 |
|
| 581 |
if __name__ == '__main__': |
| 582 |
main() |