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