2 # NEPI, a framework to manage network experiments
3 # Copyright (C) 2013 INRIA
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License version 2 as
7 # published by the Free Software Foundation;
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
19 from nepi.execution.attribute import Attribute, Flags, Types
20 from nepi.execution.resource import clsinit_copy, ResourceState
21 from nepi.resources.linux.application import LinuxApplication
22 from nepi.util.timefuncs import tnow
27 class LinuxPing(LinuxApplication):
28 _rtype = "linux::Ping"
31 def _register_attributes(cls):
32 count = Attribute("count",
33 "Sets ping -c option. Determines the number of ECHO_REQUEST "
34 "packates to send before stopping.",
38 mark = Attribute("mark",
39 "Sets ping -m option. Uses 'mark' to tag outgoing packets. ",
42 interval = Attribute("interval",
43 "Sets ping -i option. Leaves interval seconds between "
44 "successive ECHO_REUQEST packets. ",
47 address = Attribute("address",
48 "Sets ping -I option. Sets ECHO_REQUEST packets souce address "
49 "to the specified interface address ",
52 preload = Attribute("preload",
53 "Sets ping -l option. Sends preload amount of packets "
54 "without waiting for a reply ",
57 numeric = Attribute("numeric",
58 "Sets ping -n option. Disables resolution of host addresses into "
64 pattern = Attribute("pattern",
65 "Sets ping -p option. Species a up to 16 ''pad'' bytes to fill "
69 printtmp = Attribute("printTimestamp",
70 "Sets ping -D option. Prints timestamp befor each line as: "
71 "unix time + microseconds as in gettimeofday ",
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 ",
81 quiet = Attribute("quiet",
82 "Sets ping -q option. Disables ping standard output ",
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.",
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. ",
100 flags = Flags.Design)
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.Design)
107 sendbuff = Attribute("sendBuff",
108 "Sets ping -S option. Specifies the number of packets to buffer. "
110 flags = Flags.Design)
112 ttl = Attribute("ttl",
113 "Sets ping -t option. Specifies the IP Time to Live for the "
115 flags = Flags.Design)
117 timestamp = Attribute("timestamp",
118 "Sets ping -T option. Sets special IP timestamp options. ",
119 flags = Flags.Design)
121 hint = Attribute("hint",
122 "Sets ping -M option. Selects Path MTU Discovery strategy. ",
123 flags = Flags.Design)
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. ",
131 flags = Flags.Design)
133 verbose = Attribute("verbose",
134 "Sets ping -v option. Verbose output. ",
137 flags = Flags.Design)
139 flood = Attribute("flood",
140 "Sets ping -f option. Flood ping. ",
143 flags = Flags.Design)
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.Design)
150 timeout = Attribute("timeout",
151 "Sets ping -W option. Time to wait for a respone in seconds .",
152 flags = Flags.Design)
154 target = Attribute("target",
155 "The host to ping .",
156 flags = Flags.Design)
158 early_start = Attribute("earlyStart",
159 "Start ping as early as deployment. ",
162 flags = Flags.Design)
164 cls._register_attribute(count)
165 cls._register_attribute(mark)
166 cls._register_attribute(interval)
167 cls._register_attribute(address)
168 cls._register_attribute(preload)
169 cls._register_attribute(numeric)
170 cls._register_attribute(pattern)
171 cls._register_attribute(printtmp)
172 cls._register_attribute(tos)
173 cls._register_attribute(quiet)
174 cls._register_attribute(rec_route)
175 cls._register_attribute(route_bypass)
176 cls._register_attribute(packetsize)
177 cls._register_attribute(sendbuff)
178 cls._register_attribute(ttl)
179 cls._register_attribute(timestamp)
180 cls._register_attribute(hint)
181 cls._register_attribute(full_latency)
182 cls._register_attribute(verbose)
183 cls._register_attribute(flood)
184 cls._register_attribute(deadline)
185 cls._register_attribute(timeout)
186 cls._register_attribute(target)
187 cls._register_attribute(early_start)
189 def __init__(self, ec, guid):
190 super(LinuxPing, self).__init__(ec, guid)
191 self._home = "ping-%s" % self.guid
193 def upload_start_command(self):
194 super(LinuxPing, self).upload_start_command()
196 if self.get("earlyStart") == True:
197 self._run_in_background()
200 if not self.get("command"):
201 self.set("command", self._start_command)
203 super(LinuxPing, self).do_deploy()
206 if self.get("earlyStart") == True:
207 if self.state == ResourceState.READY:
208 command = self.get("command")
209 self.info("Starting command '%s'" % command)
213 msg = " Failed to execute command '%s'" % command
214 self.error(msg, out, err)
215 raise RuntimeError, msg
217 super(LinuxPing, self).do_start()
220 def _start_command(self):
223 args.append("echo 'Starting PING to %s' ;" % self.get("target"))
225 if self.get("printTimestamp") == True:
226 args.append("""echo "`date +'%Y%m%d%H%M%S'`";""")
230 if self.get("count"):
231 args.append("-c %s" % self.get("count"))
233 args.append("-m %s" % self.get("mark"))
234 if self.get("interval"):
235 args.append("-i %s" % self.get("interval"))
236 if self.get("address"):
237 args.append("-I %s" % self.get("address"))
238 if self.get("preload"):
239 args.append("-l %s" % self.get("preload"))
240 if self.get("numeric") == True:
242 if self.get("pattern"):
243 args.append("-p %s" % self.get("pattern"))
245 args.append("-Q %s" % self.get("tos"))
246 if self.get("quiet"):
247 args.append("-q %s" % self.get("quiet"))
248 if self.get("recordRoute") == True:
250 if self.get("routeBypass") == True:
252 if self.get("packetSize"):
253 args.append("-s %s" % self.get("packetSize"))
254 if self.get("sendBuff"):
255 args.append("-S %s" % self.get("sendBuff"))
257 args.append("-t %s" % self.get("ttl"))
258 if self.get("timestamp"):
259 args.append("-T %s" % self.get("timestamp"))
261 args.append("-M %s" % self.get("hint"))
262 if self.get("fullLatency") == True:
264 if self.get("verbose") == True:
266 if self.get("flood") == True:
268 if self.get("deadline"):
269 args.append("-w %s" % self.get("deadline"))
270 if self.get("timeout"):
271 args.append("-W %s" % self.get("timeout"))
272 args.append(self.get("target"))
274 command = " ".join(args)
278 def valid_connection(self, guid):