00001
00002
00003
00004
00005
00006
00007
00008
00009 __version__ = "$Revision: 1.1 $"
00010
00011 from qt import *
00012 import sys, os
00013 from LICOS.tools.proc import *
00014 from LICOS.tools.proc.ProcessMonitor import ProcessMonitor
00015
00016
00017
00018
00019
00020 def getProcStat(host, pid):
00021
00022 return linuxproc.proc_stat(pid)
00023 def getProcStatM(host, pid):
00024
00025 return linuxproc.proc_statm(pid)
00026 def totalCPU(stat):
00027
00028 return (stat['utime'] + stat['stime']) * jiffyPerSec
00029
00030 def readPidFile(pidFile):
00031 processes = {}
00032 f = open(pidFile, 'r')
00033 for line in f.readlines():
00034 host,key,value = line.split()
00035 if (host,key) in processes:
00036 msg = "process %s:%s exists multiple times in process file" % (host,key)
00037 raise msg
00038 processes[(host,key)] = (value)
00039 f.close()
00040 return processes
00041
00042
00043 class ProcessMonitorImpl(ProcessMonitor):
00044 def __init__(self, processDict, parent=None, name = None, fl = 0):
00045 ProcessMonitor.__init__(self, parent, name, fl)
00046 self.__pDict = processDict
00047 self.__procs = {}
00048 self.procBox = QGridLayout(self.mainFrame)
00049 self.mainFrame.setMargin(3)
00050 self.makeMonitor()
00051 self.updateMonitor()
00052 self.timer = QTimer(self)
00053 self.connect(self.timer, SIGNAL("timeout()"), self.updateMonitor)
00054 self.timer.start(5000)
00055
00056
00057 def makeMonitor(self):
00058 gridPos = 0
00059 for host,name in self.__pDict.keys():
00060
00061 ps = ProcSet(self.mainFrame, self.procBox, gridPos)
00062 ps.name.setText(name)
00063 ps.host.setText(host)
00064 ps.cpu.setTime(0.0)
00065
00066 self.__procs[(host,name)] = ps
00067 gridPos += 1
00068
00069
00070 def updateMonitor(self):
00071
00072 for (host,key),pid in self.__pDict.items():
00073 procSet = self.__procs[(host,key)]
00074 try:
00075 stat = getProcStat(host,pid)
00076 except IOError:
00077
00078
00079
00080 procSet.cpu.setTime(-1)
00081 procSet.led.setState(procSet.led.red)
00082 continue
00083
00084
00085 procSet.cpu.setTime(totalCPU(stat))
00086 procSet.led.setState(procSet.led.green)
00087
00088
00089
00090 class ProcSet(object):
00091 def __init__(self, parent, container, gridPos):
00092 self.name = QLabel(parent)
00093 self.host = QLabel(parent)
00094 self.cpu = ProcCpuTime(parent)
00095 self.led = ProcLED(parent)
00096 self.gridPos = gridPos
00097
00098 container.addWidget(self.name, gridPos, 0)
00099 container.addWidget(self.host, gridPos, 1)
00100 container.addWidget(self.cpu , gridPos, 2)
00101 container.addWidget(self.led , gridPos, 3)
00102
00103 self.cpu.setAlignment(QWidget.AlignRight)
00104
00105
00106 class ProcCpuTime(QLabel):
00107 def __init__(self, parent=None, name = None, fl = 0):
00108 QLabel.__init__(self, parent, name, fl)
00109
00110 def setTime(self, time):
00111 self.setText( "%10.2f s" % time)
00112
00113 class ProcLED(QWidget):
00114 def __init__(self, parent=None, name=None, fl=0):
00115 QWidget.__init__(self, parent, name, fl)
00116 self.red = QColor('red')
00117 self.green = QColor('green')
00118 self.__state = self.red
00119
00120 self.radius = 10
00121 self.minRadius = 10
00122 self.margin = 5
00123 self.size = QSize(self.radius+3*self.margin, self.radius+self.margin)
00124 self.minSize = QSize(self.minRadius+3*self.margin, self.minRadius+self.margin)
00125 self.sizePol = QSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
00126
00127 def paintEvent(self, qe):
00128
00129 paint = QPainter(self)
00130 paint.setBrush(self.__state)
00131 paint.setPen(QPen.NoPen)
00132
00133 center = self.rect().center()
00134 r = self.radius
00135 paint.drawEllipse(center.x()-r/2, center.y()-r/2, r, r)
00136
00137 pass
00138
00139 def sizeHint(self):
00140 return self.size
00141
00142 def minimumSizeHint(self):
00143 return self.minSize
00144
00145 def sizePolicy(self):
00146 return self.sizePol
00147
00148 def setState(self, color):
00149 self.__state = color
00150 self.update()
00151
00152
00153 if __name__ == '__main__':
00154
00155 pidFile = sys.argv[1]
00156
00157 if os.path.exists(pidFile):
00158 processes = readPidFile(pidFile)
00159
00160 a = QApplication(sys.argv)
00161
00162 p = ProcessMonitorImpl(processes)
00163 a.setMainWidget(p)
00164
00165 p.show()
00166 a.exec_loop()