Added LinuxPing and LinuxMtr RMs
[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 
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         target = Attribute("target",
58             "mtr target host (host that will be pinged)",
59             flags = Flags.ExecReadOnly)
60
61         cls._register_attribute(report_cycles)
62         cls._register_attribute(no_dns)
63         cls._register_attribute(address)
64         cls._register_attribute(interval)
65         cls._register_attribute(target)
66
67     def __init__(self, ec, guid):
68         super(LinuxMtr, self).__init__(ec, guid)
69         self._home = "mtr-%s" % self.guid
70
71     def deploy(self):
72         if not self.get("command"):
73             self.set("command", self._start_command)
74
75         if not self.get("depends"):
76             self.set("depends", "mtr")
77
78         super(LinuxMtr, self).deploy()
79
80     @property
81     def _start_command(self):
82         args = []
83         if self.get("reportCycles"):
84             args.append("-c %s" % self.get("reportCycles"))
85         if self.get("noDns") == True:
86             args.append("-n")
87         if self.get("address"):
88             args.append("-a %s" % self.get("address"))
89         args.append(self.get("target"))
90
91         command = """echo "Starting mtr `date +'%Y%m%d%H%M%S'`"; """
92         command += " sudo -S mtr --report "
93         command += " ".join(args)
94
95         return command
96
97     def valid_connection(self, guid):
98         # TODO: Validate!
99         return True
100