zbx_hostgroup.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #!/usr/bin/env python
  2. ''' Ansible module for hostgroup
  3. '''
  4. # vim: expandtab:tabstop=4:shiftwidth=4
  5. #
  6. # Zabbix hostgroup ansible module
  7. #
  8. #
  9. # Copyright 2015 Red Hat Inc.
  10. #
  11. # Licensed under the Apache License, Version 2.0 (the "License");
  12. # you may not use this file except in compliance with the License.
  13. # You may obtain a copy of the License at
  14. #
  15. # http://www.apache.org/licenses/LICENSE-2.0
  16. #
  17. # Unless required by applicable law or agreed to in writing, software
  18. # distributed under the License is distributed on an "AS IS" BASIS,
  19. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. # See the License for the specific language governing permissions and
  21. # limitations under the License.
  22. #
  23. # This is in place because each module looks similar to each other.
  24. # These need duplicate code as their behavior is very similar
  25. # but different for each zabbix class.
  26. # pylint: disable=duplicate-code
  27. # pylint: disable=import-error
  28. from openshift_tools.monitoring.zbxapi import ZabbixAPI, ZabbixConnection
  29. def exists(content, key='result'):
  30. ''' Check if key exists in content or the size of content[key] > 0
  31. '''
  32. if not content.has_key(key):
  33. return False
  34. if not content[key]:
  35. return False
  36. return True
  37. def main():
  38. ''' ansible module for hostgroup
  39. '''
  40. module = AnsibleModule(
  41. argument_spec=dict(
  42. zbx_server=dict(default='https://localhost/zabbix/api_jsonrpc.php', type='str'),
  43. zbx_user=dict(default=os.environ.get('ZABBIX_USER', None), type='str'),
  44. zbx_password=dict(default=os.environ.get('ZABBIX_PASSWORD', None), type='str'),
  45. zbx_debug=dict(default=False, type='bool'),
  46. name=dict(default=None, type='str'),
  47. state=dict(default='present', type='str'),
  48. ),
  49. #supports_check_mode=True
  50. )
  51. zapi = ZabbixAPI(ZabbixConnection(module.params['zbx_server'],
  52. module.params['zbx_user'],
  53. module.params['zbx_password'],
  54. module.params['zbx_debug']))
  55. #Set the instance and the template for the rest of the calls
  56. zbx_class_name = 'hostgroup'
  57. idname = "groupid"
  58. hname = module.params['name']
  59. state = module.params['state']
  60. content = zapi.get_content(zbx_class_name,
  61. 'get',
  62. {'search': {'name': hname},
  63. })
  64. if state == 'list':
  65. module.exit_json(changed=False, results=content['result'], state="list")
  66. if state == 'absent':
  67. if not exists(content):
  68. module.exit_json(changed=False, state="absent")
  69. content = zapi.get_content(zbx_class_name, 'delete', [content['result'][0][idname]])
  70. module.exit_json(changed=True, results=content['result'], state="absent")
  71. if state == 'present':
  72. params = {'name': hname}
  73. if not exists(content):
  74. # if we didn't find it, create it
  75. content = zapi.get_content(zbx_class_name, 'create', params)
  76. module.exit_json(changed=True, results=content['result'], state='present')
  77. # already exists, we need to update it
  78. # let's compare properties
  79. differences = {}
  80. zab_results = content['result'][0]
  81. for key, value in params.items():
  82. if zab_results[key] != value and zab_results[key] != str(value):
  83. differences[key] = value
  84. if not differences:
  85. module.exit_json(changed=False, results=zab_results, state="present")
  86. # We have differences and need to update
  87. differences[idname] = zab_results[idname]
  88. content = zapi.get_content(zbx_class_name, 'update', differences)
  89. module.exit_json(changed=True, results=content['result'], state="present")
  90. module.exit_json(failed=True,
  91. changed=False,
  92. results='Unknown state passed. %s' % state,
  93. state="unknown")
  94. # pylint: disable=redefined-builtin, unused-wildcard-import, wildcard-import, locally-disabled
  95. # import module snippets. This are required
  96. from ansible.module_utils.basic import *
  97. main()