-
Notifications
You must be signed in to change notification settings - Fork 292
/
Copy pathzabbix_action.py
2290 lines (2079 loc) · 85.9 KB
/
zabbix_action.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python
# -*- coding: utf-8 -*-
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import absolute_import, division, print_function
__metaclass__ = type
DOCUMENTATION = """
---
module: zabbix_action
short_description: Create/Delete/Update Zabbix actions
description:
- This module allows you to create, modify and delete Zabbix actions.
author:
- Ruben Tsirunyan (@rubentsirunyan)
- Ruben Harutyunov (@K-DOT)
requirements:
- "python >= 3.9"
options:
name:
type: str
description:
- Name of the action
required: true
event_source:
type: str
description:
- Type of events that the action will handle.
- Required when C(state=present).
required: false
choices: ["trigger", "discovery", "auto_registration", "internal"]
state:
type: str
description:
- State of the action.
- On C(present), it will create an action if it does not exist or update the action if the associated data is different.
- On C(absent), it will remove the action if it exists.
choices: ["present", "absent"]
default: "present"
status:
type: str
description:
- Status of the action.
choices: ["enabled", "disabled"]
default: "enabled"
pause_in_maintenance:
description:
- Whether to pause escalation during maintenance periods or not.
- Can be used when I(event_source=trigger).
type: "bool"
default: true
notify_if_canceled:
description:
- Weather to notify when escalation is canceled.
- Can be used when I(event_source=trigger).
type: "bool"
default: true
esc_period:
type: str
description:
- Default operation step duration. Must be greater than 60 seconds.
- Accepts seconds, time unit with suffix and user macro since => Zabbix 3.4
- Required when C(state=present).
required: false
conditions:
type: list
elements: dict
description:
- List of conditions to use for filtering results.
- For more information about suboptions of this option please
check out Zabbix API documentation U(https://www.zabbix.com/documentation/5.0/manual/api/reference/action/object#action_filter_condition)
default: []
suboptions:
type:
type: str
description:
- Type (label) of the condition.
- "Possible values when I(event_source=trigger):"
- " - C(host_group)"
- " - C(host)"
- " - C(trigger)"
- " - C(trigger_name)"
- " - C(trigger_severity)"
- " - C(time_period)"
- " - C(host_template)"
- " - C(maintenance_status) known in Zabbix 4.0 and above as 'Problem is suppressed'"
- " - C(event_tag)"
- " - C(event_tag_value)"
- "Possible values when I(event_source=discovery):"
- " - C(host_IP)"
- " - C(discovered_service_type)"
- " - C(discovered_service_port)"
- " - C(discovery_status)"
- " - C(uptime_or_downtime_duration)"
- " - C(received_value)"
- " - C(discovery_rule)"
- " - C(discovery_check)"
- " - C(proxy)"
- " - C(discovery_object)"
- "Possible values when I(event_source=auto_registration):"
- " - C(proxy)"
- " - C(host_name)"
- " - C(host_metadata)"
- "Possible values when I(event_source=internal):"
- " - C(host_group)"
- " - C(host)"
- " - C(host_template)"
- " - C(event_type)"
required: true
value:
type: str
description:
- Value to compare with.
- "When I(type=discovery_status), the choices are:"
- " - C(up)"
- " - C(down)"
- " - C(discovered)"
- " - C(lost)"
- "When I(type=discovery_object), the choices are:"
- " - C(host)"
- " - C(service)"
- "When I(type=event_type), the choices are:"
- " - C(item in not supported state)"
- " - C(item in normal state)"
- " - C(LLD rule in not supported state)"
- " - C(LLD rule in normal state)"
- " - C(trigger in unknown state)"
- " - C(trigger in normal state)"
- "When I(type=trigger_severity), the choices are (case-insensitive):"
- " - C(not classified)"
- " - C(information)"
- " - C(warning)"
- " - C(average)"
- " - C(high)"
- " - C(disaster)"
- Irrespective of user-visible names being changed in Zabbix. Defaults to C(not classified) if omitted.
- Besides the above options, this is usually either the name
of the object or a string to compare with.
value2:
type: str
description:
- Secondary value to compare with.
- Required for trigger actions when condition I(type=event_tag_value).
operator:
type: str
description:
- Condition operator.
- When I(type) is set to C(time_period), the choices are C(in), C(not in).
choices:
- "equals"
- "="
- "does not equal"
- "<>"
- "contains"
- "like"
- "does not contain"
- "not like"
- "in"
- "is greater than or equals"
- ">="
- "is less than or equals"
- "<="
- "not in"
- "matches"
- "does not match"
- "Yes"
- "No"
required: true
formulaid:
type: str
description:
- Arbitrary unique ID that is used to reference the condition from a custom expression.
- Can only contain upper-case letters.
- Required for custom expression filters and ignored otherwise.
eval_type:
type: str
description:
- Filter condition evaluation method.
- Defaults to C(andor) if conditions are less then 2 or if
I(formula) is not specified.
- Defaults to C(custom_expression) when formula is specified.
choices:
- "andor"
- "and"
- "or"
- "custom_expression"
formula:
type: str
description:
- User-defined expression to be used for evaluating conditions with a custom expression.
- The expression must contain IDs that reference each condition by its formulaid.
- The IDs used in the expression must exactly match the ones
defined in the I(conditions). No condition can remain unused or omitted.
- Required when I(eval_type=custom_expression).
- Use sequential IDs that start at "A". If non-sequential IDs are used, Zabbix re-indexes them.
This makes each module run notice the difference in IDs and update the action.
operations:
type: list
elements: dict
description:
- List of action operations
default: []
suboptions:
type:
type: str
description:
- Type of operation.
- "Valid choices when setting type for I(recovery_operations) and I(acknowledge_operations):"
- " - C(send_message)"
- " - C(remote_command)"
- " - C(notify_all_involved)"
- Choice C(notify_all_involved) only supported in I(recovery_operations) and I(acknowledge_operations).
choices:
- send_message
- remote_command
- add_host
- remove_host
- add_to_host_group
- remove_from_host_group
- link_to_template
- unlink_from_template
- enable_host
- disable_host
- set_host_inventory_mode
- notify_all_involved
required: true
esc_period:
type: str
description:
- Duration of an escalation step in seconds.
- Must be greater than 60 seconds.
- Accepts seconds, time unit with suffix and user macro.
- If set to 0 or 0s, the default action escalation period will be used.
default: 0s
esc_step_from:
type: int
description:
- Step to start escalation from.
default: 1
esc_step_to:
type: int
description:
- Step to end escalation at.
- Specify 0 for infinitely.
default: 1
send_to_groups:
type: list
elements: str
description:
- User groups to send messages to.
send_to_users:
type: list
elements: str
description:
- Users (usernames or aliases) to send messages to.
op_message:
type: str
description:
- Operation message text.
- If I(op_message) and I(subject) not defined then "default message" from media type will be used
subject:
type: str
description:
- Operation message subject.
- If I(op_message) and I(subject) not defined then "default message" from media type will be used
media_type:
type: str
description:
- Media type that will be used to send the message.
- Can be used with I(type=send_message) or I(type=notify_all_involved) inside I(acknowledge_operations).
- Set to C(all) for all media types
default: "all"
operation_condition:
type: "str"
description:
- The action operation condition object defines a condition that must be met to perform the current operation.
choices:
- acknowledged
- not_acknowledged
host_groups:
type: list
elements: str
description:
- List of host groups host should be added to.
- Required when I(type=add_to_host_group) or I(type=remove_from_host_group).
templates:
type: list
elements: str
description:
- List of templates host should be linked to.
- Required when I(type=link_to_template) or I(type=unlink_from_template).
inventory:
type: str
description:
- Host inventory mode.
- Required when I(type=set_host_inventory_mode).
choices:
- manual
- automatic
command_type:
type: str
description:
- Type of operation command.
- Required when I(type=remote_command).
choices:
- custom_script
- ipmi
- ssh
- telnet
- global_script
command:
type: str
description:
- Command to run.
- Required when I(type=remote_command) and I(command_type!=global_script).
execute_on:
type: str
description:
- Target on which the custom script operation command will be executed.
- Required when I(type=remote_command) and I(command_type=custom_script).
choices:
- agent
- server
- proxy
run_on_groups:
type: list
elements: str
description:
- Host groups to run remote commands on.
- Required when I(type=remote_command) and I(run_on_hosts) is not set.
run_on_hosts:
type: list
elements: str
description:
- Hosts to run remote commands on.
- Required when I(type=remote_command) and I(run_on_groups) is not set.
- If set to 0 the command will be run on the current host.
ssh_auth_type:
type: str
description:
- Authentication method used for SSH commands.
- Required when I(type=remote_command) and I(command_type=ssh).
choices:
- password
- public_key
ssh_privatekey_file:
type: str
description:
- Name of the private key file used for SSH commands with public key authentication.
- Required when I(ssh_auth_type=public_key).
- Can be used when I(type=remote_command).
ssh_publickey_file:
type: str
description:
- Name of the public key file used for SSH commands with public key authentication.
- Required when I(ssh_auth_type=public_key).
- Can be used when I(type=remote_command).
username:
type: str
description:
- User name used for authentication.
- Required when I(ssh_auth_type in [public_key, password]) or I(command_type=telnet).
- Can be used when I(type=remote_command).
password:
type: str
description:
- Password used for authentication.
- Required when I(ssh_auth_type=password) or I(command_type=telnet).
- Can be used when I(type=remote_command).
port:
type: int
description:
- Port number used for authentication.
- Can be used when I(command_type in [ssh, telnet]) and I(type=remote_command).
script_name:
type: str
description:
- The name of script used for global script commands.
- Required when I(command_type=global_script).
- Can be used when I(type=remote_command).
recovery_operations:
type: list
elements: dict
description:
- List of recovery operations.
- C(Suboptions) are the same as for I(operations).
default: []
suboptions:
type:
type: str
description:
- Type of operation.
choices:
- send_message
- remote_command
- notify_all_involved
required: true
command_type:
type: str
required: false
description:
- Type of operation command.
choices:
- custom_script
- ipmi
- ssh
- telnet
- global_script
command:
type: str
required: false
description:
- Command to run.
execute_on:
type: str
required: false
description:
- Target on which the custom script operation command will be executed.
choices:
- agent
- server
- proxy
ssh_auth_type:
type: str
description:
- Authentication method used for SSH commands.
- Required when I(type=remote_command) and I(command_type=ssh).
choices:
- password
- public_key
ssh_privatekey_file:
type: str
description:
- Name of the private key file used for SSH commands with public key authentication.
- Required when I(ssh_auth_type=public_key).
- Can be used when I(type=remote_command).
ssh_publickey_file:
type: str
description:
- Name of the public key file used for SSH commands with public key authentication.
- Required when I(ssh_auth_type=public_key).
- Can be used when I(type=remote_command).
run_on_groups:
type: list
elements: str
description:
- Host groups to run remote commands on.
- Required when I(type=remote_command) and I(run_on_hosts) is not set.
run_on_hosts:
type: list
elements: str
description:
- Hosts to run remote commands on.
- Required when I(type=remote_command) and I(run_on_groups) is not set.
- If set to 0 the command will be run on the current host.
send_to_groups:
type: list
elements: str
description:
- User groups to send messages to.
send_to_users:
type: list
elements: str
description:
- Users (usernames or aliases) to send messages to.
media_type:
type: str
description:
- Media type that will be used to send the message.
- Can be used with I(type=send_message) or I(type=notify_all_involved) inside I(acknowledge_operations).
- Set to C(all) for all media types
default: "all"
op_message:
type: str
description:
- Operation message text.
- If I(op_message) and I(subject) not defined then "default message" from media type will be used
subject:
type: str
description:
- Operation message subject.
- If I(op_message) and I(subject) not defined then "default message" from media type will be used
username:
type: str
description:
- User name used for authentication.
- Required when I(ssh_auth_type in [public_key, password]) or I(command_type=telnet).
- Can be used when I(type=remote_command).
password:
type: str
description:
- Password used for authentication.
- Required when I(ssh_auth_type=password) or I(command_type=telnet).
- Can be used when I(type=remote_command).
port:
type: int
description:
- Port number used for authentication.
- Can be used when I(command_type in [ssh, telnet]) and I(type=remote_command).
script_name:
type: str
description:
- The name of script used for global script commands.
- Required when I(command_type=global_script).
- Can be used when I(type=remote_command).
acknowledge_operations:
type: list
elements: dict
description:
- List of acknowledge operations.
- Action acknowledge operations are known as update operations since Zabbix 4.0.
- C(Suboptions) are the same as for I(operations).
suboptions:
type:
type: str
description:
- Type of operation.
choices:
- send_message
- remote_command
- notify_all_involved
required: true
command_type:
type: str
description:
- Type of operation command.
required: false
choices:
- custom_script
- ipmi
- ssh
- telnet
- global_script
execute_on:
type: str
required: false
description:
- Target on which the custom script operation command will be executed.
choices:
- agent
- server
- proxy
command:
type: str
required: false
description:
- Command to run.
ssh_auth_type:
type: str
description:
- Authentication method used for SSH commands.
- Required when I(type=remote_command) and I(command_type=ssh).
choices:
- password
- public_key
ssh_privatekey_file:
type: str
description:
- Name of the private key file used for SSH commands with public key authentication.
- Required when I(ssh_auth_type=public_key).
- Can be used when I(type=remote_command).
ssh_publickey_file:
type: str
description:
- Name of the public key file used for SSH commands with public key authentication.
- Required when I(ssh_auth_type=public_key).
- Can be used when I(type=remote_command).
run_on_groups:
type: list
elements: str
description:
- Host groups to run remote commands on.
- Required when I(type=remote_command) and I(run_on_hosts) is not set.
run_on_hosts:
type: list
elements: str
description:
- Hosts to run remote commands on.
- Required when I(type=remote_command) and I(run_on_groups) is not set.
- If set to 0 the command will be run on the current host.
send_to_groups:
type: list
elements: str
description:
- User groups to send messages to.
send_to_users:
type: list
elements: str
description:
- Users (usernames or aliases) to send messages to.
media_type:
type: str
description:
- Media type that will be used to send the message.
- Can be used with I(type=send_message) or I(type=notify_all_involved) inside I(acknowledge_operations).
- Set to C(all) for all media types
default: "all"
op_message:
type: str
description:
- Operation message text.
- If I(op_message) and I(subject) not defined then "default message" from media type will be used
subject:
type: str
description:
- Operation message subject.
- If I(op_message) and I(subject) not defined then "default message" from media type will be used
username:
type: str
description:
- User name used for authentication.
- Required when I(ssh_auth_type in [public_key, password]) or I(command_type=telnet).
- Can be used when I(type=remote_command).
password:
type: str
description:
- Password used for authentication.
- Required when I(ssh_auth_type=password) or I(command_type=telnet).
- Can be used when I(type=remote_command).
port:
type: int
description:
- Port number used for authentication.
- Can be used when I(command_type in [ssh, telnet]) and I(type=remote_command).
script_name:
type: str
description:
- The name of script used for global script commands.
- Required when I(command_type=global_script).
- Can be used when I(type=remote_command).
aliases: [ update_operations ]
default: []
pause_symptoms:
type: bool
description:
- Whether to pause escalation if event is a symptom event.
- I(supported) if C(event_source) is set to C(trigger)
- Works only with >= Zabbix 6.4
default: true
extends_documentation_fragment:
- community.zabbix.zabbix
"""
EXAMPLES = """
# If you want to use Username and Password to be authenticated by Zabbix Server
- name: Set credentials to access Zabbix Server API
ansible.builtin.set_fact:
ansible_user: Admin
ansible_httpapi_pass: zabbix
# If you want to use API token to be authenticated by Zabbix Server
# https://www.zabbix.com/documentation/current/en/manual/web_interface/frontend_sections/administration/general#api-tokens
- name: Set API token
ansible.builtin.set_fact:
ansible_zabbix_auth_key: 8ec0d52432c15c91fcafe9888500cf9a607f44091ab554dbee860f6b44fac895
# Trigger action with only one condition
- name: Deploy trigger action
# set task level variables as we change ansible_connection plugin here
vars:
ansible_network_os: community.zabbix.zabbix
ansible_connection: httpapi
ansible_httpapi_port: 443
ansible_httpapi_use_ssl: true
ansible_httpapi_validate_certs: false
ansible_zabbix_url_path: "zabbixeu" # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu
ansible_host: zabbix-example-fqdn.org
community.zabbix.zabbix_action:
name: "Send alerts to Admin"
event_source: "trigger"
state: present
status: enabled
esc_period: 60
conditions:
- type: "trigger_severity"
operator: ">="
value: "Information"
operations:
- type: send_message
subject: "Something bad is happening"
op_message: "Come on, guys do something"
media_type: "Email"
send_to_users:
- "Admin"
# Trigger action with multiple conditions and operations
- name: Deploy trigger action
# set task level variables as we change ansible_connection plugin here
vars:
ansible_network_os: community.zabbix.zabbix
ansible_connection: httpapi
ansible_httpapi_port: 443
ansible_httpapi_use_ssl: true
ansible_httpapi_validate_certs: false
ansible_zabbix_url_path: "zabbixeu" # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu
ansible_host: zabbix-example-fqdn.org
community.zabbix.zabbix_action:
name: "Send alerts to Admin"
event_source: "trigger"
state: present
status: enabled
esc_period: 1m
conditions:
- type: "trigger_name"
operator: "like"
value: "Zabbix agent is unreachable"
formulaid: A
- type: "trigger_severity"
operator: ">="
value: "disaster"
formulaid: B
formula: A or B
operations:
- type: send_message
media_type: "Email"
send_to_users:
- "Admin"
- type: remote_command
command: "systemctl restart zabbix-agent"
command_type: custom_script
execute_on: server
run_on_hosts:
- 0
# Trigger action with recovery and acknowledge operations
- name: Deploy trigger action
# set task level variables as we change ansible_connection plugin here
vars:
ansible_network_os: community.zabbix.zabbix
ansible_connection: httpapi
ansible_httpapi_port: 443
ansible_httpapi_use_ssl: true
ansible_httpapi_validate_certs: false
ansible_zabbix_url_path: "zabbixeu" # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu
ansible_host: zabbix-example-fqdn.org
community.zabbix.zabbix_action:
name: "Send alerts to Admin"
event_source: "trigger"
state: present
status: enabled
esc_period: 1h
conditions:
- type: "trigger_severity"
operator: ">="
value: "Information"
operations:
- type: send_message
subject: "Something bad is happening"
op_message: "Come on, guys do something"
media_type: "Email"
send_to_users:
- "Admin"
recovery_operations:
- type: send_message
subject: "Host is down"
op_message: "Come on, guys do something"
media_type: "Email"
send_to_users:
- "Admin"
acknowledge_operations:
- type: send_message
media_type: "Email"
send_to_users:
- "Admin"
"""
RETURN = """
msg:
description: The result of the operation
returned: success
type: str
sample: "Action Deleted: Register webservers, ID: 0001"
"""
from ansible.module_utils.basic import AnsibleModule
from ansible_collections.community.zabbix.plugins.module_utils.base import ZabbixBase
from ansible.module_utils.compat.version import LooseVersion
import ansible_collections.community.zabbix.plugins.module_utils.helpers as zabbix_utils
class Zapi(ZabbixBase):
def __init__(self, module, zbx=None):
super(Zapi, self).__init__(module, zbx)
self._zapi_wrapper = self
def check_if_action_exists(self, name):
"""Check if action exists.
Args:
name: Name of the action.
Returns:
The return value. True for success, False otherwise.
"""
try:
_params = {
"selectOperations": "extend",
"selectRecoveryOperations": "extend",
"selectUpdateOperations": "extend",
"selectFilter": "extend",
"filter": {"name": [name]}
}
_action = self._zapi.action.get(_params)
return _action
except Exception as e:
self._module.fail_json(msg="Failed to check if action '%s' exists: %s" % (name, e))
def get_action_by_name(self, name):
"""Get action by name
Args:
name: Name of the action.
Returns:
dict: Zabbix action
"""
try:
action_list = self._zapi.action.get({
"output": "extend",
"filter": {"name": [name]}
})
if len(action_list) < 1:
self._module.fail_json(msg="Action not found: %s" % name)
else:
return action_list[0]
except Exception as e:
self._module.fail_json(msg="Failed to get ID of '%s': %s" % (name, e))
def get_host_by_host_name(self, host_name):
"""Get host by host name
Args:
host_name: host name.
Returns:
host matching host name
"""
try:
host_list = self._zapi.host.get({
"output": "extend",
"selectInventory": "extend",
"filter": {"host": [host_name]}
})
if len(host_list) < 1:
self._module.fail_json(msg="Host not found: %s" % host_name)
else:
return host_list[0]
except Exception as e:
self._module.fail_json(msg="Failed to get host '%s': %s" % (host_name, e))
def get_hostgroup_by_hostgroup_name(self, hostgroup_name):
"""Get host group by host group name
Args:
hostgroup_name: host group name.
Returns:
host group matching host group name
"""
try:
hostgroup_list = self._zapi.hostgroup.get({
"output": "extend",
"filter": {"name": [hostgroup_name]}
})
if len(hostgroup_list) < 1:
self._module.fail_json(msg="Host group not found: %s" % hostgroup_name)
else:
return hostgroup_list[0]
except Exception as e:
self._module.fail_json(msg="Failed to get host group '%s': %s" % (hostgroup_name, e))
def get_template_by_template_name(self, template_name):
"""Get template by template name
Args:
template_name: template name.
Returns:
template matching template name
"""
try:
template_list = self._zapi.template.get({
"output": "extend",
"filter": {"host": [template_name]}
})
if len(template_list) < 1:
self._module.fail_json(msg="Template not found: %s" % template_name)
else:
return template_list[0]
except Exception as e:
self._module.fail_json(msg="Failed to get template '%s': %s" % (template_name, e))
def get_trigger_by_trigger_name(self, trigger_name):
"""Get trigger by trigger name
Args:
trigger_name: trigger name.
Returns:
trigger matching trigger name
"""
try:
trigger_list = self._zapi.trigger.get({
"output": "extend",
"filter": {"description": [trigger_name]}
})
if len(trigger_list) < 1:
self._module.fail_json(msg="Trigger not found: %s" % trigger_name)
else:
return trigger_list[0]
except Exception as e:
self._module.fail_json(msg="Failed to get trigger '%s': %s" % (trigger_name, e))
def get_discovery_rule_by_discovery_rule_name(self, discovery_rule_name):
"""Get discovery rule by discovery rule name
Args:
discovery_rule_name: discovery rule name.
Returns:
discovery rule matching discovery rule name
"""
try:
discovery_rule_list = self._zapi.drule.get({
"output": "extend",
"filter": {"name": [discovery_rule_name]}
})
if len(discovery_rule_list) < 1:
self._module.fail_json(msg="Discovery rule not found: %s" % discovery_rule_name)
else:
return discovery_rule_list[0]
except Exception as e:
self._module.fail_json(msg="Failed to get discovery rule '%s': %s" % (discovery_rule_name, e))
def get_discovery_check_by_discovery_check_name(self, discovery_check_name):
"""Get discovery check by discovery check name
Args:
discovery_check_name: discovery check name.
Returns:
discovery check matching discovery check name
"""
try:
discovery_rule_name, dcheck_type = discovery_check_name.split(": ")
dcheck_type_to_number = {
"SSH": "0",
"LDAP": "1",
"SMTP": "2",
"FTP": "3",
"HTTP": "4",
"POP": "5",
"NNTP": "6",
"IMAP": "7",
"TCP": "8",
"Zabbix agent": "9",
"SNMPv1 agent": "10",
"SNMPv2 agent": "11",
"ICMP ping": "12",
"SNMPv3 agent": "13",
"HTTPS": "14",
"Telnet": "15"
}
if dcheck_type not in dcheck_type_to_number:
self._module.fail_json(msg="Discovery check type: %s does not exist" % dcheck_type)
discovery_rule_list = self._zapi.drule.get({
"output": ["dchecks"],
"filter": {"name": [discovery_rule_name]},
"selectDChecks": "extend"
})
if len(discovery_rule_list) < 1:
self._module.fail_json(msg="Discovery check not found: %s" % discovery_check_name)
for dcheck in discovery_rule_list[0]["dchecks"]:
if dcheck_type_to_number[dcheck_type] == dcheck["type"]:
return dcheck