Changing ResourceManager naming for platform::ResourceName
[nepi.git] / src / nepi / resources / linux / nping.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 LinuxNPing(LinuxApplication):
29     _rtype = "linux::NPing"
30
31     @classmethod
32     def _register_attributes(cls):
33         c = Attribute("c",
34             "Sets nping -c option. "
35             "Stop after a given number of rounds. ",
36             type = Types.Integer,
37             flags = Flags.Design)
38
39         e = Attribute("e",
40             "Sets nping -e option. "
41             "Set the network interface to be used.",
42             flags = Flags.Design)
43
44         delay = Attribute("delay",
45             "Sets nping --delay option. "
46             "Delay between probes ",
47             flags = Flags.Design)
48
49         rate = Attribute("rate",
50             "Sets nping --rate option. "
51             "Send probes at a given rate ",
52             flags = Flags.Design)
53
54         ttl = Attribute("ttl",
55             "Sets nping --ttl option. "
56             "Time To Live. ",
57             flags = Flags.Design)
58
59         p = Attribute("p",
60             "Sets nping -p option. "
61             "Target ports. ",
62             type = Types.Integer,
63             flags = Flags.Design)
64
65         tcp = Attribute("tcp",
66             "Sets nping --tcp option. "
67             "TCP mode. ",
68             type = Types.Bool,
69             default = False,
70             flags = Flags.Design)
71
72         udp = Attribute("udp",
73             "Sets nping --udp option. "
74             "UDP mode. ",
75             type = Types.Bool,
76             default = False,
77             flags = Flags.Design)
78
79         icmp = Attribute("icmp",
80             "Sets nping --icmp option. "
81             "ICMP mode. ",
82             type = Types.Bool,
83             default = False,
84             flags = Flags.Design)
85
86         arp = Attribute("arp",
87             "Sets nping --arp option. "
88             "ARP mode. ",
89             type = Types.Bool,
90             default = False,
91             flags = Flags.Design)
92
93         traceroute = Attribute("traceroute",
94             "Sets nping --traceroute option. "
95             "Traceroute mode. ",
96             type = Types.Bool,
97             default = False,
98             flags = Flags.Design)
99
100         countinuous = Attribute("continuous",
101             "Run nping in a while loop",
102             type = Types.Bool,
103             default = False,
104             flags = Flags.Design)
105
106         print_timestamp = Attribute("printTimestamp",
107             "Print timestamp before running nping",
108             type = Types.Bool,
109             default = False,
110             flags = Flags.Design)
111
112         target = Attribute("target",
113             "nping target host (host that will be pinged)",
114             flags = Flags.Design)
115
116         cls._register_attribute(c)
117         cls._register_attribute(e)
118         cls._register_attribute(delay)
119         cls._register_attribute(rate)
120         cls._register_attribute(ttl)
121         cls._register_attribute(p)
122         cls._register_attribute(tcp)
123         cls._register_attribute(udp)
124         cls._register_attribute(icmp)
125         cls._register_attribute(arp)
126         cls._register_attribute(traceroute)
127         cls._register_attribute(countinuous)
128         cls._register_attribute(print_timestamp)
129         cls._register_attribute(target)
130
131     def __init__(self, ec, guid):
132         super(LinuxNPing, self).__init__(ec, guid)
133         self._home = "nping-%s" % self.guid
134         self._sudo_kill = True
135
136     def do_deploy(self):
137         if not self.get("command"):
138             self.set("command", self._start_command)
139
140         if not self.get("install"):
141             self.set("install", self._install)
142
143         if not self.get("env"):
144             self.set("env", "PATH=$PATH:/usr/sbin/")
145
146         if not self.get("depends"):
147             self.set("depends", "nmap")
148
149         super(LinuxNPing, self).do_deploy()
150
151     @property
152     def _start_command(self):
153         args = []
154         if self.get("continuous") == True:
155             args.append("while true; do ")
156         if self.get("printTimestamp") == True:
157             args.append("""echo "`date +'%Y%m%d%H%M%S'`";""")
158         args.append("sudo -S nping ")
159         if self.get("c"):
160             args.append("-c %s" % self.get("c"))
161         if self.get("e"):
162             args.append("-e %s" % self.get("e"))
163         if self.get("delay"):
164             args.append("--delay %s" % self.get("delay"))
165         if self.get("rate"):
166             args.append("--rate %s" % self.get("rate"))
167         if self.get("ttl"):
168             args.append("--ttl %s" % self.get("ttl"))
169         if self.get("p"):
170             args.append("-p %s" % self.get("p"))
171         if self.get("tcp") == True:
172             args.append("--tcp")
173         if self.get("udp") == True:
174             args.append("--udp")
175         if self.get("icmp") == True:
176             args.append("--icmp")
177         if self.get("arp") == True:
178             args.append("--arp")
179         if self.get("traceroute") == True:
180             args.append("--traceroute")
181
182         args.append(self.get("target"))
183
184         if self.get("continuous") == True:
185             args.append("; done ")
186
187         command = " ".join(args)
188
189         return command
190
191     @property
192     def _install(self):
193         install  = "echo 'nothing to do'"
194         if self.node.use_rpm:
195             install = (
196                 " ( "
197                 "  ( "
198                 "   if [ `uname -m` == 'x86_64' ]; then "
199                 "     wget -O nping.rpm http://nmap.org/dist/nping-0.6.25-1.x86_64.rpm ;"
200                 "   else wget -O nping.rpm http://nmap.org/dist/nping-0.6.25-1.i386.rpm ;"
201                 "   fi "
202                 " )"
203                 " && sudo -S rpm -vhU nping.rpm ) ")
204         elif self.node.use_deb:
205             from nepi.resources.linux import debfuncs 
206             install_alien = debfuncs.install_packages_command(self.node.os, "alien gcc")
207             install = (
208                 " ( "
209                 "  ( "
210                 "   if [ `uname -m` == 'x86_64' ]; then "
211                 "     wget -O nping.rpm http://nmap.org/dist/nping-0.6.25-1.x86_64.rpm ;"
212                 "   else wget -O nping.rpm http://nmap.org/dist/nping-0.6.25-1.i386.rpm ;"
213                 "   fi "
214                 " )"
215                 " && %s && sudo alien -i nping.rpm ) " % install_alien)
216
217         return ("( nping --version || %s )" % install)
218
219     def valid_connection(self, guid):
220         # TODO: Validate!
221         return True
222