Changing ResourceManager naming for platform::ResourceName
[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, ResourceState 
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 = "linux::Mtr"
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.Design)
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.Design)
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.Design)
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.Design)
56
57         countinuous = Attribute("continuous",
58             "Run mtr in a while loop",
59             type = Types.Bool,
60             default = False,
61             flags = Flags.Design)
62
63         print_timestamp = Attribute("printTimestamp",
64             "Print timestamp before running mtr",
65             type = Types.Bool,
66             default = False,
67             flags = Flags.Design)
68
69         target = Attribute("target",
70             "mtr target host (host that will be pinged)",
71             flags = Flags.Design)
72
73         early_start = Attribute("earlyStart",
74             "Start ping as early as deployment. ",
75             type = Types.Bool,
76             default = False,
77             flags = Flags.Design)
78
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)
87
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
92
93     def upload_start_command(self):
94         super(LinuxMtr, self).upload_start_command()
95         
96         if self.get("earlyStart") == True:
97             self._run_in_background()
98
99     def do_deploy(self):
100         if not self.get("command"):
101             self.set("command", self._start_command)
102
103         if not self.get("env"):
104             self.set("env", "PATH=$PATH:/usr/sbin/")
105
106         if not self.get("depends"):
107             self.set("depends", "mtr")
108
109         super(LinuxMtr, self).do_deploy()
110
111     def do_start(self):
112         if self.get("earlyStart") == True:
113             if self.state == ResourceState.READY:
114                 command = self.get("command")
115                 self.info("Starting command '%s'" % command)
116
117                 self.set_started()
118             else:
119                 msg = " Failed to execute command '%s'" % command
120                 self.error(msg, out, err)
121                 raise RuntimeError, msg
122         else:
123            super(LinuxMtr, self).do_start()
124
125     @property
126     def _start_command(self):
127         args = []
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:
136             args.append("-n")
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 ")
142
143         command = " ".join(args)
144
145         return command
146
147     def valid_connection(self, guid):
148         # TODO: Validate!
149         return True
150