zbx_application.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #!/usr/bin/env python
  2. '''
  3. Ansible module for application
  4. '''
  5. # vim: expandtab:tabstop=4:shiftwidth=4
  6. #
  7. # Zabbix application 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
  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 get_template_ids(zapi, template_names):
  39. '''
  40. get related templates
  41. '''
  42. template_ids = []
  43. # Fetch templates by name
  44. for template_name in template_names:
  45. content = zapi.get_content('template', 'get', {'search': {'host': template_name}})
  46. if content.has_key('result'):
  47. template_ids.append(content['result'][0]['templateid'])
  48. return template_ids
  49. def main():
  50. ''' Ansible module for application
  51. '''
  52. module = AnsibleModule(
  53. argument_spec=dict(
  54. server=dict(default='https://localhost/zabbix/api_jsonrpc.php', type='str'),
  55. user=dict(default=None, type='str'),
  56. password=dict(default=None, type='str'),
  57. name=dict(default=None, type='str'),
  58. template_name=dict(default=None, type='list'),
  59. debug=dict(default=False, type='bool'),
  60. state=dict(default='present', type='str'),
  61. ),
  62. #supports_check_mode=True
  63. )
  64. user = module.params.get('user', os.environ['ZABBIX_USER'])
  65. passwd = module.params.get('password', os.environ['ZABBIX_PASSWORD'])
  66. zapi = ZabbixAPI(ZabbixConnection(module.params['server'], user, passwd, module.params['debug']))
  67. #Set the instance and the application for the rest of the calls
  68. zbx_class_name = 'application'
  69. idname = 'applicationid'
  70. aname = module.params['name']
  71. state = module.params['state']
  72. # get a applicationid, see if it exists
  73. content = zapi.get_content(zbx_class_name,
  74. 'get',
  75. {'search': {'host': aname},
  76. 'selectHost': 'hostid',
  77. })
  78. if state == 'list':
  79. module.exit_json(changed=False, results=content['result'], state="list")
  80. if state == 'absent':
  81. if not exists(content):
  82. module.exit_json(changed=False, state="absent")
  83. content = zapi.get_content(zbx_class_name, 'delete', [content['result'][0][idname]])
  84. module.exit_json(changed=True, results=content['result'], state="absent")
  85. if state == 'present':
  86. params = {'hostid': get_template_ids(zapi, module.params['template_name'])[0],
  87. 'name': aname,
  88. }
  89. if not exists(content):
  90. # if we didn't find it, create it
  91. content = zapi.get_content(zbx_class_name, 'create', params)
  92. module.exit_json(changed=True, results=content['result'], state='present')
  93. # already exists, we need to update it
  94. # let's compare properties
  95. differences = {}
  96. zab_results = content['result'][0]
  97. for key, value in params.items():
  98. if key == 'templates' and zab_results.has_key('parentTemplates'):
  99. if zab_results['parentTemplates'] != value:
  100. differences[key] = value
  101. elif zab_results[key] != str(value) and zab_results[key] != value:
  102. differences[key] = value
  103. if not differences:
  104. module.exit_json(changed=False, results=content['result'], state="present")
  105. # We have differences and need to update
  106. differences[idname] = zab_results[idname]
  107. content = zapi.get_content(zbx_class_name, 'update', differences)
  108. module.exit_json(changed=True, results=content['result'], state="present")
  109. module.exit_json(failed=True,
  110. changed=False,
  111. results='Unknown state passed. %s' % state,
  112. state="unknown")
  113. # pylint: disable=redefined-builtin, unused-wildcard-import, wildcard-import, locally-disabled
  114. # import module snippets. This are required
  115. from ansible.module_utils.basic import *
  116. main()