| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
# |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
from __future__ import absolute_import, division, print_function |
| 7 |
__metaclass__ = type |
| 8 |
|
| 9 |
|
| 10 |
DOCUMENTATION = r''' |
| 11 |
--- |
| 12 |
module: zabbix_host_events_update |
| 13 |
short_description: update the status of event(s). |
| 14 |
description: |
| 15 |
- Updates the status of event(s). |
| 16 |
author: |
| 17 |
- "Andrew Lathrop (@aplathrop)" |
| 18 |
requirements: |
| 19 |
- "python >= 2.6" |
| 20 |
|
| 21 |
options: |
| 22 |
params: |
| 23 |
description: |
| 24 |
- Parameters to update event(s) with. |
| 25 |
- Parameters as defined at https://www.zabbix.com/documentation/current/en/manual/api/reference/event/acknowledge |
| 26 |
- Additionally supported parameters are below |
| 27 |
required: true |
| 28 |
type: dict |
| 29 |
suboptions: |
| 30 |
action: |
| 31 |
description: |
| 32 |
- action to update the event with |
| 33 |
- Overrides "action" in API docs |
| 34 |
- Required when I(actions) is not used. |
| 35 |
- Mutually exclusive with I(actions). |
| 36 |
required: false |
| 37 |
type: str |
| 38 |
choices: |
| 39 |
- close_problem |
| 40 |
- close |
| 41 |
- acknowledge_event |
| 42 |
- acknowledge |
| 43 |
- ack |
| 44 |
- add_message |
| 45 |
- message |
| 46 |
- msg |
| 47 |
- change_severity |
| 48 |
- severity |
| 49 |
- unacknowledge_event |
| 50 |
- unacknowledge |
| 51 |
- unack |
| 52 |
- suppress_event |
| 53 |
- suppress |
| 54 |
- unsuppress_event |
| 55 |
- unsuppress |
| 56 |
- change_event_rank_to_cause |
| 57 |
- convert_to_cause |
| 58 |
- change_event_rank_to_symptom |
| 59 |
- convert_to_symptom |
| 60 |
actions: |
| 61 |
description: |
| 62 |
- actions to update the event with |
| 63 |
- Overrides "action" in API docs |
| 64 |
- Required when I(action) is not used. |
| 65 |
- Mutually exclusive with I(action). |
| 66 |
required: false |
| 67 |
type: list |
| 68 |
elements: str |
| 69 |
choices: |
| 70 |
- close_problem |
| 71 |
- close |
| 72 |
- acknowledge_event |
| 73 |
- acknowledge |
| 74 |
- ack |
| 75 |
- add_message |
| 76 |
- message |
| 77 |
- msg |
| 78 |
- change_severity |
| 79 |
- severity |
| 80 |
- unacknowledge_event |
| 81 |
- unacknowledge |
| 82 |
- unack |
| 83 |
- suppress_event |
| 84 |
- suppress |
| 85 |
- unsuppress_event |
| 86 |
- unsuppress |
| 87 |
- change_event_rank_to_cause |
| 88 |
- convert_to_cause |
| 89 |
- change_event_rank_to_symptom |
| 90 |
- convert_to_symptom |
| 91 |
severity: |
| 92 |
description: |
| 93 |
- New severity for events. |
| 94 |
- Overrides "severity" in API docs |
| 95 |
required: False |
| 96 |
type: str |
| 97 |
choices: |
| 98 |
- not_classified |
| 99 |
- information |
| 100 |
- warning |
| 101 |
- average |
| 102 |
- high |
| 103 |
- disaster |
| 104 |
msg: |
| 105 |
description: |
| 106 |
- Text of the message. |
| 107 |
- Alias for "message" in API docs |
| 108 |
required: False |
| 109 |
type: str |
| 110 |
|
| 111 |
extends_documentation_fragment: |
| 112 |
- community.zabbix.zabbix |
| 113 |
''' |
| 114 |
|
| 115 |
EXAMPLES = r''' |
| 116 |
|
| 117 |
# If you want to use Username and Password to be authenticated by Zabbix Server |
| 118 |
- name: Set credentials to access Zabbix Server API |
| 119 |
ansible.builtin.set_fact: |
| 120 |
ansible_user: Admin |
| 121 |
ansible_httpapi_pass: zabbix |
| 122 |
|
| 123 |
# If you want to use API token to be authenticated by Zabbix Server |
| 124 |
# https://www.zabbix.com/documentation/current/en/manual/web_interface/frontend_sections/administration/general#api-tokens |
| 125 |
- name: Set API token |
| 126 |
ansible.builtin.set_fact: |
| 127 |
ansible_zabbix_auth_key: 8ec0d52432c15c91fcafe9888500cf9a607f44091ab554dbee860f6b44fac895 |
| 128 |
|
| 129 |
# Acknowledge single event |
| 130 |
- name: ack event |
| 131 |
community.zabbix.zabbix_host_events_update: |
| 132 |
params: |
| 133 |
eventids: 12345 |
| 134 |
actions: ack |
| 135 |
|
| 136 |
- name: ack and close event with a message |
| 137 |
community.zabbix.zabbix_host_events_update: |
| 138 |
params: |
| 139 |
eventids: [12345, 67890] |
| 140 |
actions: ['ack', 'msg', 'close'] |
| 141 |
msg: 'closed by user' |
| 142 |
|
| 143 |
''' |
| 144 |
|
| 145 |
from ansible.module_utils.basic import AnsibleModule |
| 146 |
|
| 147 |
from ansible_collections.community.zabbix.plugins.module_utils.base import ZabbixBase |
| 148 |
import ansible_collections.community.zabbix.plugins.module_utils.helpers as zabbix_utils |
| 149 |
|
| 150 |
|
| 151 |
class Hosteventsupdate(ZabbixBase): |
| 152 |
ACTIONS = {'close_problem': 1, |
| 153 |
'close': 1, |
| 154 |
'acknowledge_event': 2, |
| 155 |
'acknowledge': 2, |
| 156 |
'ack': 2, |
| 157 |
'add_message': 4, |
| 158 |
'message': 4, |
| 159 |
'msg': 4, |
| 160 |
'change_severity': 8, |
| 161 |
'severity': 8, |
| 162 |
'unacknowledge_event': 16, |
| 163 |
'unacknowledge': 16, |
| 164 |
'unack': 16, |
| 165 |
'suppress_event': 32, |
| 166 |
'suppress': 32, |
| 167 |
'unsuppress_event': 64, |
| 168 |
'unsuppress': 64, |
| 169 |
'change_event_rank_to_cause': 128, |
| 170 |
'convert_to_cause': 128, |
| 171 |
'change_event_rank_to_symptom': 256, |
| 172 |
'convert_to_symptom': 256} |
| 173 |
|
| 174 |
SEVERITY_TYPES = {'not_classified': 0, |
| 175 |
'information': 1, |
| 176 |
'warning': 2, |
| 177 |
'average': 3, |
| 178 |
'high': 4, |
| 179 |
'disaster': 5} |
| 180 |
|
| 181 |
def get_events(self, eventids): |
| 182 |
try: |
| 183 |
results = self._zapi.event.get({'eventids': eventids}) |
| 184 |
except Exception as e: |
| 185 |
self._module.fail_json(msg="Failed to get event: %s" % e) |
| 186 |
return results |
| 187 |
|
| 188 |
def update_event(self, params): |
| 189 |
if 'severity' in params: |
| 190 |
if params['severity'] not in self.SEVERITY_TYPES: |
| 191 |
self._module.fail_json(msg="%s is not a valid severity type" % params['severity']) |
| 192 |
severity = self.SEVERITY_TYPES[params['severity']] |
| 193 |
params['severity'] = severity |
| 194 |
if 'action' in params: |
| 195 |
if params['action'] not in self.ACTIONS: |
| 196 |
self._module.fail_json(msg="%s is not a valid action" % params['action']) |
| 197 |
action_id = self.ACTIONS[params['action']] |
| 198 |
elif 'actions' in params: |
| 199 |
action_id = 0 |
| 200 |
for action in params['actions']: |
| 201 |
if action not in self.ACTIONS: |
| 202 |
self._module.fail_json(msg="%s is not a valid action" % action) |
| 203 |
action_id += self.ACTIONS[action] |
| 204 |
params.pop('actions') |
| 205 |
else: |
| 206 |
self._module.fail_json(msg="params must contain either 'action' or 'actions'") |
| 207 |
params['action'] = action_id |
| 208 |
if 'msg' in params: |
| 209 |
params['message'] = params['msg'] |
| 210 |
params.pop('msg') |
| 211 |
if self._module.check_mode: |
| 212 |
self._module.exit_json(changed=True) |
| 213 |
try: |
| 214 |
results = self._zapi.event.acknowledge(params) |
| 215 |
except Exception as e: |
| 216 |
self._module.fail_json(msg="Failed to update event: %s" % e) |
| 217 |
return results |
| 218 |
|
| 219 |
def check_events_changed(self, eventids, old_events): |
| 220 |
new_events = self.get_events(eventids) |
| 221 |
return old_events != new_events |
| 222 |
|
| 223 |
|
| 224 |
def main(): |
| 225 |
argument_spec = zabbix_utils.zabbix_common_argument_spec() |
| 226 |
argument_spec.update( |
| 227 |
params=dict(type='dict', required=True)) |
| 228 |
module = AnsibleModule( |
| 229 |
argument_spec=argument_spec, |
| 230 |
supports_check_mode=True |
| 231 |
) |
| 232 |
|
| 233 |
params = module.params['params'] |
| 234 |
|
| 235 |
hosteventsupdate = Hosteventsupdate(module) |
| 236 |
|
| 237 |
events = hosteventsupdate.get_events(params['eventids']) |
| 238 |
results = hosteventsupdate.update_event(params) |
| 239 |
changed = hosteventsupdate.check_events_changed(params['eventids'], events) |
| 240 |
module.exit_json(changed=changed, result=results) |
| 241 |
|
| 242 |
|
| 243 |
if __name__ == '__main__': |
| 244 |
main() |