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
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42 import sys, threading, os, time, thread, re
43 from Peach.agent import Monitor
44
45 try:
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
74 '''
75 This monitor will report a fault if it cannot ping
76 the specified hostname.
77 '''
78
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
89 self.hostname = str(args['hostname'])
90 self._name = "PingMonitor"
91
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
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
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
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
143
144 print "."
145 pc.readpkts()
146
147 pc.dumpclose()
148 self.dumpClosed.set()
149
150
152 '''
153 Monitor network using pcap library.
154 '''
155
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
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
176
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
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
194 return { 'Capture.pcap' : self.data }
195
197 if self.thread != None and self.thread.isAlive():
198 self.thread.stopEvent.set()
199 self.thread.join()
200
201
202