码迷,mamicode.com
首页 > 数据库 > 详细

CMDB硬件信息管理系统开发(二)

时间:2017-09-30 16:25:26      阅读:300      评论:0      收藏:0      [点我收藏+]

标签:dict   content   系统   set   load   blog   section   objects   views   

完成任务:

  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 = []
11 
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()
nic.py
技术分享
 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   

原文地址:http://www.cnblogs.com/maple-shaw/p/7615151.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!