本文整理汇总了Python中openshift_tools.monitoring.zbxapi.ZabbixAPI类的典型用法代码示例。如果您正苦于以下问题:Python ZabbixAPI类的具体用法?Python ZabbixAPI怎么用?Python ZabbixAPI使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ZabbixAPI类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
def main():
'''
Create a triggerprototype in zabbix
'''
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'),
name=dict(default=None, type='str'),
expression=dict(default=None, type='str'),
description=dict(default=None, type='str'),
priority=dict(default='avg', type='str'),
url=dict(default=None, type='str'),
status=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 = 'triggerprototype'
idname = "triggerid"
state = module.params['state']
tname = module.params['name']
content = zapi.get_content(zbx_class_name,
'get',
{'filter': {'description': tname},
'expandExpression': True,
'selectDependencies': 'triggerid',
})
# Get
if state == 'list':
module.exit_json(changed=False, results=content['result'], state="list")
# Delete
if state == 'absent':
if not exists(content):
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")
# Create and Update
if state == 'present':
params = {'description': tname,
'comments': module.params['description'],
'expression': module.params['expression'],
'priority': get_priority(module.params['priority']),
'url': module.params['url'],
'status': get_trigger_status(module.params['status']),
}
# Remove any None valued params
_ = [params.pop(key, None) for key in params.keys() if params[key] is None]
#******#
# CREATE
#******#
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=True, results=content['error'], state="present")
module.exit_json(changed=True, results=content['result'], state='present')
########
# UPDATE
########
differences = {}
zab_results = content['result'][0]
for key, value in params.items():
if zab_results[key] != value and zab_results[key] != str(value):
differences[key] = value
if not differences:
module.exit_json(changed=False, results=zab_results, state="present")
# We have differences and need to update
differences[idname] = zab_results[idname]
content = zapi.get_content(zbx_class_name, 'update', differences)
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")
示例2: main
def main():
'''
ansible zabbix module for zbx_graphprototypes
'''
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'),
name=dict(default=None, type='str'),
height=dict(default=None, type='int'),
width=dict(default=None, type='int'),
graph_type=dict(default='normal', type='str'),
show_legend=dict(default='show', type='str'),
state=dict(default='present', type='str'),
graph_items=dict(default=None, type='list'),
),
#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 = 'graphprototype'
state = module.params['state']
content = zapi.get_content(zbx_class_name,
'get',
{'filter': {'name': module.params['name']},
#'templateids': templateid,
'selectGraphItems': 'extend',
})
#******#
# GET
#******#
if state == 'list':
module.exit_json(changed=False, results=content['result'], state="list")
#******#
# DELETE
#******#
if state == 'absent':
if not exists(content):
module.exit_json(changed=False, state="absent")
content = zapi.get_content(zbx_class_name, 'delete', [content['result'][0]['graphid']])
module.exit_json(changed=True, results=content['result'], state="absent")
# Create and Update
if state == 'present':
params = {'name': module.params['name'],
'height': module.params['height'],
'width': module.params['width'],
'graphtype': get_graph_type(module.params['graph_type']),
'show_legend': get_show_legend(module.params['show_legend']),
'gitems': get_graph_items(zapi, module.params['graph_items']),
}
# Remove any None valued params
_ = [params.pop(key, None) for key in params.keys() if params[key] is None]
#******#
# CREATE
#******#
if not exists(content):
content = zapi.get_content(zbx_class_name, 'create', params)
if content.has_key('error'):
module.exit_json(failed=True, changed=True, results=content['error'], state="present")
module.exit_json(changed=True, results=content['result'], state='present')
########
# UPDATE
########
differences = {}
zab_results = content['result'][0]
for key, value in params.items():
if key == 'gitems':
if not compare_gitems(zab_results[key], value):
differences[key] = value
elif zab_results[key] != value and zab_results[key] != str(value):
differences[key] = value
if not differences:
module.exit_json(changed=False, results=zab_results, state="present")
# We have differences and need to update
differences['graphid'] = zab_results['graphid']
content = zapi.get_content(zbx_class_name, 'update', differences)
#.........这里部分代码省略.........
示例3: main
def main():
'''
Ansible module for zabbix discovery rules
'''
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'),
name=dict(default=None, type='str'),
key=dict(default=None, type='str'),
description=dict(default=None, type='str'),
template_name=dict(default=None, type='str'),
interfaceid=dict(default=None, type='int'),
zabbix_type=dict(default='trapper', type='str'),
value_type=dict(default='float', type='str'),
delay=dict(default=60, type='int'),
lifetime=dict(default=30, type='int'),
state=dict(default='present', type='str'),
status=dict(default='enabled', type='str'),
applications=dict(default=[], type='list'),
discoveryrule_key=dict(default=None, type='str'),
interval=dict(default=60, type='int'),
delta=dict(default=0, type='int'),
multiplier=dict(default=None, type='str'),
units=dict(default=None, 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 = 'itemprototype'
idname = "itemid"
state = module.params['state']
template = get_template(zapi, module.params['template_name'])
# selectInterfaces doesn't appear to be working but is needed.
content = zapi.get_content(zbx_class_name,
'get',
{'search': {'key_': module.params['key']},
'selectApplications': 'applicationid',
'selectDiscoveryRule': 'itemid',
'templated': True,
})
#******#
# GET
#******#
if state == 'list':
module.exit_json(changed=False, results=content['result'], state="list")
#******#
# DELETE
#******#
if state == 'absent':
if not exists(content):
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")
# Create and Update
if state == 'present':
formula, use_multiplier = get_multiplier(module.params['multiplier'])
params = {'name': module.params['name'],
'key_': module.params['key'],
'hostid': template['templateid'],
'interfaceid': module.params['interfaceid'],
'ruleid': get_rule_id(zapi, module.params['discoveryrule_key'], template['templateid']),
'type': get_zabbix_type(module.params['zabbix_type']),
'value_type': get_value_type(module.params['value_type']),
'applications': get_app_ids(zapi, module.params['applications'], template['templateid']),
'formula': formula,
'multiplier': use_multiplier,
'description': module.params['description'],
'units': module.params['units'],
'delay': module.params['interval'],
'delta': module.params['delta'],
}
if params['type'] in [2, 5, 7, 8, 11, 15]:
params.pop('interfaceid')
# Remove any None valued params
_ = [params.pop(key, None) for key in params.keys() if params[key] is None]
#******#
# CREATE
#******#
if not exists(content):
#.........这里部分代码省略.........
示例4: main
def main():
'''
ansible zabbix module for users
'''
##def user(self, name, state='present', params=None):
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'),
passwd=dict(default=None, type='str'),
usergroups=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'])
password = module.params.get('password', os.environ['ZABBIX_PASSWORD'])
zbc = ZabbixConnection(module.params['server'], user, password, module.params['debug'])
zapi = ZabbixAPI(zbc)
## 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},
"selectUsrgrps": 'usergrpid',
})
if state == 'list':
module.exit_json(changed=False, results=content['result'], state="list")
if state == 'absent':
if not exists(content):
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']),
}
if not exists(content):
# if we didn't find it, create it
content = zapi.get_content(zbx_class_name, 'create', params)
module.exit_json(changed=True, results=content['result'], state='present')
# already exists, we need to update it
# let's compare properties
differences = {}
zab_results = content['result'][0]
for key, value in params.items():
if key == 'passwd':
differences[key] = value
elif zab_results[key] != value and zab_results[key] != str(value):
differences[key] = value
if not differences:
module.exit_json(changed=False, results=zab_results, state="present")
# We have differences and need to update
differences[idname] = zab_results[idname]
content = zapi.get_content(zbx_class_name, 'update', differences)
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")
示例5: main
def main():
'''
Ansible module for zabbix host
'''
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'),
name=dict(default=None, type='str'),
hostgroup_names=dict(default=[], type='list'),
template_names=dict(default=[], type='list'),
state=dict(default='present', type='str'),
interfaces=dict(default=None, type='list'),
),
#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 = 'host'
idname = "hostid"
hname = module.params['name']
state = module.params['state']
# selectInterfaces doesn't appear to be working but is needed.
content = zapi.get_content(zbx_class_name,
'get',
{'search': {'host': hname},
'selectGroups': 'groupid',
'selectParentTemplates': 'templateid',
'selectInterfaces': 'interfaceid',
})
if state == 'list':
module.exit_json(changed=False, results=content['result'], state="list")
if state == 'absent':
if not exists(content):
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':
ifs = module.params['interfaces'] or [{'type': 1, # interface type, 1 = agent
'main': 1, # default interface? 1 = true
'useip': 1, # default interface? 1 = true
'ip': '127.0.0.1', # default interface? 1 = true
'dns': '', # dns for host
'port': '10050', # port for interface? 10050
}]
params = {'host': hname,
'groups': get_group_ids(zapi, module.params['hostgroup_names']),
'templates': get_template_ids(zapi, module.params['template_names']),
'interfaces': ifs,
}
if not exists(content):
# if we didn't find it, create it
content = zapi.get_content(zbx_class_name, 'create', params)
module.exit_json(changed=True, results=content['result'], state='present')
# already exists, we need to update it
# let's compare properties
differences = {}
zab_results = content['result'][0]
for key, value in params.items():
if key == 'templates' and zab_results.has_key('parentTemplates'):
if zab_results['parentTemplates'] != value:
differences[key] = value
elif zab_results[key] != value and zab_results[key] != str(value):
differences[key] = value
if not differences:
module.exit_json(changed=False, results=zab_results, state="present")
# We have differences and need to update
differences[idname] = zab_results[idname]
content = zapi.get_content(zbx_class_name, 'update', differences)
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")
示例6: main
def main():
"""
ansible zabbix module for users
"""
##def user(self, name, state='present', params=None):
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"),
first_name=dict(default=None, type="str"),
last_name=dict(default=None, type="str"),
user_type=dict(default=None, type="str"),
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
)
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"
state = module.params["state"]
content = zapi.get_content(
zbx_class_name,
"get",
{"output": "extend", "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) 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": 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"]),
}
# 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, 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 == "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
if not differences:
module.exit_json(changed=False, results=zab_results, state="present")
# We have differences and need to update
differences[idname] = zab_results[idname]
#.........这里部分代码省略.........
示例7: main
def main():
"""
Ansible module for zabbix host
"""
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"),
name=dict(default=None, type="str"),
hostgroup_names=dict(default=[], type="list"),
template_names=dict(default=[], type="list"),
state=dict(default="present", type="str"),
interfaces=dict(default=None, type="list"),
),
# 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 = "host"
idname = "hostid"
hname = module.params["name"]
state = module.params["state"]
# selectInterfaces doesn't appear to be working but is needed.
content = zapi.get_content(
zbx_class_name,
"get",
{
"search": {"host": hname},
"selectGroups": "groupid",
"selectParentTemplates": "templateid",
"selectInterfaces": "interfaceid",
},
)
if state == "list":
module.exit_json(changed=False, results=content["result"], state="list")
if state == "absent":
if not exists(content):
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":
ifs = module.params["interfaces"] or [
{
"type": 1, # interface type, 1 = agent
"main": 1, # default interface? 1 = true
"useip": 1, # default interface? 1 = true
"ip": "127.0.0.1", # default interface? 1 = true
"dns": "", # dns for host
"port": "10050", # port for interface? 10050
}
]
hostgroup_names = list(set(module.params["hostgroup_names"]))
params = {
"host": hname,
"groups": get_group_ids(zapi, hostgroup_names),
"templates": get_template_ids(zapi, module.params["template_names"]),
"interfaces": ifs,
}
if not exists(content):
# if we didn't find it, create it
content = zapi.get_content(zbx_class_name, "create", params)
module.exit_json(changed=True, results=content["result"], state="present")
# already exists, we need to update it
# let's compare properties
differences = {}
zab_results = content["result"][0]
for key, value in params.items():
if key == "templates" and zab_results.has_key("parentTemplates"):
if zab_results["parentTemplates"] != value:
differences[key] = value
elif key == "interfaces":
if not interfaces_equal(zab_results[key], value):
differences[key] = value
elif zab_results[key] != value and zab_results[key] != str(value):
differences[key] = value
if not differences:
module.exit_json(changed=False, results=zab_results, state="present")
# We have differences and need to update
#.........这里部分代码省略.........
示例8: main
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',
{'userids': [uid for user, uid in zbx_user_query.items()]})
#####
# Get
#####
if state == 'list':
module.exit_json(changed=False, results=content['result'], state="list")
########
# Delete
########
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', [res[idname] for res in content['result']])
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")
# Create and Update
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'],
}]
else:
medias = preprocess_medias(zapi, medias)
params = {'users': [zbx_user_query],
'medias': medias,
'output': 'extend',
}
########
# Create
########
if not exists(content):
if not params['medias']:
module.exit_json(changed=False, results=content['result'], state='present')
# 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
#.........这里部分代码省略.........
示例9: main
def main():
'''
Ansible zabbix module for mediatype
'''
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'),
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'),
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']))
#Set the instance and the template for the rest of the calls
zbx_class_name = 'mediatype'
idname = "mediatypeid"
description = module.params['description']
state = module.params['state']
content = zapi.get_content(zbx_class_name, 'get', {'search': {'description': description}})
if state == 'list':
module.exit_json(changed=False, results=content['result'], state="list")
if state == 'absent':
if not exists(content):
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 = {'description': description,
'type': get_mtype(module.params['media_type']),
'smtp_server': module.params['smtp_server'],
'smtp_helo': module.params['smtp_helo'],
'smtp_email': module.params['smtp_email'],
}
if not exists(content):
# if we didn't find it, create it
content = zapi.get_content(zbx_class_name, 'create', params)
module.exit_json(changed=True, results=content['result'], state='present')
# already exists, we need to update it
# let's compare properties
differences = {}
zab_results = content['result'][0]
for key, value in params.items():
if zab_results[key] != value and \
zab_results[key] != str(value):
differences[key] = value
if not differences:
module.exit_json(changed=False, results=zab_results, state="present")
# We have differences and need to update
differences[idname] = zab_results[idname]
content = zapi.get_content(zbx_class_name, 'update', differences)
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")
示例10: main
def main():
"""
Ansible module for zabbix discovery rules
"""
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"),
name=dict(default=None, type="str"),
key=dict(default=None, type="str"),
interfaceid=dict(default=None, type="int"),
ztype=dict(default="trapper", type="str"),
value_type=dict(default="float", type="str"),
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"),
applications=dict(default=[], 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"]))
# Set the instance and the template for the rest of the calls
zbx_class_name = "itemprototype"
idname = "itemid"
dname = module.params["name"]
state = module.params["state"]
# selectInterfaces doesn't appear to be working but is needed.
content = zapi.get_content(
zbx_class_name,
"get",
{
"search": {"name": dname},
"selectApplications": "applicationid",
"selectDiscoveryRule": "itemid",
#'selectDhosts': 'dhostid',
},
)
if state == "list":
module.exit_json(changed=False, results=content["result"], state="list")
if state == "absent":
if not exists(content):
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":
template = get_template(zapi, module.params["template_name"])
params = {
"name": dname,
"key_": module.params["key"],
"hostid": template["templateid"],
"interfaceid": module.params["interfaceid"],
"ruleid": get_rule_id(zapi, module.params["discoveryrule_name"]),
"type": get_type(module.params["ztype"]),
"value_type": get_value_type(module.params["value_type"]),
"applications": get_app_ids(zapi, module.params["applications"]),
}
if params["type"] in [2, 5, 7, 8, 11, 15]:
params.pop("interfaceid")
if not exists(content):
# if we didn't find it, create it
content = zapi.get_content(zbx_class_name, "create", params)
module.exit_json(changed=True, results=content["result"], state="present")
# already exists, we need to update it
# let's compare properties
differences = {}
zab_results = content["result"][0]
for key, value in params.items():
if key == "ruleid":
if value != zab_results["discoveryRule"]["itemid"]:
differences[key] = value
elif zab_results[key] != value and zab_results[key] != str(value):
differences[key] = value
if not differences:
module.exit_json(changed=False, results=zab_results, state="present")
# We have differences and need to update
differences[idname] = zab_results[idname]
content = zapi.get_content(zbx_class_name, "update", differences)
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")
示例11: main
def main():
'''
ansible zabbix module for zbx_itservice
'''
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'),
name=dict(default=None, type='str'),
algorithm=dict(default='do not calculate', choices=['do not calculate', 'at least one', 'all'], type='str'),
show_sla=dict(default='calculate', choices=['do not calculate', 'calculate'], type='str'),
good_sla=dict(default='99.9', type='float'),
sort_order=dict(default=1, type='int'),
state=dict(default='present', type='str'),
trigger_id=dict(default=None, type='int'),
dependencies=dict(default=[], type='list'),
dep_type=dict(default='hard', choices=['hard', 'soft'], 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 = 'service'
state = module.params['state']
content = zapi.get_content(zbx_class_name,
'get',
{'filter': {'name': module.params['name']},
'selectDependencies': 'extend',
})
#******#
# GET
#******#
if state == 'list':
module.exit_json(changed=False, results=content['result'], state="list")
#******#
# DELETE
#******#
if state == 'absent':
if not exists(content):
module.exit_json(changed=False, state="absent")
content = zapi.get_content(zbx_class_name, 'delete', [content['result'][0]['serviceid']])
module.exit_json(changed=True, results=content['result'], state="absent")
# Create and Update
if state == 'present':
dependencies = get_service_id_by_name(zapi, module.params['dependencies'])
params = {'name': module.params['name'],
'algorithm': get_algorithm(module.params['algorithm']),
'showsla': get_show_sla(module.params['show_sla']),
'goodsla': module.params['good_sla'],
'sortorder': module.params['sort_order'],
'triggerid': module.params['trigger_id']
}
# Remove any None valued params
_ = [params.pop(key, None) for key in params.keys() if params[key] is None]
#******#
# CREATE
#******#
if not exists(content):
content = zapi.get_content(zbx_class_name, 'create', params)
if content.has_key('error'):
module.exit_json(failed=True, changed=True, results=content['error'], state="present")
if dependencies:
content = add_dependencies(zapi, module.params['name'], dependencies)
if content.has_key('error'):
module.exit_json(failed=True, changed=True, results=content['error'], state="present")
module.exit_json(changed=True, results=content['result'], state='present')
########
# UPDATE
########
params['dependencies'] = dependencies
differences = {}
zab_results = content['result'][0]
for key, value in params.items():
if key == 'goodsla':
if float(value) != float(zab_results[key]):
differences[key] = value
#.........这里部分代码省略.........
示例12: main
def main():
""" Ansible module for usergroup
"""
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"),
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
)
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"
uname = module.params["name"]
state = module.params["state"]
content = zapi.get_content(zbx_class_name, "get", {"search": {"name": uname}, "selectUsers": "userid"})
# ******#
# GET
# ******#
if state == "list":
module.exit_json(changed=False, results=content["result"], state="list")
# ******#
# DELETE
# ******#
if state == "absent":
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")
# Create and Update
if state == "present":
params = {
"name": uname,
"rights": get_rights(zapi, module.params["rights"]),
"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"]),
}
# Remove any None valued params
_ = [params.pop(key, None) for key in params.keys() if params[key] == None]
# ******#
# CREATE
# ******#
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=True, results=content["error"], state="present")
module.exit_json(changed=True, results=content["result"], state="present")
########
# UPDATE
########
differences = {}
zab_results = content["result"][0]
for key, value in params.items():
if key == "rights":
differences["rights"] = 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
if not differences:
module.exit_json(changed=False, results=zab_results, state="present")
#.........这里部分代码省略.........
示例13: main
def main():
'''
ansible zabbix module for zbx_item
'''
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'),
name=dict(default=None, require=True, type='str'),
agent=dict(default=None, type='str'),
template_name=dict(default=None, type='str'),
host_name=dict(default=None, type='str'),
interval=dict(default=60, type='int'),
application=dict(default=None, type='str'),
authentication=dict(default=None, type='str'),
http_user=dict(default=None, type='str'),
http_password=dict(default=None, type='str'),
state=dict(default='present', type='str'),
status=dict(default='enabled', type='str'),
steps=dict(default='present', type='list'),
verify_host=dict(default=False, type='bool'),
retries=dict(default=1, type='int'),
headers=dict(default=None, type='dict'),
query_type=dict(default='filter', choices=['filter', 'search'], type='str'),
),
#supports_check_mode=True
mutually_exclusive=[['template_name', 'host_name']],
)
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 = 'httptest'
state = module.params['state']
hostid = None
# If a template name was passed then accept the template
if module.params['template_name']:
hostid = get_template_id(zapi, module.params['template_name'])
else:
hostid = get_host_id_by_name(zapi, module.params['host_name'])
# Fail if a template was not found matching the name
if not hostid:
module.exit_json(failed=True,
changed=False,
results='Error: Could find template or host with name [%s].' %
(module.params.get('template_name', module.params['host_name'])),
state="Unkown")
content = zapi.get_content(zbx_class_name,
'get',
{module.params['query_type']: {'name': module.params['name']},
'selectSteps': 'extend',
})
#******#
# GET
#******#
if state == 'list':
module.exit_json(changed=False, results=content['result'], state="list")
#******#
# DELETE
#******#
if state == 'absent':
if not exists(content):
module.exit_json(changed=False, state="absent")
content = zapi.get_content(zbx_class_name, 'delete', [content['result'][0]['httptestid']])
module.exit_json(changed=True, results=content['result'], state="absent")
# Create and Update
if state == 'present':
params = {'name': module.params['name'],
'hostid': hostid,
'agent': module.params['agent'],
'retries': module.params['retries'],
'steps': process_steps(module.params['steps']),
'applicationid': get_app_id(zapi, module.params['application']),
'delay': module.params['interval'],
'verify_host': get_verify_host(module.params['verify_host']),
'status': get_status(module.params['status']),
'headers': module.params['headers'],
'http_user': module.params['http_user'],
'http_password': module.params['http_password'],
}
# Remove any None valued params
_ = [params.pop(key, None) for key in params.keys() if params[key] is None]
#******#
#.........这里部分代码省略.........
示例14: main
def main():
'''
Create a trigger in zabbix
Example:
"params": {
"description": "Processor load is too high on {HOST.NAME}",
"expression": "{Linux server:system.cpu.load[percpu,avg1].last()}>5",
"dependencies": [
{
"triggerid": "14062"
}
]
},
'''
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'),
expression=dict(default=None, type='str'),
description=dict(default=None, type='str'),
dependencies=dict(default=[], type='list'),
priority=dict(default='avg', 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 = 'trigger'
idname = "triggerid"
state = module.params['state']
description = module.params['description']
content = zapi.get_content(zbx_class_name,
'get',
{'filter': {'description': description},
'expandExpression': True,
'selectDependencies': 'triggerid',
})
if state == 'list':
module.exit_json(changed=False, results=content['result'], state="list")
if state == 'absent':
if not exists(content):
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 = {'description': description,
'expression': module.params['expression'],
'dependencies': get_deps(zapi, module.params['dependencies']),
'priority': get_priority(module.params['priority']),
}
if not exists(content):
# if we didn't find it, create it
content = zapi.get_content(zbx_class_name, 'create', params)
module.exit_json(changed=True, results=content['result'], state='present')
# already exists, we need to update it
# let's compare properties
differences = {}
zab_results = content['result'][0]
for key, value in params.items():
if zab_results[key] != value and zab_results[key] != str(value):
differences[key] = value
if not differences:
module.exit_json(changed=False, results=zab_results, state="present")
# We have differences and need to update
differences[idname] = zab_results[idname]
content = zapi.get_content(zbx_class_name, 'update', differences)
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")
示例15: main
def main():
'''
Ansible module for zabbix discovery rules
'''
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'),
name=dict(default=None, type='str'),
key=dict(default=None, type='str'),
interfaceid=dict(default=None, type='int'),
ztype=dict(default='trapper', type='str'),
value_type=dict(default='float', type='str'),
delay=dict(default=60, type='int'),
lifetime=dict(default=30, type='int'),
template_name=dict(default=[], type='list'),
state=dict(default='present', type='str'),
status=dict(default='enabled', type='str'),
discoveryrule_name=dict(default=None, type='str'),
applications=dict(default=[], type='list'),
),
#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 = 'itemprototype'
idname = "itemid"
dname = module.params['name']
state = module.params['state']
# selectInterfaces doesn't appear to be working but is needed.
content = zapi.get_content(zbx_class_name,
'get',
{'search': {'name': dname},
'selectApplications': 'applicationid',
'selectDiscoveryRule': 'itemid',
#'selectDhosts': 'dhostid',
})
if state == 'list':
module.exit_json(changed=False, results=content['result'], state="list")
if state == 'absent':
if not exists(content):
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':
template = get_template(zapi, module.params['template_name'])
params = {'name': dname,
'key_': module.params['key'],
'hostid': template['templateid'],
'interfaceid': module.params['interfaceid'],
'ruleid': get_rule_id(zapi, module.params['discoveryrule_name']),
'type': get_type(module.params['ztype']),
'value_type': get_value_type(module.params['value_type']),
'applications': get_app_ids(zapi, module.params['applications']),
}
if params['type'] in [2, 5, 7, 8, 11, 15]:
params.pop('interfaceid')
if not exists(content):
# if we didn't find it, create it
content = zapi.get_content(zbx_class_name, 'create', params)
module.exit_json(changed=True, results=content['result'], state='present')
# already exists, we need to update it
# let's compare properties
differences = {}
zab_results = content['result'][0]
for key, value in params.items():
if key == 'ruleid':
if value != zab_results['discoveryRule']['itemid']:
differences[key] = value
elif zab_results[key] != value and zab_results[key] != str(value):
differences[key] = value
if not differences:
module.exit_json(changed=False, results=zab_results, state="present")
# We have differences and need to update
differences[idname] = zab_results[idname]
content = zapi.get_content(zbx_class_name, 'update', differences)
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")