def updateTemplate(url, template_id, host_new, name_new, linked_template_old, linked_template_new, macros): auth_code = auth(url) method = "template.update" params = { "templateid": template_id, "template_clear": linked_template_old, } clear_info = processData(url, method, params, auth_code) print clear_info params = { "templateid": template_id, "name": name_new, "host": host_new, "templates": linked_template_new, "macros": macros, } update_info = processData(url, method, params, auth_code) if 'error' in update_info: error = processLogData('update template', update_info['error'], url, params, 'error') db.insertLog(url, log=error) else: success = {} success = processLogData('update template', update_info['result'], url, params, 'success') db.insertLog(url, log=success) document = getTemplateInfo(url, template_name=name_new)['result'][0] db.createTemplate(url, template_id=template_id, update=document) return update_info
def createMaintenance(url, params): auth_code = auth(url) method = "maintenance.create" print json.dumps(params, indent=4) create_info = processData(url, method, params, auth_code) if 'error' in create_info: error = processLogData('create maintenance', create_info['error'], url, params, 'error') db.insertLog(url, log=error) else: success = processLogData('create maintenance', create_info['result'], url, params, 'success') db.insertLog(url, log=success) return create_info
def createOneHost(url, host, macros=None): auth_code = auth(url) method = "host.create" interfaces = [{ "type": 1, "main": 1, "useip": 1, "ip": host['host_ip'].split(';')[0], "dns": "", "port": "10050" }] group_id = host['group_id'] template_id = host['template_id'] groups = [{"groupid": i} for i in group_id] templates = [{"templateid": i} for i in template_id] if macros: params = { "host": host['host_ip'].split(';')[0], "name": host['host_name'], "interfaces": interfaces, "groups": groups, "templates": templates, "proxy_hostid": host["proxy_id"], "macros": macros, } new_host_infos = processData(url, method, params, auth_code) if 'error' in new_host_infos: error = processLogData('create host', new_host_infos['error'], url, params, 'error') db.insertLog(url, log=error) else: success = processLogData('create host', new_host_infos['result'], url, params, 'success') db.insertLog(url, log=success) document = getHostInfo( url, host_name=host['host_ip'].split(';')[0])['result'][0] db.createHost(url, document) return new_host_infos else: params = { "host": host['host_ip'].split(';')[0], "name": host['host_name'], "interfaces": interfaces, "groups": groups, "templates": templates, "proxy_hostid": host["proxy_id"] } new_host_infos = processData(url, method, params, auth_code) if 'error' in new_host_infos: error = processLogData('create host', new_host_infos['error'], url, params, 'error') db.insertLog(url, log=error) else: success = processLogData('create host', new_host_infos['result'], url, params, 'success') db.insertLog(url, log=success) document = getHostInfo( url, host_name=host['host_ip'].split(';')[0])['result'][0] db.createHost(url, document) return new_host_infos
def deleteGroup(url, group_ids): auth_code = auth(url) method = "hostgroup.delete" params = group_ids delete_groups_info = processData(url, method, params, auth_code) if 'error' in delete_groups_info: error = processLogData('delete group', delete_groups_info['error'], url, params, 'error') db.insertLog(url, log=error) else: success = processLogData('delete group', delete_groups_info['result'], url, params, 'success') db.insertLog(url, log=success) db.deleteGroup(url, group_ids) return delete_groups_info
def startMaintenance(url, maintenance_name, active_since, active_till, start_time): auth_code = auth(url) method = "maintenance.update" maintenance_info = getMaintenance(url, maintenance_name=maintenance_name) groupids = [ group['groupid'] for group in maintenance_info['result'][0]['groups'] ] hostids = [ host['hostid'] for host in maintenance_info['result'][0]['hosts'] ] params = { "maintenanceid": maintenance_info['result'][0]['maintenanceid'], "active_since": active_since, "active_till": active_till, "timeperiods": [{ "start_date": active_since, "start_time": start_time, "period": "86400", "timeperoid_type": 0, }], "hostids": hostids, "groupids": groupids, } update_info = processData(url, method, params, auth_code) detail = OrderedDict() detail['maintenance-name'] = maintenance_name detail['hosts'] = ', '.join( [host['name'] for host in maintenance_info['result'][0]['hosts']]) detail['groups'] = ', '.join( [group['name'] for group in maintenance_info['result'][0]['groups']]) if 'error' in update_info: error = processLogData('start maintenance', update_info['error'], url, detail, 'error') db.insertLog(url, log=error) else: success = processLogData( 'start maintenance', {'maintenanceid': update_info['result']['maintenanceids'][0]}, url, detail, 'success') db.insertLog(url, log=success) db.changeMaintenanceStatus(url, maintenance_name, 1) return update_info
def updateGroup(url, group_id, group_name): auth_code = auth(url) method = "hostgroup.update" params = { "groupid": group_id, "name": group_name, } update_group = processData(url, method, params, auth_code) if 'error' in update_group: error = processLogData('update group', update_group['error'], url, params, 'error') db.insertLog(url, log=error) else: success = processLogData('update group', update_group['result'], url, params, 'success') db.insertLog(url, log=success) db.updateGroup(url, group_id=group_id, name=group_name) return update_group
def createGroup(url, group_name): auth_code = auth(url) method = "hostgroup.create" params = {"name": group_name} new_group_infos = processData(url, method, params, auth_code) if 'error' in new_group_infos: error = processLogData('update group', new_group_infos['error'], url, params, 'error') db.insertLog(url, log=error) else: success = processLogData('update group', new_group_infos['result'], url, params, 'success') db.insertLog(url, log=success) document = getGroupInfo(url, group_name=group_name)['result'][0] document['hosts'] = len(document['hosts']) db.createGroup(url, document=document) print new_group_infos return new_group_infos
def updateMacros(url, host_id, macros): auth_code = auth(url) method = "host.update" params = { "hostid": host_id, "macros": macros, } update_info = processData(url, method, params, auth_code) if 'error' in update_info: error = processLogData('update macros', update_info['error'], url, params, 'error') db.insertLog(url, log=error) else: success = processLogData('update macros', update_info['result'], url, params, 'success') db.insertLog(url, log=success) document = getHostInfo(url, host_id=host_id)['result'][0] db.updateHost(url=url, host_id=host_id, update=document) return update_info
def delMaintenance(url, maintenanceids): auth_code = auth(url) method = "maintenance.delete" params = maintenanceids maintenance_name = [ getMaintenance(url, maintenanceid)['result'][0]['name'] for maintenanceid in maintenanceids ] del_info = processData(url, method, params, auth_code) if "error" in del_info: error = processLogData('delete maintenance', del_info['error'], url, params, 'error') db.insertLog(url, log=error) else: success = {} success = processLogData('delete maintenance', del_info, url, {'name': '; '.join(maintenance_name)}, 'success') db.insertLog(url, log=success) for name in maintenance_name: db.delMaintenance(url, name)
def manageMonitoring(url, host_ids, status): auth_code = auth(url) method = "host.update" for host_id in host_ids: params = { "hostid": host_id, "status": status, } update_info = processData(url, method, params, auth_code) status_str = 'off' if status == "1" else 'up' if 'error' in update_info: error = processLogData('%s monitoring' % status_str, update_info['error'], url, params, 'error') db.insertLog(url, log=error) else: success = {} success = processLogData('%s monitoring' % status_str, update_info['result'], url, params, 'success') db.insertLog(url, log=success) db.manageStatus(url=url, host_id=host_id, status=status)
def updateHost(url, host_id, name_new, template_old, template_new, group_new, status, macros, info_dict=None): auth_code = auth(url) method = "host.update" params = { "hostid": host_id, "templates_clear": [{ "templateid": template_old, }] } print processData(url, method, params, auth_code) params = { "hostid": host_id, "name": name_new, "templates": [{ "templateid": template_new, }], "groups": group_new, "status": status, "macros": macros, } update_info = processData(url, method, params, auth_code) if 'error' in update_info: error = processLogData('update host', update_info['error'], url, params, 'error') db.insertLog(url, log=error) else: success = processLogData('update host', update_info['result'], url, params, 'success') db.insertLog(url, log=success) document = getHostInfo(url, host_id=host_id)['result'][0] db.updateHost(url=url, host_id=host_id, update=document) return update_info
def createHost(url, host, name, group_id, proxy_id, template_id): auth_code = zabbixapi.auth(url) method = "host.create" interfaces = [{ "type": 1, "main": 1, "useip": 1, "ip": host, "dns": "", "port": "10050" }] params = { "host": host, "name": name, "interfaces": interfaces, "groups": [{ "groupid": group_id, }], "templates": [{ "templateid": template_id, }], "proxy_hostid": proxy_id } new_host_infos = zabbixapi.processData(url, method, params, auth_code) if 'error' in new_host_infos: error = zabbixapi.processLogData('create host', new_host_infos['error'], url, params, 'error') db.insertLog(url, log=error) else: success = zabbixapi.processLogData('create host', new_host_infos['result'], url, params, 'success') db.insertLog(url, log=success) document = zabbixapi.getHostInfo(url, host_name=host)['result'][0] db.createHost(url, document) return new_host_infos
def deleteHost(url, host_id=None, group_id=None): auth_code = auth(url) method = "host.delete" if host_id: params = host_id elif group_id: hosts_info = getHostInfo(url, group_id)['result'] host_id_list = [x['hostid'] for x in hosts_info] params = host_id_list delete_info = processData(url, method, params, auth_code) if 'error' in delete_info: error = processLogData('delete host', delete_info['error'], url, params, 'error') db.insertLog(url, log=error) else: success = processLogData('delete host', delete_info['result'], url, params, 'success') db.insertLog(url, log=success) if host_id: db.deleteHost(url, host_id=host_id) else: for i in host_id_list: db.deleteHost(url, host_id=i) return delete_info
def updateHost(url, host_id, host, name_new, template_old, template_new, group_new, proxy_new): auth_code = zabbixapi.auth(url) method = "host.update" params = { "hostid": host_id, "templates_clear": [{ "templateid": template_old, }], } print zabbixapi.processData(url, method, params, auth_code) params = { "hostid": host_id, "host": host, "name": name_new, "templates": [{ "templateid": template_new, }], "groups": [{ "groupid": group_new, }], "proxy_hostid": proxy_new, } update_info = zabbixapi.processData(url, method, params, auth_code) if 'error' in update_info: error = zabbixapi.processLogData('update host', update_info['error'], url, params, 'error') db.insertLog(url, log=error) else: success = zabbixapi.processLogData('update host', update_info['result'], url, params, 'success') db.insertLog(url, log=success) document = zabbixapi.getHostInfo(url, host_id=host_id)['result'][0] db.updateHost(url=url, host_id=host_id, update=document) return update_info