merged ex_shutdown into nepi-3-dev
[nepi.git] / src / nepi / resources / linux / mtr.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, failtrap 
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 LinuxMtr(LinuxApplication):
29     _rtype = "LinuxMtr"
30
31     @classmethod
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)
38
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 ",
42             type = Types.Bool,
43             default = True,
44             flags = Flags.ExecReadOnly)
45
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)
51
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)
56
57         countinuous = Attribute("continuous",
58             "Run mtr in a while loop",
59             type = Types.Bool,
60             default = False,
61             flags = Flags.ExecReadOnly)
62
63         print_timestamp = Attribute("printTimestamp",
64             "Print timestamp before running mtr",
65             type = Types.Bool,
66             default = False,
67             flags = Flags.ExecReadOnly)
68
69         target = Attribute("target",
70             "mtr target host (host that will be pinged)",
71             flags = Flags.ExecReadOnly)
72
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)
80
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
85
86     @failtrap
87     def deploy(self):
88         if not self.get("command"):
89             self.set("command", self._start_command)
90
91         if not self.get("env"):
92             self.set("env", "PATH=$PATH:/usr/sbin/")
93
94         if not self.get("depends"):
95             self.set("depends", "mtr")
96
97         super(LinuxMtr, self).deploy()
98
99     @property
100     def _start_command(self):
101         args = []
102         if self.get("continuous") == True:
103             args.append("while true; do ")
104         if self.get("printTimestamp") == True:
105             args.append("""echo "`date +'%Y%m%d%H%M%S'`";""")
106         args.append("sudo -S mtr --report")
107         if self.get("reportCycles"):
108             args.append("-c %s" % self.get("reportCycles"))
109         if self.get("noDns") == True:
110             args.append("-n")
111         if self.get("address"):
112             args.append("-a %s" % self.get("address"))
113         args.append(self.get("target"))
114         if self.get("continuous") == True:
115             args.append("; done ")
116
117         command = " ".join(args)
118
119         return command
120
121     def valid_connection(self, guid):
122         # TODO: Validate!
123         return True
124