zbx_template.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. server=dict(default='https://localhost/zabbix/api_jsonrpc.php', type='str'),
  44. user=dict(default=None, type='str'),
  45. password=dict(default=None, type='str'),
  46. name=dict(default=None, type='str'),
  47. debug=dict(default=False, type='bool'),
  48. state=dict(default='present', type='str'),
  49. ),
  50. #supports_check_mode=True
  51. )
  52. user = module.params.get('user', os.environ['ZABBIX_USER'])
  53. passwd = module.params.get('password', os.environ['ZABBIX_PASSWORD'])
  54. zbc = ZabbixConnection(module.params['server'], user, passwd, module.params['debug'])
  55. zapi = ZabbixAPI(zbc)
  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': extend,
  68. })
  69. if state == 'list':
  70. module.exit_json(changed=False, results=content['result'], state="list")
  71. if state == 'absent':
  72. if not exists(content):
  73. module.exit_json(changed=False, state="absent")
  74. content = zapi.get_content(zbx_class_name, 'delete', [content['result'][0][idname]])
  75. module.exit_json(changed=True, results=content['result'], state="absent")
  76. if state == 'present':
  77. params = {'groups': module.params.get('groups', [{'groupid': '1'}]),
  78. 'host': tname,
  79. }
  80. if not exists(content):
  81. # if we didn't find it, create it
  82. content = zapi.get_content(zbx_class_name, 'create', params)
  83. module.exit_json(changed=True, results=content['result'], state='present')
  84. # already exists, we need to update it
  85. # let's compare properties
  86. differences = {}
  87. zab_results = content['result'][0]
  88. for key, value in params.items():
  89. if key == 'templates' and zab_results.has_key('parentTemplates'):
  90. if zab_results['parentTemplates'] != value:
  91. differences[key] = value
  92. elif zab_results[key] != str(value) and zab_results[key] != value:
  93. differences[key] = value
  94. if not differences:
  95. module.exit_json(changed=False, results=content['result'], state="present")
  96. # We have differences and need to update
  97. differences[idname] = zab_results[idname]
  98. content = zapi.get_content(zbx_class_name, 'update', differences)
  99. module.exit_json(changed=True, results=content['result'], state="present")
  100. module.exit_json(failed=True,
  101. changed=False,
  102. results='Unknown state passed. %s' % state,
  103. state="unknown")
  104. # pylint: disable=redefined-builtin, unused-wildcard-import, wildcard-import, locally-disabled
  105. # import module snippets. This are required
  106. from ansible.module_utils.basic import *
  107. main()