Package sysmon :: Module local
[hide private]
[frames] | no frames]

Source Code for Module sysmon.local

  1  ######################################################################### 
  2  # YASMon - Yet Another System Monitor                                   # 
  3  # Copyright (C) 2010  Scott Lawrence                                    # 
  4  #                                                                       # 
  5  # This program is free software: you can redistribute it and/or modify  # 
  6  # it under the terms of the GNU General Public License as published by  # 
  7  # the Free Software Foundation, either version 3 of the License, or     # 
  8  # (at your option) any later version.                                   # 
  9  #                                                                       # 
 10  # This program is distributed in the hope that it will be useful,       # 
 11  # but WITHOUT ANY WARRANTY; without even the implied warranty of        # 
 12  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         # 
 13  # GNU General Public License for more details.                          # 
 14  #                                                                       # 
 15  # You should have received a copy of the GNU General Public License     # 
 16  # along with this program.  If not, see <http://www.gnu.org/licenses/>. # 
 17  ######################################################################### 
 18   
 19  """Provides an interface to the local machine. 
 20   
 21  """ 
 22   
 23  import re; 
 24   
 25  from system import *; 
 26   
27 -def get_local():
28 """Returns an object representing the local system. 29 """ 30 system=LocalSystem("localhost") 31 uptime=LocalUptime() 32 33 #create the processors 34 cpuinfo=file("/proc/cpuinfo") 35 for line in cpuinfo: 36 match=re.match("^processor[^:]*:[ \t]*([0-9]+)",line) 37 if match: 38 system.add_processor(LocalProcessor(match.group(1),"cpu"+match.group(1))) 39 cpuinfo.close() 40 41 #create the memory bank 42 system.set_memory(LocalMemory()) 43 44 #create the filesystems 45 partitions=file("/proc/partitions") 46 for line in partitions: 47 match=re.match("^[ \t]+([0-9]+)[ \t]+([0-9]+)[ \t]+([0-9]+)[ \t]+([a-z0-9]+)",line) 48 if match: 49 major=match.group(1) 50 minor=match.group(2) 51 blocks=match.group(3) 52 name=match.group(4) 53 system.add_filesystem(LocalFilesystem(name)) 54 partitions.close() 55 56 #create the process list 57 system.set_processlist(LocalProcessList()) 58 system.set_uptime(uptime) 59 return system
60
61 -class LocalSystem(System):
62 """Represents a local system. 63 64 """
65 - def __init__(self,name="localhost"):
66 """Creates an empty local system. 67 68 This constructor does not initialize the local system - that 69 is done with the get_local() method. 70 """ 71 System.__init__(self,name)
72 73
74 -class LocalUptime(Uptime):
75 """Represents the local uptime. 76 """
77 - def __init__(self):
78 Uptime.__init__(self) 79 self._uptime=0
80
81 - def uptime(self):
82 return self._uptime
83
84 - def do_update(self):
85 with open("/proc/uptime") as uptime: 86 for line in uptime: 87 self._uptime=float(re.split(" ",line)[0]) 88 self.callback().call("uptime.updated",self)
89
90 -class LocalProcessor(Processor):
91 """Represents a local processor 92 """
93 - def __init__(self,id,name):
94 """Creates a local processor. 95 96 Ordinarily, the name will be the same as cpu<id>. 97 """ 98 Processor.__init__(self) 99 self._name=name 100 self._id=id 101 self._dict=dict() 102 103 #history-keeping for calculation (math: a crude hack that works) 104 self._totalusage=0 105 self._totalidle=0
106
107 - def name(self):
108 return self._name
109
110 - def dict(self):
111 return self._dict
112
113 - def do_update(self):
114 ignoring=True 115 #learn about our cpus 116 with open("/proc/cpuinfo") as cpuinfo: 117 for line in cpuinfo: 118 match=re.match("processor[ \t]*:[ \t]*([0-9]+)",line) 119 if match: 120 #should we ignore stuff, or pay attention? 121 if match.group(1)==str(self._id): 122 ignoring=False 123 else: 124 ignoring=True 125 if not ignoring: 126 match=re.search("([^:\t]+)[ \t]*:[ \t]*(.+)$",line) 127 if match: 128 key=match.group(1) 129 val=match.group(2) 130 self._dict[key]=val 131 132 #now look up our cpu's statistics 133 with open("/proc/stat") as stat: 134 for line in stat: 135 match=re.match("%s[ \\t]+([0-9]+)[ \\t]+([0-9]+)[ \\t]+([0-9]+)[ \\t]+([0-9]+)[ \\t]+" 136 % self._name,line) 137 if match: 138 usage=int(match.group(1))+int(match.group(2))+int(match.group(3)) 139 idle=int(match.group(4)) 140 #update our current statistics 141 newusage=usage-self._totalusage 142 newidle=idle-self._totalidle 143 if (newusage+newidle)>0: 144 pu=newusage/float(newusage+newidle) 145 else: 146 pu=0 147 #will this cause a problem with variable frequency cpus? 148 self.dict()['usage']=pu*self.max_freq() 149 self._totalusage=usage 150 self._totalidle=idle 151 self.callback().call("processor.%s.updated" % self.name(),self)
152 153
154 -class LocalMemory(Memory):
155 """Represents a local memory (RAM) bank 156 """
157 - def __init__(self,filename="/proc/meminfo"):
158 """Creates a memory bank from the file. 159 160 By default, the file /proc/meminfo is used. 161 """ 162 Memory.__init__(self) 163 self.filename=filename 164 self._dict=dict()
165
166 - def do_update(self):
167 with open(self.filename) as meminfo: 168 for line in meminfo: 169 match=re.search("([^:]+)[ \t]*:[ \t]*(.+)$",line) 170 if match: 171 key=match.group(1) 172 val=match.group(2) 173 match=re.search("([0-9]+)[ \t]*kB",val) 174 if match: 175 val=int(match.group(1))*1024 176 match=re.search("([0-9]+)[ \t]*MB",str(val)) 177 if match: 178 val=int(match.group(1))*1024*1024 179 self._dict[key]=val 180 self.callback().call("memory.updated",self)
181
182 - def dict(self):
183 return self._dict
184 185
186 -class LocalFilesystem(Filesystem):
187 """Represents a local filesystem. 188 """
189 - def __init__(self,device):
190 """Creates the filesystem from the device name. 191 """ 192 Filesystem.__init__(self)
193
194 - def do_update(self):
195 self.callback().call("filesystem.updated",self)
196 197
198 -class LocalDrive(Drive):
199 """Represents a local drive. 200 """
201 - def __init__(self,major,minor):
202 """Creates the drive from the major and minor 203 identifiers. 204 """ 205 Drive.__init__(self)
206
207 - def do_update(self):
208 self.callback().call("drive.updated",self)
209 210
211 -class LocalProcessList(ProcessList):
212 """Represents a local list of processes. 213 214 Since this class always draws its information from the /proc 215 directory, all instances created on the same system will be 216 identical. 217 """
218 - def __init__(self):
220
221 - def do_update(self):
222 self.callback().call("processlist.updated",self)
223 224
225 -class LocalProcess(Process):
226 """Represents a single local process. 227 """ 228 pass
229