diff options
Diffstat (limited to 'roles')
40 files changed, 1108 insertions, 372 deletions
diff --git a/roles/lib_zabbix/README.md b/roles/lib_zabbix/README.md new file mode 100644 index 000000000..69debc698 --- /dev/null +++ b/roles/lib_zabbix/README.md @@ -0,0 +1,38 @@ +zabbix +========= + +Automate zabbix tasks. + +Requirements +------------ + +This requires the openshift_tools rpm be installed for the zbxapi.py library. It can be found here: https://github.com/openshift/openshift-tools under openshift_tools/monitoring/zbxapi.py for now. + +Role Variables +-------------- + +None + +Dependencies +------------ + +This depeonds on the zbxapi.py library located here: https://github.com/openshift/openshift-tools under openshift_tools/monitoring/zbxapi.py for now. + +Example Playbook +---------------- + + - zbx_host: + server: zab_server + user: zab_user + password: zab_password + name: 'myhost' + +License +------- + +ASL 2.0 + +Author Information +------------------ + +OpenShift operations, Red Hat, Inc diff --git a/roles/lib_zabbix/library/__init__.py b/roles/lib_zabbix/library/__init__.py new file mode 100644 index 000000000..0c7e19e41 --- /dev/null +++ b/roles/lib_zabbix/library/__init__.py @@ -0,0 +1,3 @@ +''' +ZabbixAPI ansible module +''' diff --git a/roles/os_zabbix/library/zbx_application.py b/roles/lib_zabbix/library/zbx_application.py index 5d4acf72d..d3d08c9dd 100644 --- a/roles/os_zabbix/library/zbx_application.py +++ b/roles/lib_zabbix/library/zbx_application.py @@ -59,21 +59,21 @@ def main(): module = AnsibleModule( argument_spec=dict( - server=dict(default='https://localhost/zabbix/api_jsonrpc.php', type='str'), - user=dict(default=None, type='str'), - password=dict(default=None, type='str'), + zbx_server=dict(default='https://localhost/zabbix/api_jsonrpc.php', type='str'), + zbx_user=dict(default=os.environ.get('ZABBIX_USER', None), type='str'), + zbx_password=dict(default=os.environ.get('ZABBIX_PASSWORD', None), type='str'), + zbx_debug=dict(default=False, type='bool'), name=dict(default=None, type='str'), template_name=dict(default=None, type='list'), - debug=dict(default=False, type='bool'), state=dict(default='present', type='str'), ), #supports_check_mode=True ) - user = module.params.get('user', os.environ['ZABBIX_USER']) - passwd = module.params.get('password', os.environ['ZABBIX_PASSWORD']) - - zapi = ZabbixAPI(ZabbixConnection(module.params['server'], user, passwd, module.params['debug'])) + zapi = ZabbixAPI(ZabbixConnection(module.params['zbx_server'], + module.params['zbx_user'], + module.params['zbx_password'], + module.params['zbx_debug'])) #Set the instance and the application for the rest of the calls zbx_class_name = 'application' @@ -83,7 +83,7 @@ def main(): # get a applicationid, see if it exists content = zapi.get_content(zbx_class_name, 'get', - {'search': {'host': aname}, + {'search': {'name': aname}, 'selectHost': 'hostid', }) if state == 'list': @@ -121,6 +121,10 @@ def main(): # We have differences and need to update differences[idname] = zab_results[idname] content = zapi.get_content(zbx_class_name, 'update', differences) + + if content.has_key('error'): + module.exit_json(failed=True, changed=False, results=content['error'], state="present") + module.exit_json(changed=True, results=content['result'], state="present") module.exit_json(failed=True, diff --git a/roles/os_zabbix/library/zbx_discoveryrule.py b/roles/lib_zabbix/library/zbx_discoveryrule.py index 56b87fecc..71a0580c2 100644 --- a/roles/os_zabbix/library/zbx_discoveryrule.py +++ b/roles/lib_zabbix/library/zbx_discoveryrule.py @@ -87,9 +87,10 @@ def main(): module = AnsibleModule( argument_spec=dict( - server=dict(default='https://localhost/zabbix/api_jsonrpc.php', type='str'), - user=dict(default=os.environ['ZABBIX_USER'], type='str'), - password=dict(default=os.environ['ZABBIX_PASSWORD'], type='str'), + zbx_server=dict(default='https://localhost/zabbix/api_jsonrpc.php', type='str'), + zbx_user=dict(default=os.environ.get('ZABBIX_USER', None), type='str'), + zbx_password=dict(default=os.environ.get('ZABBIX_PASSWORD', None), type='str'), + zbx_debug=dict(default=False, type='bool'), name=dict(default=None, type='str'), key=dict(default=None, type='str'), interfaceid=dict(default=None, type='int'), @@ -97,16 +98,15 @@ def main(): delay=dict(default=60, type='int'), lifetime=dict(default=30, type='int'), template_name=dict(default=[], type='list'), - debug=dict(default=False, type='bool'), state=dict(default='present', type='str'), ), #supports_check_mode=True ) - user = module.params['user'] - passwd = module.params['password'] - - zapi = ZabbixAPI(ZabbixConnection(module.params['server'], user, passwd, module.params['debug'])) + zapi = ZabbixAPI(ZabbixConnection(module.params['zbx_server'], + module.params['zbx_user'], + module.params['zbx_password'], + module.params['zbx_debug'])) #Set the instance and the template for the rest of the calls zbx_class_name = 'discoveryrule' diff --git a/roles/os_zabbix/library/zbx_host.py b/roles/lib_zabbix/library/zbx_host.py index 12c5f3456..e26c9caf3 100644 --- a/roles/os_zabbix/library/zbx_host.py +++ b/roles/lib_zabbix/library/zbx_host.py @@ -70,23 +70,23 @@ def main(): module = AnsibleModule( argument_spec=dict( - server=dict(default='https://localhost/zabbix/api_jsonrpc.php', type='str'), - user=dict(default=os.environ['ZABBIX_USER'], type='str'), - password=dict(default=os.environ['ZABBIX_PASSWORD'], type='str'), + zbx_server=dict(default='https://localhost/zabbix/api_jsonrpc.php', type='str'), + zbx_user=dict(default=os.environ.get('ZABBIX_USER', None), type='str'), + zbx_password=dict(default=os.environ.get('ZABBIX_PASSWORD', None), type='str'), + zbx_debug=dict(default=False, type='bool'), name=dict(default=None, type='str'), hostgroup_names=dict(default=[], type='list'), template_names=dict(default=[], type='list'), - debug=dict(default=False, type='bool'), state=dict(default='present', type='str'), interfaces=dict(default=None, type='list'), ), #supports_check_mode=True ) - user = module.params['user'] - passwd = module.params['password'] - - zapi = ZabbixAPI(ZabbixConnection(module.params['server'], user, passwd, module.params['debug'])) + zapi = ZabbixAPI(ZabbixConnection(module.params['zbx_server'], + module.params['zbx_user'], + module.params['zbx_password'], + module.params['zbx_debug'])) #Set the instance and the template for the rest of the calls zbx_class_name = 'host' diff --git a/roles/os_zabbix/library/zbx_hostgroup.py b/roles/lib_zabbix/library/zbx_hostgroup.py index a1eb875d4..7f080af24 100644 --- a/roles/os_zabbix/library/zbx_hostgroup.py +++ b/roles/lib_zabbix/library/zbx_hostgroup.py @@ -46,20 +46,20 @@ def main(): module = AnsibleModule( argument_spec=dict( - server=dict(default='https://localhost/zabbix/api_jsonrpc.php', type='str'), - user=dict(default=None, type='str'), - password=dict(default=None, type='str'), + zbx_server=dict(default='https://localhost/zabbix/api_jsonrpc.php', type='str'), + zbx_user=dict(default=os.environ.get('ZABBIX_USER', None), type='str'), + zbx_password=dict(default=os.environ.get('ZABBIX_PASSWORD', None), type='str'), + zbx_debug=dict(default=False, type='bool'), name=dict(default=None, type='str'), - debug=dict(default=False, type='bool'), state=dict(default='present', type='str'), ), #supports_check_mode=True ) - user = module.params.get('user', os.environ['ZABBIX_USER']) - passwd = module.params.get('password', os.environ['ZABBIX_PASSWORD']) - - zapi = ZabbixAPI(ZabbixConnection(module.params['server'], user, passwd, module.params['debug'])) + zapi = ZabbixAPI(ZabbixConnection(module.params['zbx_server'], + module.params['zbx_user'], + module.params['zbx_password'], + module.params['zbx_debug'])) #Set the instance and the template for the rest of the calls zbx_class_name = 'hostgroup' diff --git a/roles/os_zabbix/library/zbx_item.py b/roles/lib_zabbix/library/zbx_item.py index 45ba6c2b0..bcd389e47 100644 --- a/roles/os_zabbix/library/zbx_item.py +++ b/roles/lib_zabbix/library/zbx_item.py @@ -63,6 +63,8 @@ def get_value_type(value_type): def get_app_ids(zapi, application_names): ''' get application ids from names ''' + if isinstance(application_names, str): + application_names = [application_names] app_ids = [] for app_name in application_names: content = zapi.get_content('application', 'get', {'search': {'name': app_name}}) @@ -77,25 +79,25 @@ def main(): module = AnsibleModule( argument_spec=dict( - server=dict(default='https://localhost/zabbix/api_jsonrpc.php', type='str'), - user=dict(default=None, type='str'), - password=dict(default=None, type='str'), + zbx_server=dict(default='https://localhost/zabbix/api_jsonrpc.php', type='str'), + zbx_user=dict(default=os.environ.get('ZABBIX_USER', None), type='str'), + zbx_password=dict(default=os.environ.get('ZABBIX_PASSWORD', None), type='str'), + zbx_debug=dict(default=False, type='bool'), name=dict(default=None, type='str'), key=dict(default=None, type='str'), template_name=dict(default=None, type='str'), zabbix_type=dict(default=2, type='int'), value_type=dict(default='int', type='str'), applications=dict(default=[], type='list'), - debug=dict(default=False, type='bool'), state=dict(default='present', type='str'), ), #supports_check_mode=True ) - user = module.params.get('user', os.environ['ZABBIX_USER']) - passwd = module.params.get('password', os.environ['ZABBIX_PASSWORD']) - - zapi = ZabbixAPI(ZabbixConnection(module.params['server'], user, passwd, module.params['debug'])) + zapi = ZabbixAPI(ZabbixConnection(module.params['zbx_server'], + module.params['zbx_user'], + module.params['zbx_password'], + module.params['zbx_debug'])) #Set the instance and the template for the rest of the calls zbx_class_name = 'item' @@ -118,6 +120,7 @@ def main(): {'search': {'key_': key}, 'selectApplications': 'applicationid', }) + if state == 'list': module.exit_json(changed=False, results=content['result'], state="list") @@ -147,7 +150,12 @@ def main(): zab_results = content['result'][0] for key, value in params.items(): - if zab_results[key] != value and zab_results[key] != str(value): + if key == 'applications': + zab_apps = set([item['applicationid'] for item in zab_results[key]]) + if zab_apps != set(value): + differences[key] = zab_apps + + elif zab_results[key] != value and zab_results[key] != str(value): differences[key] = value if not differences: diff --git a/roles/os_zabbix/library/zbx_itemprototype.py b/roles/lib_zabbix/library/zbx_itemprototype.py index f0eb6bbbd..24f85710d 100644 --- a/roles/os_zabbix/library/zbx_itemprototype.py +++ b/roles/lib_zabbix/library/zbx_itemprototype.py @@ -141,9 +141,10 @@ def main(): module = AnsibleModule( argument_spec=dict( - server=dict(default='https://localhost/zabbix/api_jsonrpc.php', type='str'), - user=dict(default=os.environ['ZABBIX_USER'], type='str'), - password=dict(default=os.environ['ZABBIX_PASSWORD'], type='str'), + zbx_server=dict(default='https://localhost/zabbix/api_jsonrpc.php', type='str'), + zbx_user=dict(default=os.environ.get('ZABBIX_USER', None), type='str'), + zbx_password=dict(default=os.environ.get('ZABBIX_PASSWORD', None), type='str'), + zbx_debug=dict(default=False, type='bool'), name=dict(default=None, type='str'), key=dict(default=None, type='str'), interfaceid=dict(default=None, type='int'), @@ -152,7 +153,6 @@ def main(): delay=dict(default=60, type='int'), lifetime=dict(default=30, type='int'), template_name=dict(default=[], type='list'), - debug=dict(default=False, type='bool'), state=dict(default='present', type='str'), status=dict(default='enabled', type='str'), discoveryrule_name=dict(default=None, type='str'), @@ -161,10 +161,10 @@ def main(): #supports_check_mode=True ) - user = module.params['user'] - passwd = module.params['password'] - - zapi = ZabbixAPI(ZabbixConnection(module.params['server'], user, passwd, module.params['debug'])) + zapi = ZabbixAPI(ZabbixConnection(module.params['zbx_server'], + module.params['zbx_user'], + module.params['zbx_password'], + module.params['zbx_debug'])) #Set the instance and the template for the rest of the calls zbx_class_name = 'itemprototype' diff --git a/roles/os_zabbix/library/zbx_mediatype.py b/roles/lib_zabbix/library/zbx_mediatype.py index a49aecd0f..3be232ecf 100644 --- a/roles/os_zabbix/library/zbx_mediatype.py +++ b/roles/lib_zabbix/library/zbx_mediatype.py @@ -40,6 +40,7 @@ def exists(content, key='result'): return False return True + def get_mtype(mtype): ''' Transport used by the media type. @@ -72,24 +73,28 @@ def main(): module = AnsibleModule( argument_spec=dict( - server=dict(default='https://localhost/zabbix/api_jsonrpc.php', type='str'), - user=dict(default=None, type='str'), - password=dict(default=None, type='str'), + zbx_server=dict(default='https://localhost/zabbix/api_jsonrpc.php', type='str'), + zbx_user=dict(default=os.environ.get('ZABBIX_USER', None), type='str'), + zbx_password=dict(default=os.environ.get('ZABBIX_PASSWORD', None), type='str'), + zbx_debug=dict(default=False, type='bool'), description=dict(default=None, type='str'), mtype=dict(default=None, type='str'), smtp_server=dict(default=None, type='str'), smtp_helo=dict(default=None, type='str'), smtp_email=dict(default=None, type='str'), - debug=dict(default=False, type='bool'), + passwd=dict(default=None, type='str'), + path=dict(default=None, type='str'), + username=dict(default=None, type='str'), + status=dict(default='enabled', type='str'), state=dict(default='present', type='str'), ), #supports_check_mode=True ) - user = module.params.get('user', os.environ['ZABBIX_USER']) - passwd = module.params.get('password', os.environ['ZABBIX_PASSWORD']) - - zapi = ZabbixAPI(ZabbixConnection(module.params['server'], user, passwd, module.params['debug'])) + zapi = ZabbixAPI(ZabbixConnection(module.params['zbx_server'], + module.params['zbx_user'], + module.params['zbx_password'], + module.params['zbx_debug'])) #Set the instance and the template for the rest of the calls zbx_class_name = 'mediatype' @@ -109,16 +114,30 @@ def main(): module.exit_json(changed=True, results=content['result'], state="absent") if state == 'present': + status = 1 + if module.params['status']: + status = 0 params = {'description': description, - 'type': get_mtype(module.params['media_type']), + 'type': get_mtype(module.params['mtype']), 'smtp_server': module.params['smtp_server'], 'smtp_helo': module.params['smtp_helo'], 'smtp_email': module.params['smtp_email'], + 'passwd': module.params['passwd'], + 'exec_path': module.params['path'], + 'username': module.params['username'], + 'status': status, } + # Remove any None valued params + _ = [params.pop(key, None) for key in params.keys() if params[key] is None] + if not exists(content): # if we didn't find it, create it content = zapi.get_content(zbx_class_name, 'create', params) + + if content.has_key('error'): + module.exit_json(failed=True, changed=False, results=content['error'], state="present") + module.exit_json(changed=True, results=content['result'], state='present') # already exists, we need to update it # let's compare properties diff --git a/roles/os_zabbix/library/zbx_template.py b/roles/lib_zabbix/library/zbx_template.py index 20ea48a85..ff5714bdc 100644 --- a/roles/os_zabbix/library/zbx_template.py +++ b/roles/lib_zabbix/library/zbx_template.py @@ -47,21 +47,20 @@ def main(): module = AnsibleModule( argument_spec=dict( - server=dict(default='https://localhost/zabbix/api_jsonrpc.php', type='str'), - user=dict(default=None, type='str'), - password=dict(default=None, type='str'), + zbx_server=dict(default='https://localhost/zabbix/api_jsonrpc.php', type='str'), + zbx_user=dict(default=os.environ.get('ZABBIX_USER', None), type='str'), + zbx_password=dict(default=os.environ.get('ZABBIX_PASSWORD', None), type='str'), + zbx_debug=dict(default=False, type='bool'), name=dict(default=None, type='str'), - debug=dict(default=False, type='bool'), state=dict(default='present', type='str'), ), #supports_check_mode=True ) - user = module.params.get('user', os.environ['ZABBIX_USER']) - passwd = module.params.get('password', os.environ['ZABBIX_PASSWORD']) - - zbc = ZabbixConnection(module.params['server'], user, passwd, module.params['debug']) - zapi = ZabbixAPI(zbc) + zapi = ZabbixAPI(ZabbixConnection(module.params['zbx_server'], + module.params['zbx_user'], + module.params['zbx_password'], + module.params['zbx_debug'])) #Set the instance and the template for the rest of the calls zbx_class_name = 'template' @@ -84,6 +83,12 @@ def main(): if not exists(content): module.exit_json(changed=False, state="absent") + if not tname: + module.exit_json(failed=True, + changed=False, + results='Must specifiy a template name.', + state="absent") + content = zapi.get_content(zbx_class_name, 'delete', [content['result'][0][idname]]) module.exit_json(changed=True, results=content['result'], state="absent") diff --git a/roles/os_zabbix/library/zbx_trigger.py b/roles/lib_zabbix/library/zbx_trigger.py index 7cc9356c8..43f3d2677 100644 --- a/roles/os_zabbix/library/zbx_trigger.py +++ b/roles/lib_zabbix/library/zbx_trigger.py @@ -41,7 +41,6 @@ def exists(content, key='result'): return True - def get_priority(priority): ''' determine priority ''' @@ -94,24 +93,23 @@ def main(): module = AnsibleModule( argument_spec=dict( - server=dict(default='https://localhost/zabbix/api_jsonrpc.php', type='str'), - user=dict(default=None, type='str'), - password=dict(default=None, type='str'), + zbx_server=dict(default='https://localhost/zabbix/api_jsonrpc.php', type='str'), + zbx_user=dict(default=os.environ.get('ZABBIX_USER', None), type='str'), + zbx_password=dict(default=os.environ.get('ZABBIX_PASSWORD', None), type='str'), + zbx_debug=dict(default=False, type='bool'), expression=dict(default=None, type='str'), description=dict(default=None, type='str'), dependencies=dict(default=[], type='list'), priority=dict(default='avg', type='str'), - debug=dict(default=False, type='bool'), state=dict(default='present', type='str'), ), #supports_check_mode=True ) - user = module.params.get('user', os.environ['ZABBIX_USER']) - passwd = module.params.get('password', os.environ['ZABBIX_PASSWORD']) - - - zapi = ZabbixAPI(ZabbixConnection(module.params['server'], user, passwd, module.params['debug'])) + zapi = ZabbixAPI(ZabbixConnection(module.params['zbx_server'], + module.params['zbx_user'], + module.params['zbx_password'], + module.params['zbx_debug'])) #Set the instance and the template for the rest of the calls zbx_class_name = 'trigger' diff --git a/roles/os_zabbix/library/zbx_user.py b/roles/lib_zabbix/library/zbx_user.py index c45c9a75d..c916fa96a 100644 --- a/roles/os_zabbix/library/zbx_user.py +++ b/roles/lib_zabbix/library/zbx_user.py @@ -56,6 +56,14 @@ def get_usergroups(zapi, usergroups): return ugroups or None +def get_passwd(passwd): + '''Determine if password is set, if not, return 'zabbix' + ''' + if passwd: + return passwd + + return 'zabbix' + def get_usertype(user_type): ''' Determine zabbix user account type @@ -80,54 +88,55 @@ def main(): module = AnsibleModule( argument_spec=dict( - server=dict(default='https://localhost/zabbix/api_jsonrpc.php', type='str'), - user=dict(default=None, type='str'), - password=dict(default=None, type='str'), - alias=dict(default=None, type='str'), - name=dict(default=None, type='str'), - surname=dict(default=None, type='str'), + zbx_server=dict(default='https://localhost/zabbix/api_jsonrpc.php', type='str'), + zbx_user=dict(default=os.environ.get('ZABBIX_USER', None), type='str'), + zbx_password=dict(default=os.environ.get('ZABBIX_PASSWORD', None), type='str'), + zbx_debug=dict(default=False, type='bool'), + login=dict(default=None, type='str'), + first_name=dict(default=None, type='str'), + last_name=dict(default=None, type='str'), user_type=dict(default=None, type='str'), - passwd=dict(default=None, type='str'), - usergroups=dict(default=[], type='list'), - debug=dict(default=False, type='bool'), + password=dict(default=None, type='str'), + update_password=dict(default=False, type='bool'), + user_groups=dict(default=[], type='list'), state=dict(default='present', type='str'), ), #supports_check_mode=True ) - user = module.params.get('user', os.environ['ZABBIX_USER']) - password = module.params.get('password', os.environ['ZABBIX_PASSWORD']) - - zapi = ZabbixAPI(ZabbixConnection(module.params['server'], user, password, module.params['debug'])) + zapi = ZabbixAPI(ZabbixConnection(module.params['zbx_server'], + module.params['zbx_user'], + module.params['zbx_password'], + module.params['zbx_debug'])) ## before we can create a user media and users with media types we need media zbx_class_name = 'user' idname = "userid" - alias = module.params['alias'] state = module.params['state'] content = zapi.get_content(zbx_class_name, 'get', {'output': 'extend', - 'search': {'alias': alias}, + 'search': {'alias': module.params['login']}, "selectUsrgrps": 'usergrpid', }) if state == 'list': module.exit_json(changed=False, results=content['result'], state="list") if state == 'absent': - if not exists(content): + if not exists(content) or len(content['result']) == 0: module.exit_json(changed=False, state="absent") content = zapi.get_content(zbx_class_name, 'delete', [content['result'][0][idname]]) module.exit_json(changed=True, results=content['result'], state="absent") if state == 'present': - params = {'alias': alias, - 'passwd': module.params['passwd'], - 'usrgrps': get_usergroups(zapi, module.params['usergroups']), - 'name': module.params['name'], - 'surname': module.params['surname'], + + params = {'alias': module.params['login'], + 'passwd': get_passwd(module.params['password']), + 'usrgrps': get_usergroups(zapi, module.params['user_groups']), + 'name': module.params['first_name'], + 'surname': module.params['last_name'], 'type': get_usertype(module.params['user_type']), } @@ -137,14 +146,26 @@ def main(): if not exists(content): # if we didn't find it, create it content = zapi.get_content(zbx_class_name, 'create', params) + + if content.has_key('Error'): + module.exit_json(failed=True, changed=False, results=content, state='present') + module.exit_json(changed=True, results=content['result'], state='present') # already exists, we need to update it # let's compare properties differences = {} + + # Update password + if not module.params['update_password']: + params.pop('passwd', None) + zab_results = content['result'][0] for key, value in params.items(): - if key == 'passwd': - differences[key] = value + + if key == 'usrgrps': + # this must be done as a list of ordered dictionaries fails comparison + if not all([True for _ in zab_results[key][0] if _ in value[0]]): + differences[key] = value elif zab_results[key] != value and zab_results[key] != str(value): differences[key] = value diff --git a/roles/lib_zabbix/library/zbx_user_media.py b/roles/lib_zabbix/library/zbx_user_media.py new file mode 100644 index 000000000..3f7760475 --- /dev/null +++ b/roles/lib_zabbix/library/zbx_user_media.py @@ -0,0 +1,245 @@ +#!/usr/bin/env python +''' + Ansible module for user media +''' +# vim: expandtab:tabstop=4:shiftwidth=4 +# +# Zabbix user media ansible module +# +# +# Copyright 2015 Red Hat Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +# This is in place because each module looks similar to each other. +# These need duplicate code as their behavior is very similar +# but different for each zabbix class. +# pylint: disable=duplicate-code + +# pylint: disable=import-error +from openshift_tools.monitoring.zbxapi import ZabbixAPI, ZabbixConnection + +def exists(content, key='result'): + ''' Check if key exists in content or the size of content[key] > 0 + ''' + if not content.has_key(key): + return False + + if not content[key]: + return False + + return True + +def get_mtype(zapi, mtype): + '''Get mediatype + + If passed an int, return it as the mediatypeid + if its a string, then try to fetch through a description + ''' + if isinstance(mtype, int): + return mtype + try: + return int(mtype) + except ValueError: + pass + + content = zapi.get_content('mediatype', 'get', {'search': {'description': mtype}}) + if content.has_key['result'] and content['result']: + return content['result'][0]['mediatypeid'] + + return None + +def get_user(zapi, user): + ''' Get userids from user aliases + ''' + content = zapi.get_content('user', 'get', {'search': {'alias': user}}) + if content['result']: + return content['result'][0] + + return None + +def get_severity(severity): + ''' determine severity + ''' + if isinstance(severity, int) or \ + isinstance(severity, str): + return severity + + val = 0 + sev_map = { + 'not': 2**0, + 'inf': 2**1, + 'war': 2**2, + 'ave': 2**3, + 'avg': 2**3, + 'hig': 2**4, + 'dis': 2**5, + } + for level in severity: + val |= sev_map[level[:3].lower()] + return val + +def get_zbx_user_query_data(zapi, user_name): + ''' If name exists, retrieve it, and build query params. + ''' + query = {} + if user_name: + zbx_user = get_user(zapi, user_name) + query = {'userid': zbx_user['userid']} + + return query + +def find_media(medias, user_media): + ''' Find the user media in the list of medias + ''' + for media in medias: + if all([media[key] == user_media[key] for key in user_media.keys()]): + return media + return None + +def get_active(in_active): + '''Determine active value + ''' + active = 1 + if in_active: + active = 0 + + return active + +def get_mediatype(zapi, mediatype, mediatype_desc): + ''' Determine mediatypeid + ''' + mtypeid = None + if mediatype: + mtypeid = get_mtype(zapi, mediatype) + elif mediatype_desc: + mtypeid = get_mtype(zapi, mediatype_desc) + + return mtypeid + +def main(): + ''' + Ansible zabbix module for mediatype + ''' + + module = AnsibleModule( + argument_spec=dict( + zbx_server=dict(default='https://localhost/zabbix/api_jsonrpc.php', type='str'), + zbx_user=dict(default=os.environ.get('ZABBIX_USER', None), type='str'), + zbx_password=dict(default=os.environ.get('ZABBIX_PASSWORD', None), type='str'), + zbx_debug=dict(default=False, type='bool'), + login=dict(default=None, type='str'), + active=dict(default=False, type='bool'), + medias=dict(default=None, type='list'), + mediaid=dict(default=None, type='int'), + mediatype=dict(default=None, type='str'), + mediatype_desc=dict(default=None, type='str'), + #d-d,hh:mm-hh:mm;d-d,hh:mm-hh:mm... + period=dict(default=None, type='str'), + sendto=dict(default=None, type='str'), + severity=dict(default=None, type='str'), + state=dict(default='present', type='str'), + ), + #supports_check_mode=True + ) + + zapi = ZabbixAPI(ZabbixConnection(module.params['zbx_server'], + module.params['zbx_user'], + module.params['zbx_password'], + module.params['zbx_debug'])) + + #Set the instance and the template for the rest of the calls + zbx_class_name = 'user' + idname = "mediaid" + state = module.params['state'] + + # User media is fetched through the usermedia.get + zbx_user_query = get_zbx_user_query_data(zapi, module.params['login']) + content = zapi.get_content('usermedia', 'get', zbx_user_query) + + if state == 'list': + module.exit_json(changed=False, results=content['result'], state="list") + + if state == 'absent': + if not exists(content) or len(content['result']) == 0: + module.exit_json(changed=False, state="absent") + + if not module.params['login']: + module.exit_json(failed=True, changed=False, results='Must specifiy a user login.', state="absent") + + content = zapi.get_content(zbx_class_name, 'deletemedia', [content['result'][0][idname]]) + + if content.has_key('error'): + module.exit_json(changed=False, results=content['error'], state="absent") + + module.exit_json(changed=True, results=content['result'], state="absent") + + if state == 'present': + active = get_active(module.params['active']) + mtypeid = get_mediatype(zapi, module.params['mediatype'], module.params['mediatype_desc']) + + medias = module.params['medias'] + if medias == None: + medias = [{'mediatypeid': mtypeid, + 'sendto': module.params['sendto'], + 'active': active, + 'severity': int(get_severity(module.params['severity'])), + 'period': module.params['period'], + }] + + params = {'users': [zbx_user_query], + 'medias': medias, + 'output': 'extend', + } + + if not exists(content): + # if we didn't find it, create it + content = zapi.get_content(zbx_class_name, 'addmedia', params) + + if content.has_key('error'): + module.exit_json(failed=True, changed=False, results=content['error'], state="present") + + module.exit_json(changed=True, results=content['result'], state='present') + + # mediaid signifies an update + # If user params exists, check to see if they already exist in zabbix + # if they exist, then return as no update + # elif they do not exist, then take user params only + diff = {'medias': [], 'users': {}} + _ = [diff['medias'].append(media) for media in params['medias'] if not find_media(content['result'], media)] + + if not diff['medias']: + module.exit_json(changed=False, results=content['result'], state="present") + + for user in params['users']: + diff['users']['userid'] = user['userid'] + + # We have differences and need to update + content = zapi.get_content(zbx_class_name, 'updatemedia', diff) + + if content.has_key('error'): + module.exit_json(failed=True, changed=False, results=content['error'], state="present") + + module.exit_json(changed=True, results=content['result'], state="present") + + module.exit_json(failed=True, + changed=False, + results='Unknown state passed. %s' % state, + state="unknown") + +# pylint: disable=redefined-builtin, unused-wildcard-import, wildcard-import, locally-disabled +# import module snippets. This are required +from ansible.module_utils.basic import * + +main() diff --git a/roles/os_zabbix/library/zbx_usergroup.py b/roles/lib_zabbix/library/zbx_usergroup.py index ede4c9df1..297d8ef91 100644 --- a/roles/os_zabbix/library/zbx_usergroup.py +++ b/roles/lib_zabbix/library/zbx_usergroup.py @@ -44,6 +44,9 @@ def exists(content, key='result'): def get_rights(zapi, rights): '''Get rights ''' + if rights == None: + return None + perms = [] for right in rights: hstgrp = right.keys()[0] @@ -59,16 +62,49 @@ def get_rights(zapi, rights): 'permission': permission}) return perms -def get_userids(zapi, users): - ''' Get userids from user aliases +def get_gui_access(access): + ''' Return the gui_access for a usergroup ''' - userids = [] - for alias in users: - content = zapi.get_content('user', 'get', {'search': {'alias': alias}}) - if content['result']: - userids.append(content['result'][0]['userid']) + access = access.lower() + if access == 'internal': + return 1 + elif access == 'disabled': + return 2 + + return 0 + +def get_debug_mode(mode): + ''' Return the debug_mode for a usergroup + ''' + mode = mode.lower() + if mode == 'enabled': + return 1 + + return 0 + +def get_user_status(status): + ''' Return the user_status for a usergroup + ''' + status = status.lower() + if status == 'enabled': + return 0 - return userids + return 1 + + +#def get_userids(zapi, users): +# ''' Get userids from user aliases +# ''' +# if not users: +# return None +# +# userids = [] +# for alias in users: +# content = zapi.get_content('user', 'get', {'search': {'alias': alias}}) +# if content['result']: +# userids.append(content['result'][0]['userid']) +# +# return userids def main(): ''' Ansible module for usergroup @@ -78,22 +114,25 @@ def main(): module = AnsibleModule( argument_spec=dict( - server=dict(default='https://localhost/zabbix/api_jsonrpc.php', type='str'), - user=dict(default=None, type='str'), - password=dict(default=None, type='str'), - name=dict(default=None, type='str'), - rights=dict(default=[], type='list'), - users=dict(default=[], type='list'), - debug=dict(default=False, type='bool'), + zbx_server=dict(default='https://localhost/zabbix/api_jsonrpc.php', type='str'), + zbx_user=dict(default=os.environ.get('ZABBIX_USER', None), type='str'), + zbx_password=dict(default=os.environ.get('ZABBIX_PASSWORD', None), type='str'), + zbx_debug=dict(default=False, type='bool'), + debug_mode=dict(default='disabled', type='str'), + gui_access=dict(default='default', type='str'), + status=dict(default='enabled', type='str'), + name=dict(default=None, type='str', required=True), + rights=dict(default=None, type='list'), + #users=dict(default=None, type='list'), state=dict(default='present', type='str'), ), #supports_check_mode=True ) - user = module.params.get('user', os.environ['ZABBIX_USER']) - passwd = module.params.get('password', os.environ['ZABBIX_PASSWORD']) - - zapi = ZabbixAPI(ZabbixConnection(module.params['server'], user, passwd, module.params['debug'])) + zapi = ZabbixAPI(ZabbixConnection(module.params['zbx_server'], + module.params['zbx_user'], + module.params['zbx_password'], + module.params['zbx_debug'])) zbx_class_name = 'usergroup' idname = "usrgrpid" @@ -112,15 +151,24 @@ def main(): if not exists(content): module.exit_json(changed=False, state="absent") + if not uname: + module.exit_json(failed=True, changed=False, results='Need to pass in a user.', state="error") + content = zapi.get_content(zbx_class_name, 'delete', [content['result'][0][idname]]) module.exit_json(changed=True, results=content['result'], state="absent") if state == 'present': + params = {'name': uname, 'rights': get_rights(zapi, module.params['rights']), - 'userids': get_userids(zapi, module.params['users']), + 'users_status': get_user_status(module.params['status']), + 'gui_access': get_gui_access(module.params['gui_access']), + 'debug_mode': get_debug_mode(module.params['debug_mode']), + #'userids': get_userids(zapi, module.params['users']), } + _ = [params.pop(key, None) for key in params.keys() if params[key] == None] + if not exists(content): # if we didn't find it, create it content = zapi.get_content(zbx_class_name, 'create', params) @@ -133,9 +181,9 @@ def main(): if key == 'rights': differences['rights'] = value - elif key == 'userids' and zab_results.has_key('users'): - if zab_results['users'] != value: - differences['userids'] = value + #elif key == 'userids' and zab_results.has_key('users'): + #if zab_results['users'] != value: + #differences['userids'] = value elif zab_results[key] != value and zab_results[key] != str(value): differences[key] = value diff --git a/roles/lib_zabbix/tasks/create_template.yml b/roles/lib_zabbix/tasks/create_template.yml new file mode 100644 index 000000000..022ca52f2 --- /dev/null +++ b/roles/lib_zabbix/tasks/create_template.yml @@ -0,0 +1,61 @@ +--- +- debug: var=template + +- name: Template Create Template + zbx_template: + zbx_server: "{{ server }}" + zbx_user: "{{ user }}" + zbx_password: "{{ password }}" + name: "{{ template.name }}" + register: created_template + +- debug: var=created_template + +- set_fact: + lzbx_applications: "{{ template.zitems | oo_select_keys_from_list(['applications']) | oo_flatten | unique }}" + +- debug: var=lzbx_applications + +- name: Create Application + zbx_application: + zbx_server: "{{ server }}" + zbx_user: "{{ user }}" + zbx_password: "{{ password }}" + name: "{{ item }}" + template_name: "{{ template.name }}" + with_items: lzbx_applications + register: created_application + when: template.zitems is defined + +- debug: var=created_application + +- name: Create Items + zbx_item: + zbx_server: "{{ server }}" + zbx_user: "{{ user }}" + zbx_password: "{{ password }}" + key: "{{ item.key }}" + name: "{{ item.name | default(item.key, true) }}" + value_type: "{{ item.value_type | default('int') }}" + template_name: "{{ template.name }}" + applications: "{{ item.applications }}" + with_items: template.zitems + register: created_items + when: template.zitems is defined + +#- debug: var=ctp_created_items + +- name: Create Triggers + zbx_trigger: + zbx_server: "{{ server }}" + zbx_user: "{{ user }}" + zbx_password: "{{ password }}" + description: "{{ item.description }}" + expression: "{{ item.expression }}" + priority: "{{ item.priority }}" + with_items: template.ztriggers + when: template.ztriggers is defined + +#- debug: var=ctp_created_triggers + + diff --git a/roles/lib_zabbix/tasks/create_user.yml b/roles/lib_zabbix/tasks/create_user.yml new file mode 100644 index 000000000..1f752a9e1 --- /dev/null +++ b/roles/lib_zabbix/tasks/create_user.yml @@ -0,0 +1,11 @@ +--- +- name: Update zabbix credentialss for a user + zbx_user: + server: "{{ ozb_server }}" + user: "{{ ozb_user }}" + password: "{{ ozb_password }}" + alias: "{{ ozb_username }}" + passwd: "{{ ozb_new_password | default(ozb_password, true) }}" + register: user + +- debug: var=user.results diff --git a/roles/nickhammond.logrotate/.travis.yml b/roles/nickhammond.logrotate/.travis.yml new file mode 100644 index 000000000..161023def --- /dev/null +++ b/roles/nickhammond.logrotate/.travis.yml @@ -0,0 +1,14 @@ +--- +language: python +python: "2.7" +before_install: + - sudo apt-get update -qq + - sudo apt-get install -qq python-apt python-pycurl +install: + - pip install ansible +script: + - "printf '[defaults]\nroles_path = ../' > ansible.cfg" + - ansible-playbook -i tests/inventory --syntax-check tests/test.yml + - ansible-playbook -i tests/inventory --connection=local --sudo -vvvv tests/test.yml +notifications: + email: false diff --git a/roles/nickhammond.logrotate/README.md b/roles/nickhammond.logrotate/README.md new file mode 100644 index 000000000..602b5ef6c --- /dev/null +++ b/roles/nickhammond.logrotate/README.md @@ -0,0 +1,71 @@ +[![Build Status](https://travis-ci.org/nickhammond/ansible-logrotate.svg?branch=master)](https://travis-ci.org/nickhammond/ansible-logrotate) + +Role Name +======== + +Installs logrotate and provides an easy way to setup additional logrotate scripts by specifying a list of directives. + +Requirements +------------ + +None + +Role Variables +-------------- + +**logrotate_scripts**: A list of logrotate scripts and the directives to use for the rotation. + +* name - The name of the script that goes into /etc/logrotate.d/ +* path - Path to point logrotate to for the log rotation +* options - List of directives for logrotate, view the logrotate man page for specifics +* scripts - Dict of scripts for logrotate (see Example below) + +``` +logrotate_scripts: + - name: rails + path: "/srv/current/log/*.log" + options: + - weekly + - size 25M + - missingok + - compress + - delaycompress + - copytruncate +``` + +Dependencies +------------ + +None + +Example Playbook +------------------------- + +Setting up logrotate for additional Nginx logs, with postrotate script. + +``` +logrotate_scripts: + - name: nginx + path: /var/log/nginx/*.log + options: + - weekly + - size 25M + - rotate 7 + - missingok + - compress + - delaycompress + - copytruncate + scripts: + postrotate: "[ -s /run/nginx.pid ] && kill USR1 `cat /run/nginx.pid`" + +``` + +License +------- + +BSD + +Author Information +------------------ + +Find [Nick Hammond]( http://www.nickhammond.com ) on [Twitter](http://twitter.com/nickhammond). diff --git a/roles/nickhammond.logrotate/meta/.galaxy_install_info b/roles/nickhammond.logrotate/meta/.galaxy_install_info new file mode 100644 index 000000000..0d76708c9 --- /dev/null +++ b/roles/nickhammond.logrotate/meta/.galaxy_install_info @@ -0,0 +1 @@ +{install_date: 'Thu Aug 27 15:26:31 2015', version: master} diff --git a/roles/nickhammond.logrotate/meta/main.yml b/roles/nickhammond.logrotate/meta/main.yml new file mode 100644 index 000000000..1717b6d3d --- /dev/null +++ b/roles/nickhammond.logrotate/meta/main.yml @@ -0,0 +1,15 @@ +--- +galaxy_info: + author: Nick Hammond + description: Role to configure logrotate scripts + license: BSD + min_ansible_version: 1.5 + platforms: + - name: Ubuntu + versions: + - lucid + - precise + - trusty + categories: + - system +dependencies: [] diff --git a/roles/nickhammond.logrotate/tasks/main.yml b/roles/nickhammond.logrotate/tasks/main.yml new file mode 100644 index 000000000..fda23e05e --- /dev/null +++ b/roles/nickhammond.logrotate/tasks/main.yml @@ -0,0 +1,10 @@ +--- +- name: nickhammond.logrotate | Install logrotate + action: "{{ansible_pkg_mgr}} pkg=logrotate state=present" + +- name: nickhammond.logrotate | Setup logrotate.d scripts + template: + src: logrotate.d.j2 + dest: /etc/logrotate.d/{{ item.name }} + with_items: logrotate_scripts + when: logrotate_scripts is defined diff --git a/roles/nickhammond.logrotate/templates/logrotate.d.j2 b/roles/nickhammond.logrotate/templates/logrotate.d.j2 new file mode 100644 index 000000000..6453be6b2 --- /dev/null +++ b/roles/nickhammond.logrotate/templates/logrotate.d.j2 @@ -0,0 +1,16 @@ +# {{ ansible_managed }} + +{{ item.path }} { + {% if item.options is defined -%} + {% for option in item.options -%} + {{ option }} + {% endfor -%} + {% endif %} + {%- if item.scripts is defined -%} + {%- for name, script in item.scripts.iteritems() -%} + {{ name }} + {{ script }} + endscript + {% endfor -%} + {% endif -%} +} diff --git a/roles/nickhammond.logrotate/tests/inventory b/roles/nickhammond.logrotate/tests/inventory new file mode 100644 index 000000000..2fbb50c4a --- /dev/null +++ b/roles/nickhammond.logrotate/tests/inventory @@ -0,0 +1 @@ +localhost diff --git a/roles/nickhammond.logrotate/tests/test.yml b/roles/nickhammond.logrotate/tests/test.yml new file mode 100644 index 000000000..e806b0a02 --- /dev/null +++ b/roles/nickhammond.logrotate/tests/test.yml @@ -0,0 +1,18 @@ +--- +- hosts: all + sudo: True + roles: + - ansible-logrotate + - role: ansible-logrotate + logrotate_scripts: + - name: nginx-options + path: /var/log/nginx/options.log + options: + - daily + + - role: ansible-logrotate + logrotate_scripts: + - name: nginx-scripts + path: /var/log/nginx/scripts.log + scripts: + postrotate: "echo test" diff --git a/roles/openshift_facts/library/openshift_facts.py b/roles/openshift_facts/library/openshift_facts.py index dbcfa688c..c13e96c2b 100755 --- a/roles/openshift_facts/library/openshift_facts.py +++ b/roles/openshift_facts/library/openshift_facts.py @@ -323,7 +323,7 @@ def set_fluentd_facts_if_unset(facts): facts['common']['use_fluentd'] = use_fluentd return facts -def set_cluster_metrics_facts_if_unset(facts): +def set_metrics_facts_if_unset(facts): """ Set cluster metrics facts if not already present in facts dict dict: the facts dict updated with the generated cluster metrics facts if missing @@ -717,7 +717,7 @@ class OpenShiftFacts(object): facts['current_config'] = get_current_config(facts) facts = set_url_facts_if_unset(facts) facts = set_fluentd_facts_if_unset(facts) - facts = set_cluster_metrics_facts_if_unset(facts) + facts = set_metrics_facts_if_unset(facts) facts = set_identity_providers_if_unset(facts) facts = set_registry_url_if_unset(facts) facts = set_sdn_facts_if_unset(facts) diff --git a/roles/os_zabbix/README.md b/roles/os_zabbix/README.md new file mode 100644 index 000000000..ac3dc2833 --- /dev/null +++ b/roles/os_zabbix/README.md @@ -0,0 +1,40 @@ +os_zabbix +========= + +Automate zabbix tasks. + +Requirements +------------ + +This requires the openshift_tools rpm be installed for the zbxapi.py library. It can be found here: https://github.com/openshift/openshift-tools under openshift_tools/monitoring/zbxapi.py for now. + +Role Variables +-------------- + +zab_server +zab_username +zab_password + +Dependencies +------------ + +This depeonds on the zbxapi.py library located here: https://github.com/openshift/openshift-tools under openshift_tools/monitoring/zbxapi.py for now. + +Example Playbook +---------------- + + - zbx_host: + server: zab_server + user: zab_user + password: zab_password + name: 'myhost' + +License +------- + +ASL 2.0 + +Author Information +------------------ + +OpenShift operations, Red Hat, Inc diff --git a/roles/os_zabbix/defaults/main.yml b/roles/os_zabbix/defaults/main.yml new file mode 100644 index 000000000..ed97d539c --- /dev/null +++ b/roles/os_zabbix/defaults/main.yml @@ -0,0 +1 @@ +--- diff --git a/roles/os_zabbix/handlers/main.yml b/roles/os_zabbix/handlers/main.yml new file mode 100644 index 000000000..ed97d539c --- /dev/null +++ b/roles/os_zabbix/handlers/main.yml @@ -0,0 +1 @@ +--- diff --git a/roles/os_zabbix/library/__init__.py b/roles/os_zabbix/library/__init__.py deleted file mode 100644 index e69de29bb..000000000 --- a/roles/os_zabbix/library/__init__.py +++ /dev/null diff --git a/roles/os_zabbix/library/get_drule.yml b/roles/os_zabbix/library/get_drule.yml deleted file mode 100644 index a3e39f535..000000000 --- a/roles/os_zabbix/library/get_drule.yml +++ /dev/null @@ -1,115 +0,0 @@ ---- -# This is a test playbook to create one of each of the zabbix ansible modules. -# ensure that the zbxapi module is installed -# ansible-playbook test.yml -- name: Test zabbix ansible module - hosts: localhost - gather_facts: no - vars: -#zbx_server: https://localhost/zabbix/api_jsonrpc.php -#zbx_user: Admin -#zbx_password: zabbix - - pre_tasks: - - name: Template Discovery rules - zbx_template: - server: "{{ zbx_server }}" - user: "{{ zbx_user }}" - password: "{{ zbx_password }}" - name: 'Template App HaProxy' - state: list - register: template_output - - - debug: var=template_output - - - name: Discovery rules - zbx_discovery_rule: - server: "{{ zbx_server }}" - user: "{{ zbx_user }}" - password: "{{ zbx_password }}" - name: 'haproxy.discovery sender' - state: list - register: drule - - - debug: var=drule - -# - name: Create an application -# zbx_application: -# server: "{{ zbx_server }}" -# user: "{{ zbx_user }}" -# password: "{{ zbx_password }}" -# name: 'Test App' -# template_name: "test template" -# register: item_output -# -# - name: Create an item -# zbx_item: -# server: "{{ zbx_server }}" -# user: "{{ zbx_user }}" -# password: "{{ zbx_password }}" -# name: 'test item' -# key: 'kenny.item.1' -# applications: -# - 'Test App' -# template_name: "test template" -# register: item_output -# -# - debug: var=item_output -# -# - name: Create an trigger -# zbx_trigger: -# server: "{{ zbx_server }}" -# user: "{{ zbx_user }}" -# password: "{{ zbx_password }}" -# expression: '{test template:kenny.item.1.last()}>2' -# description: 'Kenny desc' -# register: trigger_output -# -# - debug: var=trigger_output -# -# - name: Create a hostgroup -# zbx_hostgroup: -# server: "{{ zbx_server }}" -# user: "{{ zbx_user }}" -# password: "{{ zbx_password }}" -# name: 'kenny hostgroup' -# register: hostgroup_output -# -# - debug: var=hostgroup_output -# -# - name: Create a host -# zbx_host: -# server: "{{ zbx_server }}" -# user: "{{ zbx_user }}" -# password: "{{ zbx_password }}" -# name: 'kenny host' -# template_names: -# - test template -# hostgroup_names: -# - kenny hostgroup -# register: host_output -# -# - debug: var=host_output -# -# - name: Create a usergroup -# zbx_usergroup: -# server: "{{ zbx_server }}" -# user: "{{ zbx_user }}" -# password: "{{ zbx_password }}" -# name: kenny usergroup -# rights: -# - 'kenny hostgroup': rw -# register: usergroup_output -# -# - debug: var=usergroup_output -# -# - name: Create a user -# zbx_user: -# server: "{{ zbx_server }}" -# user: "{{ zbx_user }}" -# password: "{{ zbx_password }}" -# alias: kwoodson -# state: list -# register: user_output -# -# - debug: var=user_output diff --git a/roles/os_zabbix/library/test.yml b/roles/os_zabbix/library/test.yml deleted file mode 100644 index cedace1a0..000000000 --- a/roles/os_zabbix/library/test.yml +++ /dev/null @@ -1,131 +0,0 @@ ---- -# This is a test playbook to create one of each of the zabbix ansible modules. -# ensure that the zbxapi module is installed -# ansible-playbook test.yml -- name: Test zabbix ansible module - hosts: localhost - gather_facts: no - vars: - zbx_server: http://localhost:8080/zabbix/api_jsonrpc.php - zbx_user: Admin - zbx_password: zabbix - - pre_tasks: - - name: Create a template - zbx_template: - server: "{{ zbx_server }}" - user: "{{ zbx_user }}" - password: "{{ zbx_password }}" - name: 'test template' - register: template_output - - - debug: var=template_output - - - name: Create a discoveryrule - zbx_discoveryrule: - server: "{{ zbx_server }}" - user: "{{ zbx_user }}" - password: "{{ zbx_password }}" - name: test discoverule - key: test_listener - template_name: test template - lifetime: 14 - register: discoveryrule - - - debug: var=discoveryrule - - - name: Create an itemprototype - zbx_itemprototype: - server: "{{ zbx_server }}" - user: "{{ zbx_user }}" - password: "{{ zbx_password }}" - name: 'Test itemprototype on {#TEST_LISTENER}' - key: 'test[{#TEST_LISTENER}]' - template_name: test template - discoveryrule_name: test discoverule - register: itemproto - - - debug: var=itemproto - - - name: Create an application - zbx_application: - server: "{{ zbx_server }}" - user: "{{ zbx_user }}" - password: "{{ zbx_password }}" - name: 'Test App' - template_name: "test template" - register: item_output - - - name: Create an item - zbx_item: - server: "{{ zbx_server }}" - user: "{{ zbx_user }}" - password: "{{ zbx_password }}" - name: 'test item' - key: 'kenny.item.1' - applications: - - 'Test App' - template_name: "test template" - register: item_output - - - debug: var=item_output - - - name: Create an trigger - zbx_trigger: - server: "{{ zbx_server }}" - user: "{{ zbx_user }}" - password: "{{ zbx_password }}" - expression: '{test template:kenny.item.1.last()}>2' - description: 'Kenny desc' - register: trigger_output - - - debug: var=trigger_output - - - name: Create a hostgroup - zbx_hostgroup: - server: "{{ zbx_server }}" - user: "{{ zbx_user }}" - password: "{{ zbx_password }}" - name: 'kenny hostgroup' - register: hostgroup_output - - - debug: var=hostgroup_output - - - name: Create a host - zbx_host: - server: "{{ zbx_server }}" - user: "{{ zbx_user }}" - password: "{{ zbx_password }}" - name: 'kenny host' - template_names: - - test template - hostgroup_names: - - kenny hostgroup - register: host_output - - - debug: var=host_output - - - name: Create a usergroup - zbx_usergroup: - server: "{{ zbx_server }}" - user: "{{ zbx_user }}" - password: "{{ zbx_password }}" - name: kenny usergroup - rights: - - 'kenny hostgroup': rw - register: usergroup_output - - - debug: var=usergroup_output - - - name: Create a user - zbx_user: - server: "{{ zbx_server }}" - user: "{{ zbx_user }}" - password: "{{ zbx_password }}" - alias: kenny user - passwd: zabbix - usergroups: - - kenny usergroup - register: user_output - - - debug: var=user_output diff --git a/roles/os_zabbix/meta/main.yml b/roles/os_zabbix/meta/main.yml new file mode 100644 index 000000000..360f5aad2 --- /dev/null +++ b/roles/os_zabbix/meta/main.yml @@ -0,0 +1,9 @@ +--- +galaxy_info: + author: OpenShift + description: ZabbixAPI + company: Red Hat, Inc + license: ASL 2.0 + min_ansible_version: 1.2 +dependencies: +- lib_zabbix diff --git a/roles/os_zabbix/tasks/main.yml b/roles/os_zabbix/tasks/main.yml new file mode 100644 index 000000000..7111c778b --- /dev/null +++ b/roles/os_zabbix/tasks/main.yml @@ -0,0 +1,30 @@ +--- +- name: Main List all templates + zbx_template: + zbx_server: "{{ ozb_server }}" + zbx_user: "{{ ozb_user }}" + zbx_password: "{{ ozb_password }}" + state: list + register: templates + +- debug: var=templates + +- include_vars: template_heartbeat.yml +- include_vars: template_os_linux.yml + +- name: Include Template Heartbeat + include: ../../lib_zabbix/tasks/create_template.yml + vars: + template: "{{ g_template_heartbeat }}" + server: "{{ ozb_server }}" + user: "{{ ozb_user }}" + password: "{{ ozb_password }}" + +- name: Include Template os_linux + include: ../../lib_zabbix/tasks/create_template.yml + vars: + template: "{{ g_template_os_linux }}" + server: "{{ ozb_server }}" + user: "{{ ozb_user }}" + password: "{{ ozb_password }}" + diff --git a/roles/os_zabbix/vars/main.yml b/roles/os_zabbix/vars/main.yml new file mode 100644 index 000000000..ed97d539c --- /dev/null +++ b/roles/os_zabbix/vars/main.yml @@ -0,0 +1 @@ +--- diff --git a/roles/os_zabbix/vars/template_heartbeat.yml b/roles/os_zabbix/vars/template_heartbeat.yml new file mode 100644 index 000000000..3d0f3d51a --- /dev/null +++ b/roles/os_zabbix/vars/template_heartbeat.yml @@ -0,0 +1,12 @@ +--- +g_template_heartbeat: + name: Template Heartbeat + zitems: + - name: Heartbeat Ping + applications: + - Heartbeat + key: heartbeat.ping + ztriggers: + - description: 'Heartbeat.ping has failed on {HOST.NAME}' + expression: '{Template Heartbeat:heartbeat.ping.nodata(20m)}=1' + priority: avg diff --git a/roles/os_zabbix/vars/template_host.yml b/roles/os_zabbix/vars/template_host.yml new file mode 100644 index 000000000..e7cc667cb --- /dev/null +++ b/roles/os_zabbix/vars/template_host.yml @@ -0,0 +1,27 @@ +--- +g_template_host: + params: + name: Template Host + host: Template Host + groups: + - groupid: 1 # FIXME (not real) + output: extend + search: + name: Template Host + zitems: + - name: Host Ping + hostid: + key_: host.ping + type: 2 + value_type: 0 + output: extend + search: + key_: host.ping + ztriggers: + - description: 'Host ping has failed on {HOST.NAME}' + expression: '{Template Host:host.ping.last()}<>0' + priority: 3 + searchWildcardsEnabled: True + search: + description: 'Host ping has failed on*' + expandExpression: True diff --git a/roles/os_zabbix/vars/template_master.yml b/roles/os_zabbix/vars/template_master.yml new file mode 100644 index 000000000..5f9b41a4f --- /dev/null +++ b/roles/os_zabbix/vars/template_master.yml @@ -0,0 +1,27 @@ +--- +g_template_master: + params: + name: Template Master + host: Template Master + groups: + - groupid: 1 # FIXME (not real) + output: extend + search: + name: Template Master + zitems: + - name: Master Etcd Ping + hostid: + key_: master.etcd.ping + type: 2 + value_type: 0 + output: extend + search: + key_: master.etcd.ping + ztriggers: + - description: 'Master Etcd ping has failed on {HOST.NAME}' + expression: '{Template Master:master.etcd.ping.last()}<>0' + priority: 3 + searchWildcardsEnabled: True + search: + description: 'Master Etcd ping has failed on*' + expandExpression: True diff --git a/roles/os_zabbix/vars/template_node.yml b/roles/os_zabbix/vars/template_node.yml new file mode 100644 index 000000000..98c343a24 --- /dev/null +++ b/roles/os_zabbix/vars/template_node.yml @@ -0,0 +1,27 @@ +--- +g_template_node: + params: + name: Template Node + host: Template Node + groups: + - groupid: 1 # FIXME (not real) + output: extend + search: + name: Template Node + zitems: + - name: Kubelet Ping + hostid: + key_: kubelet.ping + type: 2 + value_type: 0 + output: extend + search: + key_: kubelet.ping + ztriggers: + - description: 'Kubelet ping has failed on {HOST.NAME}' + expression: '{Template Node:kubelet.ping.last()}<>0' + priority: 3 + searchWildcardsEnabled: True + search: + description: 'Kubelet ping has failed on*' + expandExpression: True diff --git a/roles/os_zabbix/vars/template_os_linux.yml b/roles/os_zabbix/vars/template_os_linux.yml new file mode 100644 index 000000000..1c9d10bb0 --- /dev/null +++ b/roles/os_zabbix/vars/template_os_linux.yml @@ -0,0 +1,173 @@ +--- +g_template_os_linux: + name: Template OS Linux + zitems: + - key: kernel.uname.sysname + applications: + - Kernel + value_type: string + + - key: kernel.all.cpu.wait.total + applications: + - Kernel + value_type: int + + - key: kernel.all.cpu.irq.hard + applications: + - Kernel + value_type: int + + - key: kernel.all.cpu.idle + applications: + - Kernel + value_type: int + + - key: kernel.uname.distro + applications: + - Kernel + value_type: string + + - key: kernel.uname.nodename + applications: + - Kernel + value_type: string + + - key: kernel.all.cpu.irq.soft + applications: + - Kernel + value_type: int + + - key: kernel.all.load.15_minute + applications: + - Kernel + value_type: float + + - key: kernel.all.cpu.sys + applications: + - Kernel + value_type: int + + - key: kernel.all.load.5_minute + applications: + - Kernel + value_type: float + + - key: mem.freemem + applications: + - Memory + value_type: int + + - key: kernel.all.cpu.nice + applications: + - Kernel + value_type: int + + - key: mem.util.bufmem + applications: + - Memory + value_type: int + + - key: swap.used + applications: + - Memory + value_type: int + + - key: kernel.all.load.1_minute + applications: + - Kernel + value_type: float + + - key: kernel.uname.version + applications: + - Kernel + value_type: string + + - key: swap.length + applications: + - Memory + value_type: int + + - key: mem.physmem + applications: + - Memory + value_type: int + + - key: kernel.all.uptime + applications: + - Kernel + value_type: int + + - key: swap.free + applications: + - Memory + value_type: int + + - key: mem.util.used + applications: + - Memory + value_type: int + + - key: kernel.all.cpu.user + applications: + - Kernel + value_type: int + + - key: kernel.uname.machine + applications: + - Kernel + value_type: string + + - key: hinv.ncpu + applications: + - Kernel + value_type: int + + - key: mem.util.cached + applications: + - Memory + value_type: int + + - key: kernel.all.cpu.steal + applications: + - Kernel + value_type: int + + - key: kernel.all.pswitch + applications: + - Kernel + value_type: int + + - key: kernel.uname.release + applications: + - Kernel + value_type: string + + - key: proc.nprocs + applications: + - Kernel + value_type: int + + - key: filesys.avail + applications: + - Disk + value_type: int + + - key: filesys.capacity + applications: + - Disk + value_type: int + + - key: filesys.free + applications: + - Disk + value_type: int + + - key: filesys.full + applications: + - Disk + value_type: float + + - key: filesys.used + applications: + - Disk + value_type: float diff --git a/roles/os_zabbix/vars/template_router.yml b/roles/os_zabbix/vars/template_router.yml new file mode 100644 index 000000000..4dae7da1e --- /dev/null +++ b/roles/os_zabbix/vars/template_router.yml @@ -0,0 +1,27 @@ +--- +g_template_router: + params: + name: Template Router + host: Template Router + groups: + - groupid: 1 # FIXME (not real) + output: extend + search: + name: Template Router + zitems: + - name: Router Backends down + hostid: + key_: router.backends.down + type: 2 + value_type: 0 + output: extend + search: + key_: router.backends.down + ztriggers: + - description: 'Number of router backends down on {HOST.NAME}' + expression: '{Template Router:router.backends.down.last()}<>0' + priority: 3 + searchWildcardsEnabled: True + search: + description: 'Number of router backends down on {HOST.NAME}' + expandExpression: True |