def regist_device(): signed_total = IP_Detail.where(type=type).count() tmp = IPrms_Segment.where(segmenttype=type).select_head() net_addr = self.get_net_addr(tmp.segmentname) sub_mask = tmp.subnetmask tmp_seg = tmp.segmentname subnet_id = self.get_subnet_id(tmp_seg) dev_id = dev_start_id #为了防止ip分配因堆叠ip存在而错误,计算堆叠ip数量 pile_ip_num = IP_Detail.where(remark='PILE_MGT_IP').count() / 2 host_id_start = signed_total - pile_ip_num + 1 host_id_end = host_id_start + dev_num check = 0 fullseg = 255 * check for i in range(host_id_start, host_id_end, 1): if i > 255: check = i / 256 fullseg = 255 * check #fullseg用来减去255的倍数得到下一子网段的正确主机号,参数check用来使子网号保持正确 ip = '%s.%s.%s' % (net_addr, subnet_id+check, i-fullseg) gateway = '%s.%s.1' % (net_addr, subnet_id+check) #la管理分配存入ip_detail表 ip_detail = IP_Detail() ip_detail.address = ip ip_detail.gateway = gateway ip_detail.subnetmask = sub_mask ip_detail.netdevicename = '%s%s' % (dev_name, dev_id) ip_detail.port = type ip_detail.type = type ip_detail_list.append(ip_detail) dev_id = dev_id + 1
def regist_device(): signed_total = IP_Detail.where(type=type).count() signed_dev_num = IP_Detail.where(netdevicename=dev_name, type=type).count() tmp = IPrms_Segment.where(segmenttype=type).select_head() net_addr = self.get_net_addr(tmp.segmentname) sub_mask = tmp.subnetmask tmp_seg = tmp.segmentname subnet_id = self.get_subnet_id(tmp_seg) dev_id = 1 host_id = signed_total + 1 #已经有设备注册过的情况 if signed_dev_num != 0: #如果有事先注册的设备,那么子网序号以及主机号从最后一次注册的顺序开始 #tmp = IP_Detail.where(netdevicename=dev_name, netdeviceid=signed_dev_num).select_head() subnet_id = self.get_subnet_id(tmp.segmentname) dev_id = signed_dev_num + 1 host_id_start = host_id host_id_end = host_id_start + dev_num check = 0 fullseg = 255 * check for i in range(host_id_start, host_id_end, 1): if i > 255: check = i / 256 fullseg = 255 * check #fullseg用来减去255的倍数得到下一子网段的正确主机号,参数check用来使子网号保持正确 ip = '%s.%s.%s' % (net_addr, subnet_id+check, i-fullseg) gateway = '%s.%s.1' % (net_addr, subnet_id+check) #la管理分配存入ip_detail表 ip_detail = IP_Detail() ip_detail.address = ip ip_detail.gateway = gateway ip_detail.subnetmask = sub_mask ip_detail.netdevicename = dev_name ip_detail.netdeviceid = dev_id ip_detail.port = type ip_detail.type = type ip_detail_list.append(ip_detail) dev_id = dev_id + 1
def regist_device(): signed_total = IP_Detail.where(type='CONN').count() tmp = IPrms_Segment.where(segmenttype='CONN').select_head() net_addr = self.get_net_addr(tmp.segmentname) subnet_id = self.get_subnet_id(tmp.segmentname) sub_mask = tmp.subnetmask host_id_start = signed_total host_id_end = host_id_start + 2 * dev1_num * dev2_num gateway_ip = '' type = 'CONN' check = 0 fullseg = 255 * check dev1_id = dev1_start_id dev2_id = 1 for i in range(host_id_start, host_id_end, 1): if i > 255: check = i / 256 fullseg = 256 * check ip = '%s.%s.%s' % (net_addr, subnet_id+check, i-fullseg) source_dev_name = dev1_name source_dev_id = dev1_id dest_dev_name = dev2_name dest_dev_id = dev2_id #判断该ip是否是源ip if issourceip(ip): dev = '%s%s' % (source_dev_name, source_dev_id) ##dev_id = source_dev_id port = 'CO%s' % (dest_dev_id) else: dev = '%s%s' % (dest_dev_name, dest_dev_id) ##dev_id = dest_dev_id port = '%s%s' % (source_dev_name, source_dev_id) dev2_id = dev2_id + 1 '''ip_detail表注册''' ip_detail = IP_Detail() ip_detail.address = ip ip_detail.gateway = gateway_ip ip_detail.subnetmask = sub_mask ip_detail.netdevicename = dev ##ip_detail.netdeviceid = dev_id ip_detail.port = port ip_detail.type = type ip_detail_list.append(ip_detail) if dev2_id > dev2_num: dev2_id = 1 dev1_id = dev1_id + 1 print "完成设备端口互联资源规划"
def query_conn_remain(self): try: seg_total = IPrms_Segment.where(segmenttype='CONN').count() tmp = IPrms_Segment.where(segmenttype='CONN').select_head() mask_id = self.get_host_id(tmp.subnetmask) subnet_id = self.get_subnet_id(tmp.subnetmask) if subnet_id == 255: total = seg_total * (256-mask_id) else: total = seg_total * 256 * (256-mask_id) signed_num = IP_Detail.where(type='CONN').count() remain_num = total - signed_num print "互联网段剩余可分配ip地址数量:%s个" % (remain_num) return remain_num except: print "尚未分配互联网段"
def query_manage_remain(self, type): try: last_seg = IPrms_Segment.where(segmenttype=type).select_tail() first_seg = IPrms_Segment.where(segmenttype=type).select_head() sign_total = IP_Detail.where(type=type).count() min_subnet_id = self.get_subnet_id(first_seg.min) max_subnet_id = self.get_subnet_id(last_seg.max) if min_subnet_id == max_subnet_id: dev_max = self.get_host_id(first_seg.max) else: dev_max = (max_subnet_id-min_subnet_id+1) * 255 remain_num = dev_max - sign_total print "管理网段剩余可分配设备数量:%s个" % (remain_num) return remain_num except: print "尚未分配管理网段"
def regist_device(): #选择第一个未使用的ip,获取alloc的信息作为注册开始信息 tmp = IPrms_IP.where(state='Off').select_head() net_addr = self.get_net_addr(tmp.ip) subnet_id_start = self.get_subnet_id(tmp.ip) seg_length = 2 ** (32-mask) if mask > 16 and mask < 24: seg_length = 256 subnet_id_end = subnet_id_start + (2**(24-mask)) * dev_num - 1 host_id_end = 255 #subnet_num指一个设备所占的最多子网数量 subnet_num = 2 ** (24-mask) else: subnet_num = 1 seg_length = 2 ** (32-mask) x = 256 / seg_length if dev_num % x == 0: subnet_id_end = subnet_id_start + dev_num / x - 1 host_id_end = 255 else: subnet_id_end = subnet_id_start + dev_num / x host_id_end = (dev_num%x) * seg_length sub_mask = self.cal_sub_mask(mask) vlan = vlan_name dev_id1 = dev_start_id1 dev_id2 = dev_start_id2 seg_id = IPrms_Segment.where().count() #将堆叠设备号加入注册队列 register_queue = [] tuple_index = 0 for i in range(dev_start_id1, dev_end_id1 + 1, 2): pile_dev_id = (dev_id1, dev_id2) register_queue.append(pile_dev_id) dev_id1 = dev_id1 + 2 dev_id2 = dev_id2 + 2 for i in range(subnet_id_start, subnet_id_end+1, 1): for j in range(0, 256*subnet_num, 1): #满足三个条件即注册信息到表:1.主机号为下一个网段号的主机号 2.够分配下一个网段 3.一个设备的分配跨过了正确数目子网号 if j % seg_length == 0 and j + seg_length <= 256 and (i-subnet_id_start) % subnet_num == 0: #当到达最后一个子网,所有设备都注册后,这时检索到的ip将跳过,不再注册到iprms_segment与ip_detail中 if i == subnet_id_end and j >= host_id_end: pass else: net_seg = '%s.%s.%s/%s' % (net_addr, i, j, mask) gateway_ip = '%s.%s.%s' % (net_addr, i, j + 1) min_value = '%s.%s.%s' % (net_addr, i, j) max_value = '%s.%s.%s' % (net_addr, i+subnet_num-1, j+seg_length-1) dev_id1 = register_queue[tuple_index][0] dev_id2 = register_queue[tuple_index][1] '''iprms_segment表注册''' iprms_segment = IPrms_Segment() iprms_segment.segmentname = net_seg iprms_segment.segmenttype = type iprms_segment.gateway = gateway_ip iprms_segment.subnetmask = sub_mask iprms_segment.netdevicename = '%s%s/%s%s' % (dev_name, dev_id1, dev_name, dev_id2) #iprms_segment.netdeviceid = dev_id iprms_segment.vlan = vlan_name iprms_segment.ipattribute = ip_attr iprms_segment.min = min_value iprms_segment.max = max_value iprms_segment_list.append(iprms_segment) '''ip_detail表注册''' for dev_id in register_queue[tuple_index]: ip_detail = IP_Detail() ip_detail.address = gateway_ip ip_detail.gateway = gateway_ip ip_detail.subnetmask = sub_mask ip_detail.netdevicename = '%s%s' % (dev_name, dev_id) #ip_detail.netdeviceid = dev_id ip_detail.port = 'VLAN%s' % vlan_name ip_detail.type = type ip_detail_list.append(ip_detail) seg_id = seg_id + 1 tuple_index = tuple_index + 1 '''iprms_ip表注册''' ip = '%s.%s.%s' % (net_addr, i, j) state = 'Available' segmentid = seg_id #判断是否为网关ip if j % seg_length == 1: state = 'Specific' if i == subnet_id_end and j > host_id_end: #对于一个子网域剩下未分配的ip,系统将把其全部浪费掉,状态由Off转为Unavaliable state = 'Unavailable' segmentid = 0 iprms_ip_list.append([ip, state, segmentid]) print "完成服务器网络资源自动分配"
def regist_device(): #选择最后一个被注册的ip,获取其信息推导出本次注册开始信息 tmp = IPrms_IP.where().select_tail() if tmp: net_addr = self.get_net_addr(tmp.ip) subnet_id_start = self.get_subnet_id(tmp.ip) + 1 else: #服务网段尚未有信息被注册 se = IPrms_Segment.where(segmenttype='SE1').select_head() tmp = IPrms_Segment.where(segmenttype='CONN').select_tail() if tmp: tmp_seg = tmp.segmentname net_addr = self.get_net_addr(tmp_seg) subnet_id_start = self.get_subnet_id(tmp_seg) + 1 else: se_min_ip = se.min subnet_start = self.get_subnet_id(se_min_ip) seg_length = 2 ** (32-mask) if mask > 16 and mask < 24: seg_length = 256 subnet_id_end = subnet_id_start + (2**(24-mask)) * dev_num - 1 host_id_end = 255 #subnet_num指一个设备所占的最多子网数量 subnet_num = 2 ** (24-mask) else: subnet_num = 1 seg_length = 2 ** (32-mask) x = 256 / seg_length if dev_num % x == 0: subnet_id_end = subnet_id_start + dev_num / x - 1 host_id_end = 255 else: subnet_id_end = subnet_id_start + dev_num / x host_id_end = (dev_num%x) * seg_length sub_mask = self.cal_sub_mask(mask) vlan = vlan_name seg_id = IPrms_Segment.where().count() dev_id = dev_start_id for i in range(subnet_id_start, subnet_id_end+1, 1): for j in range(0, 256*subnet_num, 1): #满足三个条件即注册信息到表:1.主机号为下一个网段号的主机号 2.够分配下一个网段 3.一个设备的分配跨过了正确数目子网号 if j % seg_length == 0 and j + seg_length <= 256 and (i-subnet_id_start) % subnet_num == 0: #当到达最后一个子网,所有设备都注册后,这时检索到的ip将跳过,不再注册到iprms_segment与ip_detail中 if i == subnet_id_end and j >= host_id_end: pass else: net_seg = '%s.%s.%s/%s' % (net_addr, i, j, mask) gateway_ip = '%s.%s.%s' % (net_addr, i, j + 1) min_value = '%s.%s.%s' % (net_addr, i, j) max_value = '%s.%s.%s' % (net_addr, i+subnet_num-1, j+seg_length-1) '''iprms_segment表注册''' iprms_segment = IPrms_Segment() iprms_segment.segmentname = net_seg iprms_segment.segmenttype = type iprms_segment.gateway = gateway_ip iprms_segment.subnetmask = sub_mask iprms_segment.netdevicename = '%s%s' % (dev_name, dev_id) iprms_segment.vlan = vlan_name iprms_segment.ipattribute = ip_attr iprms_segment.min = min_value iprms_segment.max = max_value iprms_segment_list.append(iprms_segment) '''ip_detail表注册''' ip_detail = IP_Detail() ip_detail.address = gateway_ip ip_detail.gateway = gateway_ip ip_detail.subnetmask = sub_mask ip_detail.netdevicename = '%s%s' % (dev_name, dev_id) ip_detail.port = 'VLAN%s' % vlan_name ip_detail.type = type ip_detail_list.append(ip_detail) seg_id = seg_id + 1 dev_id = dev_id + 1 '''iprms_ip表注册''' if j == 0: pass else: iprms_ip = IPrms_IP() ip = '%s.%s.%s' % (net_addr, i, j) iprms_ip.ip = ip iprms_ip.state = 'Available' iprms_ip.segmentid = seg_id #判断是否为网关ip if j % seg_length == 1: iprms_ip.state = 'Specific' if i == subnet_id_end and j > host_id_end: #对于一个子网域剩下未分配的ip,系统将把其全部浪费掉,状态转为Unavaliable iprms_ip.state = 'Unavailable' iprms_ip.segmentid = 0 iprms_ip_list.append(iprms_ip) print "完成服务器网络资源自动分配"
def regist_device(): #选择最后一个被注册的ip,获取其信息推导出本次注册开始信息 tmp = IPrms_IP.where().select_tail() if tmp: net_addr = self.get_net_addr(tmp.ip) subnet_id_start = self.get_subnet_id(tmp.ip) + 1 else: #服务网段尚未有信息被注册 #tmp = IPrms_Segment.where(segmenttype='SE1').select_head() #tmp_seg = tmp.segmentname #net_addr = self.get_net_addr(tmp_seg) #subnet_id_start = self.get_subnet_id(tmp_seg) + 8 net_addr = '172.16' subnet_id_start = 8 seg_length = 2 ** (32-mask) if mask > 16 and mask < 24: #seg_length = 256 #subnet_id_end = subnet_id_start + (2**(24-mask)) * dev_num - 1 host_id_end = 255 #subnet_num指一个设备所占的最多子网数量 subnet_num = 2 ** (24-mask) subnet_id_end = subnet_id_start + subnet_num ##ip数量 ip_num = subnet_num * 255 else: subnet_num = 1 #seg_length = 2 ** (32-mask) #x = 256 / seg_length subnet_id_end = subnet_id_start + 1 host_id_end = dev_num # if dev_num % x == 0: # subnet_id_end = subnet_id_start + dev_num / x - 1 # host_id_end = 255 # else: # subnet_id_end = subnet_id_start + dev_num / x # host_id_end = (dev_num%x) * seg_length ##ip数量 ip_num = host_id_end + 1 if ip_num < dev_num: print "Error:网段ip资源不够分配服务器设备!" return sub_mask = self.cal_sub_mask(mask) vlan = vlan_name '''iprms_segment表注册''' min_value = '%s.%s.%s' % (net_addr, subnet_id_start, 0) max_value = '%s.%s.%s' % (net_addr, subnet_id_end - 1, host_id_end) net_seg = '%s.%s.%s/%s' % (net_addr, subnet_id_start, 0, mask) iprms_segment = IPrms_Segment() iprms_segment.segmentname = net_seg iprms_segment.segmenttype = type iprms_segment.gateway = '%s.%s.%s' % (net_addr, subnet_id_start, 1) iprms_segment.subnetmask = sub_mask iprms_segment.netdevicename = '%s%s' % (dev_name, dev_start_id) iprms_segment.vlan = vlan_name iprms_segment.ipattribute = ip_attr iprms_segment.min = min_value iprms_segment.max = max_value iprms_segment_list.append(iprms_segment) seg_id = IPrms_Segment.where().count() + 1 dev_id = dev_start_id for i in range(subnet_id_start, subnet_id_end, 1): for j in range(0, 256*subnet_num, 1): #满足三个条件即注册信息到表:1.主机号为下一个网段号的主机号 2.够分配下一个网段 3.一个设备的分配跨过了正确数目子网号 #if j % seg_length == 0 and j + seg_length <= 256 and (i-subnet_id_start) % subnet_num == 0: #当到达最后一个子网,所有设备都注册后,这时检索到的ip将跳过,不再注册到iprms_segment与ip_detail中 if i == subnet_id_end - 1 and j >= host_id_end: pass elif dev_id - dev_start_id < dev_num: net_seg = '%s.%s.%s/%s' % (net_addr, i, j, mask) gateway_ip = '%s.%s.%s' % (net_addr, i, j + 1) # min_value = '%s.%s.%s' % (net_addr, i, j) # max_value = '%s.%s.%s' % (net_addr, i+subnet_num-1, j+seg_length-1) '''iprms_segment表注册''' # iprms_segment = IPrms_Segment() # iprms_segment.segmentname = net_seg # iprms_segment.segmenttype = type # iprms_segment.gateway = gateway_ip # iprms_segment.subnetmask = sub_mask # iprms_segment.netdevicename = '%s%s' % (dev_name, dev_id) # #iprms_segment.netdeviceid = dev_id # iprms_segment.vlan = vlan_name # iprms_segment.ipattribute = ip_attr # iprms_segment.min = min_value # iprms_segment.max = max_value # iprms_segment_list.append(iprms_segment) '''ip_detail表注册''' ip_detail = IP_Detail() ip_detail.address = gateway_ip ip_detail.gateway = gateway_ip ip_detail.subnetmask = sub_mask ip_detail.netdevicename = '%s%s' % (dev_name, dev_id) ip_detail.port = 'VLAN%s' % vlan_name ip_detail.type = type ip_detail_list.append(ip_detail) #seg_id = seg_id + 1 dev_id = dev_id + 1 '''iprms_ip表注册''' if j == 0: pass else: iprms_ip = IPrms_IP() ip = '%s.%s.%s' % (net_addr, i, j) iprms_ip.ip = ip iprms_ip.state = 'Available' if i == subnet_id_end - 1 and j >= host_id_end: iprms_ip.segmentid = 0 else: iprms_ip.segmentid = seg_id #判断是否为网关ip if j % seg_length == 1: iprms_ip.state = 'Specific' if i == subnet_id_end - 1 and j > host_id_end: #对于一个子网域剩下未分配的ip,系统将把其全部浪费掉,状态转为Unavaliable iprms_ip.state = 'Unavailable' iprms_ip.segmentid = 0 iprms_ip_list.append(iprms_ip) print "完成服务器网络资源自动分配"