def __iter__(self): '''data_list=({'bussiness':'Monitor'}, {'bussiness':'reportURLNUM', 'bussiness':'monitorURLNUM')''' '''option = CangeList_pic''' ''' query_dict: request dict''' # val = self.option.get_value(self.data_list) yield '<div class="whole">' tatal_query_dict = self.query_dict.copy() tatal_query_dict._mutable = True origin_value_list = self.query_dict.getlist('bussiness') if origin_value_list: yield '<a href="#">类型</a>' else: yield '<a class="active" href="#">类型</a>' yield '</div>' yield '<div class="others">' logger.debug(self.data_list) for item in self.data_list: val = 'bussiness' text=item[val] logger.debug(text) query_dict = self.query_dict.copy() query_dict._mutable = True if str(text) in origin_value_list: query_dict.pop(val) yield '<a class="active" href="?%s">%s</a>' %(query_dict.urlencode(),text) else: query_dict[val]=text yield '<a href="?%s">%s</a>' %(query_dict.urlencode(),text) yield '</div>'
def get_cpuinfo(self): ''' CPU(s): 32 #逻辑CPU核数 Thread(s) per core: 2 #每核超线程数 Core(s) per socket: 8 #每个cpu核数 Socket(s): 2 #物理cpu个数 Model name: Intel(R) Xeon(R) CPU E5-2650 v2 @ 2.60GHz CPU MHz: 1200.000 #cpu主频 { '座': 'Socket(s)', '型号名称': 'Model name', } :return: ''' command = 'lscpu' cpuinfo_h = self.organize_data(command) try: cpuinfo_h['Socket(s)'] = cpuinfo_h.pop('座') cpuinfo_h['Model name'] = cpuinfo_h.pop('型号名称') except Exception as e: logger.debug(e) cpu_info = { 'name': cpuinfo_h['Model name'], 'Core_number': cpuinfo_h['Core(s) per socket'], #核数 'Core_thread': cpuinfo_h['Thread(s) per core'], #每核线程 'Number': cpuinfo_h['Socket(s)'], #颗数 'Frequ': cpuinfo_h['CPU MHz'], #频率 } return cpu_info
def changelist_view(self, request): if request.method == 'POST': action_name = request.POST.get('action') action_dict = self.get_action_dict() if action_name not in action_dict: return HttpResponse('非法请求') response = getattr(self, action_name)(request) if response: return response # ##### 处理搜索 ##### search_list, q, con = self.get_search_condition(request) # ##### 处理分页 ##### from stark.utils.pagination import Pagination total_count = self.model_class.objects.filter(con).count() query_params = request.GET.copy() query_params._mutable = True page = Pagination(request.GET.get('page'), total_count, request.path_info, query_params, per_page=7) # 获取组合搜索筛选 origin_queryset = self.get_queryset() # logger.debug(con,self.get_list_filter_condition()) # 获取开始结束时间 kssj, jssj = self.get_time(request) logger.debug(kssj,jssj) queryset = origin_queryset.filter(inserttime__range=(kssj,jssj)).filter(con).filter(**self.get_list_filter_condition()).order_by( *self.get_order_by()) src=self.get_plt(request,queryset) cl = CangeList_pic(self, queryset, q, search_list, page) # ######## 组合搜索 ######### # list_filter = ['name','user'] context = { 'cl': cl, 'src':src } return render(request,'business_presen/pic.html',context)
def su_root(self, root_pwd, command): channl = self.ssh.invoke_shell() time.sleep(0.1) if self.user != 'root': #先判断提示符,然后下一步在开始发送命令,这样大部分机器就都不会出现问题 buff = '' while not buff.endswith('$ '): resp = channl.recv(9999) logger.debug(999) buff += resp.decode('utf8') time.sleep(0.1) # print('获取登录后的提示符:%s' %buff) channl.send(' export LANG=en_US.UTF-8 \n') #解决错误的关键,编码问题 channl.send('export LANGUAGE=en \n') channl.send('su - \n') buff = "" while not buff.endswith('Password: '******'utf8') channl.send(root_pwd) channl.send('\n') buff = "" while not buff.endswith('# '): resp = channl.recv(9999) buff += resp.decode('utf8') channl.send(command) #放入要执行的命令 channl.send('\n') buff = '' while not buff.endswith('# '): resp = channl.recv(9999).decode() buff += resp result = buff # print(type(buff)) return result
def get_list_filter_condition(self): comb_condition = {} for option in self.get_list_filter(): element = self.request.GET.getlist(option.field) if element: comb_condition['%s__in' % option.field] = element logger.debug(comb_condition) return comb_condition
def ip_network(self): if '/' not in self.ip: return self.ip else: net = ipaddress.ip_network(self.ip, strict=False) # 打印网络号,掩码 ip = str(net.network_address) + '/' + self.ip.split('/')[1] logger.debug(ip) return ip
def organize_data(self, command, memary=False, system=False, hostname=False): data_h = self.get_data(command) if system == True or hostname == True: logger.debug(data_h) data_h = re.sub('\r', '', data_h) return data_h data_h = re.sub(':', ':', data_h) if ':' in data_h: ansi_escape = re.compile( r''' \x1B [@-_] [0-?]* [ -/]* [@-~] ''', re.VERBOSE) data_h = ansi_escape.sub('', data_h) data_h = re.sub('\t\r', '', data_h).split('\n') del data_h[0] del data_h[-1] data_dict = {} if memary: data_dict = {'count': 0} for x in data_h: if x and ':' in x: x_list = x.split(':') data_dict[x_list[0].strip()] = x_list[1].strip() if memary and "Size" in x: data_dict['count'] = data_dict['count'] + 1 else: data_list = [] data_h = re.sub('\t', '', data_h).split('\n') logger.debug(data_h) del data_h[0] del data_h[-1] for ip_info in data_h: data_dict = {} if ip_info and 'inet' in ip_info: ip_add = ip_info.split()[1] if '/' in ip_add: ip = ip_add.split('/') data_dict['IP'] = ip[0] data_dict['NETMASK'] = ip[1] data_list.append(data_dict) else: data_dict['IP'] = ip_add data_dict['NETMASK'] = '32' data_list.append(data_dict) return data_list return data_dict
def get_time(self,request): kssj = request.GET.get('kssj') jssj = request.GET.get('jssj') logger.debug(kssj, jssj) if not kssj and not jssj: kssj=(datetime.datetime.now()+datetime.timedelta(days=-180)).strftime("%Y-%m-%d") jssj=datetime.datetime.now().strftime("%Y-%m-%d") elif kssj and not jssj: jssj = (datetime.datetime.strptime(kssj,"%Y-%m-%d")+datetime.timedelta(days=180)).strftime("%Y-%m-%d") elif jssj and not kssj: kssj = (datetime.datetime.strptime(jssj,"%Y-%m-%d")+datetime.timedelta(days=-180)).strftime("%Y-%m-%d") logger.debug(kssj, jssj) return kssj,jssj
def get_time_filter(self,request): kssj = request.GET.get('kssj') jssj = request.GET.get('jssj') logger.debug(kssj, jssj) con=Q() if kssj and not jssj: con = Q(sendtime__gt=kssj) elif jssj and not kssj: con = Q(sendtime__lt=jssj) elif kssj and jssj: con = Q(sendtime__range=(kssj,jssj)) logger.debug(kssj, jssj) return con
def get_hostinfo(self): hostname = self.get_hostname() host = self.get_host() cpu_info = self.get_cpuinfo() memory = self.get_memory() ips = self.get_ip() harddection_dict = { 'host': host, 'cpu_info': cpu_info, 'memory': memory, 'ips': ips, } logger.debug(hostname, harddection_dict) return hostname, harddection_dict
def regist_info(self): hostname = self.get_hostname() host = self.get_host() cpu_info = self.get_cpuinfo() memory = self.get_memory() ips = self.get_ip() name = 'host_info' harddection_dict = { 'host': host, 'cpu_info': cpu_info, 'memory': memory, 'ips': ips, } logger.debug(hostname) rehis_h.hset(name, hostname, json.dumps(harddection_dict))
def host_table_tmp(obj, list_name): logger.debug(obj) head_list = [] body_list = [] for name in list_name: logger.debug(name) head_list.append(obj._meta.get_field(name).verbose_name) body_list.append(getattr(obj, name)) logger.debug(head_list) logger.debug(body_list) return {'head_list': head_list, 'body_list': body_list}
def changelist_count(self,request): origin_queryset = self.get_queryset() # logger.debug(con,self.get_list_filter_condition()) # 获取开始结束时间 kssj, jssj = self.get_time(request) # logger.debug(kssj, jssj) comb_condition = {} element = request.GET.getlist('MonitorAddress') if element: comb_condition['%s__in' % 'MonitorAddress'] = element logger.debug(comb_condition) _time_format = self.get_time_format(request) _annotate_list = self.get_annotate_list(request) logger.debug(_annotate_list) queryset = origin_queryset.filter(inserttime__range=(kssj, jssj)).filter().filter(**comb_condition ).extra(select=_time_format).values('MonitorAddress__name','inserttime').annotate(*_annotate_list).order_by( *self.get_order_by()) cl = ChangeList_count(self, queryset, request) c2 = ChangeList_timeunit(self, request.GET) return render(request,'business_presen/count.html', {'cl': cl, 'c2': c2,})
def get_interva(self,min_shu,max_shu,identification): logger.debug(min_shu) logger.debug(max_shu) if identification.filed == "RATE": interva = round((max_shu-min_shu)/10,3) min_shu = round(min_shu, 4) max_shu = round(max_shu, 4) + interva elif identification.filed == "GURL_NU": interva = (max_shu - min_shu) // 10 try: interva = self.plastic(interva) except: pass max_shu = max_shu + interva elif identification.filed == 'PIC-SIZE': interva = (max_shu - min_shu) // 10 # interva = self.plastic(interva) # min_shu = self.plastic(min_shu) if interva >= 1: interva = (max_shu - min_shu) // 10 # interva = self.plastic(interva) # min_shu = self.plastic(min_shu) else: interva = (max_shu - min_shu) // 10 interva = self.plastic(interva) min_shu = self.plastic(min_shu) if interva == 0: if identification.filed == "RATE": interva = round(max_shu/10, 3) else: interva = max_shu//10 if interva==0: interva=1 if pd.isna(min_shu): min_shu =0 if pd.isna(max_shu): max_shu=1 if pd.isna(interva): interva=1 max_shu = max_shu + interva return min_shu,max_shu,interva
def find_hosts(self): iphosts = [] sys = platform.system() # name = settings.HARDNAME name = 'harddetection' if '/' not in self.ip: iphosts.append(self.ip) else: net = ipaddress.ip_network(self.ip, strict=False) for ip in net.hosts(): if sys == "Windows": comment = 'ping -n 1 %s' % ip p = subprocess.Popen(comment, shell=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) sleep(1) if p.poll() is None: logger.debug(ip, "is down") pass else: logger.debug(ip, 'is up') iphosts.append(ip) elif sys == "Linux": comment = 'ping -c2 %s &> /dev/null' % ip p = subprocess.Popen(comment, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) sleep(2) if p.poll() == 0: logger.debug(ip, "is up") iphosts.append(ip) # pass else: logger.debug(ip, "is down") # logger.debug(name,self.ip_net) s = rehis_h.hget(name, self.ip_net) v = json.loads(s) v['completiontime'] = '共探测到%s主机' % len(iphosts) rehis_h.hset(name, self.ip_net, json.dumps(v)) return iphosts
def regist_hostinfo(self, port, user, passwd): hosts = self.find_hosts() i = 0 y = 0 for host in hosts: logger.debug(111, host) try: self.sshconn(str(host), port, user, passwd) logger.debug(222) self.regist_info() self.sshclose() i += 1 except Exception as e: logger.debug(e, r'\n', host, '不能连接') y += 1 date = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') v = rehis_h.hget(self.harddetection, str(self.ip_net)) v = json.loads(v) v['endtime'] = date v["completiontime"] = v["completiontime"] + "登录成功%s台,登录失败%s台" % (i, y) # logger.debug(self.ip.split('/'][1]) rehis_h.hset(self.harddetection, self.ip_net, json.dumps(v))
if complete: date = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') v['endtime']=date rehis_h.hset('harddetection', ip_net, json.dumps(v)) def get_hash(name): for item in rehis_h.hscan_iter(name): yield item while True: time.sleep(3) vs = get_hash('harddetection') for i in vs: k = i[0] v = json.loads(i[1]) if v['endtime']: logger.debug(v['endtime']) else: v['endtime'] = '正在处理' port = '22' user = '******' passwd = 'hamob123!@#' try: s = rehis_h.hget('harddetection', k) s = json.loads(s) s['endtime'] = '正在处理' rehis_h.hset('harddetection', k, json.dumps(s)) except: logger.debug('数据有误') continue # s = rehis_h.hget('harddetection', k) # s = json.loads(s)
def plt_size(self, j, bussiness): j[bussiness] = round(j['Monitor'] / j['monitorURLNUM'], 1) j = j.fillna(0) logger.debug(j[[bussiness]]) return j[[bussiness]]
def get_hostname(self): cmmand = 'hostname' hostname = self.organize_data(cmmand, hostname=True) logger.debug(hostname) return hostname
def __init__(self,data_list,option,query_dict): self.data_list = data_list self.option = option self.query_dict = query_dict logger.debug(option.field)
def plt_rate(self,j,bussiness): j[bussiness] = round(j['reportURLNUM'] / j['monitorURLNUM'],5) logger.debug(j[[bussiness]]) j = j[[bussiness,]] return j
def get_hostname(self, hostname=True): cmmand = 'hostname' hostname = self.organize_data(cmmand, hostname=True) hostname = hostname.split('\n')[1] logger.debug(hostname) return hostname
def get_data(hostname): key = config_h.get_config('Redis_h', 'HARDINFO') host_info = json.loads(rehis_h.hget(key, hostname)) try: mem = Memory.objects.get(Size=host_info['memory']['Size']) except Exception as e: Memory.objects.create(Size=host_info['memory']['Size']) mem = Memory.objects.get(Size=host_info['memory']['Size']) try: Producer =Producer_Name.objects.get(Product_Name=host_info['host']['Product_Name']) except Exception as e: Producer_Name.objects.create(Product_Name=host_info['host']['Product_Name']) Producer = Producer_Name.objects.filter(Product_Name=host_info['host']['Product_Name']) try: Model_Name =Manufacturer.objects.get(Manufacturer=host_info['host']['Manufacturer']) except: Manufacturer.objects.create(Manufacturer=host_info['host']['Manufacturer']) Model_Name = Manufacturer.objects.get(Manufacturer=host_info['host']['Manufacturer']) Core_Number_instance = Count.objects.get(count=host_info['cpu_info']['Core_number']) try: Frequ_instance = Frequ.objects.get(Frequ=host_info['cpu_info']['Frequ']) except Exception as e: Frequ_instance = Frequ.objects.create(Frequ=host_info['cpu_info']['Frequ']) Frequ_instance = Frequ.objects.get(Frequ=host_info['cpu_info']['Frequ']) Core_thread_instance = Count.objects.get(count=host_info['cpu_info']['Core_thread']) try: CPU_instance = CPU.objects.get(name=host_info['cpu_info']['name']) except Exception as e: CPU.objects.create(name=host_info['cpu_info']['name'],Core_number=Core_Number_instance,Frequ=Frequ_instance,Core_thread=Core_thread_instance) CPU_instance = CPU.objects.get(name=host_info['cpu_info']['name']) CPU_NUMber = Count.objects.get(count=host_info['cpu_info']['Number']) Memory_NUMber = Count.objects.get(count=host_info['memory']['count']) frequ = host_info['memory'].get("Speed",None) if frequ: try: Memory_Frequ =Frequ.objects.get(Frequ=frequ) except: Frequ.objects.create(Frequ=frequ) Memory_Frequ = Frequ.objects.get(Frequ=frequ) else: Memory_Frequ = Frequ.objects.get(Frequ=0) logger.debug(host_info['host']['Serial_Number']) logger.debug(hostname, Producer,Model_Name,CPU_instance,CPU_NUMber,mem, Memory_Frequ,Memory_NUMber) try: host = Host.objects.create(HOSTNAME=hostname, Serian_Number=host_info['host']['Serial_Number'], Producer_Name=Producer, Manufacturer=Model_Name, CPU=CPU_instance, CPU_NUMber=CPU_NUMber, Memory=mem, Memory_Frequ=Memory_Frequ, Memory_NUMber=Memory_NUMber, ) for ip in host_info['ips']: try: netmask = Count.objects.get(count=int(ip['NETMASK'])) except: Count.objects.create(count=int(ip['NETMASK'])) netmask = Count.objects.get(count=int(ip['NETMASK'])) try: a =IP.objects.create(IP=ip['IP'],NETMASK=netmask) host.IP.add(a) except Exception as e: a = IP.objects.get(IP=ip['IP']) host.IP.add(a) logger.debug(e) # return HttpResponse('录入IP有误') host.save() except Exception as e: logger.debug(e) return HttpResponse('%s 主机已存在, 或者其它原因无法保存' %hostname)
def sshclose(self): try: self.ssh.ssh_close() except Exception as e: logger.debug(e)