544e66b8e27b9fc213014ee900599d39793bf5db
[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, ResourceState 
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 = "linux::Ping"
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             type = Types.Integer,
37             flags = Flags.Design)
38
39         mark = Attribute("mark",
40             "Sets ping -m option. Uses 'mark' to tag outgoing packets. ",
41             flags = Flags.Design)
42
43         interval = Attribute("interval",
44             "Sets ping -i option. Leaves interval seconds between "
45             "successive ECHO_REUQEST packets. ",
46             flags = Flags.Design)
47
48         address = Attribute("address",
49             "Sets ping -I option. Sets ECHO_REQUEST packets souce address "
50             "to the specified interface address ",
51             flags = Flags.Design)
52
53         preload = Attribute("preload",
54             "Sets ping -l option. Sends preload amount of packets "
55             "without waiting for a reply ",
56             flags = Flags.Design)
57
58         numeric = Attribute("numeric",
59             "Sets ping -n option. Disables resolution of host addresses into "
60             "symbolic names. ",
61             type = Types.Bool,
62             default = False,
63             flags = Flags.Design)
64
65         pattern = Attribute("pattern",
66             "Sets ping -p option. Species a up to 16 ''pad'' bytes to fill "
67             "out sent packets. ",
68             flags = Flags.Design)
69
70         printtmp = Attribute("printTimestamp",
71             "Sets ping -D option. Prints timestamp befor each line as: "
72             "unix time + microseconds as in gettimeofday ",
73             type = Types.Bool,
74             default = False,
75             flags = Flags.Design)
76
77         tos = Attribute("tos",
78             "Sets ping -Q option. Sets Quality of Service related bits in ICMP "
79             "datagrams. tos can be either a decimal or hexadecime number ",
80             flags = Flags.Design)
81
82         quiet = Attribute("quiet",
83             "Sets ping -q option. Disables ping standard output ",
84             type = Types.Bool,
85             default = False,
86             flags = Flags.Design)
87
88         rec_route = Attribute("recordRoute",
89             "Sets ping -R option. Includes the RECORD_ROUTE option in the "
90             "ECHO REQUEST packet and displays route buffer on the Disables "
91             "ping standard output.",
92             type = Types.Bool,
93             default = False,
94             flags = Flags.Design)
95
96         route_bypass = Attribute("routeBypass",
97             "Sets ping -r option. Bypasses normal routing tables and sends "
98             "ECHO REQUEST packets directly yo a host on an attached interface. ",
99             type = Types.Bool,
100             default = False,
101             flags = Flags.Design)
102
103         packetsize = Attribute("packetSize",
104             "Sets ping -s option. Specifies the number of data bytes to be "
105             "sent. Defaults to 56. ",
106             flags = Flags.Design)
107
108         sendbuff = Attribute("sendBuff",
109             "Sets ping -S option. Specifies the number of packets to buffer. "
110             "Defaults to one. ",
111             flags = Flags.Design)
112
113         ttl = Attribute("ttl",
114             "Sets ping -t option. Specifies the IP Time to Live for the "
115             "packets. ",
116             flags = Flags.Design)
117
118         timestamp = Attribute("timestamp",
119             "Sets ping -T option. Sets special IP timestamp options. ",
120             flags = Flags.Design)
121
122         hint = Attribute("hint",
123             "Sets ping -M option. Selects Path MTU Discovery strategy. ",
124             flags = Flags.Design)
125
126         full_latency = Attribute("fullLatency",
127             "Sets ping -U option. Calculates round trip time taking into "
128             "account the full user-to-user latency instead of only the "
129             "network round trip time. ",
130             type = Types.Bool,
131             default = False,
132             flags = Flags.Design)
133
134         verbose = Attribute("verbose",
135             "Sets ping -v option. Verbose output. ",
136             type = Types.Bool,
137             default = False,
138             flags = Flags.Design)
139
140         flood = Attribute("flood",
141             "Sets ping -f option. Flood ping. ",
142             type = Types.Bool,
143             default = False,
144             flags = Flags.Design)
145
146         deadline = Attribute("deadline",
147             "Sets ping -w option. Specify a timeout, in seconds, before ping "
148             "exits regardless of how many packets have been sent or received.",
149             flags = Flags.Design)
150
151         timeout = Attribute("timeout",
152             "Sets ping -W option. Time to wait for a respone in seconds .",
153             flags = Flags.Design)
154
155         target = Attribute("target",
156             "The host to ping .",
157             flags = Flags.Design)
158
159         early_start = Attribute("earlyStart",
160             "Start ping as early as deployment. ",
161             type = Types.Bool,
162             default = False,
163             flags = Flags.Design)
164
165         cls._register_attribute(count)
166         cls._register_attribute(mark)
167         cls._register_attribute(interval)
168         cls._register_attribute(address)
169         cls._register_attribute(preload)
170         cls._register_attribute(numeric)
171         cls._register_attribute(pattern)
172         cls._register_attribute(printtmp)
173         cls._register_attribute(tos)
174         cls._register_attribute(quiet)
175         cls._register_attribute(rec_route)
176         cls._register_attribute(route_bypass)
177         cls._register_attribute(packetsize)
178         cls._register_attribute(sendbuff)
179         cls._register_attribute(ttl)
180         cls._register_attribute(timestamp)
181         cls._register_attribute(hint)
182         cls._register_attribute(full_latency)
183         cls._register_attribute(verbose)
184         cls._register_attribute(flood)
185         cls._register_attribute(deadline)
186         cls._register_attribute(timeout)
187         cls._register_attribute(target)
188         cls._register_attribute(early_start)
189
190     def __init__(self, ec, guid):
191         super(LinuxPing, self).__init__(ec, guid)
192         self._home = "ping-%s" % self.guid
193
194     def upload_start_command(self):
195         super(LinuxPing, self).upload_start_command()
196         
197         if self.get("earlyStart") == True:
198             self._run_in_background()
199
200     def do_deploy(self):
201         if not self.get("command"):
202             self.set("command", self._start_command)
203
204         super(LinuxPing, self).do_deploy()
205
206     def do_start(self):
207         if self.get("earlyStart") == True:
208             if self.state == ResourceState.READY:
209                 command = self.get("command")
210                 self.info("Starting command '%s'" % command)
211
212                 self.set_started()
213             else:
214                 msg = " Failed to execute command '%s'" % command
215                 self.error(msg, out, err)
216                 raise RuntimeError, msg
217         else:
218            super(LinuxPing, self).do_start()
219
220     @property
221     def _start_command(self):
222         args = []
223
224         args.append("echo 'Starting PING to %s' ;" % self.get("target"))
225
226         if self.get("printTimestamp") == True:
227             args.append("""echo "`date +'%Y%m%d%H%M%S'`";""")
228
229         args.append("ping ")
230         
231         if self.get("count"):
232             args.append("-c %s" % self.get("count"))
233         if self.get("mark"):
234             args.append("-m %s" % self.get("mark"))
235         if self.get("interval"):
236             args.append("-i %s" % self.get("interval"))
237         if self.get("address"):
238             args.append("-I %s" % self.get("address"))
239         if self.get("preload"):
240             args.append("-l %s" % self.get("preload"))
241         if self.get("numeric") == True:
242             args.append("-n")
243         if self.get("pattern"):
244             args.append("-p %s" % self.get("pattern"))
245         if self.get("tos"):
246             args.append("-Q %s" % self.get("tos"))
247         if self.get("quiet"):
248             args.append("-q %s" % self.get("quiet"))
249         if self.get("recordRoute") == True:
250             args.append("-R")
251         if self.get("routeBypass") == True:
252             args.append("-r")
253         if self.get("packetSize"):
254             args.append("-s %s" % self.get("packetSize"))
255         if self.get("sendBuff"):
256             args.append("-S %s" % self.get("sendBuff"))
257         if self.get("ttl"):
258             args.append("-t %s" % self.get("ttl"))
259         if self.get("timestamp"):
260             args.append("-T %s" % self.get("timestamp"))
261         if self.get("hint"):
262             args.append("-M %s" % self.get("hint"))
263         if self.get("fullLatency") == True:
264             args.append("-U")
265         if self.get("verbose") == True:
266             args.append("-v")
267         if self.get("flood") == True:
268             args.append("-f")
269         if self.get("deadline"):
270             args.append("-w %s" % self.get("deadline"))
271         if self.get("timeout"):
272             args.append("-W %s" % self.get("timeout"))
273         args.append(self.get("target"))
274
275         command = " ".join(args)
276
277         return command
278
279     def valid_connection(self, guid):
280         # TODO: Validate!
281         return True
282