zbx_template.py 5.0 KB

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