def dump_server_data(self, dicts):
		print "正在导入到数据库表"
		for l in dicts["iprms_segment"]:
			l.save()
		for l in dicts["iprms_ip"]:
			#对于iprms_ip的注册,这里dump时对其进行update操作而不是save,因为ip在网段分配时已经生成,但是缺乏网段号信息
			IPrms_IP.where(state='Off', segmentid=0, ip=l[0]).update(state=l[1], segmentid=l[2])
		for l in dicts["ip_detail"]:
			l.save()
		print "导入完成"
	def server_seg_alloc(self, net_addr, subnet_id, host_id, mask):
		print "正在计算并分配服务器大网段空间......"
		subnet_start = subnet_id
		host_end = 256
		if mask > 32 or mask < 16:
			print "错误:输入大网段子网掩码数值超过合法范围(16-32)"
		if mask >= 16 and mask < 24:
			subnet_num = 2 ** (24-mask)
		else:
			subnet_num = 1
			host_end = 2 ** (32-mask)
		subnet_end = subnet_start + subnet_num
		#让分配从子网号为2开始,0和1留给互联设备
		for i in range(subnet_start + 8, subnet_end, 1):
			host_start = 1
			for j in range(host_start, host_end, 1):
				#分配服务网段时,因为没有掩码数据,只在iprms_ip中划分好所有ip的信息,注册时再向iprms_segment中添加具体网段
				ip = '%s.%s.%s' % (net_addr, i, j)
				iprms_ip = IPrms_IP()
				iprms_ip.segmentid = 0
				iprms_ip.ip = ip
				#网关IP状态为Specific,ip未分配时先全部设置状态为Off
				iprms_ip.state = 'Off'
				iprms_ip.save()
		print "服务大网段%s.%s.%s/%s分配完成" % (net_addr, subnet_id, host_id, mask)
	def query_server_remain(self, mask):
		try:
			remain_ip = IPrms_IP.where(state='Off').count()
			if mask > 32 or mask < 16:
				print "错误:输入大网段子网掩码数值超过合法范围(16-32)"
				return
			elif mask >= 16 and mask < 24:
				seg_num = 2 ** (24-mask)
				remain_num = (remain_ip/255) / seg_num
			else:
				seg_num = 256 / (2**(32-mask))
				remain_num = (remain_ip/255) * seg_num
			print "服务网段剩余可分配/%s设备数量:%s个" % (mask, remain_num)
			return remain_num
		except:
			print "尚未分配服务网段"
	def conn_seg_alloc(self, net_addr, subnet_id, host_id, l_mask, s_mask):
		print "正在计算并分配互联大网段空间......"
		#检查输入掩码
		if l_mask > s_mask:
			print "错误:输入大网段掩码必须小于小网段掩码"
			return
		if l_mask > 32 or l_mask < 16:
			print "错误:输入大网段子网掩码数值超过合法范围(16-32)"
			return
		elif l_mask >= 16 and l_mask < 24:
			subnet_max = 2 ** (24-l_mask)
			host_max = 255
		else:
			subnet_max = 1
			subnet_id_start = subnet_id
			subnet_id_end = subnet_id_start + subnet_max
			host_max = seg_length
		if s_mask >= 16 and s_mask < 24:
			seg_length = 2 ** (24-s_mask)
		else:
			seg_length = 2 ** (32-s_mask)
		#seg_id是ip地址所对应的网段id
		seg_id = IPrms_Segment.where().count()
		sub_mask = self.cal_sub_mask(s_mask)
		mask = s_mask
		host_id_start = host_id
		host_id_end = 256
		subnet_id_start = subnet_id
		subnet_id_end = subnet_id_start + subnet_max
		if subnet_id + subnet_max > 255:
			print "分配互联大网段%s.%s.%s/%s资源不足" % (net_addr, subnet_id, host_id, l_mask)
			return
		for i in range(subnet_id_start, subnet_id_end, 1):
			for j in range(host_id_start, host_id_end, 1):
				if j % seg_length == 0 and j + seg_length <= 256:
					seg_id = seg_id + 1
					iprms_segment = IPrms_Segment()
					iprms_segment.segmentname = '%s.%s.%s/%s' % (net_addr, i, j, mask)
					iprms_segment.segmenttype = 'CONN'
					iprms_segment.subnetmask = sub_mask
					iprms_segment.gateway = 'x.x.x.x'
					iprms_segment.netdevicename = ''
					iprms_segment.ipattribute = ''
					iprms_segment.vlan = ''
					iprms_segment.min = '%s.%s.%s' % (net_addr, i, j)
					iprms_segment.max = '%s.%s.%s' % (net_addr, i, j+seg_length-1)
					iprms_segment.save()
				#分配互联网段时,先在iprms_ip中划分好所有ip的信息
				ip = '%s.%s.%s' % (net_addr, i, j)
				iprms_ip = IPrms_IP()
				iprms_ip.segmentid = seg_id
				iprms_ip.ip = ip
				iprms_ip.state = 'Specific'
				iprms_ip.save()
		print "互联大网段分配完成"
Beispiel #5
0
	def query_server_remain(self, mask):
		try:
			#在数据库iprms_segment中保存服务大网段SE1的信息,查询时获取大网段信息
			se = IPrms_Segment.where(segmenttype='SE1').select_head()
			if se:
				se_seg = se.segmentname
				se_max_ip = se.max
				se_min_ip = se.min
				subnet_end = self.get_subnet_id(se_max_ip)
			else:
				#若未分配服务大网段,则无法注册,查询结果为0
				return 0

			subnet_end = self.get_subnet_id(se_max_ip)
			tail = IPrms_IP.where(state='Available').select_tail()
			if tail:
				tail_ip = tail.ip
				subnet_start = self.get_subnet_id(tail_ip)
			else:
				tmp = IPrms_Segment.where(segmenttype='CONN').select_tail()
				if tmp:
					tmp_seg = tmp.segmentname
					subnet_start = self.get_subnet_id(tmp_seg) + 1
				else:
					subnet_start = self.get_subnet_id(se_min_ip)
			#计算出剩余未分配ip地址的数量
			remain_ip = (subnet_end-subnet_start) * 255
			if mask > 32 or mask < 16:
				print "错误:输入大网段子网掩码数值超过合法范围(16-32)"
				return
			elif mask >= 16 and mask < 24:
				seg_num = 2 ** (24-mask)
				remain_num = (remain_ip/255) / seg_num
			else:
				seg_num = 256 / (2**(32-mask))
				remain_num = (remain_ip/255) * seg_num
			print "服务网段剩余可分配/%s设备数量:%s个" % (mask, remain_num)
			return remain_num
		except:
			print "尚未分配服务网段"
			return 0
	def query_server_remain(self, mask):
		try:
			#之前是在数据库iprms_segment中保存服务大网段SE1的信息,查询时获取大网段信息
			#后来jiwubu提出不用保存大网段信息,所以这里写死参数se_max_ip为172.16.255.255,se_min_ip为172.16.0.0
			se_min_ip = '172.16.0.0'
			se_max_ip = '172.16.255.255'

			# se = IPrms_Segment.where(segmenttype='SE1').select_head()
			# if se:
			# 	se_seg = se.segmentname
			# 	se_max_ip = se.max
			# 	subnet_end = self.get_subnet_id(se_max_ip)
			# else:
			# 	#若未分配服务大网段,则无法注册,查询结果为0
			# 	return 0

			subnet_end = self.get_subnet_id(se_max_ip)
			tail = IPrms_IP.where(state='Available').select_tail()
			if tail:
				tail_ip = tail.ip
				subnet_start = self.get_subnet_id(tail_ip)
			else:
				#预留8个子网域给互联网段
				subnet_start = self.get_subnet_id(se_min_ip) + 8
			#计算出剩余未分配ip地址的数量
			remain_ip = (subnet_end-subnet_start) * 255
			if mask > 32 or mask < 16:
				print "错误:输入大网段子网掩码数值超过合法范围(16-32)"
				return
			elif mask >= 16 and mask < 24:
				seg_num = 2 ** (24-mask)
				remain_num = (remain_ip/255) / seg_num
			else:
				seg_num = 256 / (2**(32-mask))
				remain_num = (remain_ip/255) * seg_num
			print "服务网段剩余可分配/%s设备数量:%s个" % (mask, remain_num)
			return remain_num
		except:
			print "尚未分配服务网段"
			return 0
	def manage_seg_alloc(self, net_addr, subnet_id, host_id, mask, type):
		print "正在计算并分配管理大网段空间......"
		if mask > 32 or mask < 16:
			print "错误:输入网段子网掩码数值超过合法范围(16-32)"
			return
		elif mask >= 16 and mask < 24:
			subnet_num = 2 ** (24-mask)
			seg_length = 2 ** (24-mask)
			host_max = 255
		else:
			subnet_num = 1
			seg_length = 1
			host_max = (2**(32-mask)) - 1
		#根据掩码号计算子网掩码
		sub_mask = self.cal_sub_mask(mask)
		seg_id = IPrms_Segment.where().count()
		subnet_id_start = subnet_id
		subnet_id_end = subnet_id + subnet_num
		host_id_start = 1
		host_id_end = host_id_start + host_max
		for i in range(subnet_id_start, subnet_id_end, 1):
			if (i-subnet_id) % seg_length == 0:
				#分配管理网段时在iprms_ip、iprms_segment中划分好所有信息
				seg_id = seg_id + 1
				iprms_segment = IPrms_Segment()
				iprms_segment.segmentname = '%s.%s.%s/%s' % (net_addr, i, host_id, mask)
				iprms_segment.segmenttype = type
				iprms_segment.subnetmask = sub_mask
				iprms_segment.gateway = 'x.x.x.x'
				iprms_segment.netdevicename = ''
				iprms_segment.vlan = '-'
				iprms_segment.min = '%s.%s.0' % (net_addr, i)
				iprms_segment.max = '%s.%s.%s' % (net_addr, i+seg_length-1, host_max)
				iprms_segment.save()
			for j in range(host_id_start, host_id_end, 1):
				ip = '%s.%s.%s' % (net_addr, i, j)
				dev_max = j * (i+1)
				iprms_ip = IPrms_IP()
				iprms_ip.segmentid = seg_id
				iprms_ip.ip = ip
				iprms_ip.state = 'Specific'
				iprms_ip.save()
		print "管理大网段分配完成,你最多可以为%s个设备分配管理IP" % (dev_max)
		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 "完成服务器网络资源自动分配"
Beispiel #9
0
		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 "完成服务器网络资源自动分配"