def uptime(self): """ Show uptime of the host """ if not util._is_host_up(env.host, int(env.port)): return False print env.host + "|" + util._run('uptime')
def arch(self): """ Show architecture x86_64, i386 ... """ if not util._is_host_up(env.host, int(env.port)): return False print env.host + "|" + util._run("uname --hardware-platform")
def os_type(self,display=True): """ Show OS type ( LFS, RedHat CentOS ... ) [display] True by default """ if util._is_host_up(env.host, int(env.port)) is False: return False OS = util._run("bash -c 'head -n 1 /etc/{{system,redhat,centos}-release,release} 2>/dev/null' | sort -u | grep -v '^==>'") if re.match("LC",OS,flags=re.IGNORECASE): if display: print env.host + "| LFS" return "lfs" if re.match("Red Hat",OS,flags=re.IGNORECASE): if display: print env.host + "| redhat" return "redhat" if re.match("Centos",OS,flags=re.IGNORECASE): if display: print env.host + "| centos" return "centos" if re.match("Solaris",OS,flags=re.IGNORECASE): if display: print env.host + "| solaris" return "solaris" else: if display: print env.host + "| UNKNOWN_OS" return "UNKNOWN_OS"
def mount(self,mount_point,dev,muser=None,mgroup=None,mode=None,fstype=None,mount_opts=None,sudo=False): """ Mount device <dev> into <mount_point> and create it with [muser]/[mgroup] rights """ if util._is_host_up(env.host, int(env.port)) is False: return False self.needSudo = eval(str(sudo).capitalize()) mtab = "/etc/mtab" if not util._contains(mtab,mount_point): if not util._exists(mount_point): ret = self.mkdir(path=mount_point,user=muser,group=mgroup,mode=mode) if not ret: print(red("Error: ")+"Cannot create directory <"+mount_point+">. Abording") return False if util._exists(dev): fstab = "/etc/fstab" if not util._contains(fstab,mount_point): util._append(fstab,dev+"\t"+mount_point+"\t"+fstype+"\t"+mount_opts) ret = self._fabrun("mount "+mount_point) if ret.failed: print(red("Error: ")+"Cannot mount <"+mount_point+"> partition. Abording") return False ret = self.chown(mount_point,muser,mgroup,self.needSudo) if ret: ret = self.chmod(mount_point,mode,self.needSudo) else: print(red("Error: ")+"Device <"+dev+"> cannot exists. Abording") return False return True
def netapp_filer_snapmirror_status(self): """ Show status of the snapmirror process on the filer process """ self._setup() if util._is_host_up(env.host, int(env.port)) is False: return False HOSTNAME = run('hostname') disconnect_all() if not self.first.has_key(util._func_name()): self.first[util._func_name()] = True print "HOSTNAME|Status|Source|Destination|State|Lag|Status" snapmirror = run('snapmirror status') disconnect_all() status = "" lines = [] for l in snapmirror.splitlines(): if re.match("Snapmirror\s+is\s+(.*)$", l): status = re.sub("Snapmirror\s+is\s+(.*)$", "\\1", l) elif not re.match("^Source\s+", l): lines.append(l) status = re.sub("\.", "", status) if re.match("^on$", status): for l in lines: print HOSTNAME + "|" + status + "|" + re.sub("\s+", "|", l)
def netapp_filer_check_zeroing(self): """ check if disks are being zeroed or must be zeroed on the filer """ self._setup() if util._is_host_up(env.host, int(env.port)) is False: return False HOSTNAME = run('hostname') disconnect_all() sysconfig = run('sysconfig -r') disconnect_all() for l in sysconfig.splitlines(): #print l if re.match("^(.*)\s+\(creating\,.*",l): pool_name = re.sub("^(.*)\s+\(creating\,.*","\\1",l) if re.match("^(.*)\s+disks($|\s+\(.*)",l): pool_name = re.sub("^(.*)\s+disks($|\s+\(.*)","\\1",l) if re.match("^\s*RAID\s+Disk\s+Device\s+.*",l): header = l header = re.sub("(\w)\s+([^\s])","\\1|\\2",l) if re.match("^.*\((not\s+zeroed\)|zeroing\,).*",l): l = re.sub("([\w/])\s+(\d)","\\1|\\2",l) if not self.first.has_key(util._func_name()): self.first[util._func_name()] = True print "HOSTNAME|Pool|"+header print HOSTNAME+"|"+pool_name+"|"+l
def netapp_filer_uptime(self): """ Show uptime of the host """ self._setup() if util._is_host_up(env.host, int(env.port)) is False: return False HOSTNAME = run('hostname', False, False) disconnect_all() uptime = run('uptime').split(", ") i = 0 sys.stdout.write(HOSTNAME) for col in uptime: if i != 1: sys.stdout.write("|" + col) else: j = 0 col_s = col.split() for col_ in col_s: if j != 1: sys.stdout.write(" " + col_) else: sys.stdout.write("|" + col_) j += 1 i += 1 sys.stdout.write("\n") disconnect_all()
def yum_install(self,pkg,sudo=False,disablerepo=None,disableplugin=None,enablerepo=None): """ Install package from <pkg> named argument through Yum, in sudo mode is [sudo] is set to True with [disablerepo] as a list of separated by ' ' spaces yum repo to disable with [enablerepo] as a list of separated by ' ' spaces yum repo to enable with [disableplugin] as a list of separated by ' ' spaces yum plugin to disable """ if util._is_host_up(env.host, int(env.port)) is False: return False self.needSudo = eval(str(sudo).capitalize()) disablerepo_list = "" if not disablerepo is None: disablerepo_list = disablerepo.split() disablerepo_list = map(lambda x: ' --disablerepo="'+x+'"', disablerepo_list) disablerepo_list = ''.join(disablerepo_list) enablerepo_list = "" if not enablerepo is None: enablerepo_list = enablerepo.split() enablerepo_list = map(lambda x: ' --enablerepo="'+x+'"', enablerepo_list) enablerepo_list = ''.join(enablerepo_list) disableplugin_list = "" if not disableplugin is None: disableplugin_list = disableplugin.split() disableplugin_list = map(lambda x: ' --disableplugin="'+x+'"', disableplugin_list) disableplugin_list = ''.join(disableplugin_list) result = self._fabrun("yum -y "+disableplugin_list+" "+disablerepo_list+" "+enablerepo_list+" install "+pkg) if not result.failed: print env.host + "|" + green("OK") + "|<"+pkg+"> successfully installed" else: print env.host + "|" + red("FAILED",True) + "|<"+pkg+"> not successfully installed" return result.succeeded
def find(self,directory,type=None,maxdepth=None,name=None,display=True,sudo=False,error=True): """ list all entries into <directory>, or just [type], [name] entries, into [maxdepth] (cf man find for type argument) and [display] default True, If [sudo] is True, will use sudo instead of run """ if util._is_host_up(env.host, int(env.port)) is False: return False self.needSudo = eval(str(sudo).capitalize()) type_opt = "" if not type is None: type_opt = " -type "+type+" " maxdepth_opt = "" if not maxdepth is None: maxdepth_opt = " -maxdepth "+str(maxdepth)+" " name_opt = "" if not name is None: name_opt = " -name \'"+name+"\' " error_opt = "" if not error: error_opt = " 2>/dev/null" else: error_opt = "" result = self._fabrun("find "+directory+maxdepth_opt+name_opt+type_opt+error_opt) if result.failed: if error: print env.host+"|"+red("Failed")+" search into <"+directory+"> " print result return False else: if display: for entry in result.splitlines(): print env.host+"|"+entry return result.splitlines()
def netapp_filer_check_zeroing(self): """ check if disks are being zeroed or must be zeroed on the filer """ self._setup() if util._is_host_up(env.host, int(env.port)) is False: return False HOSTNAME = run('hostname') disconnect_all() sysconfig = run('sysconfig -r') disconnect_all() for l in sysconfig.splitlines(): #print l if re.match("^(.*)\s+\(creating\,.*", l): pool_name = re.sub("^(.*)\s+\(creating\,.*", "\\1", l) if re.match("^(.*)\s+disks($|\s+\(.*)", l): pool_name = re.sub("^(.*)\s+disks($|\s+\(.*)", "\\1", l) if re.match("^\s*RAID\s+Disk\s+Device\s+.*", l): header = l header = re.sub("(\w)\s+([^\s])", "\\1|\\2", l) if re.match("^.*\((not\s+zeroed\)|zeroing\,).*", l): l = re.sub("([\w/])\s+(\d)", "\\1|\\2", l) if not self.first.has_key(util._func_name()): self.first[util._func_name()] = True print "HOSTNAME|Pool|" + header print HOSTNAME + "|" + pool_name + "|" + l
def grep(self,filename,pattern,lines=0,show_lines=False,sudo=False,display=True): """ grep <pattern> into <filename> and return [lines] lines default 0 for all matching lines, and [show_lines] False by default , whith [sudo] False by default """ if util._is_host_up(env.host, int(env.port)) is False: return False self.needSudo = eval(str(sudo).capitalize()) l_opt = "" n_opt = "" if int(lines) > 0: l_opt = "| head -n "+str(int(lines))+" " try: if eval(str(show_lines)): n_opt = "-n " except: util._print_stack_error(str(show_lines)+" is not a valid boolean value. Ignoring") result = self._fabrun("grep -E \""+pattern+"\" "+n_opt+filename+l_opt) if result.failed: print(env.host+"|"+red("FAILURE")+" during grep on <"+filename+">") puts(result.stderr) return False else: if eval(str(display).capitalize()): for l in result.splitlines(): print env.host+"|"+l return True
def netapp_filer_uptime(self): """ Show uptime of the host """ self._setup() if util._is_host_up(env.host, int(env.port)) is False: return False HOSTNAME = run('hostname',False,False) disconnect_all() uptime = run('uptime').split(", ") i = 0 sys.stdout.write(HOSTNAME) for col in uptime: if i != 1: sys.stdout.write("|" + col) else: j = 0 col_s = col.split() for col_ in col_s: if j != 1: sys.stdout.write(" " + col_) else: sys.stdout.write("|" + col_) j += 1 i += 1 sys.stdout.write("\n") disconnect_all()
def netapp_filer_snapmirror_status(self): """ Show status of the snapmirror process on the filer process """ self._setup() if util._is_host_up(env.host, int(env.port)) is False: return False HOSTNAME = run('hostname') disconnect_all() if not self.first.has_key(util._func_name()): self.first[util._func_name()] = True print "HOSTNAME|Status|Source|Destination|State|Lag|Status" snapmirror = run('snapmirror status') disconnect_all() status = "" lines = [] for l in snapmirror.splitlines(): if re.match("Snapmirror\s+is\s+(.*)$",l): status = re.sub("Snapmirror\s+is\s+(.*)$","\\1",l) elif not re.match("^Source\s+",l): lines.append(l) status=re.sub("\.","",status) if re.match("^on$",status): for l in lines: print HOSTNAME+"|"+status+"|"+re.sub("\s+","|",l)
def kernel_version(self): """ Display the kernel version running on the host """ if util._is_host_up(env.host, int(env.port)) is False: return False k_version = util._run('uname -r') print env.host + "|" + k_version
def hostname(self): """ Return configured hostname """ if not util._is_host_up(env.host, int(env.port)): return False result = util._run('hostname -f') if result.succeeded: print env.host + "|" + result return result.succeeded
def date(self): """ Show date of the host """ if not util._is_host_up(env.host, int(env.port)): return False result = util._run('date') if result.succeeded: print env.host + "|" + result return result.succeeded
def route(self,sudo=False): """ Show all configured routes, in sudo mode is [sudo] is set to True """ if util._is_host_up(env.host, int(env.port)) is False: return False self.needSudo = eval(str(sudo).capitalize()) routes = self._fabrun("netstat -nr | grep -E -v \"(Destination|Kernel)\" | awk '{print $1\"|\"$2\"|\"$3\"|\"$NF}'") routes_list = routes.split() for route in routes_list: print env.host + "|" + route
def ls_dir(self,fd,sudo=False): """ List dir <fd> set in parameter, in sudo mode is [sudo] is set to True """ if util._is_host_up(env.host, int(env.port)) is False: return False self.needSudo = eval(str(sudo).capitalize()) fd = self._fabrun("ls -dl " + fd + " | grep -vi \"^total\" | awk '{print $9 $10 $11}'") fd_list = fd.split("\r\n") for fd in fd_list: print env.host + "|" + fd
def process(self,process_pattern,sudo=False,kill=False): """ Show <process_pattern> running processes, in sudo mode is [sudo] is set to True, and [kill] if set to True """ if util._is_host_up(env.host, int(env.port)) is False: return False self.needSudo = eval(str(sudo).capitalize()) process_list = self._fabrun('ps aux | grep ' + process_pattern + ' | grep -v grep') for p in process_list.splitlines(): print env.host+"|"+process_list if eval(str(kill).capitalize()): plist = process_list.split() result = self._fabrun('kill '+plist[1])
def lvdisplay(self,sudo=False): """ List logical volumes, in sudo mode is [sudo] is set to True """ if util._is_host_up(env.host, int(env.port)) is False: return False self.needSudo = eval(str(sudo).capitalize()) result = self._fabrun("lvdisplay -C | grep -v 'LV' | awk '{print $1\"|\"$2}'") if result.failed: print env.host+"|Failed: cannot execute lvdisplay: "+result lv_list = result.split() for lv in lv_list: print env.host + "|" + lv
def sed(self,filename, before, after, limit='', sudo=False, backup='.bak', flags=''): """ sed <filename> replace <before> with <after> with [limit] default '' , in [sudo] mode default False, [backup] old file with extension default .bak and add some [flags] for sed flags """ if util._is_host_up(env.host, int(env.port)) is False: return False result = util._sed(filename, before, after, limit, sudo, backup, flags) if not result.succeeded: print(env.host+"|"+red("FAILURE")+" during sed on <"+filename+">") puts(result.stderr) return False else: print(env.host+"|"+green("SUCCESS")+" during sed on <"+filename+">") return True
def netapp_filer_version(self): """ Show the OS filer version """ self._setup() if util._is_host_up(env.host, int(env.port)) is False: return False HOSTNAME = run('hostname') disconnect_all() if not self.first.has_key(util._func_name()): self.first[util._func_name()] = True print "HOSTNAME|OS_VERSION" print HOSTNAME + '|' + run('version') disconnect_all()
def put(self,local_path,remote_path,fmode,sudo=False): """ Put <local_path> set in parameter into <remote_path> with mode <fmode> on remote host, in sudo mode is [sudo] is set to True """ if util._is_host_up(env.host, int(env.port)) is False: return False try: result = put(local_path=local_path,remote_path=remote_path,mode=int(fmode,base=0),use_sudo=eval(str(sudo).capitalize())) if result.succeeded: print env.host+"|<"+yellow(local_path)+"> "+green("successfully")+" uploaded on "+env.host+":"+remote_path else: print env.host+"|"+yellow(",".join(str(item) for item in result.failed))+" "+red("failed")+" uploaded on "+env.host+":"+remote_path except Exception as e: puts(env.host+"|"+red("ERROR:")+" During upload (Bad value for some parameter(s) ?)",e)
def version_rpm(self,name): """ Display rpm package <name> version """ if util._is_host_up(env.host, int(env.port)) is False: return False #rpms = util._run('rpm -qa | egrep \"'+name+'\"') os_version = self.os_release(display=False) rpms = util._run('rpm -q '+name+' --queryformat "%{NAME}|%{VERSION}-%{RELEASE}.%{ARCH}|%{INSTALLTIME:date}\n"') rpms_list = rpms.splitlines() if not self.first.has_key(util._func_name()): self.first[util._func_name()] = True util._print_task_header("HOSTNAME|OS_RELEASE|PACKAGE_NAME|VERSION|INSTALL_DATE") for rpm in rpms_list: print env.host + "|" + os_version + "|" + rpm
def check_tcp(self,rhost,rport,timeout=5): """ Test <rhost> <rport> tcp connectivity from host using python with [timeout] (default: 5s) """ if util._is_host_up(env.host, int(env.port)) is False: return False HOSTNAME = env.host puts("Proxy %s port %s " % (rhost,rport)) tcp_check = util._run("python -c \"import socket;sock=socket.socket();sock.settimeout("+str(timeout)+");sock.connect(('"+rhost+"',"+rport+"));sock.close()\"") if tcp_check.succeeded: print HOSTNAME + "|" + rhost + "|" + rport + "|" + green("Success") else: print HOSTNAME + "|" + rhost + "|" + rport + "|" + red("Failed",True) return tcp_check.succeeded
def netapp_filer_storage_show_fault(self): """ Display result of 'storage show fault' command on the filer """ self._setup() if util._is_host_up(env.host, int(env.port)) is False: return False HOSTNAME = run('hostname') disconnect_all() if not self.first.has_key(util._func_name()): self.first[util._func_name()] = True print "HOSTNAME|FAULTS" cf = run('storage show fault') disconnect_all() for l in cf.splitlines(): print HOSTNAME+"|"+l
def netapp_filer_exec_cmd(self,cmd): """ Execute <cmd> command on the filer """ self._setup() if util._is_host_up(env.host, int(env.port)) is False: return False HOSTNAME = run('hostname') disconnect_all() if not self.first.has_key(util._func_name()): self.first[util._func_name()] = True print "HOSTNAME|Command_Result" cf = run(cmd) disconnect_all() for l in cf.splitlines(): print HOSTNAME+"|"+l
def netapp_filer_cluster_status(self): """ Show status of the cluster on the filer """ self._setup() if util._is_host_up(env.host, int(env.port)) is False: return False HOSTNAME = run('hostname') disconnect_all() if not self.first.has_key(util._func_name()): self.first[util._func_name()] = True print "HOSTNAME|Status|Source|Destination|State|Lag|Status" cf = run('cf status') disconnect_all() for l in cf.splitlines(): print HOSTNAME+"|"+l
def cat(self,filename,sudo=False,display=True): """ Cat <filename> set in parameter, in sudo mode is [sudo] is set to True, and [display] le filename default True """ if util._is_host_up(env.host, int(env.port)) is False: return False self.needSudo = eval(str(sudo).capitalize()) output = self._fabrun("cat "+filename) if output.failed: print env.host +"|"+red("FAILED:")+" to read file <"+yellow(filename)+">" return False if eval(str(display).capitalize()): print env.host+"|"+filename+"|"+output else: return output
def netapp_filer_storage_show_fault(self): """ Display result of 'storage show fault' command on the filer """ self._setup() if util._is_host_up(env.host, int(env.port)) is False: return False HOSTNAME = run('hostname') disconnect_all() if not self.first.has_key(util._func_name()): self.first[util._func_name()] = True print "HOSTNAME|FAULTS" cf = run('storage show fault') disconnect_all() for l in cf.splitlines(): print HOSTNAME + "|" + l
def netapp_filer_exec_cmd(self, cmd): """ Execute <cmd> command on the filer """ self._setup() if util._is_host_up(env.host, int(env.port)) is False: return False HOSTNAME = run('hostname') disconnect_all() if not self.first.has_key(util._func_name()): self.first[util._func_name()] = True print "HOSTNAME|Command_Result" cf = run(cmd) disconnect_all() for l in cf.splitlines(): print HOSTNAME + "|" + l
def netapp_filer_cluster_status(self): """ Show status of the cluster on the filer """ self._setup() if util._is_host_up(env.host, int(env.port)) is False: return False HOSTNAME = run('hostname') disconnect_all() if not self.first.has_key(util._func_name()): self.first[util._func_name()] = True print "HOSTNAME|Status|Source|Destination|State|Lag|Status" cf = run('cf status') disconnect_all() for l in cf.splitlines(): print HOSTNAME + "|" + l
def exec_cmd(self,cmd,sudo=False,pty=True): """ Exec <cmd> set in sudo mode if <sudo> is set to True with a [pty] (default: True ) """ if util._is_host_up(env.host, int(env.port)) is False: return False self.needSudo = eval(str(sudo).capitalize()) fd = self._fabrun(cmd,pty) if fd.succeeded: fd_list = fd.split("\r\n") for result in fd_list: print env.host + "|" + result else: print env.host + "|Error during command execution: " +fd return fd.succeeded
def os_short(self): """ Show truncated OS version ( LFS, RedHat CentOS ... ) """ if util._is_host_up(env.host, int(env.port)) is False: return False OS = util._run("bash -c 'head -n 1 /etc/{{system,redhat,centos}-release,release} 2>/dev/null' | sort -u | grep -v '^==>'") if re.match("LC",OS,flags=re.IGNORECASE): print env.host + "|LFS " + OS if re.match("Red Hat",OS,flags=re.IGNORECASE): print env.host + "|" + OS if re.match("Centos",OS,flags=re.IGNORECASE): print env.host + "|" + OS if re.match("Solaris",OS,flags=re.IGNORECASE): print env.host + "|" + OS else: print env.host + "|UNKNOW_OS" + OS
def listen(self,sudo=False): """ Show all listen port/processes, in sudo mode is [sudo] is set to True """ if util._is_host_up(env.host, int(env.port)) is False: return False self.needSudo = eval(str(sudo).capitalize()) listen = self._fabrun("netstat -lntp 2>/dev/null | awk '{print $4,$7}' | awk -F/ '{print $1,$2}' | awk '{if ($1 ~ /[0-9]+/) {print $1\",\"$3}}' | sort -n") listen_list = listen.split() #print listen_list listen_dict = dict((key, value) for key, value in (line.split(',') for line in listen_list)) #print listen_dict if not self.first.has_key(util._func_name()): self.first[util._func_name()] = True util._print_task_header("HOSTNAME|IP|PORT|PROCESS_NAME") for port in listen_dict.iterkeys(): print env.host + "|" + re.sub(':(\d+)$','|\\1',port) + "|" + listen_dict[port]
def ipvsadm(self,sudo=False): """ Show all lvs rules configured on dispatcher host, in sudo mode is [sudo] is set to True """ if util._is_host_up(env.host, int(env.port)) is False: return False self.needSudo = eval(str(sudo).capitalize()) rules = self._fabrun("ipvsadm -ln --sort | grep -E \"(TCP|->)\" | grep -v \"Remote\" | awk '{print $1\"|\"$2\"|\"$3\"|\"$4\"|\"$5\"|\"$6}'") rules_list = rules.split() lvs_rules = {} last_entry = "" list = [] puts(rules_list) for entry in rules_list: if re.match("^->.*",entry): s = re.sub(r"^\->\|(.*)$","\\1",entry) ip = re.sub(r"^\->\|([^\:]+)\:.*$","\\1",entry) end = re.sub(r"^\->\|([^\:]+)(\:.*)$","\\2",entry) reverse = util._nslookup(ip) if reverse is not False: puts("Found real server "+ util._nslookup(ip)) s = reverse+end else: puts("Non reversed server "+ip) lvs_rules[last_entry].append(s) else: s = re.sub(r"^([^\|]+)\|([^\|]+)\|([^\|]+)\|.*$","\\1|\\2|\\3",entry) ip = re.sub(r"^([^\|]+)\|([^\:]+)\:.*$","\\2",entry) end = re.sub(r"^([^\|]+)\|([^\:]+)(\:.*)$","\\3",entry) end = re.sub(r'\|+','|',end) reverse = util._nslookup(ip) if reverse is not False: puts("Found VIP "+ util._nslookup(ip)) s = reverse+end else: puts("Non reversed VIP "+ip) last_entry = s if not lvs_rules.has_key(last_entry): lvs_rules[last_entry] = [] puts(lvs_rules) if not self.first.has_key(util._func_name()): self.first[util._func_name()] = True util._print_task_header("HOSTNAME|VIP:VPORT|VMETHOD|REALIP:REALPORT|FORWARD_METHOD|WEIGHT|ACTIVECONN|INACTCONN") for rule in lvs_rules.iterkeys(): for node in lvs_rules[rule]: print env.host + "|" + rule + node
def system_service_notonstartup(self,service,sudo=False): """ Set a <service> to run on server startup in sudo mode is [sudo] is set to True """ if util._is_host_up(env.host, int(env.port)) is False: return False self.needSudo = eval(str(sudo).capitalize()) redhatRelease = self._fabrun('cat /etc/{redhat,centos}-release') if redhatRelease.failed: print env.host + '|' + yellow("{redhat,centos}-release file missing. Probably not a CentOs, Redhat distribution.") else: chk_result = self._fabrun('chkconfig --level 3 ' + service + ' off') if chk_result.failed: print env.host + '|' + red("Failed to remove the service on startup. Check the service name.") else: print env.host + '|' + green("OK")