Ejemplo n.º 1
0
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!")
Ejemplo n.º 2
0
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(hostname=hostname)
        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
        # 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!")
Ejemplo n.º 3
0
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())