def run_module():
    # define the available arguments/parameters that a user can pass to
    # the module
    module_args = dict(
        username=dict(type='str', required=True),
        secret=dict(type='str', required=True),
        url=dict(type='str', required=True),
        hostname=dict(type='str', required=True),
    )

    # seed the result dict in the object
    # we primarily care about changed and state
    # change is if this module effectively modified the target
    # state will include any data that you want your module to pass back
    # for consumption, for example, in a subsequent task
    result = dict(changed=False, message='')

    # the AnsibleModule object will be our abstraction working with Ansible
    # this includes instantiation, a couple of common attr would be the
    # args/params passed to the execution, as well as if the module
    # supports check mode
    module = AnsibleModule(argument_spec=module_args, supports_check_mode=True)

    api = check_mk_web_api.WebApi(module.params['url'],
                                  username=module.params['username'],
                                  secret=module.params['secret'])

    try:
        result['changed'] = True
        result['discover_services'] = api.discover_services(
            module.params['hostname'])
        result['message'] = 'Discovered ' + module.params['hostname']
        result['code'] = 200
    except HTTPError as err:
        result['changed'] = False
        result['message'] = err.reason
        result['code'] = err.code
    except check_mk_web_api.exception.CheckMkWebApiException as err:
        result['changed'] = False
        result['message'] = str(err)
        result['code'] = 501

    # if the user is working with this module in only check mode we do not
    # want to make any changes to the environment, just return the current
    # state with no modifications
    if module.check_mode:
        return result

    if result['code'] == 200:
        module.exit_json(**result)
    elif result['code'] == 301:
        module.exit_json(**result)
    else:
        module.fail_json(msg=result['message'], **result)

    # in the event of a successful module execution, you will want to
    # simple AnsibleModule.exit_json(), passing the key/value results
    module.exit_json(**result)
예제 #2
0
파일: mkauto.py 프로젝트: Danielromanv/ADH
def mk(host,folder = folder):
    exist=''
    try:
        ipaddress = socket.gethostbyname(host)
    except:
        return ['El host '+ host +' no se ha podido agregar, ya que no se puede conectar por la red','','','']
    api = mka.WebApi(url, username=user, secret=password)
    try:
        a = api.add_host(host,folder=folder, tags={"tag_agent":"cmk-agent"})
        print(a)
    except mka.CheckMkWebApiException as ea:
        a=ea
        exist = a
        print("excepcion",a)
    try:
        b = api.discover_services(host)
        print(b)
    except mka.CheckMkWebApiException as ed:
        b=ed
        print(b)
        if  'Cannot get data from' in str(ed):
            try:
                a = api.edit_host(host, ipaddress = ipaddress)
                print(a)
            except mka.CheckMkWebApiException as ea:
                a=ea
                print(a)
            try:
                b = api.discover_services(host)
                print(b)
            except mka.CheckMkWebApiException as ed:
                b=ed
                print(b)
    try:
        c = api.activate_changes(allow_foreign_changes=True)
        print(c)
    except mka.CheckMkWebApiException as eac:
        c=eac
        print(c)
    print(b)
    if "already exists in the folder" in str(a) or "already exists in the folder" in str(exist):
        if "request timed out" in str(b):
            return ["El host "+ host +" ya existía en la carpeta, no se ha podido realizado un rediscover de los servicios",a,b,c]
        return ["El host "+ host +" ya existía en la carpeta, se ha realizado un rediscover de los servicios",a,b,c]
    elif ("Cannot get data from TCP" in str(b) and "{'sites': {}}" == str(c)):
        return ["El host "+ host +" se ha agregado/editado, y no se ha podido realizado un discover de los servicios, ni activar los cambios",a,b,c]
    elif "Check_MK exception" in str(b):
        return ["El host "+ host +" se ha agregado/editado, y no se ha podido realizado un discover de los servicios, verificar si el host posee el agente",a,b,c]
    return ["El host "+ host +" ha sido agregado exitosamente",a,b,c]
예제 #3
0
def run_module():
    # define the available arguments/parameters that a user can pass to
    # the module
    module_args = dict(
        username=dict(type='str', required=True),
        secret=dict(type='str', required=True),
        url=dict(type='str', required=True),
        hostname=dict(type='str', required=True),
        tags=dict(type='dict', required=False, default=None),
        alias=dict(type='str', required=False, default=None),
        folder=dict(type='str', required=False, default='/'),
        ipaddress=dict(type='str', required=False, default=None)
    )

    # seed the result dict in the object
    # we primarily care about changed and state
    # change is if this module effectively modified the target
    # state will include any data that you want your module to pass back
    # for consumption, for example, in a subsequent task
    result = dict(
        changed=False,
        message=''
    )

    # the AnsibleModule object will be our abstraction working with Ansible
    # this includes instantiation, a couple of common attr would be the
    # args/params passed to the execution, as well as if the module
    # supports check mode
    module = AnsibleModule(
        argument_spec=module_args,
        supports_check_mode=True
    )

    api = check_mk_web_api.WebApi(  module.params['url'], 
                                    username=module.params['username'], 
                                    secret=module.params['secret'])
    try:
        result['changed']=True
        api.add_host(   module.params['hostname'],
                        folder=module.params['folder'],
                        ipaddress=module.params['ipaddress'],
                        alias=module.params['alias'],
                        tags=module.params['tags'])
        result['message']='Added '+ module.params['hostname']
        result['code']=200
    except HTTPError as err:
        result['changed']=False
        result['message']=err.reason
        result['code']=err.code
    except check_mk_web_api.exception.CheckMkWebApiException as err:
        result['changed']=False
        result['message']=str(err)
        if "exists" in result['message']:
            result['code']=409
        else:
            result['code']=501   

    if module.check_mode:
        return result
    
    if result['code'] == 200:
        module.exit_json(**result)
    elif result['code'] == 301:
        module.exit_json(**result)
    else:
        module.fail_json(msg=result['message'], **result)

    # in the event of a successful module execution, you will want to
    # simple AnsibleModule.exit_json(), passing the key/value results
    module.exit_json(**result)
예제 #4
0
#!/usr/bin/env python

import check_mk_web_api

api = check_mk_web_api.WebApi('http://localhost:9090/cmk/check_mk/webapi.py',
                              username='******',
                              secret='automation')
#api.add_host('webserver01.com')
#lala = api.get_host('webserver01.com')
#lala = api.get_all_folders()
lala = api.get_all_hostgroups()
#  print(lala)
#api.activate_changes()

hostgroup_exists = False
#for groupname in all_hostgroups_before.items():
for groupname, alias in lala.items():
    print(groupname)
    #print(alias)
    #if groupname == module.params.get('hostgroup'):
    if groupname == 'hostgroup1':
        hostgroup_exists = True
        break

print(hostgroup_exists)

lele = api.get_hostgroup('hostgroup1')
print(lele)