zbx_action.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. #!/usr/bin/env python
  2. '''
  3. Ansible module for zabbix actions
  4. '''
  5. # vim: expandtab:tabstop=4:shiftwidth=4
  6. #
  7. # Zabbix action ansible module
  8. #
  9. #
  10. # Copyright 2015 Red Hat Inc.
  11. #
  12. # Licensed under the Apache License, Version 2.0 (the "License");
  13. # you may not use this file except in compliance with the License.
  14. # You may obtain a copy of the License at
  15. #
  16. # http://www.apache.org/licenses/LICENSE-2.0
  17. #
  18. # Unless required by applicable law or agreed to in writing, software
  19. # distributed under the License is distributed on an "AS IS" BASIS,
  20. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  21. # See the License for the specific language governing permissions and
  22. # limitations under the License.
  23. #
  24. # This is in place because each module looks similar to each other.
  25. # These need duplicate code as their behavior is very similar
  26. # but different for each zabbix class.
  27. # pylint: disable=duplicate-code
  28. # pylint: disable=import-error
  29. from openshift_tools.monitoring.zbxapi import ZabbixAPI, ZabbixConnection, ZabbixAPIError
  30. def exists(content, key='result'):
  31. ''' Check if key exists in content or the size of content[key] > 0
  32. '''
  33. if not content.has_key(key):
  34. return False
  35. if not content[key]:
  36. return False
  37. return True
  38. def conditions_equal(zab_conditions, user_conditions):
  39. '''Compare two lists of conditions'''
  40. c_type = 'conditiontype'
  41. _op = 'operator'
  42. val = 'value'
  43. if len(user_conditions) != len(zab_conditions):
  44. return False
  45. for zab_cond, user_cond in zip(zab_conditions, user_conditions):
  46. if zab_cond[c_type] != str(user_cond[c_type]) or zab_cond[_op] != str(user_cond[_op]) or \
  47. zab_cond[val] != str(user_cond[val]):
  48. return False
  49. return True
  50. def filter_differences(zabbix_filters, user_filters):
  51. '''Determine the differences from user and zabbix for operations'''
  52. rval = {}
  53. for key, val in user_filters.items():
  54. if key == 'conditions':
  55. if not conditions_equal(zabbix_filters[key], val):
  56. rval[key] = val
  57. elif zabbix_filters[key] != str(val):
  58. rval[key] = val
  59. return rval
  60. # This logic is quite complex. We are comparing two lists of dictionaries.
  61. # The outer for-loops allow us to descend down into both lists at the same time
  62. # and then walk over the key,val pairs of the incoming user dict's changes
  63. # or updates. The if-statements are looking at different sub-object types and
  64. # comparing them. The other suggestion on how to write this is to write a recursive
  65. # compare function but for the time constraints and for complexity I decided to go
  66. # this route.
  67. # pylint: disable=too-many-branches
  68. def operation_differences(zabbix_ops, user_ops):
  69. '''Determine the differences from user and zabbix for operations'''
  70. # if they don't match, take the user options
  71. if len(zabbix_ops) != len(user_ops):
  72. return user_ops
  73. rval = {}
  74. for zab, user in zip(zabbix_ops, user_ops):
  75. for key, val in user.items():
  76. if key == 'opconditions':
  77. for z_cond, u_cond in zip(zab[key], user[key]):
  78. if not all([str(u_cond[op_key]) == z_cond[op_key] for op_key in \
  79. ['conditiontype', 'operator', 'value']]):
  80. rval[key] = val
  81. break
  82. elif key == 'opmessage':
  83. # Verify each passed param matches
  84. for op_msg_key, op_msg_val in val.items():
  85. if zab[key][op_msg_key] != str(op_msg_val):
  86. rval[key] = val
  87. break
  88. elif key == 'opmessage_grp':
  89. zab_grp_ids = set([ugrp['usrgrpid'] for ugrp in zab[key]])
  90. usr_grp_ids = set([ugrp['usrgrpid'] for ugrp in val])
  91. if usr_grp_ids != zab_grp_ids:
  92. rval[key] = val
  93. elif key == 'opmessage_usr':
  94. zab_usr_ids = set([usr['userid'] for usr in zab[key]])
  95. usr_ids = set([usr['userid'] for usr in val])
  96. if usr_ids != zab_usr_ids:
  97. rval[key] = val
  98. elif zab[key] != str(val):
  99. rval[key] = val
  100. return rval
  101. def get_users(zapi, users):
  102. '''get the mediatype id from the mediatype name'''
  103. rval_users = []
  104. for user in users:
  105. content = zapi.get_content('user',
  106. 'get',
  107. {'filter': {'alias': user}})
  108. rval_users.append({'userid': content['result'][0]['userid']})
  109. return rval_users
  110. def get_user_groups(zapi, groups):
  111. '''get the mediatype id from the mediatype name'''
  112. user_groups = []
  113. content = zapi.get_content('usergroup',
  114. 'get',
  115. {'search': {'name': groups}})
  116. for usr_grp in content['result']:
  117. user_groups.append({'usrgrpid': usr_grp['usrgrpid']})
  118. return user_groups
  119. def get_mediatype_id_by_name(zapi, m_name):
  120. '''get the mediatype id from the mediatype name'''
  121. content = zapi.get_content('mediatype',
  122. 'get',
  123. {'filter': {'description': m_name}})
  124. return content['result'][0]['mediatypeid']
  125. def get_priority(priority):
  126. ''' determine priority
  127. '''
  128. prior = 0
  129. if 'info' in priority:
  130. prior = 1
  131. elif 'warn' in priority:
  132. prior = 2
  133. elif 'avg' == priority or 'ave' in priority:
  134. prior = 3
  135. elif 'high' in priority:
  136. prior = 4
  137. elif 'dis' in priority:
  138. prior = 5
  139. return prior
  140. def get_event_source(from_src):
  141. '''Translate even str into value'''
  142. choices = ['trigger', 'discovery', 'auto', 'internal']
  143. rval = 0
  144. try:
  145. rval = choices.index(from_src)
  146. except ValueError as _:
  147. ZabbixAPIError('Value not found for event source [%s]' % from_src)
  148. return rval
  149. def get_status(inc_status):
  150. '''determine status for action'''
  151. rval = 1
  152. if inc_status == 'enabled':
  153. rval = 0
  154. return rval
  155. def get_condition_operator(inc_operator):
  156. ''' determine the condition operator'''
  157. vals = {'=': 0,
  158. '<>': 1,
  159. 'like': 2,
  160. 'not like': 3,
  161. 'in': 4,
  162. '>=': 5,
  163. '<=': 6,
  164. 'not in': 7,
  165. }
  166. return vals[inc_operator]
  167. def get_host_id_by_name(zapi, host_name):
  168. '''Get host id by name'''
  169. content = zapi.get_content('host',
  170. 'get',
  171. {'filter': {'name': host_name}})
  172. return content['result'][0]['hostid']
  173. def get_trigger_value(inc_trigger):
  174. '''determine the proper trigger value'''
  175. rval = 1
  176. if inc_trigger == 'PROBLEM':
  177. rval = 1
  178. else:
  179. rval = 0
  180. return rval
  181. def get_template_id_by_name(zapi, t_name):
  182. '''get the template id by name'''
  183. content = zapi.get_content('template',
  184. 'get',
  185. {'filter': {'host': t_name}})
  186. return content['result'][0]['templateid']
  187. def get_host_group_id_by_name(zapi, hg_name):
  188. '''Get hostgroup id by name'''
  189. content = zapi.get_content('hostgroup',
  190. 'get',
  191. {'filter': {'name': hg_name}})
  192. return content['result'][0]['groupid']
  193. def get_condition_type(event_source, inc_condition):
  194. '''determine the condition type'''
  195. c_types = {}
  196. if event_source == 'trigger':
  197. c_types = {'host group': 0,
  198. 'host': 1,
  199. 'trigger': 2,
  200. 'trigger name': 3,
  201. 'trigger severity': 4,
  202. 'trigger value': 5,
  203. 'time period': 6,
  204. 'host template': 13,
  205. 'application': 15,
  206. 'maintenance status': 16,
  207. }
  208. elif event_source == 'discovery':
  209. c_types = {'host IP': 7,
  210. 'discovered service type': 8,
  211. 'discovered service port': 9,
  212. 'discovery status': 10,
  213. 'uptime or downtime duration': 11,
  214. 'received value': 12,
  215. 'discovery rule': 18,
  216. 'discovery check': 19,
  217. 'proxy': 20,
  218. 'discovery object': 21,
  219. }
  220. elif event_source == 'auto':
  221. c_types = {'proxy': 20,
  222. 'host name': 22,
  223. 'host metadata': 24,
  224. }
  225. elif event_source == 'internal':
  226. c_types = {'host group': 0,
  227. 'host': 1,
  228. 'host template': 13,
  229. 'application': 15,
  230. 'event type': 23,
  231. }
  232. else:
  233. raise ZabbixAPIError('Unkown event source %s' % event_source)
  234. return c_types[inc_condition]
  235. def get_operation_type(inc_operation):
  236. ''' determine the correct operation type'''
  237. o_types = {'send message': 0,
  238. 'remote command': 1,
  239. 'add host': 2,
  240. 'remove host': 3,
  241. 'add to host group': 4,
  242. 'remove from host group': 5,
  243. 'link to template': 6,
  244. 'unlink from template': 7,
  245. 'enable host': 8,
  246. 'disable host': 9,
  247. }
  248. return o_types[inc_operation]
  249. def get_action_operations(zapi, inc_operations):
  250. '''Convert the operations into syntax for api'''
  251. for operation in inc_operations:
  252. operation['operationtype'] = get_operation_type(operation['operationtype'])
  253. if operation['operationtype'] == 0: # send message. Need to fix the
  254. operation['opmessage']['mediatypeid'] = \
  255. get_mediatype_id_by_name(zapi, operation['opmessage']['mediatypeid'])
  256. operation['opmessage_grp'] = get_user_groups(zapi, operation.get('opmessage_grp', []))
  257. operation['opmessage_usr'] = get_users(zapi, operation.get('opmessage_usr', []))
  258. if operation['opmessage']['default_msg']:
  259. operation['opmessage']['default_msg'] = 1
  260. else:
  261. operation['opmessage']['default_msg'] = 0
  262. # NOT supported for remote commands
  263. elif operation['operationtype'] == 1:
  264. continue
  265. # Handle Operation conditions:
  266. # Currently there is only 1 available which
  267. # is 'event acknowledged'. In the future
  268. # if there are any added we will need to pass this
  269. # option to a function and return the correct conditiontype
  270. if operation.has_key('opconditions'):
  271. for condition in operation['opconditions']:
  272. if condition['conditiontype'] == 'event acknowledged':
  273. condition['conditiontype'] = 14
  274. if condition['operator'] == '=':
  275. condition['operator'] = 0
  276. if condition['value'] == 'acknowledged':
  277. condition['operator'] = 1
  278. else:
  279. condition['operator'] = 0
  280. return inc_operations
  281. def get_operation_evaltype(inc_type):
  282. '''get the operation evaltype'''
  283. rval = 0
  284. if inc_type == 'and/or':
  285. rval = 0
  286. elif inc_type == 'and':
  287. rval = 1
  288. elif inc_type == 'or':
  289. rval = 2
  290. elif inc_type == 'custom':
  291. rval = 3
  292. return rval
  293. def get_action_conditions(zapi, event_source, inc_conditions):
  294. '''Convert the conditions into syntax for api'''
  295. calc_type = inc_conditions.pop('calculation_type')
  296. inc_conditions['evaltype'] = get_operation_evaltype(calc_type)
  297. for cond in inc_conditions['conditions']:
  298. cond['operator'] = get_condition_operator(cond['operator'])
  299. # Based on conditiontype we need to set the proper value
  300. # e.g. conditiontype = hostgroup then the value needs to be a hostgroup id
  301. # e.g. conditiontype = host the value needs to be a host id
  302. cond['conditiontype'] = get_condition_type(event_source, cond['conditiontype'])
  303. if cond['conditiontype'] == 0:
  304. cond['value'] = get_host_group_id_by_name(zapi, cond['value'])
  305. elif cond['conditiontype'] == 1:
  306. cond['value'] = get_host_id_by_name(zapi, cond['value'])
  307. elif cond['conditiontype'] == 4:
  308. cond['value'] = get_priority(cond['value'])
  309. elif cond['conditiontype'] == 5:
  310. cond['value'] = get_trigger_value(cond['value'])
  311. elif cond['conditiontype'] == 13:
  312. cond['value'] = get_template_id_by_name(zapi, cond['value'])
  313. elif cond['conditiontype'] == 16:
  314. cond['value'] = ''
  315. return inc_conditions
  316. def get_send_recovery(send_recovery):
  317. '''Get the integer value'''
  318. rval = 0
  319. if send_recovery:
  320. rval = 1
  321. return rval
  322. # The branches are needed for CRUD and error handling
  323. # pylint: disable=too-many-branches
  324. def main():
  325. '''
  326. ansible zabbix module for zbx_item
  327. '''
  328. module = AnsibleModule(
  329. argument_spec=dict(
  330. zbx_server=dict(default='https://localhost/zabbix/api_jsonrpc.php', type='str'),
  331. zbx_user=dict(default=os.environ.get('ZABBIX_USER', None), type='str'),
  332. zbx_password=dict(default=os.environ.get('ZABBIX_PASSWORD', None), type='str'),
  333. zbx_debug=dict(default=False, type='bool'),
  334. name=dict(default=None, type='str'),
  335. event_source=dict(default='trigger', choices=['trigger', 'discovery', 'auto', 'internal'], type='str'),
  336. action_subject=dict(default="{TRIGGER.NAME}: {TRIGGER.STATUS}", type='str'),
  337. action_message=dict(default="{TRIGGER.NAME}: {TRIGGER.STATUS}\r\n" +
  338. "Last value: {ITEM.LASTVALUE}\r\n\r\n{TRIGGER.URL}", type='str'),
  339. reply_subject=dict(default="{TRIGGER.NAME}: {TRIGGER.STATUS}", type='str'),
  340. reply_message=dict(default="Trigger: {TRIGGER.NAME}\r\nTrigger status: {TRIGGER.STATUS}\r\n" +
  341. "Trigger severity: {TRIGGER.SEVERITY}\r\nTrigger URL: {TRIGGER.URL}\r\n\r\n" +
  342. "Item values:\r\n\r\n1. {ITEM.NAME1} ({HOST.NAME1}:{ITEM.KEY1}): " +
  343. "{ITEM.VALUE1}\r\n2. {ITEM.NAME2} ({HOST.NAME2}:{ITEM.KEY2}): " +
  344. "{ITEM.VALUE2}\r\n3. {ITEM.NAME3} ({HOST.NAME3}:{ITEM.KEY3}): " +
  345. "{ITEM.VALUE3}", type='str'),
  346. send_recovery=dict(default=False, type='bool'),
  347. status=dict(default=None, type='str'),
  348. escalation_time=dict(default=60, type='int'),
  349. conditions_filter=dict(default=None, type='dict'),
  350. operations=dict(default=None, type='list'),
  351. state=dict(default='present', type='str'),
  352. ),
  353. #supports_check_mode=True
  354. )
  355. zapi = ZabbixAPI(ZabbixConnection(module.params['zbx_server'],
  356. module.params['zbx_user'],
  357. module.params['zbx_password'],
  358. module.params['zbx_debug']))
  359. #Set the instance and the template for the rest of the calls
  360. zbx_class_name = 'action'
  361. state = module.params['state']
  362. content = zapi.get_content(zbx_class_name,
  363. 'get',
  364. {'search': {'name': module.params['name']},
  365. 'selectFilter': 'extend',
  366. 'selectOperations': 'extend',
  367. })
  368. #******#
  369. # GET
  370. #******#
  371. if state == 'list':
  372. module.exit_json(changed=False, results=content['result'], state="list")
  373. #******#
  374. # DELETE
  375. #******#
  376. if state == 'absent':
  377. if not exists(content):
  378. module.exit_json(changed=False, state="absent")
  379. content = zapi.get_content(zbx_class_name, 'delete', [content['result'][0]['itemid']])
  380. module.exit_json(changed=True, results=content['result'], state="absent")
  381. # Create and Update
  382. if state == 'present':
  383. conditions = get_action_conditions(zapi, module.params['event_source'], module.params['conditions_filter'])
  384. operations = get_action_operations(zapi, module.params['operations'])
  385. params = {'name': module.params['name'],
  386. 'esc_period': module.params['escalation_time'],
  387. 'eventsource': get_event_source(module.params['event_source']),
  388. 'status': get_status(module.params['status']),
  389. 'def_shortdata': module.params['action_subject'],
  390. 'def_longdata': module.params['action_message'],
  391. 'r_shortdata': module.params['reply_subject'],
  392. 'r_longdata': module.params['reply_message'],
  393. 'recovery_msg': get_send_recovery(module.params['send_recovery']),
  394. 'filter': conditions,
  395. 'operations': operations,
  396. }
  397. # Remove any None valued params
  398. _ = [params.pop(key, None) for key in params.keys() if params[key] is None]
  399. #******#
  400. # CREATE
  401. #******#
  402. if not exists(content):
  403. content = zapi.get_content(zbx_class_name, 'create', params)
  404. if content.has_key('error'):
  405. module.exit_json(failed=True, changed=True, results=content['error'], state="present")
  406. module.exit_json(changed=True, results=content['result'], state='present')
  407. ########
  408. # UPDATE
  409. ########
  410. _ = params.pop('hostid', None)
  411. differences = {}
  412. zab_results = content['result'][0]
  413. for key, value in params.items():
  414. if key == 'operations':
  415. ops = operation_differences(zab_results[key], value)
  416. if ops:
  417. differences[key] = ops
  418. elif key == 'filter':
  419. filters = filter_differences(zab_results[key], value)
  420. if filters:
  421. differences[key] = filters
  422. elif zab_results[key] != value and zab_results[key] != str(value):
  423. differences[key] = value
  424. if not differences:
  425. module.exit_json(changed=False, results=zab_results, state="present")
  426. # We have differences and need to update.
  427. # action update requires an id, filters, and operations
  428. differences['actionid'] = zab_results['actionid']
  429. differences['operations'] = params['operations']
  430. differences['filter'] = params['filter']
  431. content = zapi.get_content(zbx_class_name, 'update', differences)
  432. if content.has_key('error'):
  433. module.exit_json(failed=True, changed=False, results=content['error'], state="present")
  434. module.exit_json(changed=True, results=content['result'], state="present")
  435. module.exit_json(failed=True,
  436. changed=False,
  437. results='Unknown state passed. %s' % state,
  438. state="unknown")
  439. # pylint: disable=redefined-builtin, unused-wildcard-import, wildcard-import, locally-disabled
  440. # import module snippets. This are required
  441. from ansible.module_utils.basic import *
  442. main()