CMDB硬件信息管理系統開發(二)
阿新 • • 發佈:2017-09-30
rep spa code eat exe not key end ==
完成任務:
1、網卡、內存資產變更數據及日誌記錄;
2、get請求獲取今日未采集主機列表;
1 # -*- coding: utf-8 -*- 2 # __author__ = "maple" 3 from repository import models 4 5 6 class Nic: 7 def __init__(self, server_obj, info): 8 self.server_obj = server_obj 9 self.nic_dict = info 10 self.record_list = []nic.py11 12 def process(self): 13 # 網卡 14 new_nic_info_dict = self.nic_dict[‘data‘] 15 16 old_nic_info_list = self.server_obj.nic.values(‘name‘) 17 new_nic_name_set = set(name for name in new_nic_info_dict) 18 old_nic_name_set = {obj[‘name‘] for obj in old_nic_info_list}19 20 add_name_list = new_nic_name_set.difference(old_nic_name_set) 21 del_name_list = old_nic_name_set.difference(new_nic_name_set) 22 update_name_list = old_nic_name_set.intersection(new_nic_name_set) 23 24 if add_name_list: 25 self.add_nic(add_name_list, new_nic_info_dict)26 27 if del_name_list: 28 self.del_nic(del_name_list) 29 30 if update_name_list: 31 self.update_disk(update_name_list, new_nic_info_dict) 32 33 if self.record_list: 34 models.ServerRecord.objects.create(server_obj=self.server_obj, content=‘\n‘.join(self.record_list)) 35 36 def add_nic(self, add_name_list, new_nic_info_dict): 37 # 增加 38 for name in add_name_list: 39 value = new_nic_info_dict[name] 40 tmp = "新增一塊名稱為[%s],hwaddr為[%s]的網卡" % (name, value[‘hwaddr‘]) 41 self.record_list.append(tmp) 42 value[‘server_obj‘] = self.server_obj 43 value[‘name‘] = name 44 models.NIC.objects.create(**value) 45 46 def del_nic(self, del_name_list): 47 # 刪除 48 tmp = "名稱為%s的網卡被移除" % ([i for i in del_name_list]) 49 self.record_list.append(tmp) 50 models.NIC.objects.filter(server_obj=self.server_obj, name__in=del_name_list).delete() 51 52 def update_disk(self, update_name_list, new_disk_info_dict): 53 # 更新 54 for name in update_name_list: 55 value = new_disk_info_dict[name] 56 obj = models.NIC.objects.filter(server_obj=self.server_obj, name=name).first() 57 for k, new_val in value.items(): 58 old_val = getattr(obj, k) 59 60 if old_val != new_val: 61 print(k, old_val, type(old_val), new_val, type(new_val)) 62 tmp = "名稱%s的網卡的%s由%s變更為%s" % (name, k, old_val, new_val) 63 self.record_list.append(tmp) 64 setattr(obj, k, new_val) 65 obj.save()
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硬件信息管理系統開發(二)