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

Source Code for Module sysmon.remote

  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  """Collect data from remote systems. 
 20   
 21  """ 
 22   
 23  import cPickle,re,socket,thread 
 24  from system import * 
 25   
26 -def get_remote(addr,port=61874):
27 """Returns an object representing a remote system. 28 """ 29 contact=RemoteContact(addr,port) 30 system=RemoteSystem(contact) 31 return system
32
33 -class RemoteContact():
34 """Communicates with a remote machine. 35 36 Objects of this class may be used for communication with the 37 remote machine. The necessary locking is handled automatically, so 38 this class is thread-safe. 39 """
40 - def __init__(self,addr,port):
41 """Connects to the remote machine. 42 """ 43 #connect 44 sock=socket.socket(socket.AF_INET, socket.SOCK_STREAM) 45 sock.connect((addr,port)) 46 self._socket=sock 47 self._lock=thread.allocate_lock() 48 self._addr=addr 49 self._port=port
50
51 - def query(self,query):
52 """Queries the remote machine. 53 54 This method automatically handles the necessary locking to be 55 thread-safe. Do NOT acquire this object's lock before calling 56 this method - the method will block and never return. 57 """ 58 with self.lock(): 59 #get the file 60 f=self.socket().makefile() 61 #send the query 62 f.write("%s\n" % query) 63 f.flush() 64 info="" 65 #read until final token 66 for line in f: 67 #get rid of excess newline 68 line=re.sub("\n","",line) 69 if line=='*DONE': 70 #it's over 71 break 72 #append the line 73 info+="%s\n" % line 74 return info
75
76 - def socket(self):
77 """Returns the backing socket. 78 79 Because using this socket erases the thread safety, this 80 method should never be used. 81 """ 82 return self._socket
83
84 - def lock(self):
85 """Returns the backing lock. 86 """ 87 return self._lock
88
89 - def acquire(self):
90 """Calls acquire on the backing lock. 91 """ 92 self._lock.acquire()
93
94 - def release(self):
95 """Releases the backing lock. 96 """ 97 self._lock.release()
98
99 - def addr(self):
100 """Returns the remote address in use. 101 """ 102 return self._addr
103
104 - def port(self):
105 """Returns the remote port in use. 106 """ 107 return self._port
108 109
110 -class RemoteSystem(System):
111 """Represents a remote system. 112 """
113 - def __init__(self,contact):
114 """Creates an empty remote system. 115 116 This constructor DOES initialize the remote system, using the 117 RemoteContact passed to it (generally by get_remote). 118 """ 119 #initialize stuff 120 addr=contact.addr() 121 System.__init__(self,addr) 122 self._contact=contact 123 #assume certain things - we have uptime, memory, etc... 124 self.set_uptime(RemoteUptime(contact)) 125 self.set_memory(RemoteMemory(contact)) 126 self.set_processlist(RemoteProcessList(contact)) 127 #get information from the contact 128 info=contact.query('overview') 129 lines=re.split("\n",info) 130 for line in lines: 131 #processor? 132 match=re.match("^processor ([a-z0-9]+)",line) 133 if match: 134 #create the processor and add it to the system 135 self.add_processor(RemoteProcessor(match.group(1),contact)) 136 #filesystem? 137 match=re.match("^filesystem ([a-z0-9]+)",line) 138 if match: 139 #create the filesystem and add it to the system 140 print match.group(1)
141 142
143 - def contact(self):
144 """Returns the backing RemoteContact object. 145 """ 146 return self._contact
147 148
149 -class RemoteUptime(Uptime):
150 """Represents the uptime of a remote system. 151 """
152 - def __init__(self,contact):
153 """Creates a RemoteUptime instance based on the given 154 RemoteContact. 155 """ 156 Uptime.__init__(self) 157 self._contact=contact 158 self._uptime=0
159
160 - def uptime(self):
161 return self._uptime
162
163 - def do_update(self):
164 info=self._contact.query('uptime') 165 self._uptime=int(info) 166 self.callback().call("uptime.updated",self)
167
168 - def contact(self):
169 """Returns the backing RemoteContact object. 170 """ 171 return self._contact
172 173
174 -class RemoteProcessor(Processor):
175 """Represents a processor on a remote system. 176 """
177 - def __init__(self,name,contact):
178 """Creates a remote processor. 179 """ 180 Processor.__init__(self) 181 self._name=name 182 self._dict=dict() 183 self._contact=contact
184
185 - def name(self):
186 return self._name
187
188 - def do_update(self):
189 info=self._contact.query("processor %s" % self.name()) 190 #load as pickle'd from the string 191 self._dict=cPickle.loads(info) 192 self.callback().call("processor.%s.updated" % self.name(),self)
193
194 - def dict(self):
195 return self._dict
196 197
198 -class RemoteMemory(Memory):
199 """Represents the physical memory (RAM) of a remote system. 200 """
201 - def __init__(self,contact):
202 """Creates a RemoteMemory instance based on the given 203 RemoteContact. 204 """ 205 Memory.__init__(self) 206 self._dict=dict() 207 self._contact=contact
208
209 - def do_update(self):
210 info=self._contact.query('memory') 211 #load as pickle'd from the string 212 self._dict=cPickle.loads(info) 213 self.callback().call("memory.updated",self)
214
215 - def dict(self):
216 return self._dict
217
218 - def contact(self):
219 """Returns the backing RemoteContact object. 220 """ 221 return self._contact
222 223
224 -class RemoteProcessList(ProcessList):
225 """Represents the ProcessList of a remote system. 226 """
227 - def __init__(self,contact):
228 """Creates a RemoteProcessList instance based on the given 229 RemoteContact. 230 """ 231 ProcessList.__init__(self) 232 self._contact=contact
233
234 - def contact(self):
235 """Returns the backing RemoteContact object. 236 """ 237 return self._contact
238