예제 #1
0
파일: views.py 프로젝트: chowyu08/lepv
def pingServer(request, server='', requestId='', config='release'):
    if( request.method != 'GET' ):
        return

    try:
        if( server == '' ):
            print("LEPD serer is not specified!")
            return
        
        print("LEPD address: " + server)
        client = LepDClient(server=server, config=config)
        
        result = {}
        result['connected'] = client.ping()
        
        if (result['connected']):
            # get cpu count
            cpuMonitor = CPUMonitor(server=server, config=config)
            cpuCapacityData = cpuMonitor.getCapacity()
            result['cpuCoreCount'] = cpuCapacityData['data']['coresCount']
            
            memMonitor = MemoryMonitor(server=server, config=config)
            memoryCapacityData = memMonitor.getCapacity()
            result['memoryTotal'] = memoryCapacityData['data']['capacity']

        return JSONResponse(result)
    except Exception as ex:
        # print(ex)
        return HttpResponse(status=404)
예제 #2
0
파일: LepdTests.py 프로젝트: wusewolf/lepv
    def runAllMethodsConcurrently(self):

        # print("Running all commands concurrently...\n")
        processes = []

        client = LepDClient(self.server)
        commands = client.listAllMethods()

        timeStarts = datetime.utcnow().replace()

        for command in commands:
            # print(command)

            lepdRequestor = LepDRequestor(command, self.server)
            processes.append(lepdRequestor)
            lepdRequestor.start()
        
        # print('\nRunning and checking status...\n')
        self.checkAndReportThreads(processes)

        timeEnds = datetime.utcnow().replace()
        duration = timeEnds - timeStarts

        durationInSeconds = duration.seconds + duration.microseconds / 1000000
        timeUsed = "{:.3f}".format(durationInSeconds)
        
        print("\nAll done in " + timeUsed + " Seconds\n")
예제 #3
0
    def __init__(self, server, config='release'):
        self.server = server
        self.client = LepDClient(self.server)
        self.config = config

        # this maxDataCount should match the one defined for UI.
        self.maxDataCount = 25
예제 #4
0
    def __init__(self, lepdCommand, server='www.linuxep.com', port=12307):
        threading.Thread.__init__(self)

        self.timeUsed = 0

        self.command = lepdCommand
        self.lepDClient = LepDClient(server, port)

        self.response = ''
예제 #5
0
 def __init__(self, lepdCommand, server, port=12307):
     threading.Thread.__init__(self, name=lepdCommand)
     
     self.timeUsed = 0
     self.succeeded = False
     
     self.command = lepdCommand
     self.lepDClient = LepDClient(server, port)
     
     self.response = ''
예제 #6
0
파일: LepdTests.py 프로젝트: wusewolf/lepv
    def runAllMethodsRepeatedly(self):
        client = LepDClient(self.server)
        commands = client.listAllMethods()

        for command in commands:
            print("Running command: " + command)
            
            i = 1
            while( i <= 3 ):
                self.runMethodConcurrently(command, 1)
                i += 1
예제 #7
0
def runCommand(request, server, command):
    if (request.method != 'GET'):
        return

    try:
        if (server == ''):
            print("LEPD serer is not specified!")
            return

        print("LEPD address: " + server)
        client = LepDClient(server=server, config='debug')

        return JSONResponse(client.sendRequest(command))
    except Exception as ex:
        # print(ex)
        return HttpResponse(status=404)
예제 #8
0
class LepDRequestor(threading.Thread):
    def __init__(self, lepdCommand, server='www.linuxep.com', port=12307):
        threading.Thread.__init__(self)

        self.timeUsed = 0

        self.command = lepdCommand
        self.lepDClient = LepDClient(server, port)

        self.response = ''

    def run(self):
        timeStarts = datetime.utcnow().replace(tzinfo=utc)

        self.response = self.lepDClient.sendRequest(self.command)

        timeEnds = datetime.utcnow().replace(tzinfo=utc)
        duration = timeEnds - timeStarts
        self.timeUsed = int(duration.total_seconds())

    def report(self):
        print("")
        print("Command: " + self.command)
        print("Duration: " + str(self.timeUsed) + " seconds")
        print("Result:")
        print(self.response)

    def runAndReport(self):
        self.run()
        self.report()
예제 #9
0
def pingServer(request, server='', requestId='', config='release'):
    if (request.method != 'GET'):
        return

    try:
        if (server == ''):
            print("LEPD serer is not specified!")
            return

        print("LEPD address: " + server)
        client = LepDClient(server=server, config=config)

        result = {}
        result['connected'] = client.ping()

        return JSONResponse(result)
    except Exception as ex:
        return HttpResponse(status=404)
예제 #10
0
class LepDRequestor(threading.Thread):

    def __init__(self, lepdCommand, server, port=12307):
        threading.Thread.__init__(self, name=lepdCommand)
        
        self.timeUsed = 0
        self.succeeded = False
        
        self.command = lepdCommand
        self.lepDClient = LepDClient(server, port)
        
        self.response = ''


    def run(self):
        timeStarts = datetime.utcnow().replace()
        
        self.response = self.lepDClient.sendRequest(self.command)
        if ("{'result': 'Hello!'}" != self.response):
            self.succeeded = True
    
        timeEnds = datetime.utcnow().replace()
        duration = timeEnds - timeStarts
        
        durationInSeconds = duration.seconds + duration.microseconds / 1000000
        self.timeUsed = "{:.3f}".format(durationInSeconds)

    
    def report(self):
        
        reportMessage = self.command
        # assume the longest command name is of 30 chars
        maxLength = 30
        reportMessage += ' ' * (maxLength - len(self.command)) + ' '
        
        if (self.succeeded):
            reportMessage += "succeeded in "
        else:
            reportMessage += "failed in "
        
        reportMessage += str(self.timeUsed) + " seconds"
        
        print(reportMessage)
        # print("Command: " + self.command)
        # if (self.succeeded):
        #     print("Status: Succeeded")
        # else:
        #     print("Status: Failed!!!")
        #     print(self.response)
        #     
        # print("Duration: " + str(self.timeUsed) + " seconds")
        
    
    def runAndReport(self):
        self.run()
        self.report()
예제 #11
0
 def __init__(self, server, config='release'):
     self.server = server
     self.client = LepDClient(self.server)
     self.config = config
예제 #12
0
class IOMonitor:
    def __init__(self, server, config='release'):
        self.server = server
        self.client = LepDClient(self.server)
        self.config = config

    def getStatus(self):

        startTime = datetime.datetime.now()

        result = self.client.getIostatResult()

        endTime = datetime.datetime.now()

        rawResult = result[:]

        headerLine = result.pop(0)

        duration = "%.1f" % ((endTime - startTime).total_seconds())
        ioStatus = {}
        ioStatus['lepdDuration'] = duration
        ioStatus['disks'] = {}
        ioStatus['diskCount'] = 0
        ioStatus['ratio'] = 0
        for line in result:
            if (line.strip() == ""):
                continue

            lineValues = line.split()

            deviceName = lineValues[0]
            ioStatus['diskCount'] += 1
            ioStatus['disks'][deviceName] = {}

            ioStatus['disks'][deviceName]['rkbs'] = lineValues[5]
            ioStatus['disks'][deviceName]['wkbs'] = lineValues[6]
            ioStatus['disks'][deviceName]['ratio'] = lineValues[-1]

            thisDiskRatio = Decimal(lineValues[-1])
            if (thisDiskRatio > ioStatus['ratio']):
                ioStatus['ratio'] = thisDiskRatio

        endTime2 = datetime.datetime.now()
        duration = "%.1f" % ((endTime2 - endTime).total_seconds())
        ioStatus['lepvParsingDuration'] = duration

        responseData = {}
        responseData['data'] = ioStatus
        responseData['rawResult'] = rawResult
        return responseData

    def getCapacity(self):
        responseLines = self.client.getResponse("GetCmdDf")
        if (len(responseLines) == 0):
            return {}

        responseData = {}
        responseData['rawResult'] = responseLines[:]

        diskData = {}
        for resultLine in responseLines:
            if (not resultLine.startswith('/dev/')):
                continue

            lineValues = resultLine.split()
            diskName = lineValues[0][5:]
            diskData[diskName] = {}
            diskData[diskName]['size'] = lineValues[1]
            diskData[diskName]['used'] = lineValues[2]
            diskData[diskName]['free'] = lineValues[3]

            diskData['size'] = lineValues[1]
            diskData['used'] = lineValues[2]
            diskData['free'] = lineValues[3]

        capacity = {}
        capacity['diskTotal'] = diskData['size']
        capacity['diskUsed'] = diskData['used']

        responseData['data'] = capacity
        return responseData

    def getIoTopData(self):

        ioTopLines = self.client.getResponse('GetCmdIotop')
        ioTopResults = {}
        ioTopResults['topData'] = {}
        ioTopResults['rawResult'] = ioTopLines[:]

        dataLineStartingIndex = 0
        for line in ioTopLines:
            if (line.strip(
            ) == 'TID  PRIO  USER     DISK READ  DISK WRITE  SWAPIN      IO    COMMAND'
                ):
                break
            else:
                dataLineStartingIndex += 1

        while (dataLineStartingIndex >= 0):
            ioTopLines.pop(0)
            dataLineStartingIndex -= 1

        orderIndex = 1
        for line in ioTopLines:
            # print (line)
            if (line.strip() == ''):
                continue

            # find the 'M/s" or 'B/s', they are for disk read and write
            matches = re.findall('\s*\d+\.\d{2}\s*[G|M|B]\/s\s+', line)
            diskRead = matches[0].strip()
            diskWrite = matches[1].strip()

            # find the "0.00 %" occurrences, they are for swapin and io
            matches = re.findall('\s*\d+\.\d{2}\s*\%\s+', line)
            swapin = matches[0].strip()
            io = matches[1].strip()

            lineValues = line.split()
            pid = lineValues[0].strip()
            prio = lineValues[1].strip()
            user = lineValues[2].strip()

            lastPercentIndex = line.rfind('%')
            command = line[lastPercentIndex + 1:]

            ioTopItem = {}
            ioTopItem['TID'] = pid
            ioTopItem['PRIO'] = prio
            ioTopItem['USER'] = user
            ioTopItem['READ'] = diskRead
            ioTopItem['WRITE'] = diskWrite
            ioTopItem['SWAPIN'] = swapin
            ioTopItem['IO'] = io
            ioTopItem['COMMAND'] = command

            # use an incremental int as key, so we keey the order of the items.
            ioTopResults['topData'][orderIndex] = ioTopItem
            orderIndex += 1

        return ioTopResults

    def getIoPPData(self):

        ioTopLines = self.client.getResponse('GetCmdIopp')
        ioResults = {}
        ioResults['data'] = {}
        if (self.config == 'debug'):
            ioResults['rawResult'] = ioTopLines[:]

        headerLine = ioTopLines.pop(0)
        # TODO: validate the header column ordering here.

        # pid    rchar    wchar    syscr    syscw   rbytes   wbytes  cwbytes command
        # 1        0        0        0        0        0        0        0 init
        # 2        0        0        0        0        0        0        0 kthreadd
        # 3        0        0        0        0        0        0        0 ksoftirqd/0
        # 5        0        0        0        0        0        0        0 kworker/0:0H
        orderIndex = 1
        for line in ioTopLines:
            # print (line)
            if (line.strip() == ''):
                continue

            lineValues = line.split()

            ioTopItem = {}
            ioTopItem['pid'] = lineValues.pop(0)
            ioTopItem['rchar'] = lineValues.pop(0)
            ioTopItem['wchar'] = lineValues.pop(0)
            ioTopItem['syscr'] = lineValues.pop(0)
            ioTopItem['syscw'] = lineValues.pop(0)
            ioTopItem['rbytes'] = lineValues.pop(0)
            ioTopItem['wbytes'] = lineValues.pop(0)
            ioTopItem['cwbytes'] = lineValues.pop(0)
            ioTopItem['command'] = ' '.join([str(x) for x in lineValues])

            # use an incremental int as key, so we keey the order of the items.
            ioResults['data'][orderIndex] = ioTopItem
            orderIndex += 1

        return ioResults
예제 #13
0
class MemoryMonitor:

    def __init__(self, server, config='release'):
        self.server = server
        self.client = LepDClient(self.server)
        self.config = config
        
        self.dataCount = 25

    def convertKbToMb(self, strKbValue):
        return Decimal(int(strKbValue) / 1024).quantize(Decimal('0'))

    def getStatus(self):

        response = self.client.getResponse("GetProcMeminfo")
        if (response == None or len(response) == 0):
            return None

        responseData = {}
        if (self.config == 'debug'):
            responseData['rawResult'] = response[:]
        
        results = {}
        for line in response:
            linePairs = line.split(":")
            lineKey = linePairs[0].strip()
            lineValue = linePairs[1].replace('kB', '').strip()

            results[lineKey] = lineValue

        componentInfo = {}
        componentInfo["name"] = "memory"

        componentInfo['total'] = Decimal(int(results['MemTotal']) / 1024).quantize(Decimal('0'))
        componentInfo['free'] = Decimal(int(results['MemFree']) / 1024).quantize(Decimal('0'))
        componentInfo['buffers'] = Decimal(int(results['Buffers']) / 1024).quantize(Decimal('0'))
        componentInfo['cached'] = Decimal(int(results['Cached']) / 1024).quantize(Decimal('0'))
        componentInfo['used'] = componentInfo['total'] - componentInfo['free'] - componentInfo['buffers'] - componentInfo['cached']

        usedRatio = (componentInfo['used'] / componentInfo['total']) * 100
        #usedRatio = Decimal(usedRatio).quantize(Decimal('0.00'))
        usedRatio = ("%.2f" % usedRatio)
        componentInfo["ratio"] = usedRatio

        componentInfo['unit'] = 'MB'
        
        responseData['data'] = componentInfo
        return responseData

    def getCapacity(self, sampleDataLines = None):
        responseLines = []
        if (sampleDataLines == None):
            responseLines = self.client.getResponse("GetProcMeminfo")
        else:
            responseLines = sampleDataLines
            
        if (len(responseLines) == 0):
            return {}
        
        responseData = {}
        if (self.config == 'debug'):
            responseData['rawResult'] = responseLines[:]

        results = {}
        for line in responseLines:
            print(line)
            if (self.client.LEPDENDINGSTRING in line):
                continue
                
            linePairs = line.split(":")
            lineKey = linePairs[0].strip()
            lineValue = linePairs[1].replace('kB', '').strip()

            results[lineKey] = lineValue

        componentInfo = {}
        componentInfo["name"] = "memory"
        componentInfo["capacity"] = int(int(results['MemTotal']) / 1024)
        componentInfo["unit"] = "MB"
        
        componentInfo["summary"] = str(componentInfo["capacity"]) + " " + componentInfo["unit"]
        
        responseData['data'] = componentInfo
        return responseData

    def normalizeValue(self, valueString):
        # 1.23% -> 0.012
        if (valueString.endswith("%")):
            valueString = valueString.replace("%", "")
            valueString = Decimal(Decimal(valueString) / 100).quantize(Decimal('0.0000'))
        elif (valueString == "N/A"):
            valueString = 0
        
        return valueString
        
    # def getMemoryStat(self):
    # 
    #     memoryStatData = {}
    #     
    #     results = self.client.getResponse('GetCmdSmem')
    #     if (self.config == 'debug'):
    #         memoryStatData['rawResult'] = results[:]
    # 
    #     headerLine = results.pop(0)
    #     headers = headerLine.split()
    #     
    #     # sMemInfo['headerLine'] = headerLine
    #     for line in results:
    #         # print(line)
    #         lineValues = line.split()
    # 
    #         pid = lineValues[0]
    #         memoryStatData[pid] = {}
    #         # sMemInfo['line'] = line
    #         memoryStatData[pid]['pid'] = lineValues.pop(0)
    #         memoryStatData[pid]['user'] = lineValues.pop(0)
    # 
    #         # the command section is likely to have whitespaces in it thus hard to locate it. workaround here.
    #         memoryStatData[pid]['rss'] = self.normalizeValue(lineValues.pop())
    #         memoryStatData[pid]['pss'] = self.normalizeValue(lineValues.pop())
    #         memoryStatData[pid]['uss'] = self.normalizeValue(lineValues.pop())
    #         memoryStatData[pid]['swap'] = self.normalizeValue(lineValues.pop())
    # 
    #         memoryStatData[pid]['command'] = ' '.join([str(x) for x in lineValues])
    # 
    #         if(len(memoryStatData) >= self.dataCount):
    #             break
    # 
    #     return

    def getProcrank(self):

        procrankData = {}

        resultLines = self.client.getResponse('GetCmdProcrank')
        if (len(resultLines) == 0):
            return {}
        
        if (self.config == 'debug'):
            procrankData['rawResult'] = resultLines[:]

        procrankData['data'] = {}
        procrankData['data']['procranks'] = {}
        headerLine = resultLines.pop(0)
        lineIndex = 0
        
        for line in resultLines:
            if (re.match( r'\W+-+\W+-+\W-+.*', line, re.M|re.I)):
                break
            lineValues = line.split()

            procrankData['data']['procranks'][lineIndex] = {}
            procrankData['data']['procranks'][lineIndex]['pid'] = lineValues.pop(0)
            procrankData['data']['procranks'][lineIndex]['vss'] = Decimal(Decimal(lineValues.pop(0)[:-1]))
            procrankData['data']['procranks'][lineIndex]['rss'] = Decimal(Decimal(lineValues.pop(0)[:-1]))
            procrankData['data']['procranks'][lineIndex]['pss'] = Decimal(Decimal(lineValues.pop(0)[:-1]))
            procrankData['data']['procranks'][lineIndex]['uss'] = Decimal(Decimal(lineValues.pop(0)[:-1]))

            procrankData['data']['procranks'][lineIndex]['cmdline'] = ' '.join([str(x) for x in lineValues])
            
            lineIndex += 1

            if(len(procrankData) >= self.dataCount):
                break
        
        # now parse from end, which contains summary info
        lastLine = resultLines[-1]
        procrankData['data']['sum'] = {}
        if (lastLine.startswith('RAM:')):
            lastLine = lastLine.replace("RAM:", '')
            lastLineValuePairs = lastLine.split(", ")
            for valuePair in lastLineValuePairs:
                keyValuePair = valuePair.split()
                
                keyName = keyValuePair[1].strip()
                keyValue = keyValuePair[0].strip()

                procrankData['data']['sum'][keyName + "Unit"] = keyValue[-1:]
                procrankData['data']['sum'][keyName] = Decimal(Decimal(keyValue[:-1]))

        xssSumLine = resultLines[-3].strip()
        if (xssSumLine.endswith('TOTAL')):
            xssValues = xssSumLine.split()
            
            ussTotalString = xssValues[-2]
            procrankData['data']['sum']['ussTotalUnit'] = ussTotalString[-1:]
            procrankData['data']['sum']['ussTotal'] = Decimal(Decimal(ussTotalString[:-1]))
            
            pssTotalString = xssValues[-3]
            procrankData['data']['sum']['pssTotalUnit'] = pssTotalString[-1:]
            procrankData['data']['sum']['pssTotal'] = Decimal(Decimal(pssTotalString[:-1]))
            
        return procrankData
예제 #14
0
class CPUMonitor:
    def __init__(self, server, config='release'):
        self.server = server
        self.client = LepDClient(self.server)
        self.config = config
        # print("cpu monitor construction done")

    def getCpuInfoForArm(self, lines):

        results = {}

        line = lines.pop(0)
        results['architecture'] = "ARM"
        results['model name'] = line.split(':')[1].strip()
        results['processors'] = {}

        line = lines.pop(0)
        while (not line.startswith("Features")):
            if (line.startswith("processor")):

                processorId = line.split(":")[1].strip()
                results['processors'][processorId] = {}

                bogoMips = lines.pop(0).split(":")[1].strip()
                results['processors'][processorId]["processorId"] = processorId
                results['processors'][processorId]["bogomips"] = bogoMips

            line = lines.pop(0)

        return results

    def getCpuInfoForX86(self, lines):

        results = {}
        results['architecture'] = "X86"
        results['processors'] = {}

        for line in lines:
            if (line.strip() == ""):
                continue

            if re.match(r'processor\W+:\W+\d.*', line, re.M | re.I):
                linePairs = line.split(":")
                processorId = linePairs[1].strip()
                results['processors'][processorId] = {}
                continue

            if (":" in line):
                linePairs = line.split(":")
                lineKey = linePairs[0].strip()
                lineValue = ''
                if (len(linePairs) > 1):
                    lineValue = linePairs[1].strip()

                results['processors'][processorId][lineKey] = lineValue

        return results

    def getCpuInfo(self):

        cpuInfoLines = self.client.getResponse('GetProcCpuinfo')
        responseData = {}
        if (self.config == 'debug'):
            responseData['rawResult'] = cpuInfoLines

        firstLine = cpuInfoLines[0]
        if ("ARM" in firstLine):
            responseData['data'] = self.getCpuInfoForArm(cpuInfoLines)
        else:
            responseData['data'] = self.getCpuInfoForX86(cpuInfoLines)

        return responseData

    def getCapacity(self):

        cpuInfoData = self.getCpuInfo()

        if (not cpuInfoData):
            return {}

        responseData = {}
        if (self.config == 'debug'):
            responseData['rawResult'] = cpuInfoData['rawResult']

        capacity = {}
        capacity['processors'] = cpuInfoData['data']['processors']

        coresString = 'Core'
        coreCount = len(cpuInfoData['data']['processors'])
        capacity['coresCount'] = coreCount

        if (coreCount > 1):
            coresString = "Cores"

        for processorId, processorData in cpuInfoData['data'][
                'processors'].items():

            if (cpuInfoData['data']['architecture'] == "ARM"):
                processorData['model'] = cpuInfoData['data']['model name']

                # Summary is a string to briefly describe the CPU, like "2GHZ x 2", meaning it's a 2-core cpu with 2GHZ speed.
                capacity['summary'] = processorData[
                    'bogomips'] + " MHz x " + str(coreCount) + coresString
                capacity['model'] = processorData['model']
                capacity['bogomips'] = processorData['bogomips']
                capacity['architecture'] = 'ARM'

            else:
                modelName = processorData['model name'].replace("(R)",
                                                                "").replace(
                                                                    " CPU", "")
                if (" @" in modelName):
                    modelName = modelName[0:modelName.find(" @")]
                processorData['model'] = modelName

                processorSpeed = Decimal(processorData['cpu MHz']).quantize(
                    Decimal('0'))

                # Summary is a string to briefly describe the CPU, like "2GHZ x 2", meaning it's a 2-core cpu with 2GHZ speed.
                capacity['summary'] = str(processorSpeed) + " MHz x " + str(
                    coreCount) + coresString
                capacity['model'] = modelName
                capacity['bogomips'] = processorData['bogomips']
                capacity['architecture'] = 'X86'

            break

        responseData['data'] = capacity
        return responseData

    def getStatus(self):

        statData = self.getStat()
        allIdleRatio = Decimal(statData['data']['all']['idle'])

        componentInfo = {}
        componentInfo["name"] = "cpu"
        componentInfo["ratio"] = 100 - allIdleRatio
        componentInfo['server'] = self.server

        componentInfo['rawResult'] = statData['rawResult']

        return componentInfo

    def getStat(self):

        results = self.client.getCmdMpStat()
        if (results == None):
            return None

        statData = {}

        results.pop(0)
        statData['rawResult'] = results

        statData['data'] = {}
        for line in results:

            if (line.strip() == ''):
                break

            lineValues = line.split()

            cpuStat = {}
            cpuStat['idle'] = lineValues[-1]
            cpuStat['gnice'] = lineValues[-2]
            cpuStat['guest'] = lineValues[-3]
            cpuStat['steal'] = lineValues[-4]
            cpuStat['soft'] = lineValues[-5]
            cpuStat['irq'] = lineValues[-6]
            cpuStat['iowait'] = lineValues[-7]
            cpuStat['system'] = lineValues[-8]
            cpuStat['nice'] = lineValues[-9]
            cpuStat['user'] = lineValues[-10]
            cpuStat['name'] = lineValues[-11]

            cpuName = lineValues[-11]
            statData['data'][cpuName] = cpuStat

        statData['server'] = self.server

        return statData

    def getAverageLoad(self):
        responseLines = self.client.getResponse('GetProcLoadavg')

        responseData = {}
        responseData['rawResult'] = responseLines[:]

        response = responseLines[0].split(" ")

        # '0.00 0.01 0.05 1/103 24750
        # 'avg system load of 1 minute ago, 5 minutes ago, 15 minutes ago,
        # the fourth is A/B, A is the number of running processes
        # B is the total process count.
        # last number, like 24750 is the ID of the most recently running process.
        resultData = {}
        resultData['last1'] = Decimal(response[0])
        resultData['last5'] = Decimal(response[1])
        resultData['last15'] = Decimal(response[2])

        responseData['data'] = resultData

        return responseData

    def getTopOutput(self):

        responseLines = self.client.getResponse("GetCmdTop")
        if (len(responseLines) == 0):
            return {}

        responseData = {}
        responseData['rawResult'] = responseLines[:]

        headerLine = responseLines.pop(0)

        result = {}
        for responseLine in responseLines:
            # print(responseLine)
            lineValues = responseLine.split()

            pid = lineValues[0]
            result[pid] = {}

            result[pid]['pid'] = pid
            result[pid]['user'] = lineValues[1]
            result[pid]['pri'] = lineValues[2]
            result[pid]['ni'] = lineValues[3]
            result[pid]['vsz'] = lineValues[4]
            result[pid]['rss'] = lineValues[5]
            result[pid]['s'] = lineValues[6]
            result[pid]['cpu'] = lineValues[7]
            result[pid]['mem'] = lineValues[8]
            result[pid]['time'] = lineValues[9]

            result[pid]['command'] = ' '.join(
                [str(x) for x in lineValues[10:]])

            if (len(result) >= 25):
                break

        responseData['data'] = result
        return responseData

    def getTopHResult(self):
        response = self.client.getTopHResult()
        return response

    def getCpuByName(self, name):
        response = self.client.getTopOutput()

        for line in response:
            if name in line:
                pid = line.strip().split(" ")[0].strip()
                cpu = re.search(" .\.. ", line).group().strip()
                return (pid, cpu)
        return None

    def getCpuByPid(self, pid):
        response = self.client.getTopOutput()

        for line in response:
            if pid == line.strip().split(" ")[0].strip():
                name = re.split(":.+ ", line, 1)[1].strip()
                cpu = re.search(" .\.. ", line).group().strip()
                return (name, cpu)
        return None

    def unitTestGetProcCpuinfo(self):
        pass

    def unitTestGetCmdMpstat(self):
        pass

    def unitTestGetCmdTop(self):
        pass

    def unitTestGetProcLoadavg(self):
        pass

    def unitTests(self):
        self.unitTestGetProcCpuinfo()
        self.unitTestGetCmdMpstat()
        self.unitTestGetCmdTop()
        self.unitTestGetProcLoadavg()
예제 #15
0
class CPUMonitor:
    def __init__(self, server, config='release'):
        self.server = server
        self.client = LepDClient(self.server)
        self.config = config

        # this maxDataCount should match the one defined for UI.
        self.maxDataCount = 25

    def getCpuInfoForArm(self, lines):

        results = {}

        line = lines.pop(0)
        results['architecture'] = "ARM"
        results['model name'] = line.split(':')[1].strip()
        results['processors'] = {}

        line = lines.pop(0)
        while (not line.startswith("Features")):
            if (line.startswith("processor")):

                processorId = line.split(":")[1].strip()
                results['processors'][processorId] = {}

                bogoMips = lines.pop(0).split(":")[1].strip()
                results['processors'][processorId]["processorId"] = processorId
                results['processors'][processorId]["bogomips"] = bogoMips

            line = lines.pop(0)

        return results

    def getCpuInfoForArmArch64(self, lines):

        results = {}

        line = lines.pop(0)
        results['architecture'] = "ARM"

        results['model name'] = line.split(":")[1].strip()
        results['processors'] = {}

        line = lines.pop(0)
        while (not line.startswith("Features")):
            if (line.startswith("processor")):

                processorId = line.split(":")[1].strip()
                results['processors'][processorId] = {}
                results['processors'][processorId]["processorId"] = processorId
                results['processors'][processorId]["bogomips"] = ''

            line = lines.pop(0)

        return results

    def getCpuInfoForX86(self, lines):

        results = {}
        results['architecture'] = "X86"
        results['processors'] = {}

        for line in lines:
            if (line.strip() == ""):
                continue

            if re.match(r'processor\W+:\W+\d.*', line, re.M | re.I):
                linePairs = line.split(":")
                processorId = linePairs[1].strip()
                results['processors'][processorId] = {}
                continue

            if (":" in line):
                linePairs = line.split(":")
                lineKey = linePairs[0].strip()
                lineValue = ''
                if (len(linePairs) > 1):
                    lineValue = linePairs[1].strip()

                results['processors'][processorId][lineKey] = lineValue

        return results

    def getCpuInfo(self, cpuInfoLines=None):

        if (cpuInfoLines == None):
            cpuInfoLines = self.client.getResponse('GetProcCpuinfo')

        responseData = {}
        if (self.config == 'debug'):
            responseData['rawResult'] = cpuInfoLines

        firstLine = cpuInfoLines[0]
        if ("ARM" in firstLine):
            responseData['data'] = self.getCpuInfoForArm(cpuInfoLines)
        elif ('AArch64' in firstLine):
            responseData['data'] = self.getCpuInfoForArmArch64(cpuInfoLines)
        else:
            secondLine = cpuInfoLines[1]
            responseData['data'] = self.getCpuInfoForX86(cpuInfoLines)
            if ('GenuineIntel' not in secondLine):
                responseData['data']['architecture'] = 'ARM'

        responseData['data']['processorCount'] = 0
        for line in cpuInfoLines:
            if re.match(r'\W*processor\W*:\W*\d+', line, re.M | re.I):
                responseData['data']['processorCount'] += 1

        return responseData

    def getProcessorCount(self, cpuInfoLines=None):

        if (cpuInfoLines == None):
            cpuInfoLines = self.client.getResponse('GetCpuInfo')

        responseData = {}
        for line in cpuInfoLines:
            if line.startswith('cpunr'):
                responseData['count'] = int(line.split(":")[1].strip())
                break

        if ('count' not in responseData):
            print('failed in getting processor count by GetCpuInfo')
            print(cpuInfoLines)

        return responseData

    def getCapacity(self):

        cpuInfoData = self.getCpuInfo()

        if (not cpuInfoData):
            return {}

        responseData = {}
        if (self.config == 'debug'):
            responseData['rawResult'] = cpuInfoData['rawResult']

        capacity = {}
        capacity['processors'] = cpuInfoData['data']['processors']

        coresString = 'Core'
        coreCount = len(cpuInfoData['data']['processors'])
        capacity['coresCount'] = coreCount

        if (coreCount > 1):
            coresString = "Cores"

        for processorId, processorData in cpuInfoData['data'][
                'processors'].items():

            if (cpuInfoData['data']['architecture'] == "ARM"):
                if ('model name' in cpuInfoData['data']):
                    processorData['model'] = cpuInfoData['data']['model name']
                else:
                    processorData['model'] = ''

                # Summary is a string to briefly describe the CPU, like "2GHZ x 2", meaning it's a 2-core cpu with 2GHZ speed.
                if ('bogomips' not in processorData):
                    capacity['bogomips'] = ''
                    capacity['summary'] = ''
                else:
                    capacity['bogomips'] = processorData['bogomips']
                    capacity['summary'] = processorData[
                        'bogomips'] + " MHz x " + str(coreCount) + coresString

                capacity['model'] = processorData['model']
                capacity['architecture'] = 'ARM'

            else:
                modelName = processorData['model name'].replace("(R)",
                                                                "").replace(
                                                                    " CPU", "")
                if (" @" in modelName):
                    modelName = modelName[0:modelName.find(" @")]
                processorData['model'] = modelName

                processorSpeed = Decimal(processorData['cpu MHz']).quantize(
                    Decimal('0'))

                # Summary is a string to briefly describe the CPU, like "2GHZ x 2", meaning it's a 2-core cpu with 2GHZ speed.
                capacity['summary'] = str(processorSpeed) + " MHz x " + str(
                    coreCount) + coresString
                capacity['model'] = modelName
                capacity['bogomips'] = processorData['bogomips']
                capacity['architecture'] = 'X86'

            break

        responseData['data'] = capacity
        return responseData

    def getStatus(self):

        statData = self.getStat()
        allIdleRatio = self.client.toDecimal(statData['data']['all']['idle'])

        componentInfo = {}
        componentInfo["name"] = "cpu"
        componentInfo["ratio"] = 100 - allIdleRatio
        componentInfo['server'] = self.server

        if (self.config == 'debug'):
            componentInfo['rawResult'] = statData['rawResult']

        return componentInfo

    def getStat(self):

        results = self.client.getCmdMpStat()
        if (results == None):
            return None

        statData = {}

        results.pop(0)
        statData['rawResult'] = results

        statData['data'] = {}
        for line in results:

            if (line.strip() == ''):
                break

            lineValues = line.split()

            cpuStat = {}
            cpuStat['idle'] = self.client.toDecimal(lineValues[-1])
            cpuStat['gnice'] = self.client.toDecimal(lineValues[-2])
            cpuStat['guest'] = self.client.toDecimal(lineValues[-3])
            cpuStat['steal'] = self.client.toDecimal(lineValues[-4])
            cpuStat['soft'] = self.client.toDecimal(lineValues[-5])
            cpuStat['irq'] = self.client.toDecimal(lineValues[-6])
            cpuStat['iowait'] = self.client.toDecimal(lineValues[-7])
            cpuStat['system'] = self.client.toDecimal(lineValues[-8])
            cpuStat['nice'] = self.client.toDecimal(lineValues[-9])
            cpuStat['user'] = self.client.toDecimal(lineValues[-10])

            cpuName = lineValues[-11]
            statData['data'][cpuName] = cpuStat

        statData['server'] = self.server

        return statData

    def getAverageLoad(self):
        responseLines = self.client.getResponse('GetProcLoadavg')

        responseData = {}
        if (self.config == 'debug'):
            responseData['rawResult'] = responseLines[:]

        response = responseLines[0].split(" ")

        # '0.00 0.01 0.05 1/103 24750
        # 'avg system load of 1 minute ago, 5 minutes ago, 15 minutes ago,
        # the fourth is A/B, A is the number of running processes
        # B is the total process count.
        # last number, like 24750 is the ID of the most recently running process.
        resultData = {}
        resultData['last1'] = self.client.toDecimal(response[0])
        resultData['last5'] = self.client.toDecimal(response[1])
        resultData['last15'] = self.client.toDecimal(response[2])

        responseData['data'] = resultData

        return responseData

    def getTopOutput(self, responseLines=None):

        if (responseLines == None):
            responseLines = self.client.getResponse('GetCmdTop')

        if (len(responseLines) == 0):
            return {}

        responseData = {}
        if (self.config == 'debug'):
            responseData['rawResult'] = responseLines[:]

        headerLine = responseLines.pop(0)
        while (not re.match(r'\W*PID\W+USER\W+.*', headerLine, re.M | re.I)):
            headerLine = responseLines.pop(0)

        headerColumns = headerLine.split()

        result = {}

        for lineIndex, responseLine in enumerate(responseLines):
            if (self.client.LEPDENDINGSTRING in responseLine):
                break

            if (lineIndex > self.maxDataCount):
                break

            lineValues = responseLine.split()

            result[lineIndex] = {}

            # print(headerLine)
            for columnIndex, columnName in enumerate(headerColumns):
                if (columnName == 'Name' or columnName == 'CMD'):
                    result[lineIndex][columnName] = ' '.join(
                        [str(x) for x in lineValues[columnIndex:]])
                else:
                    result[lineIndex][columnName] = lineValues[columnIndex]

        responseData['data'] = {}
        responseData['data']['top'] = result
        responseData['data']['headerline'] = headerLine

        if (re.match(r'\W*PID\W+USER\W+PR\W+.*', headerLine, re.M | re.I)):
            # android :
            #   PID USER     PR  NI CPU% S  #THR     VSS     RSS PCY Name
            responseData['data']['os'] = 'android'
        elif (re.match(r'\W*PID\W+USER\W+PRI\W+NI\W+VSZ\W+RSS\W+.*',
                       headerLine, re.M | re.I)):
            # for Linux:
            # PID USER     PRI  NI    VSZ   RSS S %CPU %MEM     TIME CMD
            responseData['data']['os'] = 'linux'
        else:
            print("GetCmdTop command returned data from unrecognized system")

        return responseData
예제 #16
0
class IOMonitor:
    def __init__(self, server, config='release'):
        self.server = server
        self.client = LepDClient(self.server)
        self.config = config

    def getStatus(self):

        startTime = datetime.datetime.now()

        result = self.client.getIostatResult()

        endTime = datetime.datetime.now()

        rawResult = result[:]

        headerLine = result.pop(0)

        duration = "%.1f" % ((endTime - startTime).total_seconds())
        ioStatus = {}
        ioStatus['lepdDuration'] = duration
        ioStatus['disks'] = {}
        ioStatus['diskCount'] = 0
        ioStatus['ratio'] = 0
        for line in result:
            if (line.strip() == ""):
                continue

            lineValues = line.split()

            deviceName = lineValues[0]
            ioStatus['diskCount'] += 1
            ioStatus['disks'][deviceName] = {}

            ioStatus['disks'][deviceName]['rkbs'] = lineValues[5]
            ioStatus['disks'][deviceName]['wkbs'] = lineValues[6]
            ioStatus['disks'][deviceName]['ratio'] = lineValues[-1]

            thisDiskRatio = self.client.toDecimal(lineValues[-1])
            if (thisDiskRatio > ioStatus['ratio']):
                ioStatus['ratio'] = thisDiskRatio

        endTime2 = datetime.datetime.now()
        duration = "%.1f" % ((endTime2 - endTime).total_seconds())
        ioStatus['lepvParsingDuration'] = duration

        responseData = {}
        responseData['data'] = ioStatus
        responseData['rawResult'] = rawResult
        return responseData

    def getCapacity(self):
        responseLines = self.client.getResponse("GetCmdDf")
        if (len(responseLines) == 0):
            return {}

        responseData = {}
        if (self.config == 'debug'):
            responseData['rawResult'] = responseLines[:]

        diskData = {}
        for resultLine in responseLines:
            if (not resultLine.startswith('/dev/')):
                continue

            lineValues = resultLine.split()
            diskName = lineValues[0][5:]
            diskData[diskName] = {}
            diskData[diskName]['size'] = lineValues[1]
            diskData[diskName]['used'] = lineValues[2]
            diskData[diskName]['free'] = lineValues[3]

            diskData['size'] = lineValues[1]
            diskData['used'] = lineValues[2]
            diskData['free'] = lineValues[3]

        capacity = {}
        capacity['diskTotal'] = diskData['size']
        capacity['diskUsed'] = diskData['used']

        responseData['data'] = capacity
        return responseData

    def getIoTopData(self, ioTopLines=None):

        if (ioTopLines == None):
            ioTopLines = self.client.getResponse('GetCmdIotop')

        ioTopResults = {}
        ioTopResults['data'] = {}
        ioTopResults['rawResult'] = ioTopLines[:]

        dataLineStartingIndex = 0
        for line in ioTopLines:
            if (re.match(
                    r'\W*TID\W+PRIO\W+USER\W+DISK READ\W+DISK WRITE\W+SWAPIN\W+IO\W+COMMAND\W*',
                    line.strip(), re.M | re.I)):
                break
            else:
                dataLineStartingIndex += 1

        while (dataLineStartingIndex >= 0):
            ioTopLines.pop(0)
            dataLineStartingIndex -= 1

        orderIndex = 1
        for line in ioTopLines:
            # print (line)
            if (line.strip() == ''):
                continue

            if (self.client.LEPDENDINGSTRING in line):
                break

            # find the 'M/s" or 'B/s', they are for disk read and write
            matches = re.findall('\s*\d+\.\d{2}\s*[G|M|B]\/s\s+', line)
            diskRead = matches[0].strip()
            diskWrite = matches[1].strip()

            # find the "0.00 %" occurrences, they are for swapin and io
            matches = re.findall('\s*\d+\.\d{2}\s*\%\s+', line)
            swapin = matches[0].strip()
            io = matches[1].strip()

            lineValues = line.split()
            pid = lineValues[0].strip()
            prio = lineValues[1].strip()
            user = lineValues[2].strip()

            lastPercentIndex = line.rfind('%')
            command = line[lastPercentIndex + 1:]

            ioTopItem = {}
            ioTopItem['TID'] = pid
            ioTopItem['PRIO'] = prio
            ioTopItem['USER'] = user
            ioTopItem['READ'] = diskRead
            ioTopItem['WRITE'] = diskWrite
            ioTopItem['SWAPIN'] = swapin
            ioTopItem['IO'] = io
            ioTopItem['COMMAND'] = command

            # use an incremental int as key, so we keey the order of the items.
            ioTopResults['data'][orderIndex] = ioTopItem
            orderIndex += 1

        return ioTopResults