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 __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 __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 __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 __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 __collectNginxCounters(self): request = nginx_ipc.Request() response = nginx_ipc.Response() request.host = self.__conf.kConf.kNginxStatusIp request.port = self.__conf.nginxStatusPort request.url = self.__conf.kConf.kNginxStatusUrl request.timeout = self.__conf.countersFetchTimeoutSec retVal = self.__ngxIpc.sendRequest(request, response) if not retVal: self.__log("nginx-status-error").info( "Failed to receive counters from Nginx") return False if response.status != 200 or response.reason != "OK": self.__log("nginx-status-failed").notice( "Failed to read Nginx Status over HTTP - Status = '%d' : Reason = %s", response.status, response.reason) return True try: decoder = json.JSONDecoder() self.__nginxCounterDic = decoder.decode(response.data) self.__log("nginx-counters").debug5(str(self.__nginxCounterDic)) except Exception, e: self.__log("decode-counters-fail").error( "Fail to decode Nginx Counters - %s", utils.parseErrnoToString(e))
class WebBackend(object): def __init__(self, log): self.__log = log #------------------------------------------------------------------------------------------------- def init(self, deliveryConf): self.__webBackendFileName = os.path.join( deliveryConf.webStatusDir, deliveryConf.kConf.kWebAppFileName) self.__log("init-web-backend").info("Init Web Backend - File Name %s", self.__webBackendFileName) #------------------------------------------------------------------------------------------------- def writeTitelsDeliverd(self, num): fileNameTemp = self.__webBackendFileName + ".tmp" try: tempFile = open(fileNameTemp, 'w') except IOError, e: self.__log("open-temp").error("Failed to Open Temp File %s - %s", fileNameTemp, utils.parseErrnoToString(e)) return False try: tempFile.write(str(num)) retVal = True except Exception, e: self.__log("write-temp").error("Failed to write to File %s - %s", fileNameTemp, utils.parseErrnoToString(e)) retVal = 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 tick(self): # == 0 if not self.__errLogSizeByte: return False try: curSizeByte = self.__rotator.getCurrentFileSize() if curSizeByte >= self.__errLogSizeByte: self.__rotator.rotate() self.__log("log-end").debug1( "Rotate Nginx Error Log - File Size = %d > %d", curSizeByte, self.__errLogSizeByte) return True self.__log("log-end").debug3( "No Rotate of Nginx Error Log - File Size = %d < %d", curSizeByte, self.__errLogSizeByte) except Exception, e: self.__log("log-tick").error( "Failed to tick Nginx Log Rotator - %s", utils.parseErrnoToString(e)) return False
def __readDeviceL2BytesPerIpVersion(self, ipVersion, rulesMap, egressInterface, isRx): if not rulesMap.has_key(egressInterface): self._log("interface-ip-rule-match-fail").error( "Failed to Match Interface %s IPv%s Rule for reading L2 bytes (RX=%s)", egressInterface, ipVersion, isRx) return -1 rules = rulesMap[egressInterface] if len(rules) != 2: self._log("interface-ip-rule-match-fail").error( "Failed to Match Interface %s IPv%s Rule for reading L2 bytes (RX=%s)", egressInterface, ipVersion, isRx) return -1 if isRx: rule = rules[0] # input chain else: rule = rules[1] # output chain numL3Bytes = 0 numPackets = 0 matchedIpRule = rule.split(None, 2) try: numPackets = int(matchedIpRule[0]) numL3Bytes = int(matchedIpRule[1]) except (IndexError, ValueError, TypeError), e: self._log("read-l2-bytes-fail").error( "Failed to Parse Interface %s IPv%s Rule for reading L2 bytes %s - %s", egressInterface, ipVersion, matchedIpRule, utils.parseErrnoToString(e)) return -1
def __updateOrCreateIfNotExist(self, shouldUpdate): try: if not os.path.exists(self.__conf.allowLineRedirectFileName): self.__log("create-file").info( "Allow line Redirect File is created - data: %s", self.__redirectFile) else: if not shouldUpdate: return True self.__log("update-file").debug4( "Allow line Redirect File is updated - data: %s", self.__redirectFile) #timeoutGuard = TimeoutGuard(self.__log, 'update-or-create', self.DEFAULT_TIMEOUT_MILI_SEC) self.__redirectFile.writeToFile( self.__log, self.__conf.allowLineRedirectFileName) #timeoutGuard.checkAndLog(self.__redirectFile.writeToFile) return True except Exception, e: self.__log("cretae-file-error").error( "Fail to Check If Exist or Create File %s -%s", self.__conf.allowLineRedirectFileName, utils.parseErrnoToString(e))
def __getFileSize (self,fileName): try: curSize = os.path.getsize(fileName) self.__log("file-size-ok").debug3("Get %s size, Size = %s", fileName,str(curSize)) except OSError, e: self.__log("file-size-err").error("Get %s size Failed - %s",fileName,utils.parseErrnoToString(e)) return (-1)
def writeTitelsDeliverd(self, num): fileNameTemp = self.__webBackendFileName + ".tmp" try: tempFile = open(fileNameTemp, 'w') except IOError, e: self.__log("open-temp").error("Failed to Open Temp File %s - %s", fileNameTemp, utils.parseErrnoToString(e)) return False
def __getFileSize(self, fileName): try: curSize = os.path.getsize(fileName) self.__log("file-size-ok").debug3("Get %s size, Size = %s", fileName, str(curSize)) except OSError, e: self.__log("file-size-err").error("Get %s size Failed - %s", fileName, utils.parseErrnoToString(e)) return (-1)
def __deleteIfExist(self): try: if os.path.exists(self.__conf.allowLineRedirectFileName): self.__log("file-not-exist").info( "Allow line Redirect File exist - Delete it") os.remove(self.__conf.allowLineRedirectFileName) return True except Exception, e: self.__log("delete-if-exist").error( "Fail to Check If Exist or Remove File %s -%s", self.__conf.allowLineRedirectFileName, utils.parseErrnoToString(e))
def sendRequest(self, request, response): try: if request.timeout == 0: # Use Global Timeout conn = httplib.HTTPConnection(request.host, request.port) else: conn = httplib.HTTPConnection(request.host, request.port, timeout=request.timeout) except Exception, e: self.__log("connect-failed").notice( "Connect to - %s:%s%s Failed - %s", request.host, request.port, request.url, utils.parseErrnoToString(e)) return False
def end(self): try: curSizeByte = self.__rotator.getCurrentFileSize() if curSizeByte: self.__rotator.rotate() self.__log("log-end").info( "Rotate Nginx Log in Exit - File Size = %d", curSizeByte) except Exception, e: self.__log("log-end-err").error( "Failed to rotate Nginx Log in Exit - %s", utils.parseErrnoToString(e)) return False
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 _readDeviceL2Bytes(self, egressInterface, isRx): if isRx: filePath = os.path.join("/sys/class/net/", egressInterface, "statistics/rx_bytes") else: filePath = os.path.join("/sys/class/net/", egressInterface, "statistics/tx_bytes") try: tempFile = open(filePath, 'r') except IOError, e: self._log("read-l2-bytes-fail").error( "Failed to Open Interface File for reading L2 bytes %s - %s", filePath, utils.parseErrnoToString(e)) return -1
def tick (self): # == 0 if not self.__errLogSizeByte: return False try: curSizeByte = self.__rotator.getCurrentFileSize() if curSizeByte >= self.__errLogSizeByte: self.__rotator.rotate() self.__log("log-end").debug1("Rotate Nginx Error Log - File Size = %d > %d", curSizeByte, self.__errLogSizeByte) return True self.__log("log-end").debug3("No Rotate of Nginx Error Log - File Size = %d < %d", curSizeByte, self.__errLogSizeByte) except Exception, e: self.__log("log-tick").error("Failed to tick Nginx Log Rotator - %s",utils.parseErrnoToString(e)) return 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 __compareCWD (self,pid): """ Return True in case process with id = pid is running on current working directory """ # Get Current Working Directory try: currentDirectory = os.getcwd() except Exception: return False try: pPath = "/proc/" + pid + "/cwd" result = os.readlink(pPath) self.__log("read-link").debug1("Read Link = %s, Result = %s",pPath,result) if result == currentDirectory: return True else: return False except Exception, e: self.__log("read-link-failed").error("Failed to read link %s - %s",pPath,utils.parseErrnoToString(e)) return False
class NginxIpc(object): def __init__(self, name, logger): self.__name = name self.__log = logger.createLogger(G_NAME_MODULE_DELIVERY, G_NAME_GROUP_DELIVERY_NGINX_IPC) #------------------------------------------------------------------------------------------------- def sendRequest(self, request, response): try: if request.timeout == 0: # Use Global Timeout conn = httplib.HTTPConnection(request.host, request.port) else: conn = httplib.HTTPConnection(request.host, request.port, timeout=request.timeout) except Exception, e: self.__log("connect-failed").notice( "Connect to - %s:%s%s Failed - %s", request.host, request.port, request.url, utils.parseErrnoToString(e)) return False # Send Request try: self.__log("send-http-req").debug3("Send HTTP Request - %s:%s%s", request.host, request.port, request.url) conn.request("GET", request.url) except Exception, e: self.__log("send-http-req-failed").notice( "Failed to Send HTTP Request - Hostname = %s:%s, URL - %s - %s", request.host, request.port, request.url, utils.parseErrnoToString(e)) # to be on the safe side conn.close() return False
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
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 init(self, conf): logsDirectoryPath = conf.ngxLogDir logsDirectorySizeByte = conf.nginxDirectoryLogSizeByte errLogPath = conf.ngxLogFileSymLink errLogSizeByte = conf.nginxLogFileSizeByte self.__rotator = RotatingFileSizeEnforcer(self.__log, logsDirectoryPath, "log-", ".txt") self.__rotator.initSymlink(os.path.dirname(errLogPath), os.path.basename(errLogPath)) self.__log("log-rotator").info("Log Symbolic Link %s", errLogPath) self.__errLogSizeByte = errLogSizeByte try: # old files might be deleted to obey directory size self.__rotator.setTotalSize(logsDirectorySizeByte) # The sym Link is created old files might be deleted to obey directory size # the SymLink is linking between the errLogFile and Logs Directory self.__rotator.prepare() self.__log("log-rotator").info( "Init Nginx Log, Directory Size = %d File Size = %d", logsDirectorySizeByte, errLogSizeByte) except Exception, e: self.__log("log-rotator-err").exception( "Failed to init Nginx Log Rotator - %s, %s", utils.parseErrnoToString(e), sys.exc_info()[1]) return False
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 __getNginxMasterPid (self): """ Read Master process pid from Nginx PID File Returns: -1 - Error in reading PID File N - Nginx Master Process PID Value """ pidFileName = self.__conf.actualNgxPidFile try: pidFile = open(pidFileName,"r") except IOError, e: # File not found if e.errno != errno.ENOENT : self.__log("pid-file-err").error("Failed to Read Nginx Pid File %s - %s",pidFileName,utils.parseErrnoToString(e)) else : self.__log("pid-file-failed").debug2("Nginx Pid File - %s Not exist",pidFileName) return (-1)
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 __collectNginxCounters (self): request = nginx_ipc.Request() response = nginx_ipc.Response() request.host = self.__conf.kConf.kNginxStatusIp request.port = self.__conf.nginxStatusPort request.url = self.__conf.kConf.kNginxStatusUrl request.timeout = self.__conf.countersFetchTimeoutSec retVal = self.__ngxIpc.sendRequest(request, response) if not retVal: self.__log("nginx-status-error").info("Failed to receive counters from Nginx") return False if response.status != 200 or response.reason != "OK": self.__log("nginx-status-failed").notice("Failed to read Nginx Status over HTTP - Status = '%d' : Reason = %s", response.status, response.reason) return True try: decoder = json.JSONDecoder() self.__nginxCounterDic = decoder.decode(response.data) self.__log("nginx-counters").debug5(str(self.__nginxCounterDic)) except Exception, e: self.__log("decode-counters-fail").error("Fail to decode Nginx Counters - %s",utils.parseErrnoToString(e))
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 __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 init (self, conf): logsDirectoryPath = conf.ngxLogDir logsDirectorySizeByte = conf.nginxDirectoryLogSizeByte errLogPath = conf.ngxLogFileSymLink errLogSizeByte = conf.nginxLogFileSizeByte self.__rotator = RotatingFileSizeEnforcer(self.__log,logsDirectoryPath,"log-",".txt") self.__rotator.initSymlink(os.path.dirname(errLogPath),os.path.basename(errLogPath)) self.__log("log-rotator").info("Log Symbolic Link %s",errLogPath); self.__errLogSizeByte = errLogSizeByte try: # old files might be deleted to obey directory size self.__rotator.setTotalSize(logsDirectorySizeByte) # The sym Link is created old files might be deleted to obey directory size # the SymLink is linking between the errLogFile and Logs Directory self.__rotator.prepare() self.__log("log-rotator").info("Init Nginx Log, Directory Size = %d File Size = %d",logsDirectorySizeByte,errLogSizeByte) except Exception, e: self.__log("log-rotator-err").exception("Failed to init Nginx Log Rotator - %s, %s",utils.parseErrnoToString(e), sys.exc_info()[1]) return False
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
try: self.__log("send-http-req").debug3("Send HTTP Request - %s:%s%s",request.host,request.port,request.url) conn.request("GET",request.url) except Exception, e: self.__log("send-http-req-failed").notice("Failed to Send HTTP Request - Hostname = %s:%s, URL - %s - %s", request.host,request.port,request.url,utils.parseErrnoToString(e)) # to be on the safe side conn.close() return False # Get Response try: httpResponse = conn.getresponse() except Exception, e : self.__log("http-get-response-failed").notice("Failed to receive HTTP Response - Hostname = %s:%s, URL - %s - %s", request.host,request.port,request.url,utils.parseErrnoToString(e)) conn.close() return False response.status = httpResponse.status response.reason = httpResponse.reason while 1: # Get Response try: response.data = httpResponse.read() self.__log("http-get-response").debug3("Received Http Response on URL: %s Status = %s Reason = %s", request.url,str(httpResponse.status),str(httpResponse.reason)) except Exception, e :
def end (self): try: curSizeByte = self.__rotator.getCurrentFileSize() if curSizeByte : self.__rotator.rotate() self.__log("log-end").info("Rotate Nginx Log in Exit - File Size = %d",curSizeByte) except Exception, e: self.__log("log-end-err").error("Failed to rotate Nginx Log in Exit - %s",utils.parseErrnoToString(e)) return False
def sendRequest (self, request, response): try: if request.timeout == 0: # Use Global Timeout conn = httplib.HTTPConnection(request.host,request.port) else: conn = httplib.HTTPConnection(request.host,request.port,timeout=request.timeout) except Exception, e: self.__log("connect-failed").notice("Connect to - %s:%s%s Failed - %s",request.host,request.port,request.url,utils.parseErrnoToString(e)) return False
def _readDeviceL2Bytes (self, egressInterface, isRx): if isRx: filePath = os.path.join("/sys/class/net/", egressInterface, "statistics/rx_bytes") else: filePath = os.path.join("/sys/class/net/", egressInterface, "statistics/tx_bytes") try: tempFile = open(filePath,'r') except IOError, e: self._log("read-l2-bytes-fail").error("Failed to Open Interface File for reading L2 bytes %s - %s", filePath, utils.parseErrnoToString(e)) return -1
self.__log("send-http-req-failed").notice( "Failed to Send HTTP Request - Hostname = %s:%s, URL - %s - %s", request.host, request.port, request.url, utils.parseErrnoToString(e)) # to be on the safe side conn.close() return False # Get Response try: httpResponse = conn.getresponse() except Exception, e: self.__log("http-get-response-failed").notice( "Failed to receive HTTP Response - Hostname = %s:%s, URL - %s - %s", request.host, request.port, request.url, utils.parseErrnoToString(e)) conn.close() return False response.status = httpResponse.status response.reason = httpResponse.reason while 1: # Get Response try: response.data = httpResponse.read() self.__log("http-get-response").debug3( "Received Http Response on URL: %s Status = %s Reason = %s", request.url, str(httpResponse.status), str(httpResponse.reason))
def __readDeviceL2BytesPerIpVersion (self, ipVersion, rulesMap, egressInterface,isRx): if not rulesMap.has_key(egressInterface): self._log("interface-ip-rule-match-fail").error("Failed to Match Interface %s IPv%s Rule for reading L2 bytes (RX=%s)", egressInterface, ipVersion, isRx) return -1 rules = rulesMap[egressInterface] if len(rules) != 2: self._log("interface-ip-rule-match-fail").error("Failed to Match Interface %s IPv%s Rule for reading L2 bytes (RX=%s)", egressInterface, ipVersion, isRx) return -1 if isRx: rule = rules[0] # input chain else: rule = rules[1] # output chain numL3Bytes = 0 numPackets = 0 matchedIpRule = rule.split(None,2) try: numPackets = int(matchedIpRule[0]) numL3Bytes = int(matchedIpRule[1]) except (IndexError, ValueError, TypeError), e: self._log("read-l2-bytes-fail").error("Failed to Parse Interface %s IPv%s Rule for reading L2 bytes %s - %s", egressInterface, ipVersion, matchedIpRule, utils.parseErrnoToString(e)) return -1
def writeTitelsDeliverd(self, num): fileNameTemp = self.__webBackendFileName + ".tmp" try: tempFile = open(fileNameTemp,'w') except IOError, e: self.__log("open-temp").error("Failed to Open Temp File %s - %s",fileNameTemp,utils.parseErrnoToString(e)) return False