zbx_hostgroup.py 4.1 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. server=dict(default='https://localhost/zabbix/api_jsonrpc.php', type='str'),
  43. user=dict(default=None, type='str'),
  44. password=dict(default=None, type='str'),
  45. name=dict(default=None, type='str'),
  46. debug=dict(default=False, type='bool'),
  47. state=dict(default='present', type='str'),
  48. ),
  49. #supports_check_mode=True
  50. )
  51. user = module.params.get('user', os.environ['ZABBIX_USER'])
  52. passwd = module.params.get('password', os.environ['ZABBIX_PASSWORD'])
  53. zapi = ZabbixAPI(ZabbixConnection(module.params['server'], user, passwd, module.params['debug']))
  54. #Set the instance and the template for the rest of the calls
  55. zbx_class_name = 'hostgroup'
  56. idname = "groupid"
  57. hname = module.params['name']
  58. state = module.params['state']
  59. content = zapi.get_content(zbx_class_name,
  60. 'get',
  61. {'search': {'name': hname},
  62. })
  63. if state == 'list':
  64. module.exit_json(changed=False, results=content['result'], state="list")
  65. if state == 'absent':
  66. if not exists(content):
  67. module.exit_json(changed=False, state="absent")
  68. content = zapi.get_content(zbx_class_name, 'delete', [content['result'][0][idname]])
  69. module.exit_json(changed=True, results=content['result'], state="absent")
  70. if state == 'present':
  71. params = {'name': hname}
  72. if not exists(content):
  73. # if we didn't find it, create it
  74. content = zapi.get_content(zbx_class_name, 'create', params)
  75. module.exit_json(changed=True, results=content['result'], state='present')
  76. # already exists, we need to update it
  77. # let's compare properties
  78. differences = {}
  79. zab_results = content['result'][0]
  80. for key, value in params.items():
  81. if zab_results[key] != value and zab_results[key] != str(value):
  82. differences[key] = value
  83. if not differences:
  84. module.exit_json(changed=False, results=zab_results, state="present")
  85. # We have differences and need to update
  86. differences[idname] = zab_results[idname]
  87. content = zapi.get_content(zbx_class_name, 'update', differences)
  88. module.exit_json(changed=True, results=content['result'], state="present")
  89. module.exit_json(failed=True,
  90. changed=False,
  91. results='Unknown state passed. %s' % state,
  92. state="unknown")
  93. # pylint: disable=redefined-builtin, unused-wildcard-import, wildcard-import, locally-disabled
  94. # import module snippets. This are required
  95. from ansible.module_utils.basic import *
  96. main()