Added LinuxTcpdump
[nepi.git] / src / nepi / resources / linux / ping.py
1 #
2 #    NEPI, a framework to manage network experiments
3 #    Copyright (C) 2013 INRIA
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 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
19
20 from nepi.execution.attribute import Attribute, Flags, Types
21 from nepi.execution.resource import clsinit_copy 
22 from nepi.resources.linux.application import LinuxApplication
23 from nepi.util.timefuncs import tnow
24
25 import os
26
27 @clsinit_copy
28 class LinuxPing(LinuxApplication):
29     _rtype = "LinuxPing"
30
31     @classmethod
32     def _register_attributes(cls):
33         count = Attribute("count",
34             "Sets ping -c option. Determines the number of ECHO_REQUEST "
35             "packates to send before stopping.",
36             flags = Flags.ExecReadOnly)
37
38         mark = Attribute("mark",
39             "Sets ping -m option. Uses 'mark' to tag outgoing packets. ",
40             flags = Flags.ExecReadOnly)
41
42         interval = Attribute("interval",
43             "Sets ping -i option. Leaves interval seconds between "
44             "successive ECHO_REUQEST packets. ",
45             flags = Flags.ExecReadOnly)
46
47         address = Attribute("address",
48             "Sets ping -I option. Sets ECHO_REQUEST packets souce address "
49             "to the specified interface address ",
50             flags = Flags.ExecReadOnly)
51
52         preload = Attribute("preload",
53             "Sets ping -l option. Sends preload amount of packets "
54             "without waiting for a reply ",
55             flags = Flags.ExecReadOnly)
56
57         numeric = Attribute("numeric",
58             "Sets ping -n option. Disables resolution of host addresses into "
59             "symbolic names. ",
60             type = Types.Bool,
61             default = False,
62             flags = Flags.ExecReadOnly)
63
64         pattern = Attribute("pattern",
65             "Sets ping -p option. Species a up to 16 ''pad'' bytes to fill "
66             "out sent packets. ",
67             flags = Flags.ExecReadOnly)
68
69         printtmp = Attribute("printTimestamp",
70             "Sets ping -D option. Prints timestamp befor each line as: "
71             "unix time + microseconds as in gettimeofday ",
72             type = Types.Bool,
73             default = False,
74             flags = Flags.ExecReadOnly)
75
76         tos = Attribute("tos",
77             "Sets ping -Q option. Sets Quality of Service related bits in ICMP "
78             "datagrams. tos can be either a decimal or hexadecime number ",
79             flags = Flags.ExecReadOnly)
80
81         quiet = Attribute("quiet",
82             "Sets ping -q option. Disables ping standard output ",
83             type = Types.Bool,
84             default = False,
85             flags = Flags.ExecReadOnly)
86
87         rec_route = Attribute("recordRoute",
88             "Sets ping -R option. Includes the RECORD_ROUTE option in the "
89             "ECHO REQUEST packet and displays route buffer on the Disables "
90             "ping standard output.",
91             type = Types.Bool,
92             default = False,
93             flags = Flags.ExecReadOnly)
94
95         route_bypass = Attribute("routeBypass",
96             "Sets ping -r option. Bypasses normal routing tables and sends "
97             "ECHO REQUEST packets directly yo a host on an attached interface. ",
98             type = Types.Bool,
99             default = False,
100             flags = Flags.ExecReadOnly)
101
102         packetsize = Attribute("packetSize",
103             "Sets ping -s option. Specifies the number of data bytes to be "
104             "sent. Defaults to 56. ",
105             flags = Flags.ExecReadOnly)
106
107         sendbuff = Attribute("sendBuff",
108             "Sets ping -S option. Specifies the number of packets to buffer. "
109             "Defaults to one. ",
110             flags = Flags.ExecReadOnly)
111
112         ttl = Attribute("ttl",
113             "Sets ping -t option. Specifies the IP Time to Live for the "
114             "packets. ",
115             flags = Flags.ExecReadOnly)
116
117         timestamp = Attribute("timestamp",
118             "Sets ping -T option. Sets special IP timestamp options. ",
119             flags = Flags.ExecReadOnly)
120
121         hint = Attribute("hint",
122             "Sets ping -M option. Selects Path MTU Discovery strategy. ",
123             flags = Flags.ExecReadOnly)
124
125         full_latency = Attribute("fullLatency",
126             "Sets ping -U option. Calculates round trip time taking into "
127             "account the full user-to-user latency instead of only the "
128             "network round trip time. ",
129             type = Types.Bool,
130             default = False,
131             flags = Flags.ExecReadOnly)
132
133         verbose = Attribute("verbose",
134             "Sets ping -v option. Verbose output. ",
135             type = Types.Bool,
136             default = False,
137             flags = Flags.ExecReadOnly)
138
139         flood = Attribute("flood",
140             "Sets ping -f option. Flood ping. ",
141             type = Types.Bool,
142             default = False,
143             flags = Flags.ExecReadOnly)
144
145         deadline = Attribute("deadline",
146             "Sets ping -w option. Specify a timeout, in seconds, before ping "
147             "exits regardless of how many packets have been sent or received.",
148             flags = Flags.ExecReadOnly)
149
150         timeout = Attribute("timeout",
151             "Sets ping -W option. Time to wait for a respone in seconds .",
152             flags = Flags.ExecReadOnly)
153
154         target = Attribute("target",
155             "The host to ping .",
156             flags = Flags.ExecReadOnly)
157
158         cls._register_attribute(count)
159         cls._register_attribute(mark)
160         cls._register_attribute(interval)
161         cls._register_attribute(address)
162         cls._register_attribute(preload)
163         cls._register_attribute(numeric)
164         cls._register_attribute(pattern)
165         cls._register_attribute(printtmp)
166         cls._register_attribute(tos)
167         cls._register_attribute(quiet)
168         cls._register_attribute(rec_route)
169         cls._register_attribute(route_bypass)
170         cls._register_attribute(packetsize)
171         cls._register_attribute(sendbuff)
172         cls._register_attribute(ttl)
173         cls._register_attribute(timestamp)
174         cls._register_attribute(hint)
175         cls._register_attribute(full_latency)
176         cls._register_attribute(verbose)
177         cls._register_attribute(flood)
178         cls._register_attribute(deadline)
179         cls._register_attribute(timeout)
180         cls._register_attribute(target)
181
182     def __init__(self, ec, guid):
183         super(LinuxPing, self).__init__(ec, guid)
184         self._home = "ping-%s" % self.guid
185
186     def deploy(self):
187         if not self.get("command"):
188             self.set("command", self._start_command)
189
190         super(LinuxPing, self).deploy()
191
192     @property
193     def _start_command(self):
194         args = []
195
196         if self.get("printTimestamp") == True:
197             args.append("""echo "`date +'%Y%m%d%H%M%S'`";""")
198
199         args.append("ping ")
200         
201         if self.get("count"):
202             args.append("-c %s" % self.get("count"))
203         if self.get("mark"):
204             args.append("-m %s" % self.get("mark"))
205         if self.get("interval"):
206             args.append("-i %s" % self.get("interval"))
207         if self.get("address"):
208             args.append("-I %s" % self.get("address"))
209         if self.get("preload"):
210             args.append("-l %s" % self.get("preload"))
211         if self.get("numeric") == True:
212             args.append("-n")
213         if self.get("pattern"):
214             args.append("-p %s" % self.get("pattern"))
215         if self.get("tos"):
216             args.append("-Q %s" % self.get("tos"))
217         if self.get("quiet"):
218             args.append("-q %s" % self.get("quiet"))
219         if self.get("recordRoute") == True:
220             args.append("-R")
221         if self.get("routeBypass") == True:
222             args.append("-r")
223         if self.get("packetSize"):
224             args.append("-s %s" % self.get("packetSize"))
225         if self.get("sendBuff"):
226             args.append("-S %s" % self.get("sendBuff"))
227         if self.get("ttl"):
228             args.append("-t %s" % self.get("ttl"))
229         if self.get("timestamp"):
230             args.append("-T %s" % self.get("timestamp"))
231         if self.get("hint"):
232             args.append("-M %s" % self.get("hint"))
233         if self.get("fullLatency") == True:
234             args.append("-U")
235         if self.get("verbose") == True:
236             args.append("-v")
237         if self.get("flood") == True:
238             args.append("-f")
239         if self.get("deadline"):
240             args.append("-w %s" % self.get("deadline"))
241         if self.get("timeout"):
242             args.append("-W %s" % self.get("timeout"))
243         args.append(self.get("target"))
244
245         command = " ".join(args)
246
247         return command
248
249     def valid_connection(self, guid):
250         # TODO: Validate!
251         return True
252