class MemoryChart(QAbstractItemModel): def __init__(self, memType, parent=None): super(MemoryChart, self).__init__(parent) self._type = "virt" if memType == "virt" else "swap" # fetch initial memory data if self._type == "virt": memUsage = psutil.virtual_memory().percent else: memUsage = psutil.swap_memory().percent self._data = [memUsage] * 10 self.timer = QTimer(self) self.timer.timeout.connect(self._refresh) self.timer.start(1000) self._refresh() def _refresh(self): if self._type == "virt": newMemUsage = psutil.virtual_memory().percent else: newMemUsage = psutil.swap_memory().percent self._data.pop(0) self._data.append(newMemUsage) self.dataChanged.emit(0, 0) print("Memory chart refresh called") def data(self, index, role=QtCore.Qt.DisplayRole): return self._data
class MemoryInfo(QAbstractItemModel): totalChanged = Signal(str) availableChanged = Signal(str) percentChanged = Signal(str) usedChanged = Signal(str) freeChanged = Signal(str) def __init__(self, memType, parent=None): super(MemoryInfo, self).__init__(parent) self._type = 'virt' if memType == 'virt' else 'swap' self._total = None self._available = None self._percent = None self._used = None self._free = None self._firstRun = True self.timer = QTimer(self) self.timer.timeout.connect(self._refresh) self.timer.start(1000) def _refresh(self): if self._type == 'virt': memUsage = psutil.virtual_memory() else: memUsage = psutil.swap_memory() if self._total != humanize_bytes(memUsage.total, 2): self._total = humanize_bytes(memUsage.total, 2) self.totalChanged.emit(self._total) if self._type == 'virt': if self._available != humanize_bytes(memUsage.available, 2): self._available = humanize_bytes(memUsage.available, 2) self.availableChanged.emit(self._available) if self._percent != str(memUsage.percent) + '%': self._percent = str(memUsage.percent) + '%' self.percentChanged.emit(self._percent) if self._used != humanize_bytes(memUsage.used, 2): self._used = humanize_bytes(memUsage.used, 2) self.usedChanged.emit(self._used) if self._free != humanize_bytes(memUsage.free, 2): self._free = humanize_bytes(memUsage.free, 2) self.freeChanged.emit(self._free) def getCounters(self, *args): return { 'total': self._total, 'available': self._available, 'percent': self._percent, 'used': self._used, 'free': self._free}
def __init__(self, parent=None): super(Process, self).__init__(parent) self._refresh() self.timer = QTimer(self) self.timer.timeout.connect(self._refresh) self.timer.start(3000)
class CpuChart(QAbstractItemModel): """ Model class for monitoring CPU usage Can be used as a data source for app.chart view. Stores CPU usage values for the last 10 seconds and updates CPU info every second """ def __init__(self, cpuNum, parent=None): super(CpuChart, self).__init__(parent) self._cpuNum = cpuNum cpuUsage = psutil.cpu_percent(interval=None, percpu=True)[self._cpuNum] self._data = [cpuUsage] * 10 # update CPU usage values every second self.timer = QTimer(self) self.timer.timeout.connect(self._refresh) self.timer.start(1000) self._refresh() def _refresh(self): """ Get CPU usage values and emit dataChanged signal Protected method """ cpuUsage = psutil.cpu_percent(interval=None, percpu=True)[self._cpuNum] if self._cpuNum == 1: print cpuUsage self._data.pop(0) self._data.append(cpuUsage) self.dataChanged.emit(0, 0) def data(self, index=None, role=QtCore.Qt.DisplayRole): """ Return list of CPU usage values """ return self._data
def __init__(self, cpuNum, parent=None): super(CpuChart, self).__init__(parent) self._cpuNum = cpuNum cpuUsage = psutil.cpu_percent(interval=None, percpu=True)[self._cpuNum] self._data = [cpuUsage] * 10 # update CPU usage values every second self.timer = QTimer(self) self.timer.timeout.connect(self._refresh) self.timer.start(1000) self._refresh()
def __init__(self, memType, parent=None): super(MemoryInfo, self).__init__(parent) self._type = 'virt' if memType == 'virt' else 'swap' self._total = None self._available = None self._percent = None self._used = None self._free = None self._firstRun = True self.timer = QTimer(self) self.timer.timeout.connect(self._refresh) self.timer.start(1000)
def __init__(self, memType, parent=None): super(MemoryChart, self).__init__(parent) self._type = "virt" if memType == "virt" else "swap" # fetch initial memory data if self._type == "virt": memUsage = psutil.virtual_memory().percent else: memUsage = psutil.swap_memory().percent self._data = [memUsage] * 10 self.timer = QTimer(self) self.timer.timeout.connect(self._refresh) self.timer.start(1000) self._refresh()
def __init__(self, iface, refreshRate = 1, parent=None): super(NetworkInfo, self).__init__(parent) self._iface = iface self._refreshRate = refreshRate self._bytesSent = None self._bytesReceived = None self._packetsSent = None self._packetsReceived = None self._bytesSentSpeed = None self._bytesReceivedSpeed = None self._packetsSentSpeed = None self._packetsReceivedSpeed = None self._firstRun = True self.timer = QTimer(self) self.timer.timeout.connect(self._refresh) self.timer.start(self._refreshRate * 1000)
class NetworkInfo(QAbstractItemModel): bytesSentChanged = Signal(str) bytesReceivedChanged = Signal(str) packetsSentChanged = Signal(str) packetsReceivedChanged = Signal(str) bytesSentSpeedChanged = Signal(str) bytesReceivedSpeedChanged = Signal(str) packetsSentSpeedChanged = Signal(str) packetsReceivedSpeedChanged = Signal(str) def __init__(self, iface, refreshRate = 1, parent=None): super(NetworkInfo, self).__init__(parent) self._iface = iface self._refreshRate = refreshRate self._bytesSent = None self._bytesReceived = None self._packetsSent = None self._packetsReceived = None self._bytesSentSpeed = None self._bytesReceivedSpeed = None self._packetsSentSpeed = None self._packetsReceivedSpeed = None self._firstRun = True self.timer = QTimer(self) self.timer.timeout.connect(self._refresh) self.timer.start(self._refreshRate * 1000) def _refresh(self): if self._iface != 'total': netinfo = psutil.network_io_counters(pernic=True)[self._iface] else: netinfo = psutil.network_io_counters() if self._firstRun: self._bytesSentSpeed = '0 bytes/s' self._bytesReceivedSpeed = '0 bytes/s' self._packetsSentSpeed = '0 packets/s' self._packetsReceivedSpeed = '0 packets/s' self.bytesSentSpeedChanged.emit(self._bytesSentSpeed) self.bytesReceivedSpeedChanged.emit(self._bytesReceivedSpeed) self.packetsSentSpeedChanged.emit(self._packetsSentSpeed) self.packetsReceivedSpeedChanged.emit(self._packetsReceivedSpeed) self._bytesSent = netinfo.bytes_sent self._bytesReceived = netinfo.bytes_recv self._packetsSent = netinfo.packets_sent self._packetsReceived = netinfo.packets_recv self.bytesSentChanged.emit(humanize_bytes(self._bytesSent, 2)) self.bytesReceivedChanged.emit(humanize_bytes(self._bytesReceived, 2)) self.packetsSentChanged.emit(str(self._packetsSent)) self.packetsReceivedChanged.emit(str(self._packetsReceived)) self._firstRun = False else: # calculate data transmision speeds per minute speed = humanize_bytes((netinfo.bytes_sent - self._bytesSent) / self._refreshRate, 2) + '/s' if speed != self._bytesSentSpeed: self._bytesSentSpeed = speed self.bytesSentSpeedChanged.emit(self._bytesSentSpeed) speed = humanize_bytes((netinfo.bytes_recv - self._bytesReceived) / self._refreshRate, 2) + '/s' if speed != self._bytesReceivedSpeed: self._bytesReceivedSpeed = speed self.bytesReceivedSpeedChanged.emit(self._bytesReceivedSpeed) speed = str(netinfo.packets_sent - self._packetsSent) + ' packets/s' if speed != self._packetsSentSpeed: self._packetsSentSpeed = speed self.packetsSentSpeedChanged.emit(self._packetsSentSpeed) speed = str(netinfo.packets_recv - self._packetsReceived) + ' packets/s' if speed != self._packetsReceivedSpeed: self._packetsReceivedSpeed = speed self.packetsReceivedSpeedChanged.emit(self._packetsReceivedSpeed) # calculate total data transfered if (netinfo.bytes_sent != self._bytesSent): self._bytesSent = netinfo.bytes_sent #print(self._iface + ': Bytes sent: ' + str(self._bytesSent)) self.bytesSentChanged.emit(humanize_bytes(self._bytesSent, 2)) if (netinfo.bytes_recv != self._bytesReceived): self._bytesReceived = netinfo.bytes_recv #print(self._iface + ': Bytes received' + str(self._bytesReceived)) self.bytesReceivedChanged.emit(humanize_bytes(self._bytesReceived, 2)) if (netinfo.packets_sent != self._packetsSent): self._packetsSent = netinfo.packets_sent self.packetsSentChanged.emit(str(self._packetsSent)) if (netinfo.packets_recv != self._packetsReceived): self._packetsReceived = netinfo.packets_recv self.packetsReceivedChanged.emit(str(self._packetsReceived))
class Process(QAbstractTableModel): def __init__(self, parent=None): super(Process, self).__init__(parent) self._refresh() self.timer = QTimer(self) self.timer.timeout.connect(self._refresh) self.timer.start(3000) def _refresh(self): self._data = [] for p in psutil.process_iter(): mem = p.get_memory_info() self._data.append([ p.pid, p.name, p.username, str(p.status), humanize_bytes(mem.vms), humanize_bytes(mem.rss), str(round(p.get_memory_percent(), 2)) + "%", str(p.get_cpu_percent(interval=None)) + "%" ]) self._data = sorted(self._data, key=lambda p: float(p[-1][:-1]), reverse=True) #self.reset() self.dataChanged.emit(self.index(0, 0), self.index(self.rowCount() - 1, self.columnCount() - 1)) def rowCount(self, parent=QtCore.QModelIndex()): return len(self._data) def columnCount(self, parent=QtCore.QModelIndex()): return len(self._data[0]) def data(self, index, role=QtCore.Qt.DisplayRole): if role != QtCore.Qt.DisplayRole: return None row = index.row() column = index.column() return self._data[row][column] def headerData(self, section, orientation = None, role=QtCore.Qt.DisplayRole): if role != QtCore.Qt.DisplayRole: return None if section == 0: return 'Id' elif section == 1: return 'Name' elif section == 2: return 'Owner' elif section == 3: return 'Status' elif section == 4: return 'Memory (virtual)' elif section == 5: return 'Memory (resident)' elif section == 6: return 'Memory usage' elif section == 7: return 'CPU usage' print section def allData(self): return self._data