Exemple #1
0
def process_memory(info, server):
    ############## 更新内存信息 ##############
    memory_info = info['memory']['data']
    memory_query = models.Memory.objects.filter(server=server)  # 获取数据库中的内存信息

    memory_info_set = set(memory_info)  # 将从数据库和采集客户端获取到的数据都转化为集合
    memory_query_set = {str(i.slot)
                        for i in memory_query}  # 从数据对象中获取到槽位信息并转化为str类型

    # ———————————————— 进行集合运算(根据内存的槽位slot来确定) ————————————————
    ###### 新增集合

    add_slot_set = memory_info_set - memory_query_set
    memory_list = []
    for slot in add_slot_set:
        memory_info[slot]['server'] = server  # 增加内存表中主机外键信息
        memory_list.append(
            models.Memory(**memory_info[slot]))  # 将要写入的数据添加到对象列表中
    if memory_list:
        models.Memory.objects.bulk_create(memory_list)  # 一次性写入

    ###### 删除集合
    del_slot_set = memory_query_set - memory_info_set
    if del_slot_set:  # 查询出对应主机中被移除内存的槽位号,删除数据
        models.Memory.objects.filter(server=server,
                                     slot__in=del_slot_set).delete()

    ###### 更新集合
    update_slot_set = memory_info_set & memory_query_set
    for slot in update_slot_set:
        models.Memory.objects.filter(server=server,
                                     slot=slot).update(**memory_info[slot])
Exemple #2
0
def process_memory(info, server):
    memory_info = info['memory']['data']
    memory_query = models.Memory.objects.filter(server=server)
    memory_info_set = set(memory_info)
    memory_query_set = set(str(i.slot) for i in memory_query)

    # 新增
    add_slot_set = memory_info_set - memory_query_set
    add_memory_list = []
    for slot in add_slot_set:
        memory_info[slot]['server'] = server
        add_memory_list.append(models.Memory(**memory_info[slot]))
    if add_memory_list:
        models.Memory.objects.bulk_create(add_memory_list)

    # 更新
    update_slot_set = memory_info_set & memory_query_set
    for slot in update_slot_set:
        models.Memory.objects.filter(server=server,
                                     slot=slot).update(**memory_info[slot])

    # 删除
    del_slot_set = memory_query_set - memory_info_set
    if del_slot_set:
        models.Memory.objects.filter(server=server,
                                     slot__in=del_slot_set).delete()
Exemple #3
0
    def process(self, host_obj, memory):
        memory_data = memory['data']
        old_memory_queryset = models.Memory.objects.filter(
            memory_host_id=host_obj.id)
        old_memory_obj = {obj.slot: obj for obj in old_memory_queryset}
        old_memory_set = set(old_memory_obj.keys())
        new_memory_set = set(memory_data.keys())
        # print(old_memory_set, new_memory_set)

        record_msg_list = []
        # 增加数据
        append_memory_set = new_memory_set - old_memory_set
        # print(append_memory_set)
        creat_obj_list = []
        for slot_num in append_memory_set:
            slot_dict = memory_data[slot_num]
            # 方式一增加数据
            # models.memory.objects.create(**slot_dict, memory_host_id=host_obj.id)

            # 方式二增加数据bulk_create
            memory_obj = models.Memory(**slot_dict, memory_host_id=host_obj.id)
            creat_obj_list.append(memory_obj)

        if creat_obj_list:
            models.Memory.objects.bulk_create(creat_obj_list, batch_size=10)
            append_msg = '在%s槽位新增内存' % (slot_num)
            record_msg_list.append(append_msg)

        # 删除内存
        delete_memory_set = old_memory_set - new_memory_set
        # print(delete_memory_set)
        models.Memory.objects.filter(slot__in=delete_memory_set).delete()
        if delete_memory_set:
            delete_msg = '在%s槽位删除内存' % ','.join(delete_memory_set)
            record_msg_list.append(delete_msg)

        # 更新数据
        update_memory_set = new_memory_set & old_memory_set
        print('update_memory_set: %s' % update_memory_set)
        for slot in update_memory_set:
            old_memory_row_obj = old_memory_obj[slot]  # 磁盘表对应行的对象
            new_memory_row_dict = memory_data[
                slot]  # {'slot': '4', 'pd_type': 'SATA', 'caity': '479', 'model': 'NSA'}
            for k, v in new_memory_row_dict.items():
                # 判断每个新获取的字段值是否和老表里的值相等,不相等就更新
                if v != getattr(old_memory_row_obj, k):
                    update_msg = '%s由%s变成了%s' % (
                        k, getattr(old_memory_row_obj, k), v)
                    msg = '内存槽位%s: %s' % (slot, update_msg)
                    print(getattr(old_memory_row_obj, k))
                    record_msg_list.append(msg)
                    setattr(old_memory_row_obj, k, v)

            old_memory_row_obj.save()
        # for k, v in memory_data.items():
        #     print(k, v)
        #     models.memory.objects.create(**v, memory_host_id=1)
        # for memory_k, memory_v in v.items():
        #     print(memory_k, memory_v)
        # pass
        # models.memory.objects.create()

        if record_msg_list:
            print(record_msg_list)
            models.Record.objects.create(record_host_id=host_obj.id,
                                         content='\n'.join(record_msg_list))
Exemple #4
0
    def post(self, request):
        # info = request.data
        date = decrypt(request.body)
        info = json.loads(date.decode('utf-8'))
        active_type = info.get('type')

        if active_type == 'create':
            # 新增主机
            basic_info = info['basic']['data']
            cpu = info['cpu']['data']
            main_board = info['main_board']['data']
            server_obj = models.Server.objects.create(**basic_info, **cpu,
                                                      **main_board)

            # 添加硬盘信息
            disk_info = info['disk']['data']
            disk_list = []
            for disk in disk_info.values():
                disk_list.append(models.Disk(**disk, server=server_obj))
            models.Disk.objects.bulk_create(disk_list)

            # 添加网卡信息
            nic_info = info['nic']['data']
            nic_list = []
            for name, nic in nic_info.items():
                nic_list.append(models.NIC(**nic, name=name,
                                           server=server_obj))
            models.NIC.objects.bulk_create(nic_list)

            # 添加内存信息
            memory_info = info['memory']['data']
            memory_list = []
            for memory in memory_info.values():
                memory_list.append(models.Memory(**memory, server=server_obj))
            models.Memory.objects.bulk_create(memory_list)

        elif active_type == 'update':
            hostname = info['basic']['data']['hostname']
            server_list = models.Server.objects.filter(hostname=hostname)
            server = server_list.first()
            process_basic(info, server_list)
            process_disk(info, server)
            process_memory(info, server)
            process_nic(info, server)

        elif active_type == 'update_hostname':
            # 更新主机资产信息 + 主机名
            old_hostname = info['old_hostname']
            hostname = info['basic']['data']['hostname']
            server_list = models.Server.objects.filter(hostname=old_hostname)
            server = server_list.first()
            server.hostname = hostname
            # server.save()

            process_basic(info, server_list)
            process_disk(info, server)
            process_memory(info, server)
            process_nic(info, server)

        ret = {'status': True, 'hostname': info['basic']['data']['hostname']}
        return Response(ret)
Exemple #5
0
    def post(self, request):

        info = json.loads(decrypt(request.body).decode('utf-8'))
        action_type = info.get('type')
        print(info)
        ret = {'status': True, 'error': None, 'hostname': None}

        if action_type == 'create':

            # 新增主机信息

            server_dict = {}
            server_dict.update(info['basic']['data'])
            server_dict.update(info['cpu']['data'])
            server_dict.update(info['board']['data'])

            # 将字典装换为关键字参数的形式: name=value, 存入到数据库中,返回此条记录对象
            server = models.Server.objects.create(**server_dict)

            # 新增磁盘信息

            disk_info = info['disk']['data']
            disk_list = []
            for i in disk_info.values():
                i['server'] = server
                disk_list.append(models.Disk(**i))

            models.Disk.objects.bulk_create(disk_list)

            # 新增内存信息

            memory_info = info['memory']['data']
            memory_list = []
            for i in memory_info.values():
                i['server'] = server
                memory_list.append(models.Memory(**i))

            models.Memory.objects.bulk_create(memory_list)

            # 新增网卡信息
            nic_info = info['nic']['data']
            nic_list = []
            for name, i in nic_info.items():
                i['server'] = server
                i['name'] = name
                nic_list.append(models.NIC(**i))

            models.NIC.objects.bulk_create(nic_list)

        ## ****************************** 更新资产信息 ****************************
        elif action_type == 'update':
            # 更新资产信息

            # 查找要修改的主机
            hostname = info['basic']['data']['hostname']
            server_list = models.Server.objects.filter(hostname=hostname)
            server = server_list.first()

            service.process_basic(info, server_list)
            service.process_disk(info, server)
            service.process_memory(info, server)
            service.process_nic(info, server)



        ## ****************************** 更新主机和资产信息 ****************************

        elif action_type == 'host_change':
            # 更新资产信息和主机名
            print('更新资产信息和主机名')

            #### 更新主机名
            cert = info.get('cert')
            server_list = models.Server.objects.filter(hostname=cert)
            server = server_list.first()
            server.hostname = info['basic']['data']['hostname']
            server.save()
            service.process_basic(info, server_list)
            service.process_disk(info, server)
            service.process_memory(info, server)
            service.process_nic(info, server)

        ret['hostname'] = info['basic']['data']['hostname']
        return Response(ret)