Adding LinuxTraceroute RM
[nepi.git] / src / nepi / resources / linux / traceroute.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 LinuxTraceroute(LinuxApplication):
29     _rtype = "LinuxTraceroute"
30
31     @classmethod
32     def _register_attributes(cls):
33         countinuous = Attribute("continuous",
34             "Run traceroute in a while loop",
35             type = Types.Bool,
36             default = False,
37             flags = Flags.ExecReadOnly)
38
39         print_timestamp = Attribute("printTimestamp",
40             "Print timestamp before running traceroute",
41             type = Types.Bool,
42             default = False,
43             flags = Flags.ExecReadOnly)
44
45         target = Attribute("target",
46             "Traceroute target host (host that will be pinged)",
47             flags = Flags.ExecReadOnly)
48
49         cls._register_attribute(countinuous)
50         cls._register_attribute(print_timestamp)
51         cls._register_attribute(target)
52
53     def __init__(self, ec, guid):
54         super(LinuxTraceroute, self).__init__(ec, guid)
55         self._home = "traceroute-%s" % self.guid
56
57     def deploy(self):
58         if not self.get("command"):
59             self.set("command", self._start_command)
60
61         if not self.get("depends"):
62             self.set("depends", "traceroute")
63
64         super(LinuxTraceroute, self).deploy()
65
66     @property
67     def _start_command(self):
68         args = []
69         if self.get("continuous") == True:
70             args.append("while true; do ")
71         if self.get("printTimestamp") == True:
72             args.append("""echo "`date +'%Y%m%d%H%M%S'`";""")
73         args.append("traceroute")
74         args.append(self.get("target"))
75         if self.get("continuous") == True:
76             args.append("; done ")
77
78         command = " ".join(args)
79
80         return command
81
82     def valid_connection(self, guid):
83         # TODO: Validate!
84         return True
85