logger.warning("The input file is not a file: %s", input_file) sys.exit(1) # # xlrd workbook = xlrd.open_workbook(input_file) sheet = workbook.sheet_by_index(0) # # about zabbix zapi = ZabbixAPI(zabbix_server) zapi.login(zabbix_user, zabbix_password) try: for rindex in range(1, sheet.nrows): host_name = myutils.xlrd_cell_value_getstr(sheet, rindex, 0) host_sofeware = myutils.xlrd_cell_value_getstr(sheet, rindex, 1) host_system = myutils.xlrd_cell_value_getstr(sheet, rindex, 2) host_ip = myutils.xlrd_cell_value_getstr(sheet, rindex, 3) host_proxy = myutils.xlrd_cell_value_getstr(sheet, rindex, 4) host_group = myutils.xlrd_cell_value_getstr(sheet, rindex, 5) if host_name == "": continue hostgroups = zapi.hostgroup.get(filter={"name": host_group}) if len(hostgroups) == 0: logger.warning("Can't find hostgroup %s.", host_group) continue gid = hostgroups[0]["groupid"]
workbook = xlrd.open_workbook(input_file) sheet = workbook.sheet_by_name("user") # # about zabbix zapi = ZabbixAPI(zabbix_server) zapi.login(zabbix_user, zabbix_password) try: mediatypes = zapi.mediatype.get() mediatype_wechat_id = get_mediatypeid_by_description(mediatypes, u'微信') mediatype_phone_id = get_mediatypeid_by_description(mediatypes, u'短信') mediatype_email_id = get_mediatypeid_by_description(mediatypes, u'邮件') for rindex in range(1, sheet.nrows): alias = myutils.xlrd_cell_value_getstr(sheet, rindex, 0) name = myutils.xlrd_cell_value_getstr(sheet, rindex, 1) usrgrps = usergroups_get(myutils.xlrd_cell_value_getstr(sheet, rindex, 2)) usertype = usertype_get(myutils.xlrd_cell_value_getstr(sheet, rindex, 3)) wechat = { "mediatypeid": mediatype_wechat_id, "sendto": myutils.xlrd_cell_value_getstr(sheet, rindex, 4) } phone = { "mediatypeid": mediatype_phone_id, "sendto": myutils.xlrd_cell_value_getstr(sheet, rindex, 5) } email = { "mediatypeid": mediatype_email_id, "sendto": myutils.xlrd_cell_value_getstr(sheet, rindex, 6) } user_medias = user_medias_get(wechat, phone, email) # 判断用户是否已经存在 users = zapi.user.get(filter={"alias": alias}) if len(users) == 0: zapi.user.create( alias=alias,
logger.warning("The input file is not a file: %s", input_file) sys.exit(1) # # xlrd workbook = xlrd.open_workbook(input_file) sheet = workbook.sheet_by_index(0) # # about zabbix zapi = ZabbixAPI(zabbix_server) zapi.login(zabbix_user, zabbix_password) try: for rindex in range(1, sheet.nrows): name = myutils.xlrd_cell_value_getstr(sheet, rindex, 0) if name == "": continue # 创建用户组 usergroup_id = None usergroups = zapi.usergroup.get(filter={"name": name}) if len(usergroups) == 0: ret = zapi.usergroup.create(name=name) usergroup_id = ret["usrgrpids"][0] logger.info( "====> create success, usergroup name: %s, usergroup_id: %s", name, usergroup_id) else: usergroup_id = usergroups[0]["usrgrpid"]
sys.exit(1) # # xlrd workbook = xlrd.open_workbook(input_file) sheet = workbook.sheet_by_index(0) # # about zabbix zapi = ZabbixAPI(zabbix_server) zapi.login(zabbix_user, zabbix_password) try: for rindex in range(1, sheet.nrows): hostgroup = myutils.xlrd_cell_value_getstr(sheet, rindex, 0) if hostgroup == "": continue hostgroups = zapi.hostgroup.get(filter={"name": hostgroup}) if len(hostgroups) == 0: ret = zapi.hostgroup.create(name=hostgroup) logger.info("====> create success, name: %s", hostgroup) else: logger.info("==> already exist, name: %s", hostgroup) except ZabbixAPIException as e: logger.error(e) sys.exit()