Debian Codegraph @

ansible_collections/azure/azcollection/plugins/modules/azure_rm_monitordatacollectionrules.py

raw symbols SCIP
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2025 Klaas Demter (@Klaas-)
4 #
5 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 from __future__ import absolute_import, division, print_function
8 __metaclass__ = type
9
10 DOCUMENTATION = '''
11 ---
12 module: azure_rm_monitordatacollectionrules
13 version_added: "3.7.0"
14 short_description: Create, update and delete Data Collection Rules
15 description:
16 - Create, update and delete Data Collection Rules
17
18 options:
19 name:
20 description:
21 - The name of the data collection rule you're creating/changing
22 required: true
23 type: str
24 resource_group:
25 description:
26 - The name of the resource group
27 required: true
28 type: str
29 location:
30 description:
31 - Location of the data colelction rule
32 - defaults to location of exiting data collection rule or
33 - location of the resource group if unspecified
34 required: false
35 type: str
36 kind:
37 description:
38 - Kind of the data collection rule
39 - Use C(Linux) for Linux.
40 - Use C(Windows) for Windows.
41 type: str
42 choices:
43 - Linux
44 - Windows
45 description:
46 description:
47 - Description for the data collection rule
48 type: str
49 data_collection_endpoint_id:
50 description:
51 - The resource ID of the data collection endpoint that this rule can be used with
52 type: str
53 stream_declarations:
54 description:
55 - Declaration of a custom stream. Sub dict is a list of columns used by data in this stream.
56 - top level key is the name of the stream_declaration
57 - U(https://learn.microsoft.com/en-us/python/api/azure-mgmt-monitor/azure.mgmt.monitor.v2022_06_01.models.streamdeclaration?view=azure-python)
58 type: dict
59 suboptions:
60 name_of_stream:
61 description:
62 - Name of the stream
63 type: dict
64 suboptions:
65 columns:
66 description:
67 - Declaration of a custom stream.
68 type: list
69 elements: dict
70 suboptions:
71 name:
72 description:
73 - The name of the column.
74 type: str
75 type:
76 description:
77 - The type of the column data.
78 type: str
79 choices:
80 - string
81 - int
82 - long
83 - real
84 - boolean
85 - datetime
86 - dynamic
87 data_sources:
88 description:
89 - The specification of data sources.
90 - This property is optional and can be omitted if the rule is meant to be used via direct calls to the provisioned endpoint.
91 - >-
92 U(https://learn.microsoft.com/en-us/python/api/azure-mgmt-monitor/azure.mgmt.monitor.v2022_06_01.models.datacollectionruledatasources?view=azure-python)
93 type: dict
94 suboptions:
95 performance_counters:
96 description:
97 - Definition of which performance counters will be collected and how they will be collected by this data collection rule.
98 - Collected from both Windows and Linux machines where the counter is present.
99 - >-
100 U(https://learn.microsoft.com/en-us/python/api/azure-mgmt-monitor/azure.mgmt.monitor.v2022_06_01.models.perfcounterdatasource?view=azure-python)
101 type: list
102 elements: dict
103 suboptions:
104 streams:
105 description:
106 - List of streams that this data source will be sent to.
107 - A stream indicates what schema will be used for this data and usually what table in Log Analytics the data will be sent to.
108 type: list
109 elements: str
110 sampling_frequency_in_seconds:
111 description:
112 - The number of seconds between consecutive counter measurements (samples).
113 type: int
114 counter_specifiers:
115 description:
116 - A list of specifier names of the performance counters you want to collect.
117 - Use a wildcard (*) to collect a counter for all instances.
118 - To get a list of performance counters on Windows, run the command 'typeperf'
119 type: list
120 elements: str
121 name:
122 description:
123 - A friendly name for the data source.
124 - This name should be unique across all data sources (regardless of type) within the data collection rule.
125 type: str
126 windows_event_logs:
127 description:
128 - Definition of which Windows Event Log events will be collected and how they will be collected.
129 - Only collected from Windows machines.
130 - >-
131 U(https://learn.microsoft.com/en-us/python/api/azure-mgmt-monitor/azure.mgmt.monitor.v2022_06_01.models.windowseventlogdatasource?view=azure-python)
132 type: list
133 elements: dict
134 suboptions:
135 streams:
136 description:
137 - List of streams that this data source will be sent to.
138 - A stream indicates what schema will be used for this data and usually what table in Log Analytics the data will be sent to.
139 type: list
140 elements: str
141 x_path_queries:
142 description:
143 - A list of Windows Event Log queries in XPATH format.
144 type: list
145 elements: str
146 name:
147 description:
148 - A friendly name for the data source.
149 - This name should be unique across all data sources (regardless of type) within the data collection rule.
150 type: str
151 syslog:
152 description:
153 - Definition of which syslog data will be collected and how it will be collected. Only collected from Linux machines.
154 - >-
155 U(https://learn.microsoft.com/en-us/python/api/azure-mgmt-monitor/azure.mgmt.monitor.v2022_06_01.models.syslogdatasource?view=azure-python)
156 type: list
157 elements: dict
158 suboptions:
159 streams:
160 description:
161 - List of streams that this data source will be sent to.
162 - A stream indicates what schema will be used for this data and usually what table in Log Analytics the data will be sent to.
163 type: list
164 elements: str
165 facility_names:
166 description:
167 - The list of facility names.
168 type: list
169 elements: str
170 log_levels:
171 description:
172 - The log levels to collect.
173 type: list
174 elements: str
175 name:
176 description:
177 - A friendly name for the data source.
178 - This name should be unique across all data sources (regardless of type) within the data collection rule.
179 type: str
180 extensions:
181 description:
182 - Definition of which data will be collected from a separate VM extension that integrates with the Azure Monitor Agent.
183 - Collected from either Windows and Linux machines, depending on which extension is defined.
184 - >-
185 U(https://learn.microsoft.com/en-us/python/api/azure-mgmt-monitor/azure.mgmt.monitor.v2022_06_01.models.extensiondatasource?view=azure-python)
186 type: list
187 elements: dict
188 suboptions:
189 streams:
190 description:
191 - List of streams that this data source will be sent to.
192 - A stream indicates what schema will be used for this data and usually what table in Log Analytics the data will be sent to.
193 type: list
194 elements: str
195 extension_name:
196 description:
197 - The name of the VM extension.
198 type: list
199 elements: str
200 extension_settings:
201 description:
202 - The extension settings. The format is specific for particular extension.
203 type: str
204 input_data_sources:
205 description:
206 - The list of data sources this extension needs data from.
207 type: list
208 elements: str
209 name:
210 description:
211 - A friendly name for the data source.
212 - This name should be unique across all data sources (regardless of type) within the data collection rule.
213 type: str
214 log_files:
215 description:
216 - Definition of which custom log files will be collected by this data collection rule.
217 - >-
218 U(https://learn.microsoft.com/en-us/python/api/azure-mgmt-monitor/azure.mgmt.monitor.v2022_06_01.models.logfilesdatasource?view=azure-python)
219 type: list
220 elements: dict
221 suboptions:
222 streams:
223 description:
224 - List of streams that this data source will be sent to.
225 - A stream indicates what schema will be used for this data and usually what table in Log Analytics the data will be sent to.
226 type: list
227 elements: str
228 file_patterns:
229 description:
230 - File Patterns where the log files are located
231 type: list
232 elements: str
233 format:
234 description:
235 - The data format of the log files.
236 type: str
237 settings:
238 description:
239 - The log files specific settings.
240 type: dict
241 suboptions:
242 text:
243 description:
244 - Text settings
245 type: str
246 name:
247 description:
248 - A friendly name for the data source.
249 - This name should be unique across all data sources (regardless of type) within the data collection rule.
250 type: str
251 iis_logs:
252 description:
253 - Enables IIS logs to be collected by this data collection rule.
254 - >-
255 U(https://learn.microsoft.com/en-us/python/api/azure-mgmt-monitor/azure.mgmt.monitor.v2022_06_01.models.iislogsdatasource?view=azure-python)
256 type: list
257 elements: dict
258 suboptions:
259 streams:
260 description:
261 - IIS streams.
262 type: list
263 elements: str
264 log_directories:
265 description:
266 - Absolute paths file location.
267 type: list
268 elements: str
269 name:
270 description:
271 - A friendly name for the data source.
272 - This name should be unique across all data sources (regardless of type) within the data collection rule.
273 type: str
274 windows_firewall_logs:
275 description:
276 - Enables Firewall logs to be collected by this data collection rule.
277 - >-
278 U(https://learn.microsoft.com/en-us/python/api/azure-mgmt-monitor/azure.mgmt.monitor.v2022_06_01.models.windowsfirewalllogsdatasource?view=azure-python)
279 type: list
280 elements: dict
281 suboptions:
282 streams:
283 description:
284 - Firewall logs streams.
285 type: list
286 elements: str
287 name:
288 description:
289 - A friendly name for the data source.
290 - This name should be unique across all data sources (regardless of type) within the data collection rule.
291 type: str
292 prometheus_forwarder:
293 description:
294 - Definition of Prometheus metrics forwarding configuration.
295 - >-
296 U(https://learn.microsoft.com/en-us/python/api/azure-mgmt-monitor/azure.mgmt.monitor.v2022_06_01.models.prometheusforwarderdatasource?view=azure-python)
297 type: list
298 elements: dict
299 suboptions:
300 streams:
301 description:
302 - List of streams that this data source will be sent to.
303 type: list
304 elements: str
305 label_include_filter:
306 description:
307 - The list of label inclusion filters in the form of label "name-value" pairs.
308 - Currently only one label is supported "microsoft_metrics_include_label". Label values are matched case-insensitively.
309 type: dict
310 name:
311 description:
312 - A friendly name for the data source.
313 - This name should be unique across all data sources (regardless of type) within the data collection rule.
314 type: str
315 platform_telemetry:
316 description:
317 - Definition of platform telemetry data source configuration.
318 - >-
319 U(https://learn.microsoft.com/en-us/python/api/azure-mgmt-monitor/azure.mgmt.monitor.v2022_06_01.models.platformtelemetrydatasource?view=azure-python)
320 type: list
321 elements: dict
322 suboptions:
323 streams:
324 description:
325 - List of platform telemetry streams to collect.
326 type: list
327 elements: str
328 name:
329 description:
330 - A friendly name for the data source.
331 - This name should be unique across all data sources (regardless of type) within the data collection rule.
332 type: str
333 data_imports:
334 description:
335 - Specifications of pull based data sources.
336 - >-
337 U(https://learn.microsoft.com/en-us/python/api/azure-mgmt-monitor/azure.mgmt.monitor.v2022_06_01.models.datasourcesspecdataimports?view=azure-python)
338 type: dict
339 suboptions:
340 event_hub:
341 description:
342 - Definition of Event Hub configuration.
343 type: dict
344 suboptions:
345 name:
346 description:
347 - A friendly name for the data source.
348 - This name should be unique across all data sources (regardless of type) within the data collection rule.
349 type: str
350 consumer_group:
351 description:
352 - Event Hub consumer group name.
353 type: str
354 stream:
355 description:
356 - The stream to collect from EventHub.
357 type: str
358 destinations:
359 description:
360 - >-
361 U(https://learn.microsoft.com/en-us/python/api/azure-mgmt-monitor/azure.mgmt.monitor.v2022_06_01.models.datacollectionruledestinations?view=azure-python)
362 type: dict
363 suboptions:
364 log_analytics:
365 description:
366 - List of Log Analytics destinations.
367 - >-
368 U(https://learn.microsoft.com/en-us/python/api/azure-mgmt-monitor/azure.mgmt.monitor.v2022_06_01.models.loganalyticsdestination?view=azure-python)
369 type: list
370 elements: dict
371 suboptions:
372 workspace_resource_id:
373 description:
374 - The resource ID of the Log Analytics workspace.
375 type: str
376 name:
377 description:
378 - A friendly name for the destination.
379 - This name should be unique across all destinations (regardless of type) within the data collection rule.
380 type: str
381 monitoring_accounts:
382 description:
383 - List of monitoring account destinations.
384 - >-
385 U(https://learn.microsoft.com/en-us/python/api/azure-mgmt-monitor/azure.mgmt.monitor.v2022_06_01.models.monitoringaccountdestination?view=azure-python)
386 type: list
387 elements: dict
388 suboptions:
389 account_resource_id:
390 description:
391 - The resource ID of the monitoring account.
392 type: str
393 name:
394 description:
395 - A friendly name for the destination.
396 - This name should be unique across all destinations (regardless of type) within the data collection rule.
397 type: str
398 azure_monitor_metrics:
399 description:
400 - Azure Monitor Metrics destination
401 - >-
402 U(https://learn.microsoft.com/en-us/python/api/azure-mgmt-monitor/azure.mgmt.monitor.v2022_06_01.models.destinationsspecazuremonitormetrics?view=azure-python)
403 type: str
404 event_hubs:
405 description:
406 - List of Event Hubs destinations.
407 - >-
408 U(https://learn.microsoft.com/en-us/python/api/azure-mgmt-monitor/azure.mgmt.monitor.v2022_06_01.models.eventhubdestination?view=azure-python)
409 type: list
410 elements: dict
411 suboptions:
412 event_hub_resource_id:
413 description:
414 - The resource ID of the event hub.
415 type: str
416 name:
417 description:
418 - A friendly name for the destination.
419 - This name should be unique across all destinations (regardless of type) within the data collection rule.
420 type: str
421 event_hubs_direct:
422 description:
423 - List of Event Hubs Direct destinations.
424 - >-
425 U(https://learn.microsoft.com/en-us/python/api/azure-mgmt-monitor/azure.mgmt.monitor.v2022_06_01.models.eventhubdirectdestination?view=azure-python)
426 type: list
427 elements: dict
428 suboptions:
429 event_hub_resource_id:
430 description:
431 - The resource ID of the event hub.
432 type: str
433 name:
434 description:
435 - A friendly name for the destination.
436 - This name should be unique across all destinations (regardless of type) within the data collection rule.
437 type: str
438 storage_blobs_direct:
439 description:
440 - List of Storage Blob Direct destinations. To be used only for sending data directly to store from the agent.
441 - >-
442 U(https://learn.microsoft.com/en-us/python/api/azure-mgmt-monitor/azure.mgmt.monitor.v2022_06_01.models.storageblobdestination?view=azure-python)
443 type: list
444 elements: dict
445 suboptions:
446 container_name:
447 description:
448 - The container name of the Storage Blob.
449 type: str
450 storage_account_resource_id:
451 description:
452 - The resource ID of the storage account.
453 type: str
454 name:
455 description:
456 - A friendly name for the destination.
457 - This name should be unique across all destinations (regardless of type) within the data collection rule.
458 type: str
459 storage_tables_direct:
460 description:
461 - List of Storage Table Direct destinations.
462 - >-
463 U(https://learn.microsoft.com/en-us/python/api/azure-mgmt-monitor/azure.mgmt.monitor.v2022_06_01.models.storagetabledestination?view=azure-python)
464 type: list
465 elements: dict
466 suboptions:
467 table_name:
468 description:
469 - The name of the Storage Table.
470 type: str
471 storage_account_resource_id:
472 description:
473 - The resource ID of the storage account.
474 type: str
475 name:
476 description:
477 - A friendly name for the destination.
478 - This name should be unique across all destinations (regardless of type) within the data collection rule.
479 type: str
480 storage_accounts:
481 description:
482 - List of storage accounts destinations.
483 - >-
484 U(https://learn.microsoft.com/en-us/python/api/azure-mgmt-monitor/azure.mgmt.monitor.v2022_06_01.models.storageblobdestination?view=azure-python)
485 type: list
486 elements: dict
487 suboptions:
488 container_name:
489 description:
490 - The container name of the Storage Blob.
491 type: str
492 storage_account_resource_id:
493 description:
494 - The resource ID of the storage account.
495 type: str
496 name:
497 description:
498 - A friendly name for the destination.
499 - This name should be unique across all destinations (regardless of type) within the data collection rule.
500 type: str
501 data_flows:
502 description:
503 - Definition of which streams are sent to which destinations.
504 - >-
505 U(https://learn.microsoft.com/en-us/python/api/azure-mgmt-monitor/azure.mgmt.monitor.v2022_06_01.models.dataflow?view=azure-python)
506 type: list
507 elements: dict
508 suboptions:
509 streams:
510 description:
511 - List of streams for this data flow.
512 - >-
513 U(https://learn.microsoft.com/en-us/rest/api/monitor/data-collection-rules/create?view=rest-monitor-2023-03-11&tabs=HTTP&tryIt=true#knowndataflowstreams)
514 type: list
515 elements: str
516 destinations:
517 description:
518 - List of destinations for this data flow.
519 type: list
520 elements: str
521 transform_kql:
522 description:
523 - The KQL query to transform stream data.
524 type: str
525 output_stream:
526 description:
527 - The output stream of the transform. Only required if the transform changes data to a different stream.
528 type: str
529 built_in_transform:
530 description:
531 - The builtIn transform to transform stream data.
532 type: str
533 state:
534 description:
535 - State of the data collection rule
536 - Use C(present) for creating/updating a data collection rule.
537 - Use C(absent) for deleting a data collection rule.
538 default: present
539 type: str
540 choices:
541 - present
542 - absent
543 extends_documentation_fragment:
544 - azure.azcollection.azure
545 - azure.azcollection.azure_tags
546
547 author:
548 - Klaas Demter (@Klaas-)
549 '''
550
551 EXAMPLES = '''
552 - name: Add a data collection rule
553 azure.azcollection.azure_rm_monitordatacollectionrules:
554 state: present
555 name: data_collection_rule_name
556 resource_group: resource_group_name
557 location: westeurope
558 kind: Linux
559 description: This is an example description of a data collection rule
560 data_sources:
561 performance_counters:
562 - name: perfCounterDataSource
563 streams:
564 - Microsoft-Perf
565 sampling_frequency_in_seconds: 60
566 counter_specifiers:
567 - Processor(*)\\% Processor Time
568 - Processor(*)\\% Idle Time
569 destinations:
570 log_analytics:
571 - workspace_resource_id: "/subscriptions/<sub-id>/resourceGroups/rg/providers/Microsoft.OperationalInsights/workspaces/log_analytics_workspace_name"
572 name: log_analytics_workspace_name
573 data_flows:
574 - destinations:
575 - log_analytics_workspace_name
576 streams:
577 - Microsoft-Perf
578 append_tags: false
579 tags:
580 ThisIsAnExampleTag: ExampleValue
581
582 - name: Add a data collection rule
583 azure.azcollection.azure_rm_monitordatacollectionrules:
584 state: present
585 name: data_collection_rule_name
586 resource_group: resource_group_name
587 append_tags: true
588 tags:
589 ThisIsAnAddedExampleTag: ExampleValue
590
591 # Note this needs a DCR endpoint, not sure why, creating one via portal does not need that
592 # Also the table in your log analytics workspace has to already exist
593 - name: Add a data collection rule for collecting a custom log
594 azure.azcollection.azure_rm_monitordatacollectionrules:
595 name: data_collection_rule_name
596 resource_group: resource_group_name
597 location: westeurope
598 kind: Linux
599 data_sources:
600 log_files:
601 - file_patterns:
602 - /var/log/dnf.rpm.log
603 format: text
604 name: Custom-Text-CustomLogs_CL
605 streams:
606 - Custom-Text-CustomLogs_CL
607 destinations:
608 log_analytics:
609 - workspace_resource_id: "/subscriptions/<sub-id>/resourceGroups/rg/providers/Microsoft.OperationalInsights/workspaces/log_analytics_workspace_name"
610 name: log_analytics_workspace_name
611 data_flows:
612 - destinations:
613 - log_analytics_workspace_name
614 output_stream: Custom-CustomLogs_CL
615 streams:
616 - Custom-Text-CustomLogs_CL
617 transform_kql: source
618 data_collection_endpoint_id: "/subscriptions/<sub-id>/resourceGroups/rg/providers/Microsoft.Insights/dataCollectionEndpoints/dcr-endpoint"
619 stream_declarations:
620 Custom-Text-CustomLogs_CL:
621 columns:
622 - name: TimeGenerated
623 type: datetime
624 - name: RawData
625 type: string
626 - name: FilePath
627 type: string
628 - name: Computer
629 type: string
630
631
632 - name: Delete a data collection rule
633 azure.azcollection.azure_rm_monitordatacollectionrules:
634 state: absent
635 name: data_collection_rule_name
636 resource_group: resource_group_name
637 '''
638
639 RETURN = '''
640 datacollectionrule:
641 description:
642 - Details of the data collection rule
643 - Is null on state==absent (data collection rule does not exist or will be deleted)
644 - Assumes you make legal changes in check mode
645 type: dict
646 returned: always
647 sample: {
648 "data_flows": [...],
649 "data_sources": {},
650 "description": "Description of your data collection rule",
651 "destinations": {},
652 "etag": "str",
653 "id": \
654 "/subscriptions/<sub-id>/resourceGroups/resource_group_name/providers/Microsoft.Insights/dataCollectionRules/data_collection_rule_name",
655 "immutable_id": "dcr-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
656 "kind": "Linux",
657 "location": "westeurope",
658 "name": "data_collection_rule_name",
659 "provisioning_state": "Succeeded",
660 "system_data": {
661 "created_at": "2025-01-01T00:00:00.000000Z",
662 "created_by": "[email protected]",
663 "created_by_type": "User",
664 "last_modified_at": "2025-01-01T00:00:00.000000Z",
665 "last_modified_by": "[email protected]",
666 "last_modified_by_type": "User"
667 },
668 "tags": {},
669 "type": "Microsoft.Insights/dataCollectionRules"
670 }
671 '''
672
673 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_ext import AzureRMModuleBaseExt
674
675 try:
676 from azure.core.exceptions import ResourceNotFoundError
677
678 except ImportError:
679 # This is handled in azure_rm_common
680 pass
681
682 AZURE_OBJECT_CLASS = 'DataCollectionRules'
683
684 data_flows_spec = dict(
685 streams=dict(type='list', elements='str'),
686 destinations=dict(type='list', elements='str'),
687 transform_kql=dict(type='str'),
688 output_stream=dict(type='str'),
689 built_in_transform=dict(type='str'),
690 )
691
692 destinations_spec_log_analytics_options = dict(
693 workspace_resource_id=dict(type='str'),
694 name=dict(type='str')
695 )
696
697 destinations_spec_monitoring_accounts_options = dict(
698 account_resource_id=dict(type='str'),
699 name=dict(type='str')
700 )
701
702 destinations_spec_event_hubs_options = dict(
703 event_hub_resource_id=dict(type='str'),
704 name=dict(type='str')
705 )
706
707 destinations_spec_storage_blobs_options = dict(
708 container_name=dict(type='str'),
709 storage_account_resource_id=dict(type='str'),
710 name=dict(type='str')
711 )
712
713 destinations_spec_storage_tables_options = dict(
714 table_name=dict(type='str'),
715 storage_account_resource_id=dict(type='str'),
716 name=dict(type='str')
717 )
718
719 destinations_spec = dict(
720 log_analytics=dict(type='list', elements='dict', options=destinations_spec_log_analytics_options),
721 monitoring_accounts=dict(type='list', elements='dict', options=destinations_spec_monitoring_accounts_options),
722 azure_monitor_metrics=dict(type='str'),
723 event_hubs=dict(type='list', elements='dict', options=destinations_spec_event_hubs_options),
724 event_hubs_direct=dict(type='list', elements='dict', options=destinations_spec_event_hubs_options),
725 storage_blobs_direct=dict(type='list', elements='dict', options=destinations_spec_storage_blobs_options),
726 storage_tables_direct=dict(type='list', elements='dict', options=destinations_spec_storage_tables_options),
727 storage_accounts=dict(type='list', elements='dict', options=destinations_spec_storage_blobs_options)
728 )
729
730 data_sources_spec_performance_counters_options = dict(
731 streams=dict(type='list', elements='str'),
732 sampling_frequency_in_seconds=dict(type='int'),
733 counter_specifiers=dict(type='list', elements='str'),
734 name=dict(type='str')
735 )
736
737 data_sources_spec_windows_event_logs_options = dict(
738 streams=dict(type='list', elements='str'),
739 x_path_queries=dict(type='list', elements='str'),
740 name=dict(type='str')
741 )
742
743 data_sources_spec_syslog_options = dict(
744 streams=dict(type='list', elements='str'),
745 facility_names=dict(type='list', elements='str'),
746 log_levels=dict(type='list', elements='str'),
747 name=dict(type='str')
748 )
749
750 data_sources_spec_extensions_options = dict(
751 streams=dict(type='list', elements='str'),
752 extension_name=dict(type='list', elements='str'),
753 extension_settings=dict(type='str'),
754 input_data_sources=dict(type='list', elements='str'),
755 name=dict(type='str')
756 )
757
758 data_sources_spec_log_files_options = dict(
759 streams=dict(type='list', elements='str'),
760 file_patterns=dict(type='list', elements='str'),
761 format=dict(type='str'),
762 settings=dict(type='dict', options=dict(text=dict(type='str'))),
763 name=dict(type='str')
764 )
765
766 data_sources_spec_iis_logs_options = dict(
767 streams=dict(type='list', elements='str'),
768 log_directories=dict(type='list', elements='str'),
769 name=dict(type='str')
770 )
771
772 data_sources_spec_windows_firewall_logs_options = dict(
773 streams=dict(type='list', elements='str'),
774 name=dict(type='str')
775 )
776
777 data_sources_spec_prometheus_forwarder_options = dict(
778 streams=dict(type='list', elements='str'),
779 label_include_filter=dict(type='dict'),
780 name=dict(type='str')
781 )
782
783 data_sources_spec_data_imports_event_hub_options = dict(
784 name=dict(type='str'),
785 consumer_group=dict(type='str'),
786 stream=dict(type='str')
787 )
788
789 data_sources_spec_data_imports_options = dict(
790 event_hub=dict(type='dict', options=data_sources_spec_data_imports_event_hub_options)
791 )
792
793 data_sources_spec = dict(
794 performance_counters=dict(type='list', elements='dict', options=data_sources_spec_performance_counters_options),
795 windows_event_logs=dict(type='list', elements='dict', options=data_sources_spec_windows_event_logs_options),
796 syslog=dict(type='list', elements='dict', options=data_sources_spec_syslog_options),
797 extensions=dict(type='list', elements='dict', options=data_sources_spec_extensions_options),
798 log_files=dict(type='list', elements='dict', options=data_sources_spec_log_files_options),
799 iis_logs=dict(type='list', elements='dict', options=data_sources_spec_iis_logs_options),
800 windows_firewall_logs=dict(type='list', elements='dict', options=data_sources_spec_windows_firewall_logs_options),
801 prometheus_forwarder=dict(type='list', elements='dict', options=data_sources_spec_prometheus_forwarder_options),
802 # Platform telemetry options are currently same as windows_firewall_logs
803 platform_telemetry=dict(type='list', elements='dict', options=data_sources_spec_windows_firewall_logs_options),
804 data_imports=dict(type='dict', options=data_sources_spec_data_imports_options)
805 )
806
807
808 class AzureRMDataCollectionRules(AzureRMModuleBaseExt):
809 """Information class for an Azure RM Data Collection Rules"""
810
811 def __init__(self):
812 self.module_arg_spec = dict(
813 name=dict(type='str', required=True),
814 resource_group=dict(type='str', required=True),
815 location=dict(type='str'),
816 kind=dict(type='str', choices=['Linux', 'Windows']),
817 description=dict(type='str'),
818 data_collection_endpoint_id=dict(type='str'),
819 # https://github.com/ansible/ansible/issues/74001
820 # Can't properly define this in arg spec
821 stream_declarations=dict(type='dict'),
822 data_sources=dict(type='dict', options=data_sources_spec),
823 destinations=dict(type='dict', options=destinations_spec),
824 data_flows=dict(type='list', elements='dict', options=data_flows_spec),
825 state=dict(type='str', choices=['present', 'absent'], default='present')
826 )
827
828 self.name = None
829 self.resource_group = None
830 self.location = None
831 self.kind = None
832 self.description = None
833 self.data_collection_endpoint_id = None
834 self.stream_declarations = None
835 self.data_sources = None
836 self.destinations = None
837 self.data_flows = None
838 self.state = None
839 self.tags = None
840 self.log_path = None
841 self.log_mode = None
842
843 self.results = dict(
844 changed=False,
845 datacollectionrule=dict(),
846 diff=dict(
847 before=None,
848 after=None
849 )
850 )
851
852 super(AzureRMDataCollectionRules, self).__init__(derived_arg_spec=self.module_arg_spec,
853 supports_check_mode=True,
854 supports_tags=True)
855
856 def exec_module(self, **kwargs):
857 """Main module execution method"""
858
859 for key in list(self.module_arg_spec.keys()) + ['tags']:
860 if hasattr(self, key):
861 setattr(self, key, kwargs[key])
862
863 # Defaults for variables
864 result = None
865 result_compare = dict(compare=[])
866 before_dict = None
867
868 # Get current data collection rule if it exists
869 before_dict = self.get_data_collection_rule()
870
871 # Create dict from input, without None values
872 # https://learn.microsoft.com/en-us/python/api/azure-mgmt-monitor/azure.mgmt.monitor.v2021_04_01.models.datacollectionruleresource?view=azure-python
873 data_collection_rule_template = {
874 "location": self.location,
875 "kind": self.kind,
876 "description": self.description,
877 "data_collection_endpoint_id": self.data_collection_endpoint_id,
878 "stream_declarations": self.stream_declarations,
879 "data_sources": self.data_sources,
880 "destinations": self.destinations,
881 "data_flows": self.data_flows
882 }
883 # Filter out all None values
884 data_collection_rule_input = {key: value for key, value in data_collection_rule_template.items() if value is not None}
885
886 # Create/Update if state==present
887 if self.state == 'present':
888 if before_dict is None:
889 # Data collection rule does not exist, create
890 # On creation default to location of resource group unless otherwise noted in input variables
891 if not self.location:
892 resource_group = self.get_resource_group(self.resource_group)
893 data_collection_rule_input['location'] = resource_group.location
894 # On creation input == what we send to api
895 data_collection_rule_update = data_collection_rule_input
896 # Needs to be extended by tags if set
897 if self.tags:
898 data_collection_rule_update['tags'] = self.tags
899 self.results['changed'] = True
900 if self.check_mode:
901 # Check mode, skipping actual creation
902 pass
903 else:
904 create_response = self.create_or_update(data_collection_rule_update)
905 else:
906 # Data collection rule already exists, updating it
907 # Dict for update is the union of existing object overwritten by input data
908 data_collection_rule_update = before_dict | data_collection_rule_input
909
910 # Enhanced with tags (special behaviour because of append_tags possibility)
911 update_tags, update_tags_content = self.update_tags(before_dict.get('tags'))
912 # Check if we need to update the data collection rule
913 if update_tags or not self.default_compare({}, data_collection_rule_update, before_dict, '', result_compare):
914 data_collection_rule_update['tags'] = update_tags_content
915 # Need to create/update the Data collection rule; changed -> True
916 self.results['changed'] = True
917 if self.check_mode:
918 # Check mode, skipping actual creation
919 pass
920 else:
921 create_response = self.create_or_update(data_collection_rule_update)
922
923 if self.check_mode or not self.results['changed']:
924 # When object was not updated or when running in check mode
925 # assume data_collection_rule_update is resulting object
926 result = data_collection_rule_update
927 else:
928 # otherwise take resulting new object from response of create call
929 result = create_response
930
931 # Delete data collection rule if state is absent and it exists
932 # if it doesn't exist, it's already absent
933 elif self.state == 'absent' and before_dict is not None:
934 self.results['changed'] = True
935 if self.check_mode:
936 # do not delete in check mode
937 pass
938 else:
939 self.delete()
940
941 self.results['diff']['before'] = before_dict
942 self.results['diff']['after'] = result
943 self.results['datacollectionrule'] = result
944
945 return self.results
946
947 def get_data_collection_rule(self):
948 '''
949 Gets the properties of the specified data collection rule.
950
951 :return: List of Data Collection Rules
952 '''
953 self.log("Checking if data collection rule {0} in resource group {1} is present".format(self.name,
954 self.resource_group))
955
956 result = None
957 response = None
958
959 try:
960 response = self.monitor_management_client_data_collection_rules.data_collection_rules.get(data_collection_rule_name=self.name,
961 resource_group_name=self.resource_group)
962 except ResourceNotFoundError as ex:
963 self.log("Could not find data collection rule {0} in resource group {1}".format(self.name, self.resource_group))
964 if response:
965 result = self.serialize_obj(response, AZURE_OBJECT_CLASS)
966
967 return result
968
969 def create_or_update(self, data_collection_rule_update):
970 result = None
971 response = None
972 try:
973 response = self.monitor_management_client_data_collection_rules.data_collection_rules.create(resource_group_name=self.resource_group,
974 data_collection_rule_name=self.name,
975 body=data_collection_rule_update,
976 logging_enable=False)
977 except Exception as ex:
978 self.fail("Error creating or update data collection rule {0} in resource group {1}: {2}".format(self.name, self.resource_group, str(ex)))
979
980 if response:
981 result = self.serialize_obj(response, AZURE_OBJECT_CLASS)
982
983 return result
984
985 def delete(self):
986 response = None
987 try:
988 response = self.monitor_management_client_data_collection_rules.data_collection_rules.delete(resource_group_name=self.resource_group,
989 data_collection_rule_name=self.name)
990 except Exception as ex:
991 self.fail("Error deleting data collection rule {0} in resource group {1}: {2}".format(self.name, self.resource_group, str(ex)))
992
993 return response
994
995
996 def main():
997 """Main execution"""
998 AzureRMDataCollectionRules()
999
1000
1001 if __name__ == '__main__':
1002 main()