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
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.",
37 flags = Flags.ExecReadOnly)
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 ",
44 flags = Flags.ExecReadOnly)
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. ",
50 flags = Flags.ExecReadOnly)
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 ",
55 flags = Flags.ExecReadOnly)
57 countinuous = Attribute("continuous",
58 "Run mtr in a while loop",
61 flags = Flags.ExecReadOnly)
63 print_timestamp = Attribute("printTimestamp",
64 "Print timestamp before running mtr",
67 flags = Flags.ExecReadOnly)
69 target = Attribute("target",
70 "mtr target host (host that will be pinged)",
71 flags = Flags.ExecReadOnly)
73 cls._register_attribute(report_cycles)
74 cls._register_attribute(no_dns)
75 cls._register_attribute(address)
76 cls._register_attribute(interval)
77 cls._register_attribute(countinuous)
78 cls._register_attribute(print_timestamp)
79 cls._register_attribute(target)
81 def __init__(self, ec, guid):
82 super(LinuxMtr, self).__init__(ec, guid)
83 self._home = "mtr-%s" % self.guid
84 self._sudo_kill = True
87 if not self.get("command"):
88 self.set("command", self._start_command)
90 if not self.get("env"):
91 self.set("env", "PATH=$PATH:/usr/sbin/")
93 if not self.get("depends"):
94 self.set("depends", "mtr")
96 super(LinuxMtr, self).deploy()
99 def _start_command(self):
101 if self.get("continuous") == True:
102 args.append("while true; do ")
103 if self.get("printTimestamp") == True:
104 args.append("""echo "`date +'%Y%m%d%H%M%S'`";""")
105 args.append("sudo -S mtr --report")
106 if self.get("reportCycles"):
107 args.append("-c %s" % self.get("reportCycles"))
108 if self.get("noDns") == True:
110 if self.get("address"):
111 args.append("-a %s" % self.get("address"))
112 args.append(self.get("target"))
113 if self.get("continuous") == True:
114 args.append("; done ")
116 command = " ".join(args)
120 def valid_connection(self, guid):