def add_hostname(self): ''' 添加hostname 清理原有hostname ''' remote_user = self.client_info['client_user'] remote_passwd = self.client_info['client_password'] remote_port = self.client_info['client_port'] inner_domain = "sogou-in.domain" look_hosts_cmd = "cat /etc/hosts" look_network_cmd = "cat /etc/sysconfig/network" short_host_name = "" hostname_list = [] for host_ip in self.host_ip_list: if common_lib.get_idc_name(host_ip.strip()): idcname,host_busi_ip,host_data_ip = common_lib.get_idc_name(host_ip) else: logger.write_log("host: %s,get_idc_name failed." % host_ip) return False #hostname信息 result = deal_ssh.remote_ssh_password_simple_online(host_ip,remote_user,remote_passwd,look_network_cmd) if isinstance(result,bool): logger.write_log("host: %s,get host info failed." % host_ip) return False buf = StringIO.StringIO(result) for line in buf.readlines(): line = line.strip() if line.startswith("HOSTNAME"): short_host_name = line.split("=")[1] hostname = short_host_name + "." + inner_domain p1 = re.compile("(.*)%s()" % short_host_name) #hostname信息 result = deal_ssh.remote_ssh_password_simple_online(host_ip,remote_user,remote_passwd,look_hosts_cmd) if isinstance(result,bool): logger.write_log("host: %s,get host info failed." % host_ip) return False buf = StringIO.StringIO(result) for line in buf.readlines(): line = line.strip() if p1.match(line): delete_old_cmd = "sed -i /%s/d /etc/hosts" % short_host_name deal_ssh.remote_ssh_password_simple_online(host_ip,remote_user,remote_passwd,delete_old_cmd) #添加hostname add_hostname = "%s %s %s" % (host_data_ip,hostname,short_host_name) add_hostname_cmd = "echo -e \"%s\" >> /etc/hosts" % add_hostname deal_ssh.remote_ssh_password_simple_online(host_ip,remote_user,remote_passwd,add_hostname_cmd) hostname_list.append(hostname) return hostname_list
def add_hostname(self,host_ip): ''' 添加hostname 清理原有hostname ''' inner_domain = "sogou-in.domain" look_hosts_cmd = "cat /etc/hosts" look_network_cmd = "cat /etc/sysconfig/network" short_host_name = "" hostname_list = [] if common_lib.get_idc_name(host_ip.strip()): idcname,host_busi_ip,host_data_ip = common_lib.get_idc_name(host_ip) else: logger.write_log("host: %s,get_idc_name failed." % host_ip) return False #hostname信息 result,error = self.ssh_con.do_remote_by_passwd_exec(look_network_cmd) if result == "wrong": logger.write_log("host: %s,get host info failed.Detail: %s" % host_ip,error) return False buf = StringIO.StringIO(result) for line in buf.readlines(): line = line.strip() if line.startswith("HOSTNAME"): short_host_name = line.split("=")[1] hostname = short_host_name + "." + inner_domain p1 = re.compile("(.*)%s()" % short_host_name) #hostname信息 result,error = self.ssh_con.do_remote_by_passwd_exec(look_hosts_cmd) if result == "wrong": logger.write_log("host: %s,get host info failed.Detail: %s" % host_ip,error) return False buf = StringIO.StringIO(result) for line in buf.readlines(): line = line.strip() if p1.match(line): delete_old_cmd = "sed -i /%s/d /etc/hosts" % short_host_name self.ssh_con.do_remote_by_passwd_exec(delete_old_cmd) #添加hostname add_hostname = "%s %s %s" % (host_data_ip,hostname,short_host_name) add_hostname_cmd = "echo -e \"%s\" >> /etc/hosts" % add_hostname self.ssh_con.do_remote_by_passwd_exec(add_hostname_cmd) return hostname
def set_server_status(self): ''' 设置服务器状态信息 ''' relase,hostname,idcname,host_busi_ip,host_data_ip = "","","","","" eth0_mac,eth1_mac,eth2_mac,eth3_mac = "","","","" server_manufacturer,server_sn,product_name = "","","" temp_dic = {} temp_list = [] password_cls = PassWorder() for server_info in self.init_server_info: temp_dic = server_info['client_server'].copy() relase = common_lib.get_system_release(server_info) hostname = common_lib.get_system_hostname(server_info) if common_lib.get_idc_name(temp_dic['client_ip']): idcname,host_busi_ip,host_data_ip = common_lib.get_idc_name(temp_dic['client_ip']) else: logger.write_log("get_idc_name failed.") if common_lib.get_adapter_mac(server_info): eth0_mac,eth1_mac,eth2_mac,eth3_mac = common_lib.get_adapter_mac(server_info) else: logger.write_log("get_adapter_mac failed.") if common_lib.get_system_product(server_info): server_manufacturer,server_sn,product_name = common_lib.get_system_product(server_info) else: logger.write_log("get_system_product failed.") temp_dic['client_hostname'] = hostname temp_dic['client_relase'] = relase temp_dic['idc_name'] = idcname temp_dic['host_busi_ip'] = host_busi_ip temp_dic['host_data_ip'] = host_data_ip temp_dic['eth0_mac'] = eth0_mac temp_dic['eth1_mac'] = eth1_mac temp_dic['eth2_mac'] = eth2_mac temp_dic['eth3_mac'] = eth3_mac temp_dic['server_manufacturer'] = server_manufacturer temp_dic['server_sn'] = server_sn temp_dic['product_name'] = product_name temp_dic['cloud_type'] = self.judge_cloud_type(temp_dic['server_package']) self.save_server_info(temp_dic) temp_dic = {}
def get_server_info(self,host_ip,server_package,client_info,machine_type): ''' 保存机器信息 ''' relase,hostname,idcname,host_busi_ip,host_data_ip = "","","","","" eth0_mac,eth1_mac,eth2_mac,eth3_mac = "","","","" server_manufacturer,server_sn,product_name = "","","" relase = self.get_system_release() hostname = self.get_system_hostname(host_ip) if common_lib.get_idc_name(host_ip): idcname,host_busi_ip,host_data_ip = common_lib.get_idc_name(host_ip) else: logger.write_log("get_idc_name failed.") if self.get_adapter_mac(): eth0_mac,eth1_mac,eth2_mac,eth3_mac = self.get_adapter_mac() else: logger.write_log("get_adapter_mac failed.") if self.get_system_product(): server_manufacturer,server_sn,product_name = self.get_system_product() else: logger.write_log("get_system_product failed.") temp_dic = {} temp_dic['client_hostname'] = hostname.strip("\n") temp_dic['client_relase'] = relase temp_dic['idc_name'] = idcname temp_dic['host_busi_ip'] = host_busi_ip temp_dic['host_data_ip'] = host_data_ip temp_dic['eth0_mac'] = eth0_mac temp_dic['eth1_mac'] = eth1_mac temp_dic['eth2_mac'] = eth2_mac temp_dic['eth3_mac'] = eth3_mac temp_dic['server_manufacturer'] = server_manufacturer temp_dic['server_sn'] = server_sn temp_dic['product_name'] = product_name temp_dic['cloud_type'] = self.judge_cloud_type(server_package) temp_dic['machine_type'] = machine_type temp_dic['client_user'] = client_info['client_user'] self.save_server_info(temp_dic)
def save_partition_info(self,server_partition_info): ''' 保存磁盘分区信息 ''' insert_id = 0 idcname,host_busi_ip,host_data_ip = "","","" for host_ip,info in server_partition_info.items(): if common_lib.get_idc_name(host_ip.strip()): idcname,host_busi_ip,host_data_ip = common_lib.get_idc_name(host_ip) else: print "get_idc_name failed." sql = "select `id` from server_info where host_data_ip = '%s' or host_busi_ip = '%s'" result = super(Parted_Disk_init, self).select_advanced(sql,host_data_ip,host_busi_ip) if len(result) > 0: server_id = result[0] else: server_id = -1 #先清理表 sql = "delete from partition_info where server_busi_ip = '%s'" super(Parted_Disk_init, self).delete(sql,host_busi_ip) for partitions in info: #更新表 sql = "insert into partition_info (server_id,server_busi_ip,partition_mount_point,block_num,partition_size,partition_name,partition_alias_name,partition_type) values (%s,'%s','%s','%s','%s','%s','%s','%s');" if int(partitions['block_size'])/1024/1024 > 0: size = str(int(partitions['block_size'])/1024/1024) + "G" else: size = str(int(partitions['block_size'])/1024) + "M" insert_id = super(Parted_Disk_init, self).insert_advanced(sql,server_id,host_busi_ip,partitions['partitions_point'],partitions['block_num'],size,partitions['partition_name'],partitions['partition_alias_name'],partitions['partition_type']) if insert_id > 0: pass
def get_init_partition_info(self): ''' 查看block信息,返回数据盘信息,以及挂载信息 类似 df命令 ''' lvm_info,pv_info = {},{} lvm_info,pv_info = self.read_block_info() idcname,host_busi_ip,host_data_ip = "","","" block_info = {} #存放分区信息 result_info = {} #存放完整挂载信息 server_partition_info = {} #存放完整服务器磁盘挂载信息 block_info_command = "cat /proc/partitions" mounts_info_cmd = "cat /proc/mounts" num = re.compile("^[0-9](.*)") block_device_num = re.compile("[a-z]d[a-z][0-9]*") if common_lib.get_idc_name(self.host_ip.strip()): idcname,host_busi_ip,host_data_ip = common_lib.get_idc_name(self.host_ip) else: logger.write_log("host:%s get_idc_name failed." % self.host_ip) #分区信息 result,error = self.ssh_con.do_remote_by_passwd_exec(block_info_command) if result == "wrong": logger.write_log("host: %s . read lvm info failed." % self.host_ip) return False templist = [] buf = StringIO.StringIO(result) for line in buf.readlines(): line = line.strip() for key,value in lvm_info.items(): if key.strip() == self.host_ip: if num.match(line): partition_mount_point = "/dev/%s" % line.split()[-1] for alias_name,value_point in value.items(): if alias_name == line.split()[-1]: partition_mount_point = value_point block_info[partition_mount_point] = {'partition_alias_name':line.split()[-1],'block_size':line.split()[-2],'block_num':line.split()[0]} if len(block_info) > 0: templist.append(block_info) block_info = {} #挂载信息 result100,error = self.ssh_con.do_remote_by_passwd_exec(mounts_info_cmd) if result100 == "wrong": logger.write_log("host: %s . read lvm info failed." % self.host_ip) return False templist2,templist3,templist4 = [],[],[] partition_mount_point_list = [] buf = StringIO.StringIO(result100) vgroot_rule = re.compile("(.*)vgroot-(.*)") lvm_rule = re.compile("(.*)lv(.*)") root_rule = re.compile("^\/dev\/root$") boot_rule = re.compile("(.*)(sda[0-9]|vda[0-9])$") for line in buf.readlines(): line = line.strip() for blocks_info in templist: for key,value in blocks_info.items(): #keys = key.replace("/","\/") keys = key.split("/")[-1].split("-") if len(keys) > 1 : use_key = keys[1] if use_key == "lvroot": use_key = "root" else: use_key = keys[0] p1 = re.compile("(.*)%s" % use_key) if p1.match(line): if line not in templist2: if line.split()[2] != "rootfs": partition_mount_point = line.split()[0] templist2.append(line) #整理/proc/mounts下的挂载点 if not vgroot_rule.match(line.split()[0]): if lvm_rule.match(line.split()[0]): partition_mount_point = "/dev/mapper/vgroot-%s" % line.split()[0].split("/")[-1] if root_rule.match(line.split()[0]): partition_mount_point = "/dev/mapper/vgroot-lvroot" result_info[partition_mount_point] = {'partition_name':line.split()[1],'partition_type':line.split()[2]} templist3.append(result_info) result_info = {} #区分挂载分区 has_mount_partition = [] #所有分区列表 all_partition_list = [] #存放挂载分区信息 dictMerged1 = {} #存放非挂载分区信息 dictMerged2 = {} swap = re.compile("(.*)swap(.*)") for blocks_info in templist: for key,value in blocks_info.items(): for partion_info in templist3: if key in partion_info.keys(): has_mount_partition.append(key) dictMerged1 = dict(partion_info[key].items() + blocks_info[key].items()) dictMerged1['partitions_point'] = key if key not in has_mount_partition: dictMerged2 = blocks_info[key] dictMerged2.setdefault('partition_name',"Null") dictMerged2.setdefault('partition_type',"Unknow") dictMerged2.setdefault('partitions_point',"Unknow") dictMerged2['partitions_point'] = key if pv_info[self.host_ip] == key: dictMerged2['partition_type'] = 'LVM' if swap.match(key): dictMerged2['partition_type'] = 'swap' dictMerged2['partition_name'] = 'swap' if len(dictMerged1) > 0: all_partition_list.append(dictMerged1) if len(dictMerged2) > 0: all_partition_list.append(dictMerged2) #print dictMerged1 dictMerged1 = {} dictMerged2 = {} server_partition_info[self.host_ip] = all_partition_list return server_partition_info
def deal_disk_info(self,ld_blocks_list,pd_blocks_list): ''' 保存raid和磁盘信息,不做重新raid等处理 ''' if len(ld_blocks_list) == 0: logger.write_log("host: %s,ld_blocks_list no data" % self.host_ip) return False,False if len(pd_blocks_list) == 0: logger.write_log("host: %s,pd_blocks_list no data" % self.host_ip) return False,False p1 = re.compile("(.*)Primary-([0-9])(.*)") ld_begin = re.compile("^Virtual Drive") disk_list = [] raid_info,disk_info = {},{} if common_lib.get_idc_name(self.host_ip.strip()): idcname,host_busi_ip,host_data_ip = common_lib.get_idc_name(self.host_ip) else: logger.write_log("host: %s,get_idc_name failed." % self.host_ip) client_ip = host_busi_ip for ld_info in ld_blocks_list: for line in ld_info.split("\n"): if line.startswith("Virtual Drive:"): key = line.split()[2] if p1.match(line): raid_name = 'raid-' + p1.match(line).group(2) elif line.startswith("Size"): raid_size = line.split(":")[1] elif line.startswith("Number Of Drives"): drives = line.split(":")[1] elif line.startswith("Span Depth"): if len(drives) > 0: disk_num = int(drives) * int(line.split(":")[1]) if int(line.split(":")[1]) > 1: raid_name = 'raid-10' info_key = "%s_vd_%s" % (client_ip,key) raid_info[info_key] = {'raid_name':raid_name,'disk_num':disk_num,'raid_size':raid_size} #盘组列表 for pd_info in pd_blocks_list: #一块磁盘字典(key为raid相关) for key,value in pd_info.items(): #一个磁盘的详细信息 for line in value.split("\n"): #raid组信息 for raid_key,raid_value in raid_info.items(): if int(raid_key.split("_")[2]) == int(key): if line.startswith("WWN"): disk_sn = line.split(":")[1] elif line.startswith("PD Type:"): disk_type = line.split(":")[1] elif line.startswith("Slot Number:"): slot_number = line.split(":")[1] elif line.startswith("Media Error Count:"): media_error = line.split(":")[1] elif line.startswith("Other Error Count:"): other_error = line.split(":")[1] elif line.startswith("Raw Size:"): disk_size = line.split(":")[1].split()[0]+line.split(":")[1].split()[1] disk_raw_size = int(float(line.split(":")[1].split()[0])) elif line.startswith("Firmware state:"): disk_status = line.split(":")[1].split(",")[0] raid_name = raid_value['raid_name'] disk_list.append({'client_ip':client_ip,'disk_sn':disk_sn.strip(),'virtual_group':key,'disk_type':disk_type.strip(),'disk_size':disk_size.strip(),'disk_raw_size':disk_raw_size,'disk_status':disk_status.strip(),'slot_number':slot_number.strip(),'media_error':media_error,'other_error':other_error,'raid_info':raid_name.strip()}) return disk_list,raid_info