Example #1
0
def process_nic(info, server):
    nic_info = info['nic']['data']  # 新提交的数据

    nic_name_set = set(nic_info)
    nic_name__db_set = {
        i.name
        for i in models.NIC.objects.filter(server=server)
    }

    # 新增  删除  更新
    add_name_set = nic_name_set - nic_name__db_set  # 新增的槽位
    del_name_set = nic_name__db_set - nic_name_set  # 删除的槽位
    update_name_set = nic_name__db_set & nic_name_set  # 更新的槽位

    # 新增网卡

    add_nic_lit = []
    for name in add_name_set:
        nic = nic_info.get(name)
        nic['name'] = name
        add_nic_lit.append(models.NIC(**nic, server=server))

    if add_nic_lit:
        models.NIC.objects.bulk_create(add_nic_lit)

    # 删除网卡
    if del_name_set:
        models.NIC.objects.filter(server=server,
                                  name__in=del_name_set).delete()

    # 更新网卡
    for name in update_name_set:
        nic = nic_info.get(name)
        nic['name'] = name
        models.NIC.objects.filter(server=server, name=name).update(**nic)
Example #2
0
    def post(self, request):

        info = request.data
        print(request)
        action = info.get('action')

        hostname = info['basic']['data']['hostname']
        print("提交的所有信息", info)
        result = {'status': True, 'hostname': hostname}
        # print(info)

        if action == 'create':
            # 新增资产信息
            # 新增server
            server_info = {}

            basic = info['basic']['data']
            main_board = info.get('main_board').get('data')
            cpu = info['cpu']['data']
            server_info.update(basic)
            server_info.update(main_board)
            server_info.update(cpu)
            print('#serverinfo#===', server_info)
            server = models.Server.objects.create(**server_info)
            # 新增disk
            disk_info = info.get('disk').get('data')
            disk_obj_list = []
            for disk in disk_info.values():
                disk_obj_list.append(models.Disk(**disk, server=server))
            if disk_obj_list:
                models.Disk.objects.bulk_create(disk_obj_list)

            # 新增memory

            memory_info = info['memory']['data']
            memory_obj_list = []
            for memory in memory_info.values():
                memory_obj_list.append(models.Memory(**memory, server=server))
            if memory_obj_list:
                models.Memory.objects.bulk_create(memory_obj_list)

            # 新增nic
            nic_info = info['nic']['data']
            nic_obj_list = []
            for name, nic in nic_info.items():
                nic_obj_list.append(models.NIC(**nic, name=name,
                                               server=server))
            if nic_obj_list:
                models.NIC.objects.bulk_create(nic_obj_list)

        elif action == 'update' or action == 'update_host':
            # 只更新资产信息
            # 更新主机表
            server = process_basic(info)
            process_disk(info, server)
            process_memory(info, server)
            process_nic(info, server)

        return Response(result)
Example #3
0
 def add(self, new_slots, latest_network_dict):
     print('+++++ ', new_slots)
     nic_list = []
     record_list = []
     for slot in new_slots:
         new_nic = latest_network_dict[slot]
         nic_obj = models.NIC(**new_nic)
         nic_obj.server_obj = self.server_obj
         nic_list.append(nic_obj)
         record = '[{server}]新增网卡,名称为[{slot}]'.format(
             server=self.server_obj, slot=slot
         )
         record_list.append(record)
     models.NIC.objects.bulk_create(nic_list)
     record_info = '\n'.join(record_list)
     print('新增网卡信息..\n', record_info)
Example #4
0
 def process(self, new, server_obj):
     new_data = new.get('data')
     if new.get('status'):
         new_slot = list(new_data.keys())  # 新采集网卡插槽列表
         objs = server_obj.nic.all()
         old_slot = [obj.name for obj in objs]  # 旧网卡插槽列表(槽位字段为name)
         add_slot = list(set(new_slot).difference(set(old_slot)))  # 新增插槽
         del_slot = list(set(old_slot).difference(set(new_slot)))  # 删除插槽
         update_slot = list(set(new_slot).intersection(set(old_slot)))  # 更新插槽
         # 新增槽位
         log = []
         add_objs = []
         for slot in add_slot:
             new_data[slot]['server_obj_id'] = server_obj.id
             new_data[slot]['name'] = slot
             log.append('网卡信息:新增网卡{name};网卡mac地址{hwaddr};子网掩码{netmask};IP地址{ipaddrs};状态{up}'.format(
                 **new_data[slot]))
             add_objs.append(models.NIC(**new_data.get(slot)))
         models.NIC.objects.bulk_create(add_objs, 5)
         if log:
             models.AssetRecord.objects.create(asset_obj=server_obj.asset, content='新增网卡:%s' % (';'.join(log)))
         # 删除槽位
         if del_slot:
             models.NIC.objects.filter(server_obj=server_obj, slot__in=del_slot).delete()
             models.AssetRecord.objects.create(asset_obj=server_obj.asset, content='移除网卡:%s' % (';'.join(del_slot)))
         # 更新槽位
         log = []
         field_map = {'name': '网卡名称', 'hwaddr': '网卡mac地址', 'netmask': '子网掩码', 'ipaddrs': 'IP地址', 'up': '状态'}
         for slot in update_slot:
             slot_data = new_data.get(slot)
             slot_obj = models.NIC.objects.filter(name=slot, server_obj=server_obj).first()
             for k, v in slot_data.items():
                 value = getattr(slot_obj, k)
                 if v != value:
                     log.append('网卡槽位:%s,%s由%s变为%s' % (slot, field_map.get(k), value, v))
                     setattr(slot_obj, k, v)
             slot_obj.save()
         if log:
             models.AssetRecord.objects.create(asset_obj=server_obj.asset, content=';'.join(log))
     else:
         models.ErrorLog.objects.create(title='网卡网卡信息采集出错', content=new_data)
Example #5
0
    def process(self, server_info, server_obj, compare_new_old):
        nic_info = server_info["nic"]
        if not nic_info["status"]:
            models.ErrorLog.objects.create(
                content=nic_info["data"],
                asset_obj=server_obj.asset,
                title="【%s】网卡采集错误信息" % (server_obj.hostname),
            )

        new_nic_dict = nic_info['data']  # 客户端传来的相关数据
        old_nic_list = models.NIC.objects.filter(
            server_obj=server_obj)  # 数据库中的数据obj列表
        new_slot_list = list(new_nic_dict.keys())  # 客户端传来的所有slot

        old_slot_list = []
        for item in old_nic_list:
            old_slot_list.append(item.name)

        update_list, create_list, del_list = compare_new_old(
            new_slot_list, old_slot_list)

        # 删除【4,】
        models.NIC.objects.filter(server_obj=server_obj,
                                  name__in=del_list).delete()
        if del_list:
            content = "删除网卡%s" % ("/".join(del_list))
            models.AssetRecord.objects.create(
                asset_obj=server_obj.asset,
                content=content,
            )

        # 创建【3,】
        record_list = []
        create_obj_list = []
        for slot in create_list:
            nic_dict = new_nic_dict[slot]
            nic_dict["server_obj"] = server_obj
            nic_dict["name"] = slot
            create_obj_list.append(models.NIC(**nic_dict))
            temp = "新增网卡:名称:{name},网卡mac地址{hwaddr},ip地址{ipaddrs},掩码{netmask},是否启动{up}".format(
                **nic_dict)
            record_list.append(temp)

        models.NIC.objects.bulk_create(create_obj_list)
        if record_list:
            models.AssetRecord.objects.create(
                asset_obj=server_obj.asset,
                content=";".join(record_list),
            )

        # 交集:更新【5,】
        record_list = []
        row_map = {
            'name': "名称",
            'hwaddr': 'mac地址',
            'ipaddrs': 'ip地址',
            'netmask': '掩码',
            'up': '是否启动'
        }
        for slot in update_list:
            new_nic_row = new_nic_dict[slot]
            # {'capacity': 1024, 'slot': 'DIMM #0', 'model': 'DRAM', 'speed': '667 MHz', 'manufacturer': 'Not Specified', 'sn': 'Not Specified'}
            old_nic_row = models.NIC.objects.filter(server_obj=server_obj,
                                                    name=slot).first()
            for k, v in new_nic_row.items():
                if hasattr(old_nic_row, k):
                    value = getattr(old_nic_row, k)
                    if v != value:
                        setattr(old_nic_row, k, v)
                        record_list.append("资产%s,%s由%s变更为%s" %
                                           (slot, row_map[k], value, v))
            old_nic_row.save()

        if record_list:
            content = ";".join(record_list)
            models.AssetRecord.objects.create(asset_obj=server_obj.asset,
                                              content=content)
Example #6
0
    def post_assets(request):
        """
        添加资产信息
        :param request:
        :return:
        """

        response = BaseResponse()

        # 添加防火墙
        isfirewall = request.POST.get('isfirewall', None)

        if isfirewall:
            nic_brand = request.POST.get('fire_brand')
            nic_model = request.POST.get('fire_model')
            nic_ip = request.POST.get('fire_ip')
            nic_sn = request.POST.get('fire_sn')
            nic_idc = request.POST.get('fire_idc')
            nic_cabinet = request.POST.get('fire_cabinet')
            nic_putaway = request.POST.get('fire_putaway')
            nic_service = request.POST.get('fire_service')

            num = models.NetWork.objects.filter(sn=nic_sn).count()
            if num != 0:
                response.status = False
                response.message = '相同的sn资产已经存在!--%s' % nic_sn
                return response

            try:
                asset_obj = models.Asset(host_ip=nic_ip, host_name=nic_model, host_status=1, host_type=4, host_cpu=2,
                                         host_memory=2)
                asset_obj.save()

                models.NetWork.objects.create(model=nic_model, ip=nic_ip, idc=nic_idc, cabinet=nic_cabinet, sn=nic_sn,
                                              putaway=nic_putaway, service=nic_service, asset=asset_obj.id, brand=nic_brand)

            except Exception as e:
                response.status = False
                response.message = '添加资产错误'
                return response

            response.status = True
            return response

        # 添加网卡设备
        nic_model = request.POST.get('nic_model', None)
        if nic_model:
            nic_brand = request.POST.get('nic_brand')
            nic_ip = request.POST.get('nic_ip')
            nic_sn = request.POST.get('nic_sn')
            nic_idc = request.POST.get('nic_idc')
            nic_cabinet = request.POST.get('nic_cabinet')
            nic_putaway = request.POST.get('nic_putaway')
            nic_service = request.POST.get('nic_service')

            num = models.NetWork.objects.filter(sn=nic_sn).count()
            if num != 0:
                response.status = False
                response.message = '相同的sn资产已经存在!--%s' % nic_sn
                return response

            try:
                asset_obj = models.Asset(host_ip=nic_ip, host_name=nic_model, host_status=1, host_type=3, host_cpu=2,
                                         host_memory=2)
                asset_obj.save()

                models.NetWork.objects.create(model=nic_model, ip=nic_ip, idc=nic_idc, cabinet=nic_cabinet, sn=nic_sn,
                                              putaway=nic_putaway, service=nic_service, asset=asset_obj.id, brand=nic_brand)

            except Exception as e:
                response.status = False
                response.message = '添加资产错误'
                return response

            response.status = True
            return response

        host = request.POST.get('host')
        ip = request.POST.get('ip')
        memory = request.POST.get('memory')
        idc = request.POST.get('idc')
        cabinet = request.POST.get('cabinet')
        putaway = request.POST.get('putaway')
        machine = request.POST.get('machine')
        sn = request.POST.get('sn')
        cpu = request.POST.get('cpu')
        cpu_num = request.POST.get('cpu_num')
        core_num = request.POST.get('core_num')
        raid = request.POST.get('raid')
        service = request.POST.get('service')
        disk_list = request.POST.getlist('disk_list')
        nic_list = request.POST.getlist('nic_list')
        os = request.POST.get('os')

        for item in nic_list[2:]:
            item_list = item.split(',')
            ippaddrs = item_list[3]

        # 检查重复IP
        num = models.Asset.objects.filter(host_ip=ippaddrs).count()
        if num != 0:
            response.status = False
            response.message = '资产IP地址已经存在!'
            return response

        # 首先创建 Assets资产表 ➡️ 创建Server表关联Asset ➡️ 创建Disk表关联Server表 ➡️ 创建Memory表关联Server表
        asset_obj = models.Asset(host_ip=ippaddrs, host_name=host, host_status=1, host_type=1, host_cpu=int(cpu_num)*int(core_num), host_memory=memory)
        asset_obj.save()

        server_obj = models.DellServer(asset_id=asset_obj.id, hostname=host, manage_ip=ip, idc=idc, cabinet=cabinet,
                                       putaway=putaway, model=machine, sn=sn, cpu_id=cpu, raid=raid, service=service,
                                       cpu_num=cpu_num, core_num=core_num, os=os)
        server_obj.save()

        # ['', '', ',1,500G,SAS,7200,SEAGATE ST300MM0006 LS08S0K2B5NV']
        for item in disk_list[2:]:
            item_list = item.split(',')
            slot = item_list[1]
            capacity = item_list[2]
            model = item_list[3]
            rpm = item_list[4]
            pd_type = item_list[5]
            disk_obj = models.HardDisk(slot=slot, capacity=capacity, model=model, rpm=rpm, pd_type=pd_type, server_obj_id=server_obj.id)
            disk_obj.save()

        # ['', '', ',eth0,00:1c:42:a5:57:7a,192.168.1.1,192.168.1.254,8']
        for item in nic_list[2:]:
            item_list = item.split(',')
            name = item_list[1]
            hwaddr = item_list[2]
            ippaddrs = item_list[3]
            switch_ip = item_list[4]
            switch_port = item_list[5]
            nic_obj = models.NIC(name=name, hwaddr=hwaddr, ipaddrs=ippaddrs, switch_ip=switch_ip,
                                 switch_port=switch_port, server_obj_id=server_obj.id)
            nic_obj.save()

        response.status = True
        return response