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