def host_add(request): if request.method == 'POST': f = AddHostForm(request.POST) if f.is_valid(): cleaned_data = f.cleaned_data try: host = Host() host.ipaddr = cleaned_data['ipaddr'] host.provider = cleaned_data['provider'] host.root_password = cleaned_data['root_password'] host.hostname = cleaned_data['hostname'] host.status = cleaned_data['status'] host.comment = cleaned_data['comment'] host.save() messages.info(request, '修改成功') except: messages.error(request, '修改失败') finally: return redirect('/admin/cmdb/host') else: f = AddHostForm() return render( request, 'admin/cmdb/add_host.html', { "form": f, "status_choices": Host.STATUS_CHOICES, "provider_choices": Host.PROVIDER_CHOICES, })
def post(self, request, format=None): file_obj = request.data["file"] data = get_data(file_obj) data_list = [] header_cols = data['Sheet1'][1] rows = data['Sheet1'] print(rows) for row in range(2, len(rows)): for col in range(len(rows[row])): Host(header_cols[col], rows[row][col]).save()
def collect(request): date_json = json.loads(request.body) if request.method == 'POST': hostname = date_json['hostname']['hostname'] ip = date_json['hostname']['ipaddr'] os = date_json['hostname']['os'] cpu_p = date_json['cpu']['physical'] #pylint问题,可以忽略 try: host = Host.objects.get(hostname=hostname) except Exception as msg: print('collect save is error:', msg) host = Host() host.hostname = hostname host.ip = ip host.os = os host.cpu_p = cpu_p host.save() return HttpResponse('Post save successful!') else: return HttpResponse('No data save!')
def collect(request): asset_info = json.loads(request.body) if request.method == 'POST': vendor = asset_info['vendor'] # group = asset_info['group'] disk = asset_info['disk'] cpu_model = asset_info['cpu_model'] cpu_num = asset_info['cpu_num'] memory = asset_info['memory'] sn = asset_info['sn'] osver = asset_info['osver'] hostname = asset_info['hostname'] ip = asset_info['ip'] if not ip or not hostname: return HttpResponse( "Error your agent ip or hostname is empty! Please resolve your hostname." ) # asset_type = "" # status = "" try: host = Host.objects.get(ip=ip) except Exception as msg: print(msg) host = Host() level = get_dir("log_level") ssh_pwd = get_dir("ssh_pwd") log_path = get_dir("log_path") log("cmdb.log", level, log_path) logging.info("==========sshkey deploy start==========") data = deploy_key(ip, ssh_pwd) logging.info(data) logging.info("==========sshkey deploy end==========") # if req.POST.get('identity'): # identity = req.POST.get('identity') # try: # host = Host.objects.get(identity=identity) # except: # host = Host() host.hostname = hostname # host.group = group host.cpu_num = int(cpu_num) host.cpu_model = cpu_model host.memory = int(memory) host.sn = sn host.disk = disk host.os = osver host.vendor = vendor host.ip = ip #如果值为空或NULL,则设置一个默认值 if re.match('xen|vmware|kvm', host.vendor, flags=re.I): host.asset_type = 2 host.status = 1 else: # if host.asset_type == '' or not host.asset_type: host.asset_type = 1 # if host.status == '' or not host.status: host.status = 1 host.save() return HttpResponse("Post asset data to server successfully!") else: return HttpResponse("No any post data!")
def asset_import(request): if request.method == "POST": uf = request.FILES.get('asset_import') with open("/var/opt/alerts/data/asset.csv", "wb+") as f: for chunk in uf.chunks(chunk_size=1024): f.write(chunk) try: filename = "/var/opt/alerts/data/asset.csv" with open(filename, "rb") as f: title = next(csv.reader(f)) for data in csv.reader(f): data0 = str2gb2utf8(data[0]) if data0 == u"主机名": continue try: host = Host.objects.get(hostname=data0) except Exception as msg: host = Host() host.hostname = data0 host.ip = data[1] host.other_ip = str2gb2utf8(data[2]) if data[3]: try: idc_name = str2gb2utf8(data[3]) print("idc name is : {}".format(idc_name)) print("idc name type: {}".format(type(idc_name))) item = Idc.objects.get(name=idc_name) host.idc_id = item.id except Exception as e: print(e) print("idc info import error") host.asset_no = str2gb2utf8(data[4]) if data[5]: asset_type = str2gb2utf8(data[5]) for x, v in ASSET_TYPE: if v == asset_type: ret = x host.asset_type = ret if data[6]: status = str2gb2utf8(data[6]) for x, v in ASSET_STATUS: if v == status: ret = x host.status = ret host.os = str2gb2utf8(data[7]) host.vendor = str2gb2utf8(data[8]) host.cpu_model = str2gb2utf8(data[9]) host.cpu_num = str2gb2utf8(data[10]) host.memory = str2gb2utf8(data[11]) host.disk = (data[12]) host.sn = str2gb2utf8(data[13]) host.position = str2gb2utf8(data[14]) host.memo = str2gb2utf8(data[15]) host.save() os.remove(filename) status = 1 except Exception as e: print(e) print("import asset csv file error!") status = 2 return render(request, 'cmdb/import.html', locals())
def collect(request): asset_info = json.loads(request.body) if request.method == 'POST': vendor = asset_info['vendor'] disk = asset_info['disk'] cpu_model = asset_info['cpu_model'] cpu_num = asset_info['cpu_num'] memory = asset_info['memory'] sn = asset_info['sn'] osver = asset_info['osver'] hostname = asset_info['hostname'] ip = asset_info['ip'] asset_type = "" status = "" try: host = Host.objects.get(hostname=hostname) except: host = Host() host.hostname = hostname host.cpu_num = int(cpu_num) host.cpu_model = cpu_model host.memory = int(memory) host.sn = sn host.disk = disk host.os = osver host.vendor = vendor host.ip = ip host.asset_type = asset_type host.status = status host.save() return HttpResponse("Post asset data to server successfully!") else: return HttpResponse("No any post data!")
def import_from_public_cloud_as_api(request): provider = request.GET.get('provider') region = request.GET.get('region') settingService = SettingService() auth_info = settingService.get_public_cloud_info() count = 0 vps_list = [] if provider == ALIYUN: aliyun = AliyunService(auth_info.aliyun_access_key_id, auth_info.aliyun_access_key_secret) page = 1 while page < 100: json_data = json.loads(aliyun.get_allhost(region, PageNumber=page)) instances = json_data['Instances']['Instance'] page += 1 if instances: instances = json_data['Instances']['Instance'] for item in instances: try: ipaddr = item['VpcAttributes']['PrivateIpAddress'][ 'IpAddress'][0] except: continue try: hostname = item['HostName'] host = Host.objects.get( Q(ipaddr=ipaddr) | Q(hostname=hostname)) host.hostname = hostname host.comment = item['Description'] if item['Status'] == 'Running': host.status = Host.STATUS_ENABLED else: host.status = Host.STATUS_DISABLED host.save() except: host = Host() host.provider = ALIYUN host.ipaddr = ipaddr host.hostname = item['HostName'] host.instance_id = item['InstanceId'] host.comment = item['Description'] if item['Status'] == 'Running': host.status = Host.STATUS_ENABLED else: host.status = Host.STATUS_DISABLED host.save() finally: count += 1 vps_list.append(ipaddr + '-' + item['HostName']) else: break elif provider == QCLOUD: qcloud = QcloudService(auth_info.qcloud_secret_id, auth_info.qcloud_secret_key) try: json_data = json.loads(qcloud.get_allhost(region)) if ('TotalCount' in json_data) and (json_data['TotalCount'] > 0): instances = json_data['InstanceSet'] for item in instances: try: ipaddr = item['PrivateIpAddresses'][0] except: continue try: host = Host.objects.get(ipaddr=ipaddr) host.hostname = item['InstanceName'] host.save() except: host = Host() host.provider = QCLOUD host.ipaddr = ipaddr host.hostname = item['InstanceName'] host.instance_id = item['InstanceId'] host.status = Host.STATUS_ENABLED host.save() finally: count += 1 vps_list.append(ipaddr + '-' + item['InstanceName']) except: pass else: pass res = {'count': count, 'vps_list': vps_list} return JsonResponse(res, safe=False)
def scanHostJob(): logger.info('扫描Minion启动状态列表') upList = [] try: manageInstance = salt_api_token({'fun': 'manage.status'}, SALT_REST_URL, {'X-Auth-Token': token_id()}) statusResult = manageInstance.runnerRun() upList = statusResult['return'][0]['up'] except Exception as e: logger.info("没有任何主机启动状态信息:%s" % e) logger.info("扫描客户端注册列表") minions_rejected = [] minions_denied = [] minions_pre = [] try: minionsInstance = salt_api_token({'fun': 'key.list_all'}, SALT_REST_URL, {'X-Auth-Token': token_id()}) minionList = minionsInstance.wheelRun()['return'][0]['data']['return'] minions_pre = minionList['minions_pre'] logger.info("待接受主机:%s" % len(minions_pre)) # minions = minionList['minions'] minions_rejected = minionList['minions_rejected'] logger.info("已拒绝主机:%s", len(minions_rejected)) minions_denied = minionList['minions_denied'] logger.info("已禁用主机:%s", len(minions_denied)) except Exception as e: logger.info("扫描主机键值状态异常:%s" % e) # logger.info("自动主机") # for minion in minions_pre: # logger.info("自动接受主机:%s" % minion) # salt_api_token({'fun': 'key.accept', 'match': minion}, # SALT_REST_URL, {'X-Auth-Token': token_id()}).wheelRun() # rs = Host.objects.filter(host_name=minion) # if len(rs) == 0: # try: # device = Host(host_name=minion, minion_status=2) # device.save() # except Exception as e: # logger.info(e) logger.info("获取Minion主机资产信息") result = salt_api_token({ 'fun': 'grains.items', 'tgt': '*' }, SALT_REST_URL, { 'X-Auth-Token': token_id() }).CmdRun()['return'][0] logger.info("扫描Minion数量为[%s]", len(result)) Host.objects.update(minion_status=0) for host in result: try: minionstatus = 0 if host in upList: minionstatus = 1 if host in minions_rejected: minionstatus = 3 if host in minions_denied: minionstatus = 4 rs = Host.objects.filter(host_name=host, host=result[host]["host"]) if len(rs) == 0: logger.info("新增主机:%s", result[host]["host"]) device = Host( host_name=host, kernel=result[host]["kernel"], kernel_release=result[host]["kernelrelease"], virtual=result[host]["virtual"], host=result[host]["host"], osrelease=result[host]["osrelease"], saltversion=result[host]["saltversion"], osfinger=result[host]["osfinger"], os_family=result[host]["os_family"], num_gpus=result[host]["num_gpus"], system_serialnumber=result[host]["system_serialnumber"] if 'system_serialnumber' in result[host] else result[host]["serialnumber"], cpu_model=result[host]["cpu_model"], productname=result[host]["productname"], osarch=result[host]["osarch"], cpuarch=result[host]["osarch"], os=result[host]["os"], # num_cpus=int(result[host]["num_cpus"]), mem_total=result[host]["mem_total"], minion_status=minionstatus) device.save() for ip in result[host]["ipv4"]: hostip = HostIP(ip=ip, host=device) hostip.save() else: entity = rs[0] logger.info("更新主机:%s", entity) entity.kernel = result[host]["kernel"] # entity.num_cpus = result[host]["num_cpus"], entity.kernel_release = result[host]["kernelrelease"] entity.virtual = result[host]["virtual"] entity.osrelease = result[host]["osrelease"], entity.saltversion = result[host]["saltversion"] entity.osfinger = result[host]["osfinger"] entity.os_family = result[host]["os_family"] entity.num_gpus = result[host]["num_gpus"] entity.system_serialnumber = result[host]["system_serialnumber"] \ if 'system_serialnumber' in result[host] else result[host]["serialnumber"] entity.cpu_model = result[host]["cpu_model"] entity.productname = result[host]["productname"] entity.osarch = result[host]["osarch"] entity.cpuarch = result[host]["osarch"] entity.os = result[host]["os"] entity.mem_total = result[host]["mem_total"] entity.minion_status = minionstatus entity.save() HostIP.objects.filter(host=entity).delete() for ip in result[host]["ipv4"]: hostip = HostIP(ip=ip, host=entity) hostip.save() except Exception as e: logger.error("自动扫描出现异常:%s", e) logger.info("扫描Salt-SSH主机信息") sshResult = salt_api_token({ 'fun': 'grains.items', 'tgt': '*' }, SALT_REST_URL, { 'X-Auth-Token': token_id() }).sshRun()['return'][0] logger.info("扫描主机数量为[%s]", len(sshResult)) for host in sshResult: try: if 'return' in sshResult[host]: rs = Host.objects.filter(host=host) if rs is not None: entity = rs[0] logger.info("更新主机:%s", host) entity.host_name = sshResult[host]['return'][ 'fqdn'] if 'fqdn' in sshResult[host]['return'] else "" entity.kernel = sshResult[host]['return']['kernel'] entity.kernel_release = sshResult[host]['return'][ 'kernelrelease'] entity.virtual = sshResult[host]['return']['virtual'] entity.osrelease = sshResult[host]['return']['osrelease'] entity.saltversion = sshResult[host]['return'][ 'saltversion'] entity.osfinger = sshResult[host]['return']['osfinger'] entity.os_family = sshResult[host]['return']['os_family'] entity.num_gpus = sshResult[host]['return']['num_gpus'] entity.system_serialnumber = sshResult[host]['return'][ "serialnumber"] entity.cpu_model = sshResult[host]['return']["cpu_model"] entity.productname = sshResult[host]['return'][ "productname"] entity.osarch = sshResult[host]['return']["osarch"] entity.cpuarch = sshResult[host]['return']["cpuarch"] entity.os = sshResult[host]['return']["os"] # entity.num_cpus = int(sshResult[host]['return']["num_cpus"]), # entity.mem_total = int(sshResult[host]['return']["mem_total"]), entity.minion_status = 1 entity.save() HostIP.objects.filter(host=entity).delete() for ip in sshResult[host]['return']["ipv4"]: hostip = HostIP(ip=ip, host=entity) hostip.save() except Exception as e: traceback.print_exc()
def scan_host_job(): """ 扫描客户端信息 :return: """ logger.info('扫描Minion启动状态列表') upList = [] try: manageInstance = salt_api_token({'fun': 'manage.status'}, SALT_REST_URL, {'X-Auth-Token': token_id()}) statusResult = manageInstance.runnerRun() upList = statusResult['return'][0]['up'] logger.debug("SaltMinion状态列表[%s]" % upList) except Exception as e: logger.error("没有任何主机启动状态信息:%s") logger.error(traceback.format_exc()) logger.info("扫描客户端注册列表") minions_rejected = [] minions_denied = [] minions_pre = [] try: minionsInstance = salt_api_token({'fun': 'key.list_all'}, SALT_REST_URL, {'X-Auth-Token': token_id()}) minionList = minionsInstance.wheelRun()['return'][0]['data']['return'] minions_pre = minionList['minions_pre'] logger.info("待接受主机:%s" % len(minions_pre)) logger.debug("待接受主机[%s]" % minions_pre) minions_rejected = minionList['minions_rejected'] logger.info("已拒绝主机:%s", len(minions_rejected)) logger.debug("已拒绝主机[%s]", minions_rejected) minions_denied = minionList['minions_denied'] logger.info("已禁用主机:%s", len(minions_denied)) logger.debug("已禁用主机[%s]" % minions_denied) except Exception as e: logger.info("扫描主机键值状态异常:%s" % e) logger.error(traceback.format_exc()) logger.info("获取Minion主机资产信息") result = salt_api_token({'fun': 'grains.items', 'tgt': '*'}, SALT_REST_URL, {'X-Auth-Token': token_id()}).CmdRun()['return'][0] logger.info("扫描Minion数量为[%s]", len(result)) logger.debug("Minions资产信息[%s]" % result) Host.objects.update(minion_status=0) for host in result: try: minionstatus = 0 if host in upList: minionstatus = 1 if host in minions_rejected: minionstatus = 3 if host in minions_denied: minionstatus = 4 rs = Host.objects.filter(host_name=host, host=result[host]["host"]) if len(rs) == 0: logger.info("新增主机:%s", result[host]["host"]) productname = "" if "productname" in result[host]: productname = result[host]['productname'] device = Host(host_name=host, kernel=result[host]["kernel"] if 'kernel' in result[host] else "", kernel_release=result[host]["kernelrelease"] if 'kernelrelease' in result[host] else "", virtual=result[host]["virtual"] if 'virtual' in result[host] else "", host=result[host]["host"] if 'host' in result[host] else "", osrelease=result[host]["osrelease"] if 'osrelease' in result[host] else "", saltversion=result[host]["saltversion"] if 'saltversion' in result[host] else "", osfinger=result[host]["osfinger"] if 'osfinger' in result[host] else "", os_family=result[host]["os_family"] if 'os_family' in result[host] else "", num_gpus=result[host]["num_gpus"] if 'num_gpus' in result[host] else 0, system_serialnumber=result[host]['serialnumber'] if 'serialnumber' in result[ host] else "", cpu_model=result[host]["cpu_model"] if 'cpu_model' in result[host] else "", productname=result[host]['productname'] if "productname" in result[host]else"", osarch=result[host]["osarch"] if 'osarch' in result[host] else "", cpuarch=result[host]["cpuarch"] if 'cpuarch' in result[host] else "", os=result[host]["os"] if 'os' in result[host] else "", # num_cpus=int(result[host]["num_cpus"]), mem_total=result[host]["mem_total"] if 'mem_total' in result[host] else 0, minion_status=minionstatus ) device.save() for ip in result[host]["ipv4"]: hostip = HostIP(ip=ip, host=device) hostip.save() else: entity = rs[0] logger.info("更新主机:%s", entity) entity.kernel = result[host]["kernel"] if 'kernel' in result[host] else "" # entity.num_cpus = result[host]["num_cpus"], entity.kernel_release = result[host]["kernelrelease"] if 'kernelrelease' in result[host] else "" entity.virtual = result[host]["virtual"] if 'virtual' in result[host] else "" entity.osrelease = result[host]["osrelease"] if 'osrelease' in result[host] else "", entity.saltversion = result[host]["saltversion"] if 'saltversion' in result[host] else "" entity.osfinger = result[host]["osfinger"] if 'osfinger' in result[host] else "" entity.os_family = result[host]["os_family"] if 'os_family' in result[host] else "" entity.num_gpus = result[host]["num_gpus"] if 'num_gpus' in result[host] else 0 entity.system_serialnumber = result[host]["serialnumber"] if 'serialnumber' in result[host] else "" entity.cpu_model = result[host]["cpu_model"] if 'cpu_model' in result[host] else "" entity.productname = result[host]["productname"] if 'productname' in result[host] else "" entity.osarch = result[host]["osarch"] if 'osarch' in result[host] else "" entity.cpuarch = result[host]["cpuarch"] if 'cpuarch' in result[host] else "" entity.os = result[host]["os"] if 'os' in result[host] else "" entity.mem_total = result[host]["mem_total"] if 'mem_total' in result[host] else 0 entity.minion_status = minionstatus entity.save() oldip_list = [i.ip for i in HostIP.objects.filter(host=entity)] for ip in set(result[host]["ipv4"]) - set(oldip_list): hostip = HostIP(ip=ip, host=entity) hostip.save() for ip in set(oldip_list) - set(result[host]["ipv4"]): HostIP.objects.filter(ip=ip).delete() except Exception as e: logger.error("自动扫描出现异常:%s", e) logger.error(traceback.format_exc()) logger.info("扫描Salt-SSH主机信息") sshResult = salt_api_token({'fun': 'grains.items', 'tgt': '*'}, SALT_REST_URL, {'X-Auth-Token': token_id()}).sshRun()['return'][0] logger.info("扫描主机数量为[%s]", len(sshResult)) for host in sshResult: try: if 'return' in sshResult[host]: rs = Host.objects.filter(host=host) if rs is not None: entity = rs[0] logger.info("更新主机:%s", host) if 'fqdn' in sshResult[host]['return']: entity.host_name = sshResult[host]['return']['fqdn'] else: entity.host_name = '' if 'kernel' in sshResult[host]['return']: entity.kernel = sshResult[host]['return']['kernel'] else: entity.kernel = '' if 'kernelrelease' in sshResult[host]['return']: entity.kernel_release = sshResult[host]['return']['kernelrelease'] else: entity.kernel_release = '' if 'virtual' in sshResult[host]['return']: entity.virtual = sshResult[host]['return']['virtual'] else: entity.virtual = '' if 'osrelease' in sshResult[host]['return']: entity.osrelease = sshResult[host]['return']['osrelease'] else: entity.osrelease = '' if 'saltversion' in sshResult[host]['return']: entity.saltversion = sshResult[host]['return']['saltversion'] else: entity.saltversion = '' if 'osfinger' in sshResult[host]['return']: entity.osfinger = sshResult[host]['return']['osfinger'] else: entity.osfinger = '' if 'os_family' in sshResult[host]['return']: entity.os_family = sshResult[host]['return']['os_family'] else: entity.os_family = '' if 'num_gpus' in sshResult[host]: entity.num_gpus = sshResult[host]['return']['num_gpus'] else: entity.num_gpus = 0 if "serialnumber" in sshResult[host]: entity.system_serialnumber = sshResult[host]['return']["serialnumber"] else: entity.system_serialnumber = '' if "cpu_model" in sshResult[host]['return']: entity.cpu_model = sshResult[host]['return']["cpu_model"] else: entity.cpu_model = '' if "productname" in sshResult[host]['return']: entity.productname = sshResult[host]['return']["productname"] else: entity.productname = '' if "osarch" in sshResult[host]['return']: entity.osarch = sshResult[host]['return']["osarch"] else: entity.osarch = '' if "cpuarch" in sshResult[host]['return']: entity.cpuarch = sshResult[host]['return']["cpuarch"] else: entity.cpuarch = '' if "os" in sshResult[host]['return']: entity.os = sshResult[host]['return']["os"] else: entity.os = '' # entity.num_cpus = sshResult[host]['return']["num_cpus"], # entity.mem_total = sshResult[host]['return']["mem_total"], entity.minion_status = 1 entity.save() oldip_list = [i.ip for i in HostIP.objects.filter(host=entity)] for ip in set(sshResult[host]['return']["ipv4"]) - set(oldip_list): hostip = HostIP(ip=ip, host=entity) hostip.save() for ip in set(oldip_list) - set(sshResult[host]['return']["ipv4"]): HostIP.objects.filter(ip=ip).delete() except Exception as e: logger.error(traceback.format_exc())