时间:2021-07-01 10:21:17 帮助过:8人阅读
1 # -*- coding: utf-8 -*- 2 # __author__ = "maple" 3 from repository import models 4 5 6 class Memory: 7 def __init__(self, server_obj, info): 8 self.server_obj = server_obj 9 self.memory_dict = info 10 self.record_list = [] 11 12 def process(self): 13 # 内存 14 new_memory_info_dict = self.memory_dict[‘data‘] 15 old_memory_info_list = self.server_obj.memory.all() 16 new_memory_slot_set = set(new_memory_info_dict.keys()) 17 old_memory_slot_set = {obj.slot for obj in old_memory_info_list} 18 19 add_slot_list = new_memory_slot_set.difference(old_memory_slot_set) 20 del_slot_list = old_memory_slot_set.difference(new_memory_slot_set) 21 update_slot_list = old_memory_slot_set.intersection(new_memory_slot_set) 22 23 if add_slot_list: 24 self.add_memory(add_slot_list, new_memory_info_dict) 25 26 if del_slot_list: 27 self.del_memory(del_slot_list) 28 29 if update_slot_list: 30 self.update_memory(update_slot_list, new_memory_info_dict) 31 32 if self.record_list: 33 models.ServerRecord.objects.create(server_obj=self.server_obj, content=‘\n‘.join(self.record_list)) 34 35 def add_memory(self, add_slot_list, new_memory_info_dict): 36 # 增加 37 for slot in add_slot_list: 38 value = new_memory_info_dict[slot] 39 tmp = "槽位[%s]添加一块[%sG]内存" % (slot, value[‘capacity‘]) 40 self.record_list.append(tmp) 41 value[‘server_obj‘] = self.server_obj 42 models.Memory.objects.create(**value) 43 44 def del_memory(self, del_slot_list): 45 # 删除 46 tmp = "槽位%s的内存被移除" % ([i for i in del_slot_list]) 47 self.record_list.append(tmp) 48 models.Memory.objects.filter(server_obj=self.server_obj, slot__in=del_slot_list).delete() 49 50 def update_memory(self, update_slot_list, new_memory_info_dict): 51 # 删除 52 for slot in update_slot_list: 53 value = new_memory_info_dict[slot] 54 obj = models.Memory.objects.filter(server_obj=self.server_obj, slot=slot).first() 55 for k, new_val in value.items(): 56 old_val = getattr(obj, k) 57 if str(old_val) != new_val: 58 print(k, old_val, type(old_val), new_val, type(new_val)) 59 tmp = "槽位%s的硬盘的%s由%s变更为%s" % (slot, k, old_val, new_val) 60 self.record_list.append(tmp) 61 setattr(obj, k, new_val) 62 obj.save()memory.py
1 import json 2 from django.shortcuts import render, HttpResponse 3 from django.views.decorators.csrf import csrf_exempt 4 from .plugins import PluginManger 5 from datetime import date 6 from repository import models 7 from django.db.models import Q 8 9 10 @csrf_exempt # 不再验证csrf 11 def server(request): 12 if request.method == "GET": 13 current_date = date.today() 14 # 获取今日未采集的主机列表 15 host_list = models.Server.objects.filter( 16 Q(Q(latest_date=None) | Q(latest_date__date__lt=current_date)) & Q(server_status_id=2) 17 ).values(‘hostname‘) 18 host_list = list(host_list) 19 print(host_list) 20 return HttpResponse(json.dumps(host_list)) 21 if request.method == "POST": 22 # 客服端提交的最新资产数据 23 server_dict = json.loads(request.body.decode(‘utf-8‘)) 24 25 if not server_dict[‘basic‘][‘status‘]: 26 return HttpResponse("臣妾做不到啊") 27 28 manager = PluginManger() 29 response = manager.exec(server_dict) 30 31 return HttpResponse(json.dumps(response))API/views.py
CMDB硬件信息管理系统开发(二)
标签:dict content 系统 set load blog section objects views