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