def get_system_hostname(host_info): ''' 获取服务器主机hostname ''' string = "" _command = 'hostname -f' result,error = deal_ssh.remote_ssh_key_exec(host_info,_command) if result == "wrong": logger.write_log("host: %s 获取hostname 失败." % host_info['client_server']['client_ip']) else: logger.write_log("host: %s 获取hostname 成功." % host_info['client_server']['client_ip']) return result
def get_adapter_mac(host_info): ''' 获取服务器物理网卡地址 ''' eth0_mac,eth1_mac,eth2_mac,eth3_mac = "","","","" temp_list = {} begin = re.compile("(.*)(eth[0-9])(.*)") end = re.compile("(.*)link/ether\s(\S*)") string = "" _command = 'ip link' result,error = deal_ssh.remote_ssh_key_exec(host_info,_command) if result == "wrong": return False else: new_block = False for line in result.split("\n"): if new_block: if end.match(line): adapter_mac = end.match(line).group(2) temp_list[adapter_name] = adapter_mac new_block = False else: if begin.match(line): adapter_name = begin.match(line).group(2) new_block = True for key,value in temp_list.items(): if key == "eth0": eth0_mac = value elif key == "eth1": eth1_mac = value elif key == "eth2": eth2_mac = value elif key == "eth3": eth3_mac = value return eth0_mac,eth1_mac,eth2_mac,eth3_mac
def change_server_password(self,new_password,server_info): ''' 改变服务器密码 ''' user = server_info['client_server']['client_user'] _command = 'echo \"%s\" | passwd %s --stdin' % (new_password,user) result,error = deal_ssh.remote_ssh_key_exec(server_info,_command) if result == "wrong": logger.write_log("%s change password failed." % server_info['client_server']['client_ip']) return False else: # f = open('/tmp/machine.list.pass','a+') # f.write(server_info['client_server']['client_ip']) # f.write(",") # f.write(new_password) # f.write("\n") # f.close() # print "%s change password sucess." % server_info['client_server']['client_ip'] logger.write_log("%s change password sucess." % server_info['client_server']['client_ip']) return True
def get_system_release(host_info): ''' 获取操作系统版本 ''' string = "" _command = 'cat /etc/issue' result,error = deal_ssh.remote_ssh_key_exec(host_info,_command) for i in result.split(" "): if i == "(Santiago)\nKernel": continue elif i == "on": continue elif i == "an": continue else: string = string + " " + i.strip("\n").strip("\r").strip("\m") return string
def get_system_product(host_info): ''' 获取服务器主机序列号和生产商 ''' _command = 'dmidecode -t system' result,error = deal_ssh.remote_ssh_key_exec(host_info,_command) if result == "wrong": return False else: for line in result.split("\n"): line = line.strip() if line.startswith("Manufacturer:"): server_manufacturer = line.split(":")[1] elif line.startswith("Serial Number:"): server_sn = line.split(":")[1] elif line.startswith("Product Name:"): product_name = line.split(":")[1] return server_manufacturer,server_sn,product_name
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]*") for server_info in self.init_server_info: host_ip = server_info['client_server']['client_ip'] 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." #分区信息 result,error = deal_ssh.remote_ssh_key_exec(server_info,block_info_command) if result == "wrong": print "host: %s . read lvm info failed." % server_info['client_server']['client_ip'] continue templist = [] buf = StringIO.StringIO(result) for line in buf.readlines(): line = line.strip() for key,value in lvm_info.items(): if key.strip() == 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 = deal_ssh.remote_ssh_key_exec(server_info,mounts_info_cmd) if result100 == "wrong": print "host: %s . read lvm info failed." % server_info['client_server']['client_ip'] continue 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] # else: # partition_mount_point = line.split()[0] # else: # partition_mount_point = line.split()[0] if root_rule.match(line.split()[0]): partition_mount_point = "/dev/mapper/vgroot-lvroot" # else: # partition_mount_point = line.split()[0] #partition_mount_point_list.append(line.split()[0]) 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[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[host_ip] = all_partition_list return server_partition_info
def get_search_ted_info(self): ''' 处理search ted相关 ''' partition_dic = {} partition_dic['opt'] = {} partition_dic['ted'] = {} result,error = 0,0 opt_mount = re.compile("(.*)opt") ted_mount = re.compile("(.*)ted") ext_rule = re.compile("ext[0-4]") xfs_rule = re.compile("xfs") for server_info in self.init_server_info: host_ip = server_info['client_server']['client_ip'] sql = "select partition_mount_point,partition_size,partition_type from partition_info where server_busi_ip = ( select host_busi_ip from server_info WHERE ( host_busi_ip = '%s' or host_data_ip = '%s')) and ( partition_mount_point like '%%lvted%%' or partition_mount_point like '%%lvopt%%')" for key in ['size','partition_type','partition_mount_point']: partition_dic['opt'].setdefault(key,'') partition_dic['ted'].setdefault(key,'') result = super(Parted_Disk_init, self).select_with_desc(sql,host_ip,host_ip) if len(result) > 0: for data in result: if opt_mount.match(data['partition_mount_point']): partition_dic['opt'] = { 'size':data['partition_size'], 'partition_type':data['partition_type'], 'partition_mount_point':data['partition_mount_point'], } elif ted_mount.match(data['partition_mount_point']): partition_dic['ted'] = { 'size':data['partition_size'], 'partition_type':data['partition_type'], 'partition_mount_point':data['partition_mount_point'], } else: continue if len(partition_dic['opt']['partition_mount_point']) > 0 and len(partition_dic['ted']['partition_mount_point']) > 0: #保证ted分区没有任何内容才能重新进行分区 ted_subdir_exist_cmd = "test -e /search/ted&&ls /search/ted/ " result,error = deal_ssh.remote_ssh_key_exec(server_info,ted_subdir_exist_cmd) if len(result) > 0: print "host: %s /search/ted is not NULL" % host_ip return resize = "resize2fs" if len(partition_dic['opt']['partition_type']) > 0: if ext_rule.match(partition_dic['opt']['partition_type']): resize = "resize2fs" elif xfs_rule.match(partition_dic['opt']['partition_type']): resize = "xfs_growfs" else: print "host: %s. FS is not correct!" return #size = int(partition_dic['ted']['partition_mount_point'][:-1]) - 10 opt_size = '140G' remove_ted_cmd = "umount %(ted_mount)s;lvremove -f %(ted)s;lvextend -L %(size)s %(opt)s;sed -i '/lvted/d' /etc/fstab;%(resize)s %(opt)s" % {'ted_mount':partition_dic['ted']['partition_mount_point'],'ted':partition_dic['ted']['partition_mount_point'],'size':opt_size,'resize':resize,'opt':partition_dic['opt']['partition_mount_point']} deal_ssh.remote_ssh_key_exec(server_info,remove_ted_cmd)
def read_block_info(self): ''' 读取lvm 配置信息 ''' p1 = re.compile("^LV Name") p2 = re.compile("^VG Name") lvname,vgname = "","" swap = False lvm_info_command = "lvdisplay" pv_info_command = "pvdisplay" lvm_info,blocks,partition_info,pv_info = {},{},[],{} begin = re.compile("(.*)Logical volume(.*)") end = re.compile("Block device") new_block = False for server_info in self.init_server_info: result,error = deal_ssh.remote_ssh_key_exec(server_info,lvm_info_command) if result == "wrong": print "host: %s . read lvm info failed." % server_info['client_server']['client_ip'] continue buf = StringIO.StringIO(result) for line in buf.readlines(): line = line.strip() if new_block: if end.match(line): name = "%(vgname)s-%(lvname)s" % {'vgname':vgname,'lvname':lvname} key = "dm-%s" % line.split()[-1].split(":")[-1] blocks[key] = '/%s/%s/%s' % ('dev','mapper',name) new_block = False else: if p1.match(line): if len(line.split("/")) > 2: lvname = line.split("/")[-1] else: lvname = line.split()[-1] elif p2.match(line): vgname = line.split()[-1] else: if begin.match(line): new_block = True host_ip = server_info['client_server']['client_ip'] lvm_info[host_ip] = blocks partition_info = [] blocks = {} result,error = deal_ssh.remote_ssh_key_exec(server_info,pv_info_command) if result == "wrong": print "host: %s . read pv info failed." % server_info['client_server']['client_ip'] continue buf = StringIO.StringIO(result) for line in buf.readlines(): line = line.strip() if line.startswith("PV Name"): pv_name = line.split()[2] pv_info[host_ip] = pv_name return lvm_info,pv_info