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 LinuxMtr(LinuxApplication):
31 def _register_attributes(cls):
32 report_cycles = Attribute("reportCycles",
33 "sets mtr --report-cycles (-c) option. Determines the number of "
34 "pings sent to determine both machines in the networks. Each "
35 "cycle lasts one sencond.",
38 no_dns = Attribute("noDns",
39 "sets mtr --no-dns (-n) option. Forces mtr to display IPs intead of "
40 "trying to resolve to host names ",
45 address = Attribute("address",
46 "sets mtr --address (-a) option. Binds the socket to send outgoing "
47 "packets to the interface of the specified address, so that any "
48 "any packets are sent through this interface. ",
51 interval = Attribute("interval",
52 "sets mtr --interval (-i) option. Specifies the number of seconds "
53 "between ICMP ECHO requests. Default value is one second ",
56 countinuous = Attribute("continuous",
57 "Run mtr in a while loop",
62 print_timestamp = Attribute("printTimestamp",
63 "Print timestamp before running mtr",
68 target = Attribute("target",
69 "mtr target host (host that will be pinged)",
72 early_start = Attribute("earlyStart",
73 "Start ping as early as deployment. ",
78 cls._register_attribute(report_cycles)
79 cls._register_attribute(no_dns)
80 cls._register_attribute(address)
81 cls._register_attribute(interval)
82 cls._register_attribute(countinuous)
83 cls._register_attribute(print_timestamp)
84 cls._register_attribute(target)
85 cls._register_attribute(early_start)
87 def __init__(self, ec, guid):
88 super(LinuxMtr, self).__init__(ec, guid)
89 self._home = "mtr-%s" % self.guid
90 self._sudo_kill = True
92 def upload_start_command(self):
93 super(LinuxMtr, self).upload_start_command()
95 if self.get("earlyStart") == True:
96 self._run_in_background()
99 if not self.get("command"):
100 self.set("command", self._start_command)
102 if not self.get("env"):
103 self.set("env", "PATH=$PATH:/usr/sbin/")
105 if not self.get("depends"):
106 self.set("depends", "mtr")
108 super(LinuxMtr, self).do_deploy()
111 if self.get("earlyStart") == True:
112 if self.state == ResourceState.READY:
113 command = self.get("command")
114 self.info("Starting command '%s'" % command)
118 msg = " Failed to execute command '%s'" % command
119 self.error(msg, out, err)
120 raise RuntimeError, msg
122 super(LinuxMtr, self).do_start()
125 def _start_command(self):
127 if self.get("continuous") == True:
128 args.append("while true; do ")
129 if self.get("printTimestamp") == True:
130 args.append("""echo "`date +'%Y%m%d%H%M%S'`";""")
131 args.append("sudo -S mtr --report")
132 if self.get("reportCycles"):
133 args.append("-c %s" % self.get("reportCycles"))
134 if self.get("noDns") == True:
136 if self.get("address"):
137 args.append("-a %s" % self.get("address"))
138 args.append(self.get("target"))
139 if self.get("continuous") == True:
140 args.append("; done ")
142 command = " ".join(args)
146 def valid_connection(self, guid):