Package Peach :: Package Agent :: Module network
[hide private]

Source Code for Module Peach.Agent.network

  1   
  2  ''' 
  3  Networking monitor for peach agent.  Uses pylibpcap to perform network 
  4  captures which can be reported back and logged. 
  5   
  6  Todo: 
  7   
  8   Implement! http://pylibpcap.sourceforge.net/ 
  9   
 10  @author: Michael Eddington 
 11  @version: $Id: Peach.Agent.network-pysrc.html 1138 2008-08-16 19:39:03Z meddingt $ 
 12  ''' 
 13   
 14  # 
 15  # Copyright (c) 2007 Michael Eddington 
 16  # 
 17  # Permission is hereby granted, free of charge, to any person obtaining a copy  
 18  # of this software and associated documentation files (the "Software"), to deal 
 19  # in the Software without restriction, including without limitation the rights  
 20  # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell  
 21  # copies of the Software, and to permit persons to whom the Software is  
 22  # furnished to do so, subject to the following conditions: 
 23  # 
 24  # The above copyright notice and this permission notice shall be included in     
 25  # all copies or substantial portions of the Software. 
 26  # 
 27  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR  
 28  # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  
 29  # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE  
 30  # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER  
 31  # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 32  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
 33  # SOFTWARE. 
 34  # 
 35   
 36  # Authors: 
 37  #   Michael Eddington (mike@phed.org) 
 38   
 39  # $Id: Peach.Agent.network-pysrc.html 1138 2008-08-16 19:39:03Z meddingt $ 
 40   
 41   
 42  import sys, threading, os, time, thread, re 
 43  from Peach.agent import Monitor 
 44   
 45  try: 
46 - def search_file(filename):
47 """ 48 Find a file in a search path 49 """ 50 51 search_path = os.getenv("path") 52 paths = search_path.split(os.path.pathsep) 53 for path in paths: 54 if os.path.exists(os.path.join(path, filename)): 55 return True 56 57 return False
58 59 if sys.platform == 'win32' and search_file("wpcap.dll"): 60 import pcap 61 62 elif sys.platform != 'win32': 63 import pcap 64 65 else: 66 print "Warning: pypcap not found, disabling network monitor." 67 68 except: 69 print "Warning: pypcap not found, disabling network monitor." 70 pass 71 72
73 -class PingMonitor(Monitor):
74 ''' 75 This monitor will report a fault if it cannot ping 76 the specified hostname. 77 ''' 78
79 - def __init__(self, args):
80 ''' 81 Constructor. Arguments are supplied via the Peach XML 82 file. 83 84 @type args: Dictionary 85 @param args: Dictionary of parameters 86 ''' 87 88 # Our name for this monitor 89 self.hostname = str(args['hostname']) 90 self._name = "PingMonitor"
91
92 - def DetectedFault(self):
93 ''' 94 Check if a fault was detected. 95 ''' 96 pipe = os.popen("ping -n 2 " + self.hostname) 97 buff = pipe.read() 98 pipe.close() 99 100 if re.compile(r"Reply from \d+\.\d+\.\d+\.\d+: bytes=", re.M).search(buff) != None: 101 return False 102 103 # If we didn't see a ping, lets try again with 3 pings just to make sure 104 105 pipe = os.popen("ping -n 3 " + self.hostname) 106 buff = pipe.read() 107 pipe.close() 108 109 if re.compile(r"Reply from \d+\.\d+\.\d+\.\d+: bytes=", re.M).search(buff) != None: 110 return False 111 112 return True
113 114
115 -class PcapThread(threading.Thread):
116 - def __init__(self, parent, device, filter, pcapFile):
117 threading.Thread.__init__(self) 118 threading.Thread.setDaemon(self, True) 119 120 self._device = device 121 self._filter = filter 122 self._pcapFile = pcapFile 123 self.stopEvent = threading.Event() 124 self.stopEvent.clear() 125 self.dumpClosed = threading.Event() 126 self.dumpClosed.clear() 127 self._packets = []
128
129 - def run(self):
130 print "PcapThread(): Starting up pcap" 131 132 pc = pcap.pcap(self._device) 133 pc.dumpopen(self._pcapFile) 134 135 if self._filter != None: 136 pc.setfilter(self._filter) 137 138 pc.setnonblock() 139 140 print "PcapThread(): Packet capture loop" 141 while not self.stopEvent.isSet(): 142 # Do not remove print. For some reason packets 143 # are only captures when it's there!!! 144 print "." 145 pc.readpkts() 146 147 pc.dumpclose() 148 self.dumpClosed.set()
149 150
151 -class PcapMonitor(Monitor):
152 ''' 153 Monitor network using pcap library. 154 ''' 155
156 - def __init__(self, args):
157 try: 158 self.device = str(args['device']) 159 if len(self.device) < 1: 160 self.device = pcap.getDefaultName() 161 162 except: 163 self.device = pcap.getDefaultName() 164 165 self.filter = str(args['filter']) 166 self.data = None 167 self.tempFile = os.tmpnam()
168
169 - def OnTestStarting(self):
170 self.thread = PcapThread(self, self.device, self.filter, self.tempFile) 171 self.thread.start() 172 self.data = None 173 print "PcapMonitor: OnTestStarting done"
174
175 - def OnTestFinished(self):
176 # Stop thread 177 178 self.data = None 179 if self.thread != None and self.thread.isAlive(): 180 self.thread.stopEvent.set() 181 self.thread.join() 182 self.thread.dumpClosed.wait() 183 184 # Read dump 185 f = open(self.tempFile, "rb") 186 self.data = f.read() 187 f.close() 188 189 print "PcapMonitor: Thread joined, dump saved" 190 191 self.thread = None
192
193 - def GetMonitorData(self):
194 return { 'Capture.pcap' : self.data }
195
196 - def OnShutdown(self):
197 if self.thread != None and self.thread.isAlive(): 198 self.thread.stopEvent.set() 199 self.thread.join()
200 201 # end 202