def sync_cmdb(self): """ 返回结果写入CMDB :return: """ server_list = self.get_server_info() if not server_list: print('Not Fount Server Info') return False with DBContext('w') as session: for server in server_list: ip = server.get('public_ip') private_ip = server.get('private_ip') instance_id = server.get('instance_id', 'Null') hostname = server.get('hostname', instance_id) if not hostname.strip(): hostname = instance_id if re.search('syncserver', hostname): hostname = '{}_{}'.format(hostname, private_ip) region = server.get('region', 'Null') instance_type = server.get('instance_type', 'Null') instance_state = server.get('instance_state', 'Null') cpu = server.get('cpu', 'Null') cpu_cores = server.get('cpu_cores', 'Null') memory = server.get('memory', 'Null') disk = server.get('disk', 'Null') os_type = server.get('os_type', 'Null') os_kernel = server.get('os_kernel', 'Null') sn = server.get('sn', 'Null') exist_hostname = session.query(Server).filter(Server.hostname == hostname).first() exist_ip = session.query(Server).filter(Server.ip == ip).first() if not exist_hostname and not exist_ip: new_serve = Server(ip=ip, public_ip=ip, private_ip=private_ip, hostname=hostname, port=22, idc=self.account, region=region, state=self.state, admin_user=self.default_admin_user) new_serve_detail = ServerDetail(ip=ip, instance_id=instance_id, instance_type=instance_type, instance_state=instance_state, cpu=cpu, cpu_cores=cpu_cores, memory=memory, disk=disk, os_type=os_type, os_kernel=os_kernel, sn=sn) session.add(new_serve) session.add(new_serve_detail) else: session.query(Server).filter(Server.hostname == hostname).update( {Server.ip: ip, Server.private_ip: private_ip, Server.public_ip: ip, Server.idc: self.account, Server.region: region, Server.admin_user: self.default_admin_user}) session.query(ServerDetail).filter(ServerDetail.ip == ip).update( {ServerDetail.instance_id: instance_id, ServerDetail.instance_type: instance_type, ServerDetail.instance_state: instance_state}) session.commit()
def sync_cmdb(self): """ 写入CMDB :return: """ server_list = self.get_server_info() if not server_list: ins_log.read_log('info', 'Not fount server info...') # print('Not Fount Server Info') return False with DBContext('w') as session: for server in server_list: ins_log.read_log('info', '资产信息:{}'.format(server)) ip = server.get('public_ip') instance_id = server.get('instance_id', 'Null') hostname = server.get('hostname', instance_id) if not hostname.strip(): hostname = instance_id region = server.get('region', 'Null') instance_type = server.get('instance_type', 'Null') instance_state = server.get('instance_state', 'Null') # AWS=接口没看到CPU这类信息 cpu = server.get('cpu', 'Null') # CPU型号 cpu_cores = server.get('cpu_cores', 'Null') memory = server.get('memory', 'Null') disk = server.get('disk', 'Null') os_type = server.get('os_type', 'Null') os_kernel = server.get('os_kernel', 'Null') sn = server.get('sn', 'Null') exist_hostname = session.query(Server).filter(Server.hostname == hostname).first() # exist_ip = session.query(Server).filter(Server.ip == ip).first() if exist_hostname: session.query(Server).filter(Server.hostname == hostname).update( {Server.ip: ip, Server.public_ip: ip, Server.idc: 'AWS', Server.region: region}) else: new_server = Server(ip=ip, public_ip=ip, hostname=hostname, port=22, idc=self.account, region=region, state=self.state, admin_user=self.default_admin_user) session.add(new_server) exist_ip = session.query(ServerDetail).filter(ServerDetail.ip == ip).first() if exist_ip: session.query(ServerDetail).filter(ServerDetail.ip == ip).update( {ServerDetail.instance_id: instance_id, ServerDetail.instance_type: instance_type, ServerDetail.instance_state: instance_state}) else: new_serve_detail = ServerDetail(ip=ip, instance_id=instance_id, instance_type=instance_type, instance_state=instance_state, cpu=cpu, cpu_cores=cpu_cores, memory=memory, disk=disk, os_type=os_type, os_kernel=os_kernel, sn=sn) session.add(new_serve_detail) session.commit()
def update_asset(self, sucess_hosts_list): """ 更新资产到数据库 :param host_data: 主机返回的资产采集基础数据 :return: """ # print(sucess_hosts_list) with DBContext('w') as session: for hosts in sucess_hosts_list: print(hosts) for k, v in hosts.items(): exist_detail = session.query(ServerDetail).filter( ServerDetail.ip == k).first() try: if not exist_detail: new_server_detail = ServerDetail( ip=k, cpu=v.get('cpu'), memory=v.get('memory'), disk=v.get('disk'), disk_utilization=v.get('disk_utilization'), instance_id=v.get('instance_id'), instance_type=v.get('instance_type'), os_distribution=v.get('os_distribution'), os_version=v.get('os_version'), sn=v.get('sn')) session.add(new_server_detail) session.query(Server).filter( Server.ip == k).update({ Server.hostname: v.get('hostname'), Server.state: 'true' }) session.commit() else: session.query(ServerDetail).filter( ServerDetail.ip == k).update({ ServerDetail.cpu: v.get('cpu'), ServerDetail.memory: v.get('memory'), ServerDetail.disk: v.get('disk'), ServerDetail.disk_utilization: v.get('disk_utilization'), ServerDetail.os_version: v.get('os_version'), ServerDetail.os_distribution: v.get('os_distribution'), ServerDetail.sn: v.get('sn'), }) session.query(Server).filter(Server.ip == k).update({ Server.hostname: v.get('hostname'), Server.state: 'true' }) session.commit() # except sqlalchemy.exc.IntegrityError as e: except Exception as e: # 状态改为Flse->删除主机Detail--记录错误信息 session.query(Server).filter(Server.ip == k).update( {Server.state: 'false'}) session.query(ServerDetail).filter( ServerDetail.ip == k).delete( synchronize_session=False) error_log = 'IP:{},错误信息:{}'.format(k, e) new_error_log = AssetErrorLog(error_log=error_log) session.add(new_error_log) session.commit() return False
def sync_cmdb(self): """ 写入CMDB :return: """ server_list = self.get_server_info() if not server_list: ins_log.read_log('info', 'Not fount server info') return False with DBContext('w') as session: for server in server_list: ip = server.get('public_ip') instance_id = server.get('instance_id', 'Null') hostname = server.get('hostname', instance_id) if hostname == '' or not hostname: hostname = instance_id region = server.get('region', 'Null') instance_type = server.get('instance_type', 'Null') instance_state = server.get('instance_state', 'Null') cpu = server.get('cpu', 'Null') cpu_cores = server.get('cpu_cores', 'Null') memory = server.get('memory', 'Null') disk = server.get('disk', 'Null') # 阿里云接口里面好像没有disk信息 os_type = server.get('os_type', 'Null') os_name = server.get('os_name', 'Null') os_kernel = server.get('os_kernel', 'Null') sn = server.get('sn', 'Null') exist_hostname = session.query(Server).filter( Server.hostname == hostname).first() if exist_hostname: session.query(Server).filter( Server.hostname == hostname).update({ Server.ip: ip, Server.public_ip: ip, Server.idc: self.account, Server.region: region, Server.admin_user: self.default_admin_user }) else: if os_type == 'windows': # windows机器不绑定管理用户,资产信息只是平台拿到的一些基础信息 new_windows_server = Server(ip=ip, public_ip=ip, hostname=hostname, port=3389, idc=self.account, region=region, state=self.state) session.add(new_windows_server) else: # unix机器给默认绑定上管理用户,用于后续登陆机器拿详细资产使用的 new_server = Server(ip=ip, public_ip=ip, hostname=hostname, port=22, idc=self.account, region=region, state=self.state, admin_user=self.default_admin_user) session.add(new_server) exist_ip = session.query(ServerDetail).filter( ServerDetail.ip == ip).first() if exist_ip: session.query(ServerDetail).filter( ServerDetail.ip == ip).update({ ServerDetail.instance_id: instance_id, ServerDetail.instance_type: instance_type, ServerDetail.instance_state: instance_state }) else: if os_type == 'windows': new_serve_detail = ServerDetail( ip=ip, instance_id=instance_id, instance_type=instance_type, instance_state=instance_state, cpu=cpu, cpu_cores=cpu_cores, memory=memory, os_type=os_name, disk=disk, os_kernel=os_kernel, sn=sn) session.add(new_serve_detail) else: new_serve_detail = ServerDetail( ip=ip, instance_id=instance_id, instance_type=instance_type, instance_state=instance_state, cpu=cpu, cpu_cores=cpu_cores, memory=memory, os_type=os_name, disk=disk, os_kernel=os_kernel, sn=sn) session.add(new_serve_detail) session.commit()
def update_asset(self, host_info): """ 更新资产到数据库 :param host_data: 主机返回的资产采集基础数据 :return: """ with DBContext('w') as session: for k, v in host_info.items(): try: if host_info[k].get('status'): _sn = v.get('sn', None) _hostname = v.get('host_name', None) _cpu = v.get('cpu', None) _cpu_cores = v.get('cpu_cores', None) _memory = v.get('memory', None) _disk = v.get('disk', None) _os_type = v.get('os_type', None) _os_kernel = v.get('os_kernel', None) # _instance_id = v.get('instance_id', None) # _instance_type = v.get('instance_type', None) # _instance_state = v.get('instance_state', None) exist_detail = session.query(ServerDetail).filter( ServerDetail.ip == k).first() if not exist_detail: # 不存在就新建 new_server_detail = ServerDetail( ip=k, sn=_sn, cpu=_cpu, cpu_cores=_cpu_cores, memory=_memory, disk=_disk, os_type=_os_type, os_kernel=_os_kernel) session.add(new_server_detail) session.commit() session.query(Server).filter( Server.ip == k).update({ Server.hostname: _hostname, Server.state: 'true' }) session.commit() else: # 存在就更新 session.query(ServerDetail).filter( ServerDetail.ip == k).update({ ServerDetail.sn: _sn, ServerDetail.ip: k, ServerDetail.cpu: _cpu, ServerDetail.cpu_cores: _cpu_cores, ServerDetail.disk: _disk, ServerDetail.memory: _memory, ServerDetail.os_type: _os_type, ServerDetail.os_kernel: _os_kernel, }) session.query(Server).filter( Server.ip == k).update({ Server.hostname: _hostname, Server.state: 'true' }) session.commit() except sqlalchemy.exc.IntegrityError as e: ins_log.read_log('error', e) # 状态改为Flse->删除主机Detail--记录错误信息 session.query(Server).filter(Server.ip == k).update( {Server.state: 'false'}) session.query(ServerDetail).filter( ServerDetail.ip == k).delete(synchronize_session=False) exist_ip = session.query(AssetErrorLog).filter( AssetErrorLog.ip == k).first() error_log = str(e) if exist_ip: session.query(AssetErrorLog).filter( AssetErrorLog.ip == k).update( {AssetErrorLog.error_log: error_log}) else: new_error_log = AssetErrorLog(ip=k, error_log=error_log) session.add(new_error_log) session.commit() return False