def __readDiskStats(self): cmd = "cat /proc/diskstats" self.__log("read-diskstat").debug5("Perfoem - %s", cmd) (rc, exception, sp) = utils.runPopen(cmd, stdout=subprocess.PIPE) if not rc: self.__log("popen-disk-failed").error( "Fail to run Cmd %s - %s", cmd, utils.parseErrnoToString(exception)) return False try: for line in sp.stdout: value = line.split() if (len(value) >= 3 and self.__diskDeviceRegex.match(value[2])): for i in range(len(self.__diskFieldsNames)): if len(value) >= (i + 3): key = "disk_hdd_" + value[ 2] + "-" + self.__diskFieldsNames[i] self.__diskStatDic[key] = value[i + 3].rstrip('\r\n') self.__log("disk-stat-value").debug4( "i = %d key - %s, value - %s", i, key, self.__diskStatDic[key]) except Exception, e: if not utils.isEINTR(e): self.__log("run-diskstat-err").error( "Failed to read from sp.stdout diskstat info %s", utils.parseErrnoToString(e)) return False
def __readDiskStats (self): cmd = "cat /proc/diskstats" self.__log("read-diskstat").debug5("Perfoem - %s",cmd) (rc,exception,sp) = utils.runPopen(cmd, stdout=subprocess.PIPE) if not rc: self.__log("popen-disk-failed").error("Fail to run Cmd %s - %s",cmd,utils.parseErrnoToString(exception)) return False try: for line in sp.stdout: value = line.split() if (len(value) >= 3 and self.__diskDeviceRegex.match(value[2])): for i in range(len(self.__diskFieldsNames)): if len(value) >= (i+3): key = "disk_hdd_" + value[2] + "-" + self.__diskFieldsNames[i] self.__diskStatDic[key] = value[i+3].rstrip('\r\n') self.__log("disk-stat-value").debug4("i = %d key - %s, value - %s",i,key,self.__diskStatDic[key]) except Exception, e: if not utils.isEINTR(e): self.__log("run-diskstat-err").error("Failed to read from sp.stdout diskstat info %s",utils.parseErrnoToString(e)) return False
def __getAllNginxRunningWorkersPids (self, ppid): """ return (rc,pid list) rc = False in operation Failure pid list = All Process PID's with the same User Name and Current Working Directory """ # display all current running worker processes whos parent is 'master pid' cmd = "ps f --ppid %s -o pid,cmd | grep -v \"%s\" | awk '{print $1}' " % (ppid, self.__conf.kConf.kOldWorkerShutdownCmd) self.__log("get-running-workers").info("Collect All Nginx Running Workers using Popen - %s",cmd) (rc,exception,sp) = utils.runPopen(cmd, stdout=subprocess.PIPE, shell=True) pidList = [] # ps Failed - Kill All Nginx running Workers if not rc: self.__log("ps-failed").error("Failed to perfrom %s- %s",cmd,utils.parseErrnoToString(exception)) return (False,pidList) # Try to Print all Up running Workers and Kill them try: for line in sp.stdout: self.__log("running-worker-next-pid").debug1("Next Nginx Running Worker Process - %s",line) pid = line.strip() pidList.append(pid) self.__log("num-nginx-running-workers").notice("Found Total of %d Nginx Running Workers: %s", len(pidList), pidList) retVal = True except Exception, e: self.__log("read-stdout-failed").error("Failed to read ps result from stdout - %s",utils.parseErrnoToString(e)) retVal = False
def __readVmStats(self): cmd = "cat /proc/vmstat" self.__log("read-vmstat").debug5("Perfoem - %s", cmd) (rc, exception, sp) = utils.runPopen(cmd, stdout=subprocess.PIPE) if not rc: self.__log("popen-vm-failed").error( "Fail to run Cmd %s - %s", cmd, utils.parseErrnoToString(exception)) return False try: for line in sp.stdout: value = line.split(" ") if (value[0] in self.__vmRelevantFields): key = "vm_stat_" + value[0] self.__vmStatDic[key] = value[1].rstrip('\r\n') except Exception, e: if not utils.isEINTR(e): self.__log("run-vmstat-err").error( "Failed to read from sp.stdout vmstat info %s", utils.parseErrnoToString(e)) return False
def __isMasterProcessUp (self, masterPid): """ Search Nginx Master PID, Use Linux Command 'ps' If valid it means Nginx is up TBD - Check if number of processes is the same as configured workers Args: masterPid - Nginx Master Process pid value Returns: True in case Nginx Master Process is up """ cmd = "ps --no-heading " + str(masterPid) self.__log("is-master-up").debug5("Collect All Nginx Processes using Popen - %s",cmd) (rc,exception,sp) = utils.runPopen(cmd, stdout=subprocess.PIPE) if not rc: self.__log("popen-failed").error("Fail to run Cmd %s - %s",cmd,utils.parseErrnoToString(exception)) return False while 1: try: if len(sp.stdout.readline()) == 0: self.__log("sp.stdout").debug1("Master Process is not in Process List") return False except Exception, e: if utils.isEINTR(e): continue else: self.__log("run-pgrep-failed").error("Failed to read from sp.stdout pid list, Master Pid = %d -%s",masterPid,utils.parseErrnoToString(e)) return False break
def __runCommand (self, strCommand, command , maxTimeMsec = 0, useShell = False): (rc,exception,sp) = utils.runPopen(command, close_fds = True, maxTimeMsec = maxTimeMsec, shell=useShell) if rc: self.__log("run-cmd").info("%s - %s", strCommand, command) else: self.__log("run-cmd-err").error("Failed to Execute Nginx Command - %s - %s - %s", strCommand, command, utils.parseErrnoToString(exception)) return rc
def __runCommand(self, strCommand, command, maxTimeMsec=0, useShell=False): (rc, exception, sp) = utils.runPopen(command, close_fds=True, maxTimeMsec=maxTimeMsec, shell=useShell) if rc: self.__log("run-cmd").info("%s - %s", strCommand, command) else: self.__log("run-cmd-err").error( "Failed to Execute Nginx Command - %s - %s - %s", strCommand, command, utils.parseErrnoToString(exception)) return rc
def __isTopperFileOpen(self, isLogResult): """ Return True if topper file is opened by other process """ cmd = "lsof " + self.__topperTempFile self.__log("is-file-opend").debug3( "Check if temp topper file is still open by any other process - %s", cmd) (rc, exception, sp) = utils.runPopen(cmd, stdout=subprocess.PIPE) if not rc: self.__log("is-open-failed").error( "Fail to run Cmd %s - %s", cmd, utils.parseErrnoToString(exception)) return False while 1: try: if isLogResult: for line in sp.stdout: self.__log("sp.stdout-log").error( "Open Worker on Record Log File After Reopen Signal - %s", line) return True else: if len(sp.stdout.readline()) == 0: self.__log("sp.stdout").debug3( "File %s, is not Opened by any Other processes", self.__topperTempFile) return False else: self.__log("sp.stdout-notempty").debug1( "File %s is still opened By Nginx processes", self.__topperTempFile) except Exception, e: if utils.isEINTR(e): continue else: self.__log("run-isopen-failed").error( "Failed to read from sp.stdout for is Open File = %s -%s", self.__topperTempFile, utils.parseErrnoToString(e)) return False break
def __killNginxProcessesAll (self): """ Kill all processes with name 'self.__conf.kConf.kNginxExeName' Return True on successes """ cmd = "killall " + self.__conf.kConf.kNginxExeName + " -s TERM" self.__log("kill-nginx-processes").info("Kill All Nginx Processes using Popen - %s",cmd) (rc,exception,sp) = utils.runPopen(cmd) if not rc: self.__log("kill-nginx-process-all").error("Killall All Nginx Processes Failed - %s",utils.parseErrnoToString(exception)) return rc
def __killNginxProcessesAllByUser (self): """ Kill all processes with name 'self.__conf.kConf.kNginxExeName' Return True on successes """ userName = getpass.getuser() cmd = "killall "+ self.__conf.kConf.kNginxExeName + " -u " + str(userName) + " -s TERM" self.__log("kill-nginx-processes-user").info("Kill All Nginx Processes of User - %s using Popen - %s",userName,cmd) (rc,exception,sp) = utils.runPopen(cmd) if not rc: self.__log("kill-nginx-process-user").error("Killall All User - %s, Nginx Processes Failed - %s",userName,utils.parseErrnoToString(exception)) return rc
def __KillNginxProcess (self,pid): """ Kill processes with id = pId Return True on successes """ cmd = "kill " + "-s TERM " + pid self.__log("kill-nginx-process").info("Kill Nginx Process - %s using Popen - %s",pid,cmd) (rc,exception,sp) = utils.runPopen(cmd) if not rc: self.__log("kill-nginx-process-error").error("Kill Nginx Processes with PID = %s Failed - %s",pid,utils.parseErrnoToString(exception)) return rc
def __logIfConfig (self): cmd = "ifconfig" (rc,exception,sp) = utils.runPopen(cmd, stdout=subprocess.PIPE) if not rc: self.__log("log-if-config").error("Fail to run ifconfig as part exec Nginx failure - %s",utils.parseErrnoToString(exception)) return try: for line in sp.stdout: self.__log("log-ifconfig").debug3("%s",line) except Exception: pass return
def __readVmStats (self): cmd = "cat /proc/vmstat" self.__log("read-vmstat").debug5("Perfoem - %s",cmd) (rc,exception,sp) = utils.runPopen(cmd, stdout=subprocess.PIPE) if not rc: self.__log("popen-vm-failed").error("Fail to run Cmd %s - %s",cmd,utils.parseErrnoToString(exception)) return False try: for line in sp.stdout: value = line.split(" ") if (value[0] in self.__vmRelevantFields): key = "vm_stat_" + value[0] self.__vmStatDic[key] = value[1].rstrip('\r\n') except Exception, e: if not utils.isEINTR(e): self.__log("run-vmstat-err").error("Failed to read from sp.stdout vmstat info %s",utils.parseErrnoToString(e)) return False
def __isTopperFileOpen (self, isLogResult): """ Return True if topper file is opened by other process """ cmd = "lsof " + self.__topperTempFile self.__log("is-file-opend").debug3("Check if temp topper file is still open by any other process - %s",cmd) (rc,exception,sp) = utils.runPopen(cmd, stdout=subprocess.PIPE) if not rc: self.__log("is-open-failed").error("Fail to run Cmd %s - %s",cmd,utils.parseErrnoToString(exception)) return False while 1: try: if isLogResult: for line in sp.stdout: self.__log("sp.stdout-log").error("Open Worker on Record Log File After Reopen Signal - %s",line) return True else: if len(sp.stdout.readline()) == 0: self.__log("sp.stdout").debug3("File %s, is not Opened by any Other processes",self.__topperTempFile) return False else: self.__log("sp.stdout-notempty").debug1("File %s is still opened By Nginx processes",self.__topperTempFile) except Exception, e: if utils.isEINTR(e): continue else: self.__log("run-isopen-failed").error("Failed to read from sp.stdout for is Open File = %s -%s",self.__topperTempFile,utils.parseErrnoToString(e)) return False break
def __getAllNginxProcessIdMini (self): """ return (rc,pid list) rc = False in operation Failure pid list = All Process PID's with the same User Name and Current Worling Directory """ userName = getpass.getuser() cmd = "pgrep -u "+ str(userName) + " " + self.__conf.kConf.kNginxExeName self.__log("get-mini").info("Collect All Nginx Processes using Popen - %s",cmd) (rc,exception,sp) = utils.runPopen(cmd, stdout=subprocess.PIPE) pidList = [] # Pgrep Failed - Kill All Nginx Processes if not rc: self.__log("pgrep-failed").error("Failed to perfrom %s- %s",cmd,utils.parseErrnoToString(exception)) return (False,pidList) # Try to Print all Up Processes and Kill them try: num = 0 self.__log("cwd").info("Current Working Directory - %s",os.getcwd()) for pid in sp.stdout: num = num+1 spid = pid.rstrip() if self.__compareCWD(spid): pidList.append(spid) self.__log("num-nginx-compare-cwd").info("Found Total of %d Nginx Processes, %d Running in my CWD",num,len(pidList)) retVal = True except Exception, e: self.__log("read-stdout-failed").error("Failed to read Pgrep result from stdout - %s",utils.parseErrnoToString(e)) retVal = False