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 as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
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.
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/>.
18 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
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
28 class LinuxMtr(LinuxApplication):
32 def _register_attributes(cls):
33 report_cycles = Attribute("reportCycles",
34 "sets mtr --report-cycles (-c) option. Determines the number of "
35 "pings sent to determine both machines in the networks. Each "
36 "cycle lasts one sencond.",
39 no_dns = Attribute("noDns",
40 "sets mtr --no-dns (-n) option. Forces mtr to display IPs intead of "
41 "trying to resolve to host names ",
46 address = Attribute("address",
47 "sets mtr --address (-a) option. Binds the socket to send outgoing "
48 "packets to the interface of the specified address, so that any "
49 "any packets are sent through this interface. ",
52 interval = Attribute("interval",
53 "sets mtr --interval (-i) option. Specifies the number of seconds "
54 "between ICMP ECHO requests. Default value is one second ",
57 countinuous = Attribute("continuous",
58 "Run mtr in a while loop",
63 print_timestamp = Attribute("printTimestamp",
64 "Print timestamp before running mtr",
69 target = Attribute("target",
70 "mtr target host (host that will be pinged)",
73 early_start = Attribute("earlyStart",
74 "Start ping as early as deployment. ",
79 cls._register_attribute(report_cycles)
80 cls._register_attribute(no_dns)
81 cls._register_attribute(address)
82 cls._register_attribute(interval)
83 cls._register_attribute(countinuous)
84 cls._register_attribute(print_timestamp)
85 cls._register_attribute(target)
86 cls._register_attribute(early_start)
88 def __init__(self, ec, guid):
89 super(LinuxMtr, self).__init__(ec, guid)
90 self._home = "mtr-%s" % self.guid
91 self._sudo_kill = True
93 def upload_start_command(self):
94 super(LinuxMtr, self).upload_start_command()
96 if self.get("earlyStart") == True:
97 self._run_in_background()
100 if not self.get("command"):
101 self.set("command", self._start_command)
103 if not self.get("env"):
104 self.set("env", "PATH=$PATH:/usr/sbin/")
106 if not self.get("depends"):
107 self.set("depends", "mtr")
109 super(LinuxMtr, self).do_deploy()
112 if self.get("earlyStart") == True:
113 if self.state == ResourceState.READY:
114 command = self.get("command")
115 self.info("Starting command '%s'" % command)
119 msg = " Failed to execute command '%s'" % command
120 self.error(msg, out, err)
121 raise RuntimeError, msg
123 super(LinuxMtr, self).do_start()
126 def _start_command(self):
128 if self.get("continuous") == True:
129 args.append("while true; do ")
130 if self.get("printTimestamp") == True:
131 args.append("""echo "`date +'%Y%m%d%H%M%S'`";""")
132 args.append("sudo -S mtr --report")
133 if self.get("reportCycles"):
134 args.append("-c %s" % self.get("reportCycles"))
135 if self.get("noDns") == True:
137 if self.get("address"):
138 args.append("-a %s" % self.get("address"))
139 args.append(self.get("target"))
140 if self.get("continuous") == True:
141 args.append("; done ")
143 command = " ".join(args)
147 def valid_connection(self, guid):