def __str__(self): msg = "PID: {}\n Name: {}\n UserName: {}\n inodeNumber: {}\n User Mode:\tprev:{}\tcurr:{}\n Sys Mode:\tprev:{}\tcurr:{}\n Vmem Size:\tprev:{}\tcurr:{}\n Rss:\tprev:{}\tcurr:{}\n Time:\tprev:{}\tcurr:{}\n".format( self.pid, self.name, self.userName, self.inodeNumber, self.userMode["prev"], self.userMode["curr"], self.sysMode["prev"], self.sysMode["curr"], self.vmem["prev"], self.vmem["curr"], self.rss["prev"], self.rss["curr"], round2(self.time["prev"]), round2(self.time["curr"])) return msg
def __str__(self): msg = "Name: {}\n Bytes in\tprev:{}\tcurr:{}\n Bytes out\tprev:{}\tcurr:{}\n Network Bandwidth\tprev:{}\tcurr:{}\n Average Utilization:\t{} Bytes per sec\n Read Time\tprev:{}\tcurr:{}\n".format( self.name, self.bytesIn["prev"], self.bytesIn["curr"], self.bytesOut["prev"], self.bytesOut["curr"], self.networkBandwidth["prev"], self.networkBandwidth["curr"], round2(self.calculateAveragePerSecond()), round2(self.time["prev"]), round2(self.time["curr"])) return msg
def toJSON(self): data = {} data['name'] = self.name data['bytesIn'] = round2(self.calculateDelta(self.bytesIn)) data['bytesOut'] = round2(self.calculateDelta(self.bytesOut)) data['networkBandwidth'] = round2( self.calculateAverage(self.networkBandwidth)) data['networkUtilization'] = round2(self.calculateAveragePerSecond()) json_data = json.dumps(data) return json_data
def toJSON(self): freqs = self.calculateFrequencies() data = {} data['name'] = self.name data['diskReads'] = round2(freqs["diskRead"]) data['diskWrites'] = round2(freqs["diskWrite"]) data['blockReads'] = round2(freqs["blockRead"]) data['blockWrites'] = round2(freqs["blockWrite"]) json_data = json.dumps(data) return json_data
def calculatePhyMemUtil(self, phyMemTotal): try: #delta_phyMem = int(self.rss["curr"]) - int(self.rss["prev"]) delta_phyMem = int(self.rss["curr"]) totalUtil = (delta_phyMem / int(phyMemTotal)) * 100 return round2(totalUtil) except: return 0
def calculateAveragePerSecond(self): try: timeInterval = self.calculateDelta(self.time) avg = ((self.calculateDelta(self.bytesIn)) + (self.calculateDelta(self.bytesOut))) / timeInterval return round2(avg) except: print("Error average network utilization per second") return 0
def calculateCpuUtilization(self, sysWidetime): delta_userMode = int(self.userMode["curr"]) - int( self.userMode["prev"]) delta_sysMode = int(self.sysMode["curr"]) - int(self.sysMode["prev"]) userModeUtil = 0 sysModeUtil = 0 totalUtil = 0 try: userModeUtil = (delta_userMode / sysWidetime) * 100 sysModeUtil = (delta_sysMode / sysWidetime) * 100 totalUtil = ((delta_sysMode + delta_userMode) / sysWidetime) * 100 except ZeroDivisionError: pass return { "userMode": round2(userModeUtil), "sysMode": round2(sysModeUtil), "total": round2(totalUtil) }
def calculateVMemAvg(self): """ return vmem persec in bytes """ try: timeInterval = int(self.time["curr"]) - int(self.time["prev"]) avg_vmem = (int(self.vmem["curr"]) + int(self.vmem["prev"])) / 2 avgVMem = (avg_vmem / timeInterval) return round2(avgVMem) except: #print("Error calculation vmem", vMemTotal, self.vmem) return 0
def calculateVMemUtil(self, vMemTotal): """ return vmem persec in bytes """ try: """ #this calculated vmem % per second delta_vmem = int(self.vmem["curr"]) - int(self.vmem["prev"]) totalUtil = (delta_vmem/int(vMemTotal))*100 return round2(totalUtil)""" timeInterval = int(self.time["curr"]) - int(self.time["prev"]) delta_vmem = int(self.vmem["curr"]) - int(self.vmem["prev"]) totalUtil = (delta_vmem / timeInterval) return round2(totalUtil) except: #print("Error calculation vmem", vMemTotal, self.vmem) return 0
def parseInfo(memFile, readTime): """ This function parses the contents of /proc/meminfo and returns the memory information in a dictionary. Parameters: memFile (str): The contents of /proc/meminfo Returns list: A list that holds the available memory as a BasicGauge object, total memory in MB, and the memory utilization in percentage. """ global memTotal if not memTotal: try: # get the MemTotal value and convert it to MB from kB memTotal = float(re.findall(r'MemTotal: .*', memFile)[0].split(" ")[-2]) memTotal = memTotal/1024 except: print("Error: Unable to retrieve MemTotal") memTotal = None try: # get the MemAvailable value and convert it to MB from kB avail = float(re.findall(r'MemAvailable: .*', memFile)[0].split(" ")[-2]) avail = avail/1024 memUsed.updateAll(memTotal-avail, readTime) except: print("Error: Unable to retrieve memAvail") try: # calculate memory utilization during the time interval memUtil = round2((memUsed.calculateAverage()/memTotal)*100) except: print("Error: Unable to calculate memUtil") memUtil = 0 return [memTotal, memUsed, memUtil]
def __str__(self): freqs = self.calculateFrequencies() msg = "Name: {}\n Disk Read\tprev:{}\tcurr:{}\tfreq:{}/s\n Disk Write\tprev:{}\tcurr:{}\tfreq:{}/s\n Block Read\tprev:{}\tcurr:{}\tfreq:{}/s\n Block Write\tprev:{}\tcurr:{}\tfreq:{}/s\n Read Time\tprev:{}\tcurr:{}\n".format( self.name, self.diskRead["prev"], self.diskRead["curr"], round2(freqs["diskRead"]), self.diskWrite["prev"], self.diskWrite["curr"], round2(freqs["diskWrite"]), self.blockRead["prev"], self.blockRead["curr"], round2(freqs["blockRead"]), self.blockWrite["prev"], self.blockWrite["curr"], round2(freqs["blockWrite"]), round2(self.time["prev"]), round2(self.time["curr"]) ) return msg
def updateDataThread(): try: while (True): eel.sleep(TIME_INTERVAL) """ ********************************************************* Memory Stats TYPES: memTotal:float memUsed:BasicGauge memUtil:float ********************************************************* """ memTotal, memUsed, memUtil = memStats.fetchAll() """ eel.setMemoryTotal(round2(memTotal)) eel.setMemoryAvailable(round2(memUsed.calculateAverage())) eel.setMemoryUtilization(round2(memUtil)) """ eel.setMemoryStats([ round2(memTotal), round2(memUsed.calculateAverage()), round2(memUtil) ]) """ ********************************************************* Disk Stats TYPES diskList:list of disk.Disk ********************************************************* """ diskList = diskStats.fetchAll() diskDictList = [] for eachDisk in diskList: diskDictList.append(eachDisk.toJSON()) eel.setDiskStats(diskDictList) """ ********************************************************* Cpu Stats TYPES intr:BasicCounter ctxt:BasicCounter cpuList:list of cpu.Cpu ********************************************************* """ intr, ctxt, cpuList, _ = cpuStats.fetchAll() eel.setIntr(round2(intr.calculateFrequencies())) eel.setCtxt(round2(ctxt.calculateFrequencies())) cpuDictList = [] for eachCpu in cpuList: cpuDictList.append(eachCpu.toJSON()) eel.setCpuStats(cpuDictList) """ ********************************************************* Process Stats TYPES processDict:dictionary of process.Process ********************************************************* """ processStats.setSysWideCpuTime(cpuStats.getSystemWideCpuTime()) processStats.setPhyMemTotal(int(memStats.getMemTotal())) processDictList = processStats.toJSON() eel.setProcesses(processDictList) """ ********************************************************* Network Stats TYPES networkDeviceList:list of networkDevice.NetworkDevice ********************************************************* """ networkDeviceList = netStats.fetchAll() networkDictList = [] for eachNetworkDevice in networkDeviceList: networkDictList.append(eachNetworkDevice.toJSON()) eel.setNetworkStats(networkDictList) """ ********************************************************* TCP/UDP Stats TYPES tcp:list of connection.Connection udp:list of connection.Connection establishedTcp:int ********************************************************* """ tcpUdpConnStats.updateInodeDict(processStats.getInodeDict()) tcp, udp, establishedTcp = tcpUdpConnStats.fetchAll() tcpDictList = [] for eachTcpConn in tcp: tcpDictList.append(eachTcpConn.toJSON()) eel.setTcpConnections(tcpDictList) udpDictList = [] for eachUdpConn in udp: udpDictList.append(eachUdpConn.toJSON()) eel.setUdpConnections(udpDictList) eel.setEstTcp(establishedTcp, len(tcp)) except KeyboardInterrupt: print("Closing")