-
Notifications
You must be signed in to change notification settings - Fork 2
/
fault_injector_noshutdown.py
287 lines (223 loc) · 11 KB
/
fault_injector_noshutdown.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
from threading import Thread
import subprocess
import thread
import time
import datetime
import numpy
import argparse
import pandas as pd
import numpy as np
#e.g.
# python fault_injector.py --component nginx --monitoring-interval 5 --fault-rate 10 --repair-rate 5
# python fault_injector.py -c nginx -i 5 -f 10 -r 5
#function fault injection in the application
def fault_injection_application(app_names,fault, repair, fault_command, repair_command):
global killapp
global password
global ip_address
while 1:
fault_time = np.mean(numpy.random.exponential(fault, 2))
print app_names + " UP, next FAULT will be in: " + str(fault_time) + "s"
time.sleep(fault_time)
repair_time = np.mean(numpy.random.exponential(repair, 2))
fault_command = "sshpass -p " + password + " ssh -f -o StrictHostKeyChecking=no " + ip_address + " -l root \"ifconfig wlan0 down && sleep " + str(int(repair_time)) + " && ifconfig wlan0 up &>/dev/null &\""
while is_os_alive(ip_address) == '0':
pass
subprocess.Popen(fault_command, shell=True, stdout=subprocess.PIPE)
print app_names + " DOWN, next REPAIR will be in: " + str(repair_time) + "s"
time.sleep(repair_time)
# subprocess.Popen(repair_command, shell=True, stdout=subprocess.PIPE).stdout.read().rstrip()
# fault_time = np.mean(numpy.random.exponential(fault, 2))
# print app_names + " UP, next FAULT will be in: " + str(fault_time) + "s"
# time.sleep(fault_time)
# while is_os_alive(ip_address) == '0':
# pass
# output = subprocess.check_output(['bash','-c', fault_command])
# repair_time = np.mean(numpy.random.exponential(repair, 2))
# print app_names + " DOWN, next REPAIR will be in: " + str(repair_time) + "s"
# time.sleep(repair_time)
# while is_os_alive(ip_address) == '0':
# pass
# output = subprocess.check_output(['bash','-c', repair_command])
#function fault injection in the OS
def fault_injection_os(fault, repair):
global killapp
global killos
global fault_app
global repair_app
global password
global ip_address
global app_name
global command_stop_app
global command_start_app
while True:
fault_time = np.mean(numpy.random.exponential(fault, 2))
print "OS UP, next FAULT will be: " + str(fault_time) + "s"
time.sleep(fault_time)
repair_time = np.mean(numpy.random.exponential(repair, 2))
fault_command = "sshpass -p " + password + " ssh -f -o StrictHostKeyChecking=no " + ip_address + " -l root \"ifconfig wlan0 down && sleep " + str(int(repair_time)) + " && ifconfig wlan0 up &>/dev/null &\""
subprocess.Popen(fault_command, shell=True, stdout=subprocess.PIPE)
print "OS DOWN, next REPAIR will be: " + str(repair_time) + "s"
time.sleep(repair_time)
# subprocess.Popen(repair_command, shell=True, stdout=subprocess.PIPE).stdout.read().rstrip()
while is_os_alive(ip_address) == '0':
pass
# if killos == True: break
# for i in range(len(app_name)):
# if not ("service" in command_start_app[i] or "start" in command_start_app[i] or "systemctl" in command_start_app[i]):
# tapps = Thread(target=fault_injection_application, args=(app_name[i],float(fault_app),float(repair_app),
# "sshpass -p " + password + " ssh -f -o StrictHostKeyChecking=no " + ip_address + " -l root killall " + app_name[i],
# "sshpass -p " + password + " ssh -f -o StrictHostKeyChecking=no " + ip_address + " -l root \"" + command_start_app[i] + " . &>/dev/null &\""))
# else:
# tapps = Thread(target=fault_injection_application, args=(app_name[i],float(fault_app),float(repair_app),
# "sshpass -p " + password + " ssh -f -o StrictHostKeyChecking=no " + ip_address + " -l root killall " + app_name[i],
# "sshpass -p " + password + " ssh -f -o StrictHostKeyChecking=no " + ip_address + " -l root " + command_start_app[i] + " . &>/dev/null &\""))
# tapps.daemon = True
# tapps.start()
# tapplications.append(tapps)
def fault_injection_hw(fault, repair):
global killapp
global killos
global fault_os
global repair_os
global password
global ip_address
global address_mac
# if is_os_alive(ip_address) == '0':
# time.sleep(900)
# tos = Thread(target=fault_injection_os, args=(float(fault_os),float(repair_os),
# "sshpass -p " + password + " ssh -n -o StrictHostKeyChecking=no " + ip_address + " -l root shutdown -h now",
# "sudo wakeonlan " + address_mac))
# tos.daemon = True
# tos.start()
while True:
fault_time = np.mean(numpy.random.exponential(fault, 2))
print "HARDWARE UP, next FAULT will be: " + str(fault_time) + "s"
time.sleep(fault_time)
repair_time = np.mean(numpy.random.exponential(repair, 2))
fault_command = "sshpass -p " + password + " ssh -f -o StrictHostKeyChecking=no " + ip_address + " -l root \"ifconfig wlan0 down && sleep " + str(int(repair_time)) + " && ifconfig wlan0 up . &>/dev/null &\""
while is_os_alive(ip_address) == '0':
pass
print "HARDWARE DOWN, next REPAIR will be: " + str(repair_time) + "s"
subprocess.Popen(fault_command, shell=True, stdout=subprocess.PIPE)
time.sleep(repair_time)
#monitor
def monitor(app_names, monitor_time, password, ip_address):
while True:
output = verify_service(app_names, password, ip_address)
if output=='0':
with open("monitoramento_raspberry.txt", "a") as myfile:
data = datetime.datetime.now()
print str(data) + " service D 1"
myfile.write(str(data) + " service D 1" + "\n")
elif output!='0':
with open("monitoramento_raspberry.txt", "a") as myfile:
data = datetime.datetime.now()
print str(data) + " service U 1"
myfile.write(str(data) + " service U 1" + "\n")
time.sleep(monitor_time)
def verify_service(app_names, password, ip_address):
command = "ping -w 1 " + ip_address + " | grep received | awk '{print $4}'"
output = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE).stdout.read().rstrip()
# if output != '0':
# #command = "sshpass -p " + password + " ssh -o StrictHostKeyChecking=no " + ip_address + " -l root \"if systemctl status " + service_name + ".service | grep \'Active: active\' > /dev/null;then echo 1;else echo 0;fi\""
# for i in range(len(app_names)):
# command = "sshpass -p " + password + " ssh -o StrictHostKeyChecking=no " + ip_address + " -l root \"top -n1 -b | grep " + app_names[i] + " | wc -l\""
# output = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE).stdout.read().rstrip()
# if output == '0':
# break
# return output
# else:
# return '0'
return output
def is_app_alive(app_names, password, ip_address):
output = '0'
for i in range(len(app_names)):
command = "sshpass -p " + password + " ssh -o StrictHostKeyChecking=no " + ip_address + " -l root \"top -n1 -b | grep " + app_names[i] + " | wc -l\""
output = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE).stdout.read().rstrip()
if output == '0': break
return output
def is_os_alive(ip_address):
command = "ping -w 1 " + ip_address + " | grep received | awk '{print $4}'"
return subprocess.Popen(command, shell=True, stdout=subprocess.PIPE).stdout.read().rstrip()
# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--monitoring-interval", required=True, type=float,
help="how long the interval for collecting samples will be in seconds")
ap.add_argument("-p", "--password", required=True,
help="the root password to connect remotely to the computer that the faults will be injected in")
ap.add_argument("-n", "--network-interface", required=True,
help="the network interface of the computer that the faults will be injected in")
ap.add_argument("-a", "--ip-address", required=True,
help="the ip address to connect remotely to the computer that the faults will be injected in")
args = vars(ap.parse_args())
print """
__ ___ _____ _________ _____ _ __
/ |/ /____ / __ \ / ___/___/ /_ _/___ (_)__ _____/ /_____ _____
/ /|_/ / __ / / \ / / / /__ / // __ \ / / _ \/ ___/ __/ __ \/ ___/
/ / / / /_/ / /___/ / /__(__ / _/ // / / / / / __/ /__/ /_/ /_/ / /
/_/ /_/\____/_______/\___/____/ /___/_/ /_/_/ /\___/\___/\__/\____/_/
/___/ """
print("[1] FAULT INJECTOR")
print("[2] STATISTICAL ANALYSIS")
operation = raw_input("option: ")
if operation == '1':
n_applications = raw_input("Number of Applications: ")
app_name = []
command_stop_app = []
command_start_app = []
for i in range(int(n_applications)):
app_name.append(raw_input(str(i + 1) + "- Application Name: "))
#command_stop_app.append(raw_input("Command to stop Application[" + str(i + 1) + "]: "))
command_start_app.append(raw_input("Command to start Application[" + str(i + 1) + "]: "))
password = args["password"]
ip_address = args["ip_address"]
killapp = False
fault_app = raw_input("Application Fault Time: ")
repair_app = raw_input("Application Repair Time: ")
killos = False
fault_os = raw_input("OS Fault Time: ")
repair_os = raw_input("OS Repair Time: ")
fault_hw = raw_input("Hardware Fault Time: ")
repair_hw = raw_input("Hardware Repair Time: ")
address_mac = subprocess.Popen("sshpass -p " + password + " ssh -o StrictHostKeyChecking=no " + ip_address + " -l root cat /sys/class/net/" + args["network_interface"] + "/address", shell=True, stdout=subprocess.PIPE).stdout.read().rstrip()
try:
print("\n\nSTARTING MONITOR...")
tmonitor = Thread(target=monitor, args=(app_name,args["monitoring_interval"],password,ip_address))
tmonitor.daemon = True
tmonitor.start()
print("MONITOR STARTED!\n\n")
tapplications = []
for i in range(len(app_name)):
if not ("service" in command_start_app[i] or "start" in command_start_app[i] or "systemctl" in command_start_app[i]):
tapps = Thread(target=fault_injection_application, args=(app_name[i],float(fault_app),float(repair_app),
"sshpass -p " + password + " ssh -f -o StrictHostKeyChecking=no " + ip_address + " -l root killall " + app_name[i],
"sshpass -p " + password + " ssh -f -o StrictHostKeyChecking=no " + ip_address + " -l root \"" + command_start_app[i] + " . &>/dev/null &\""))
else:
tapps = Thread(target=fault_injection_application, args=(app_name[i],float(fault_app),float(repair_app),
"sshpass -p " + password + " ssh -f -o StrictHostKeyChecking=no " + ip_address + " -l root killall " + app_name[i],
"sshpass -p " + password + " ssh -f -o StrictHostKeyChecking=no " + ip_address + " -l root " + command_start_app[i]))
tapps.daemon = True
tapps.start()
tapplications.append(tapps)
tos = Thread(target=fault_injection_os, args=(float(fault_os),float(repair_os)))
tos.daemon = True
tos.start()
thw = Thread(target=fault_injection_hw, args=(float(fault_hw),float(repair_hw)))
thw.daemon = True
thw.start()
except Exception as e:
print "erro" + str(e)
while True:
pass
elif operation == '2':
data = pd.read_csv('monitoramento_raspberry.txt', sep=" ", header=None)
cont_u = 0
cont_d = 0
for i in range(len(data[4])):
if data[3][i] == 'U':
cont_u += 1
else:
cont_d += 1
else:
print("Invalid input!")