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 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 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 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)