from api.cmdbuild_api import CMDBuildAPI from action.finallogging import FinalLogger from api.fiberswitch_api import mult_ssh2_fiberswitch from action.cost import cost from action.excel import excel reload(sys) sys.setdefaultencoding("UTF-8") logger = FinalLogger.getLogger() excel_flag = False update_flag = True logger = FinalLogger.getLogger() cmdbuild_api = CMDBuildAPI() firberswitch_fact = [] firberswitch_port_fact = [] firberswitch_cmdb = [] firberswitch_port_cmdb = [] firberswitch_final = [] firberswitch_port_final = [] firberswitch_excel = [] firberswitch_port_excel = [] def get_cmdbuild_firberswitch():
if update_flag: if update_netdevice_num != 0 or add_netdevice_num != 0: netdevice_dict = {"NetDevice": netdevice_final} cmdbuild_api = CMDBuildAPI() token = cmdbuild_api.get_token() update_result = cmdbuild_api.put_update( token, json.dumps(netdevice_dict)) # print update_result.decode("UTF-8") excel(netdevice_final, 'netdevice') cost_time = cost(import_netdevice_time) logger.info("更新数据成功! 耗时%d秒", cost_time) logger.info("cmdbuild平台更新返回结果:%s", update_result) else: cost_time = cost(import_netdevice_time) logger.info("两边数据一致,无需更新! 耗时%d秒", cost_time) elif not netdevice_h3c: logger.info("H3C平台没有获取数据!") elif not netdevice_cmdb: logger.info("cmdbuild没有获取数据!") except Exception, e: logger.info("更新数据失败!原因:%s", e) if __name__ == "__main__": cmdbuild_api = CMDBuildAPI() token = cmdbuild_api.get_token() if token: get_cmdbuild_netdevice() get_h3c_netdevice() import_netdevice()
def import_netdevice(): import_netdevice_time = datetime.datetime.now() logger.info("网络设备数据开始比较!") update_netdevice_num = 0 add_netdevice_num = 0 try: if netdevice_h3c and netdevice_cmdb: for i in netdevice_h3c: import_temp = None for j in netdevice_cmdb: if j['DevName'] and i['Series_No'] and j['Series_No']: while i['Series_No'].upper() == j['Series_No'].upper(): import_temp = i['Series_No'] if cmp(i, j) != 0: netdevice_final.append({ 'Description': j['Description'], 'DevName': i['DevName'], 'Manage_IP_Addr': i['Manage_IP_Addr'], 'Series_No': i['Series_No'], 'CI_modelid': i['CI_modelid'], 'MANUFACTURE_Factory': i['MANUFACTURE_Factory'], 'OS_Version': i['OS_Version'], 'OS_Bin': i['OS_Bin'] }) if excel_flag: netdevice_final_excel.append({ 'Description': j['Description'], 'DevName': j['DevName'], 'Manage_IP_Addr': j['Manage_IP_Addr'], 'Series_No': j['Series_No'], 'CI_modelid': j['CI_modelid'], 'MANUFACTURE_Factory': j['MANUFACTURE_Factory'], 'OS_Version': j['OS_Version'], 'OS_Bin': j['OS_Bin'], 'Status': 'O' }) netdevice_final_excel.append({ 'Description': j['Description'], 'DevName': i['DevName'], 'Manage_IP_Addr': i['Manage_IP_Addr'], 'Series_No': i['Series_No'], 'CI_modelid': i['CI_modelid'], 'MANUFACTURE_Factory': i['MANUFACTURE_Factory'], 'OS_Version': i['OS_Version'], 'OS_Bin': i['OS_Bin'], 'Status': 'U' }) update_netdevice_num += 1 break if import_temp is None: add_netdevice_num += 1 netdevice_final.append(i) if excel_flag: netdevice_final_excel.append({ 'Description': i['Description'], 'DevName': i['DevName'], 'Manage_IP_Addr': i['Manage_IP_Addr'], 'Series_No': i['Series_No'], 'CI_modelid': i['CI_modelid'], 'MANUFACTURE_Factory': i['MANUFACTURE_Factory'], 'OS_Version': i['OS_Version'], 'OS_Bin': i['OS_Bin'], 'Status': 'A' }) logger.info("需要更新%d条,新增%d条!", update_netdevice_num, add_netdevice_num) if excel_flag: excel(netdevice_final_excel, 'netdevice') if update_flag: if update_netdevice_num != 0 or add_netdevice_num != 0: netdevice_dict = {"NetDevice": netdevice_final} cmdbuild_api = CMDBuildAPI() token = cmdbuild_api.get_token() update_result = cmdbuild_api.put_update( token, json.dumps(netdevice_dict)) # print update_result.decode("UTF-8") excel(netdevice_final, 'netdevice') cost_time = cost(import_netdevice_time) logger.info("更新数据成功! 耗时%d秒", cost_time) logger.info("cmdbuild平台更新返回结果:%s", update_result) else: cost_time = cost(import_netdevice_time) logger.info("两边数据一致,无需更新! 耗时%d秒", cost_time) elif not netdevice_h3c: logger.info("H3C平台没有获取数据!") elif not netdevice_cmdb: logger.info("cmdbuild没有获取数据!") except Exception, e: logger.info("更新数据失败!原因:%s", e)
def import_netdevice(): import_netdevice_time = datetime.datetime.now() logger.info("网络设备数据开始比较!") update_netdevice_num = 0 add_netdevice_num = 0 try: if netdevice_h3c and netdevice_cmdb: for i in netdevice_h3c: import_temp = None for j in netdevice_cmdb: if j['DevName'] and i['Series_No'] and j['Series_No']: while i['Series_No'].upper() == j['Series_No'].upper(): import_temp = i['Series_No'] if cmp(i, j) != 0: netdevice_final.append( {'Description': j['Description'], 'DevName': i['DevName'], 'Manage_IP_Addr': i['Manage_IP_Addr'], 'Series_No': i['Series_No'], 'CI_modelid': i['CI_modelid'], 'MANUFACTURE_Factory': i['MANUFACTURE_Factory'], 'OS_Version': i['OS_Version'], 'OS_Bin': i['OS_Bin']}) if excel_flag: netdevice_final_excel.append({'Description': j['Description'], 'DevName': j['DevName'], 'Manage_IP_Addr': j['Manage_IP_Addr'], 'Series_No': j['Series_No'], 'CI_modelid': j['CI_modelid'], 'MANUFACTURE_Factory': j[ 'MANUFACTURE_Factory'], 'OS_Version': j['OS_Version'], 'OS_Bin': j['OS_Bin'], 'Status': 'O'}) netdevice_final_excel.append({'Description': j['Description'], 'DevName': i['DevName'], 'Manage_IP_Addr': i['Manage_IP_Addr'], 'Series_No': i['Series_No'], 'CI_modelid': i['CI_modelid'], 'MANUFACTURE_Factory': i[ 'MANUFACTURE_Factory'], 'OS_Version': i['OS_Version'], 'OS_Bin': i['OS_Bin'], 'Status': 'U'}) update_netdevice_num += 1 break if import_temp is None: add_netdevice_num += 1 netdevice_final.append(i) if excel_flag: netdevice_final_excel.append({'Description': i['Description'], 'DevName': i['DevName'], 'Manage_IP_Addr': i['Manage_IP_Addr'], 'Series_No': i['Series_No'], 'CI_modelid': i['CI_modelid'], 'MANUFACTURE_Factory': i[ 'MANUFACTURE_Factory'], 'OS_Version': i['OS_Version'], 'OS_Bin': i['OS_Bin'], 'Status': 'A'}) logger.info("需要更新%d条,新增%d条!", update_netdevice_num, add_netdevice_num) if excel_flag: excel(netdevice_final_excel, 'netdevice') if update_flag: if update_netdevice_num != 0 or add_netdevice_num != 0: netdevice_dict = {"NetDevice": netdevice_final} cmdbuild_api = CMDBuildAPI() token = cmdbuild_api.get_token() update_result = cmdbuild_api.put_update(token, json.dumps(netdevice_dict)) # print update_result.decode("UTF-8") excel(netdevice_final, 'netdevice') cost_time = cost(import_netdevice_time) logger.info("更新数据成功! 耗时%d秒", cost_time) logger.info("cmdbuild平台更新返回结果:%s", update_result) else: cost_time = cost(import_netdevice_time) logger.info("两边数据一致,无需更新! 耗时%d秒", cost_time) elif not netdevice_h3c: logger.info("H3C平台没有获取数据!") elif not netdevice_cmdb: logger.info("cmdbuild没有获取数据!") except Exception, e: logger.info("更新数据失败!原因:%s", e)
excel(netdevice_final_excel, 'netdevice') if update_flag: if update_netdevice_num != 0 or add_netdevice_num != 0: netdevice_dict = {"NetDevice": netdevice_final} cmdbuild_api = CMDBuildAPI() token = cmdbuild_api.get_token() update_result = cmdbuild_api.put_update(token, json.dumps(netdevice_dict)) # print update_result.decode("UTF-8") excel(netdevice_final, 'netdevice') cost_time = cost(import_netdevice_time) logger.info("更新数据成功! 耗时%d秒", cost_time) logger.info("cmdbuild平台更新返回结果:%s", update_result) else: cost_time = cost(import_netdevice_time) logger.info("两边数据一致,无需更新! 耗时%d秒", cost_time) elif not netdevice_h3c: logger.info("H3C平台没有获取数据!") elif not netdevice_cmdb: logger.info("cmdbuild没有获取数据!") except Exception, e: logger.info("更新数据失败!原因:%s", e) if __name__ == "__main__": cmdbuild_api = CMDBuildAPI() token = cmdbuild_api.get_token() if token: get_cmdbuild_netdevice() get_h3c_netdevice() import_netdevice()
import datetime import sys from api.cmdbuild_api import CMDBuildAPI from api.storage_api import mult_ssh2_storage from action.finallogging import FinalLogger from action.cost import cost from action.excel import excel reload(sys) sys.setdefaultencoding("UTF-8") excel_flag = True update_flag = False logger = FinalLogger.getLogger() cmdbuild_api = CMDBuildAPI() storage_fact = [] storage_lun_fact = [] Storage_cmdb = [] Storage_lun_cmdb = [] Storage_final = [] Storage_lun_final = [] Storage_excel = [] Storage_lun_excel = [] def get_cmdbuild_storage():
@time:2017/2/20 16:40 """ import datetime import json import sys from action.cost import cost from api.cmdbuild_api import CMDBuildAPI from action.finallogging import FinalLogger from passwd.get_check_conf import GetCheckConf from IPy import IP reload(sys) sys.setdefaultencoding("UTF-8") logger = FinalLogger.getLogger() cmdbuild_api = CMDBuildAPI() getconf = GetCheckConf() ip_segment_cmdb = [] host_ip_cmdb = [] black_ip = [] class GetIPList(object): def get_cmdbuild_ip_segment(self, token): """ 获取CMDB的IP网段 :return:主键、网段、网络区域、网络类型 """ get_cmdbuild_time = datetime.datetime.now() logger.info("开始在cmdbuild系统中收集ip_segment数据!")