Debian Codegraph @

ansible_collections/netapp/storagegrid/plugins/modules/na_sg_grid_snmp.py

raw
1 #!/usr/bin/python
2
3 # (c) 2025, NetApp Inc
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6 """NetApp StorageGRID - Manage SNMP monitoring"""
7
8 from __future__ import absolute_import, division, print_function
9
10 __metaclass__ = type
11
12
13 ANSIBLE_METADATA = {
14 "metadata_version": "1.0",
15 "status": ["preview"],
16 "supported_by": "community",
17 }
18
19
20 DOCUMENTATION = """
21 module: na_sg_grid_snmp
22 short_description: Configure SNMP agent on StorageGRID.
23 extends_documentation_fragment:
24 - netapp.storagegrid.netapp.sg
25 version_added: '21.15.0'
26 author: NetApp Ansible Team (@vinaykus) <[email protected]>
27 description:
28 - Configure SNMP monitoring on NetApp StorageGRID.
29 options:
30 state:
31 description:
32 - The snmp agent should be present.
33 choices: ['present']
34 default: 'present'
35 type: str
36 enable_snmp:
37 description:
38 - Enable or disable SNMP monitoring.
39 type: bool
40 default: false
41 community_strings:
42 description:
43 - List of community strings.
44 type: list
45 elements: str
46 ro_users:
47 description:
48 - USM users allowed read-only access.
49 type: list
50 elements: str
51 sys_location:
52 description:
53 - SNMP system location.
54 type: str
55 sys_contact:
56 description:
57 - SNMP system contact.
58 type: str
59 trap_community:
60 description:
61 - SNMP trap community.
62 type: str
63 auth_trap_enable:
64 description:
65 - 1 - enable SNMP authentication traps.
66 - 2 - disable SNMP authentication traps.
67 choices: [1, 2]
68 default: 2
69 type: int
70 disable_notifications:
71 description:
72 - Disable all SNMP notifications.
73 type: bool
74 default: false
75 trap_destinations:
76 description:
77 - SNMP trap destinations for V1, V2C, and Inform notifications.
78 type: list
79 elements: dict
80 suboptions:
81 type:
82 description:
83 - SNMP trap destination type.
84 choices: ['trapsink', 'trap2sink', 'informsink', 'trapsess', 'informsess']
85 type: str
86 required: true
87 host:
88 description:
89 - SNMP trap destination host.
90 type: str
91 required: true
92 port:
93 description:
94 - SNMP trap destination port.
95 type: int
96 community:
97 description:
98 - SNMP trap destination community (cannot be used with C(usm_user)).
99 type: str
100 usm_user:
101 description:
102 - USM user to send notification under (cannot be used with C(community)).
103 type: str
104 protocol:
105 description:
106 - SNMP trap destination protocol.
107 choices: ['udp', 'tcp']
108 default: 'udp'
109 type: str
110 agent_addresses:
111 description:
112 - Local binding addresses for the SNMP agent.
113 type: list
114 elements: dict
115 suboptions:
116 protocol:
117 description:
118 - SNMP agent address protocol.
119 choices: ['udp', 'udp6', 'tcp', 'tcp6']
120 default: 'udp'
121 type: str
122 network:
123 description:
124 - SNMP agent local network interface address.
125 choices: ['grid', 'admin', 'client', 'all']
126 default: 'all'
127 type: str
128 port:
129 description:
130 - SNMP agent local address port.
131 type: int
132 default: 161
133 usm_users:
134 description:
135 - USM user.
136 type: list
137 elements: dict
138 suboptions:
139 name:
140 description:
141 - USM user name.
142 type: str
143 required: true
144 security_level:
145 description:
146 - USM user security level.
147 choices: ['authNoPriv', 'authPriv']
148 type: str
149 required: true
150 auth_protocol:
151 description:
152 - USM user authentication protocol.
153 choices: ['SHA']
154 type: str
155 required: true
156 auth_passphrase:
157 description:
158 - USM user authentication passphrase.
159 type: str
160 required: true
161 priv_protocol:
162 description:
163 - USM user privacy protocol.
164 choices: ['AES']
165 type: str
166 priv_passphrase:
167 description:
168 - USM user privacy passphrase.
169 type: str
170 authoritative_engine_id:
171 description:
172 - The engine ID to use for localized key hashing. From 5 to 32 bytes in hex.
173 - Only for use on users specified in informsess trap destinations.
174 type: str
175 """
176
177 EXAMPLES = """
178 - name: Configure SNMP monitoring
179 na_sg_grid_snmp:
180 api_url: "https://<storagegrid-endpoint-url>"
181 auth_token: "storagegrid-auth-token"
182 enable_snmp: true
183 community_strings:
184 - public
185 sys_location: RTP-L1-R23S10
186 sys_contact: [email protected]
187 trap_community: public
188 auth_trap_enable: 2
189 disable_notifications: false
190 trap_destinations:
191 - type: trapsink
192 host: 172.12.10.100
193 port: 162
194 community: public
195 protocol: udp
196 agent_addresses:
197 - protocol: udp
198 network: admin
199 port: 161
200
201 - name: Configure usm user
202 na_sg_grid_snmp:
203 api_url: "https://<storagegrid-endpoint-url>"
204 auth_token: "storagegrid-auth-token"
205 enable_snmp: true
206 usm_users:
207 - name: user1
208 security_level: authNoPriv
209 auth_protocol: SHA
210 auth_passphrase: password
211 priv_protocol: AES
212 priv_passphrase: privpass
213 """
214
215 RETURN = """
216 resp:
217 description: Returns information about the StorageGRID SNMP agent.
218 returned: If state is 'present'.
219 type: dict
220 sample: {
221 "enable_snmp": false,
222 "community_strings": [
223 "public"
224 ],
225 "rousers": [
226 "user1"
227 ],
228 "sysLocation": "RTP-L1-R23S10",
229 "sysContact": "[email protected]",
230 "trapcommunity": "public",
231 "authtrapenable": 2,
232 "disable_notifications": false,
233 "trap_destinations": [
234 {
235 "type": "trapsink",
236 "host": "172.16.10.100",
237 "port": 162,
238 "community": "public",
239 "usmUser": "user1",
240 "protocol": "udp"
241 }
242 ],
243 "agent_addresses": [
244 {
245 "protocol": "udp",
246 "network": "admin",
247 "port": 161
248 }
249 ],
250 "usm_users": [
251 {
252 "name": "user1",
253 "securityLevel": "authPriv",
254 "authProtocol": "SHA",
255 "authPassphrase": "authpass",
256 "privProtocol": "AES",
257 "privPassphrase": "privpass",
258 "authoritativeEngineId": "0x803f482ba75d00000000"
259 }
260 ]
261 }
262 """
263
264 import ansible_collections.netapp.storagegrid.plugins.module_utils.netapp as netapp_utils
265 from ansible.module_utils.basic import AnsibleModule
266 from ansible_collections.netapp.storagegrid.plugins.module_utils.netapp_module import NetAppModule
267 from ansible_collections.netapp.storagegrid.plugins.module_utils.netapp import SGRestAPI
268
269
270 class SgSnmp:
271 """
272 Configure SNMP monitoring on NetApp StorageGRID
273 """
274
275 def __init__(self):
276 """
277 Parse arguments, setup state variables,
278 check parameters and ensure request module is installed
279 """
280 self.argument_spec = netapp_utils.na_storagegrid_host_argument_spec()
281 self.argument_spec.update(
282 dict(
283 state=dict(type="str", choices=["present"], default="present"),
284 enable_snmp=dict(required=False, type="bool", default=False),
285 community_strings=dict(required=False, type="list", elements="str"),
286 ro_users=dict(required=False, type="list", elements="str"),
287 sys_location=dict(required=False, type="str"),
288 sys_contact=dict(required=False, type="str"),
289 trap_community=dict(required=False, type="str"),
290 auth_trap_enable=dict(required=False, type="int", choices=[1, 2], default=2),
291 disable_notifications=dict(required=False, type="bool", default=False),
292 trap_destinations=dict(
293 required=False,
294 type="list",
295 elements="dict",
296 options=dict(
297 type=dict(required=True, type="str", choices=["trapsink", "trap2sink", "informsink", "trapsess", "informsess"]),
298 host=dict(required=True, type="str"),
299 port=dict(required=False, type="int"),
300 community=dict(required=False, type="str"),
301 usm_user=dict(required=False, type="str"),
302 protocol=dict(required=False, type="str", choices=["udp", "tcp"], default="udp"),
303 )
304 ),
305 agent_addresses=dict(
306 required=False,
307 type="list",
308 elements="dict",
309 options=dict(
310 protocol=dict(required=False, type="str", choices=["udp", "udp6", "tcp", "tcp6"], default="udp"),
311 network=dict(required=False, type="str", choices=["grid", "admin", "client", "all"], default="all"),
312 port=dict(required=False, type="int", default=161),
313 )
314 ),
315 usm_users=dict(
316 required=False,
317 type="list",
318 elements="dict",
319 options=dict(
320 name=dict(required=True, type="str"),
321 security_level=dict(required=True, type="str", choices=["authNoPriv", "authPriv"]),
322 auth_protocol=dict(required=True, type="str", choices=["SHA"]),
323 auth_passphrase=dict(required=True, type="str", no_log=True),
324 priv_protocol=dict(required=False, type="str", choices=["AES"]),
325 priv_passphrase=dict(required=False, type="str", no_log=True),
326 authoritative_engine_id=dict(required=False, type="str"),
327 )
328 )
329 )
330 )
331
332 self.module = AnsibleModule(
333 argument_spec=self.argument_spec,
334 supports_check_mode=True
335 )
336 self.na_helper = NetAppModule()
337
338 # set up state variables
339 self.parameters = self.na_helper.set_parameters(self.module.params)
340 # Calling generic SG rest_api class
341 self.rest_api = SGRestAPI(self.module)
342 # Get API version
343 self.rest_api.get_sg_product_version()
344 self.api_version = self.rest_api.get_api_version()
345
346 # Checking for the parameters passed and create new parameters list
347 self.data = {}
348
349 if self.parameters["enable_snmp"] is not None:
350 self.data["enable_snmp"] = self.parameters["enable_snmp"]
351 if self.parameters.get("community_strings") is not None:
352 self.data["community_strings"] = self.parameters["community_strings"]
353 if self.parameters.get("ro_users") is not None:
354 self.data["rousers"] = self.parameters["ro_users"]
355 if self.parameters.get("sys_location"):
356 self.data["sysLocation"] = self.parameters["sys_location"]
357 if self.parameters.get("sys_contact"):
358 self.data["sysContact"] = self.parameters["sys_contact"]
359 if self.parameters.get("trap_community"):
360 self.data["trapcommunity"] = self.parameters["trap_community"]
361 if self.parameters.get("auth_trap_enable"):
362 self.data["authtrapenable"] = self.parameters["auth_trap_enable"]
363 if self.parameters["disable_notifications"] is not None:
364 self.data["disable_notifications"] = self.parameters["disable_notifications"]
365
366 if self.parameters.get("trap_destinations"):
367 self.data["trap_destinations"] = [
368 {
369 "type": trap_destination["type"],
370 "host": trap_destination["host"],
371 "port": trap_destination.get("port"),
372 "community": trap_destination.get("community"),
373 "usmUser": trap_destination.get("usm_user"),
374 "protocol": trap_destination.get("protocol"),
375 }
376 for trap_destination in self.parameters["trap_destinations"]
377 ]
378
379 if self.parameters.get("agent_addresses"):
380 self.data["agent_addresses"] = self.parameters["agent_addresses"]
381
382 if self.parameters.get("usm_users"):
383 self.data["usm_users"] = [
384 {
385 "name": usm_user["name"],
386 "securityLevel": usm_user["security_level"],
387 "authProtocol": usm_user["auth_protocol"],
388 "authPassphrase": usm_user["auth_passphrase"],
389 "privProtocol": usm_user.get("priv_protocol"),
390 "privPassphrase": usm_user.get("priv_passphrase"),
391 "authoritativeEngineId": usm_user.get("authoritative_engine_id"),
392 }
393 for usm_user in self.parameters["usm_users"]
394 ]
395
396 def get_snmp_config(self):
397 """ Get SNMP configuration """
398 api = "api/%s/grid/snmp" % self.api_version
399 response, error = self.rest_api.get(api)
400
401 if error:
402 self.module.fail_json(msg=error)
403 else:
404 return response["data"]
405
406 def update_snmp_config(self):
407 """ Update SNMP configuration """
408 api = "api/%s/grid/snmp" % self.api_version
409 response, error = self.rest_api.put(api, self.data)
410
411 if error:
412 self.module.fail_json(msg=error)
413 else:
414 return response["data"]
415
416 def apply(self):
417 ''' Apply SNMP configuration '''
418
419 current_snmp = self.get_snmp_config()
420
421 cd_action = self.na_helper.get_cd_action(current_snmp, self.parameters)
422
423 if cd_action is None and self.parameters["state"] == "present":
424 if not current_snmp["enable_snmp"] and not self.parameters["enable_snmp"]:
425 self.module.exit_json(changed=False, msg="SNMP is disabled.")
426
427 # Remove passphrases from usm_users for both current_snmp and self.data to make it idempotent
428 if current_snmp.get("usm_users") and self.data.get("usm_users"):
429 current_usm_users = {user["name"]: user for user in current_snmp["usm_users"]}
430 for user in self.data.get("usm_users"):
431 if user["name"] in current_usm_users:
432 current_usm_users[user["name"]].pop("authPassphrase", None)
433 current_usm_users[user["name"]].pop("privPassphrase", None)
434 user.pop("authPassphrase", None)
435 user.pop("privPassphrase", None)
436
437 modify = self.na_helper.get_modified_attributes(current_snmp, self.data)
438
439 result_message = ""
440 resp_data = current_snmp
441 if self.na_helper.changed:
442 if self.module.check_mode:
443 pass
444 elif modify:
445 resp_data = self.update_snmp_config()
446 result_message = "SNMP configuration updated successfully."
447
448 self.module.exit_json(changed=self.na_helper.changed, msg=result_message, resp=resp_data)
449
450
451 def main():
452 """
453 Main function
454 """
455 na_sg_grid_snmp = SgSnmp()
456 na_sg_grid_snmp.apply()
457
458
459 if __name__ == "__main__":
460 main()