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
28 class LinuxTraceroute(LinuxApplication):
29 _rtype = "linux::Traceroute"
32 def _register_attributes(cls):
33 countinuous = Attribute("continuous",
34 "Run traceroute in a while loop",
39 print_timestamp = Attribute("printTimestamp",
40 "Print timestamp before running traceroute",
45 use_ip = Attribute("useIP",
46 "Use the IP address instead of the host domain name. "
47 "Useful for environments were dns resolution problems occur "
53 target = Attribute("target",
54 "Traceroute target host (host that will be pinged)",
57 early_start = Attribute("earlyStart",
58 "Start ping as early as deployment. ",
63 cls._register_attribute(countinuous)
64 cls._register_attribute(print_timestamp)
65 cls._register_attribute(use_ip)
66 cls._register_attribute(target)
67 cls._register_attribute(early_start)
69 def __init__(self, ec, guid):
70 super(LinuxTraceroute, self).__init__(ec, guid)
71 self._home = "traceroute-%s" % self.guid
73 def upload_start_command(self):
74 super(LinuxTraceroute, self).upload_start_command()
76 if self.get("earlyStart") == True:
77 self._run_in_background()
80 if not self.get("command"):
81 self.set("command", self._start_command)
83 if not self.get("depends"):
84 self.set("depends", "traceroute")
86 super(LinuxTraceroute, self).do_deploy()
89 if self.get("earlyStart") == True:
90 if self.state == ResourceState.READY:
91 command = self.get("command")
92 self.info("Starting command '%s'" % command)
96 msg = " Failed to execute command '%s'" % command
97 self.error(msg, out, err)
98 raise RuntimeError(msg)
100 super(LinuxTraceroute, self).do_start()
103 def _start_command(self):
105 if self.get("continuous") == True:
106 args.append("while true; do ")
107 if self.get("printTimestamp") == True:
108 args.append("""echo "`date +'%Y%m%d%H%M%S'`";""")
109 args.append("traceroute")
111 target = self.get("target")
112 if self.get("useIP") == True:
113 target = socket.gethostbyname(target)
116 if self.get("continuous") == True:
117 args.append("; sleep 2 ; done ")
119 command = " ".join(args)
123 def valid_connection(self, guid):