-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
349 lines (282 loc) · 12.9 KB
/
main.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#!/usr/bin/env python
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
import os
import time
import argparse
from datetime import datetime
import itertools
import logging
import shutil
import json
from collections import namedtuple
import ansible.constants as C
from ansible.executor.task_queue_manager import TaskQueueManager
from ansible.module_utils.common.collections import ImmutableDict
from ansible.inventory.manager import InventoryManager
from ansible.parsing.dataloader import DataLoader
from ansible.playbook.play import Play
from ansible.plugins.callback import CallbackBase
from ansible.vars.manager import VariableManager
from ansible import context
from tcnetempy.TcNetem import TcNetem
from tcnetempy.Fault import *
from tcnetempy.Event import FaultEvent
from tcnetempy.StressNg import StressNg
from tcnetempy.utils import get_interfaces, calculate_time
# Create a callback plugin so we can capture the output
class ResultsCollectorJSONCallback(CallbackBase):
"""A sample callback plugin used for performing an action as results come in.
If you want to collect all results into a single object for processing at
the end of the execution, look into utilizing the ``json`` callback plugin
or writing your own custom callback plugin.
"""
def __init__(self, *args, **kwargs):
super(ResultsCollectorJSONCallback, self).__init__(*args, **kwargs)
self.host_ok = {}
self.host_unreachable = {}
self.host_failed = {}
def v2_runner_on_unreachable(self, result):
host = result._host
self.host_unreachable[host.get_name()] = result
def v2_runner_on_ok(self, result, *args, **kwargs):
"""Print a json representation of the result.
Also, store the result in an instance attribute for retrieval later
"""
host = result._host
self.host_ok[host.get_name()] = result
print(json.dumps({host.name: result._result}, indent=4))
def v2_runner_on_failed(self, result, *args, **kwargs):
host = result._host
self.host_failed[host.get_name()] = result
DEFAULT_LOG_PATH = "../logs"
# Fault Injection Campaign
def get_args():
parser = argparse.ArgumentParser(description='Process args for retrieving arguments')
# log file
parser.add_argument('-l', "--log", help="log file", default=None, type=str)
parser.add_argument("--log_dir", help="log directory", default=DEFAULT_LOG_PATH, type=str)
# schedule
parser.add_argument("-p", "--padding", help="padding_time (second)", required=True, type=int)
parser.add_argument("-d", "--duation", help="fault injection duation (second)", required=True, type=int)
parser.add_argument("--dry", action="store_true", help="dry run")
parser.add_argument("--interface_num", help="number of interfaces", type=int, )
parser.add_argument("-g", "--group", type=str)
parser.add_argument("-H", "--hosts", type=str)
parser.add_argument("-i", "--inventory", required=True, type=str)
parser.add_argument("-m", "--mode", required=True, choices=["cpu", "memory", "disk", "network", "blank"], type=str)
parser.add_argument("-u", "--user", default="root")
parser.add_argument("--stressng_path")
return parser.parse_args()
CPU_FAULTS = reversed(["2", "4", "8", "16", "32"])
MEMORY_FAULTS = reversed([1, 2, 4, 6, 8])
DISK_FAULTS = reversed([1, 2, 4, 8, 16])
# delay: ms, loss: percentage
NETWORK_FAULTS = [(1000, 0), (1000, 25), (1000, 50), (0, 25), (0, 50)]
# FAULTS = [
# {
# "type": "delay",
# "values": [0, 1000, 5000],
# "unit": "ms",
# "args": "20ms"
# },
# {
# "type": "loss",
# "values": [0, 15, 30],
# "unit": "%",
# "args": "25%"
# },
# # {
# # "type": "duplicate",
# # "values": [0],
# # "unit": "%",
# # "args": ""
# # }
# ]
if __name__ == "__main__":
args = get_args()
if args.log is None:
os.makedirs(DEFAULT_LOG_PATH, exist_ok=True)
args.log = f"{args.log_dir}/{args.mode}_{datetime.now().strftime('%Y-%m-%d-%H:%M:%S')}.log"
print("log file: ", args.log)
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler(args.log),
logging.StreamHandler()
])
if args.user == "root":
context.CLIARGS = ImmutableDict(connection='smart', module_path=['/to/mymodules', '/usr/share/ansible'], forks=10, become=True,
become_method="sudo", become_user="root", check=False, diff=False, verbosity=0)
elif args.user == "vagrant":
context.CLIARGS = ImmutableDict(connection='smart', module_path=['/to/mymodules', '/usr/share/ansible'], forks=10, become=True, check=False, diff=False, verbosity=0)
else:
context.CLIARGS = ImmutableDict(connection='smart', module_path=['/to/mymodules', '/usr/share/ansible'], forks=10, become=True,
become_method="sudo", become_user=args.user, check=False, diff=False, verbosity=0)
loader = DataLoader()
passwords = dict(vault_pass='secret')
results_callback = ResultsCollectorJSONCallback()
inventory = InventoryManager(loader=loader, sources = [args.inventory, ])
variable_manager = VariableManager(loader=loader, inventory=inventory)
if args.group:
host_list = inventory.get_groups_dict()[args.group]
elif args.hosts:
host_list = args.hosts.split(",")
else:
raise Exception
print("host_list:", host_list)
stressNg = StressNg()
if args.mode == "network":
devices = ["eth0", "eth1"]
# TODO: get interfaces from remote servers
# devices = get_interfaces()
# devices = [ device for device in devices if device != "lo" and not device.startswith("v") and not device.startswith("b")]
# if args.interface_num:
# devices = devices[:args.interface_num]
# logging.info(f"Devices: {devices}")
print(f"Devices: {devices}")
commands = [TcNetem(interface=device, faults=[
Delay(time=f"{fault_values[0]}ms", jitter="20ms"),
Loss(rate=f"{fault_values[1]}%", correlation="25%"),
]).build_campaign_command(args.duation) for device in devices for fault_values in NETWORK_FAULTS]
elif args.mode == "cpu":
commands = [stressNg.cpu(fault, args.duation) for fault in CPU_FAULTS]
elif args.mode == "memory":
commands = [stressNg.memory(vm=fault, vm_bytes="1G", vm_hang=args.duation, timeout=args.duation) for fault in MEMORY_FAULTS]
elif args.mode == "disk":
commands = [stressNg.disk(io=fault, hdd=fault, timeout=args.duation) for fault in DISK_FAULTS]
elif args.mode == "blank":
commands = ["echo 'Hello, World!'"]
print(commands)
# Prepare the environment/tools for fault injection
## Install stress-ng
if args.stressng_path == None:
tqm = TaskQueueManager(
inventory=inventory,
variable_manager=variable_manager,
loader=loader,
passwords=passwords,
stdout_callback=results_callback, # Use our custom callback instead of the ``default`` callback plugin, which prints to stdout
)
play_source = dict(
name="Ansible Play",
hosts=host_list,
gather_facts='no',
tasks=[
dict(
action=dict(module='apt',
args=dict(
name = "stress-ng"
),
), register="shell_out"
),
],
)
play = Play().load(play_source, variable_manager=variable_manager, loader=loader)
# Actually run it
try:
result = tqm.run(play) # most interesting data for a play is actually sent to the callback's methods
except Exception as e:
print(e)
finally:
# we always need to cleanup child procs and the structures we use to communicate with them
tqm.cleanup()
if loader:
loader.cleanup_all_tmp_files()
# Remove ansible tmpdir
shutil.rmtree(C.DEFAULT_LOCAL_TMP, True)
print("UP ***********")
for host, result in results_callback.host_ok.items():
print('{0} >>> {1}'.format(host, result._result['stdout']))
print("FAILED *******")
for host, result in results_callback.host_failed.items():
print('{0} >>> {1}'.format(host, result._result['msg']))
print("DOWN *********")
for host, result in results_callback.host_unreachable.items():
print('{0} >>> {1}'.format(host, result._result['msg']))
time.sleep(args.padding)
if args.dry:
exit(0)
while True:
for command in commands:
tqm = TaskQueueManager(
inventory=inventory,
variable_manager=variable_manager,
loader=loader,
passwords=passwords,
stdout_callback=results_callback, # Use our custom callback instead of the ``default`` callback plugin, which prints to stdout
)
play_source = dict(
name="Ansible Play",
hosts=host_list,
gather_facts='no',
tasks=[
# dict(action=dict(module='shell', args='ls'), register='shell_out'),
# dict(action=dict(module='debug', args=dict(msg='{{shell_out.stdout}}'))),
# dict(action=dict(module='command', args=dict(cmd='/usr/bin/uptime'))),
dict(
action=dict(module='command',
args=dict(
cmd= command,
chdir="/root"
),
), register="shell_out"
),
# dict(
# action=dict(module='debug', args=dict(msg='{{shell_out.stdout}}'))
# )
],
# become="true",
# become_user="root"
)
# print(play_source)
logging.info(f"Start_fault_injection, {host_list}, {command}")
play = Play().load(play_source, variable_manager=variable_manager, loader=loader)
# Actually run it
try:
result = tqm.run(play) # most interesting data for a play is actually sent to the callback's methods
except Exception as e:
print(e)
finally:
# we always need to cleanup child procs and the structures we use to communicate with them
tqm.cleanup()
if loader:
loader.cleanup_all_tmp_files()
# Remove ansible tmpdir
shutil.rmtree(C.DEFAULT_LOCAL_TMP, True)
print("UP ***********")
for host, result in results_callback.host_ok.items():
print('{0} >>> {1}'.format(host, result._result['stdout']))
print("FAILED *******")
for host, result in results_callback.host_failed.items():
print('{0} >>> {1}'.format(host, result._result['msg']))
print("DOWN *********")
for host, result in results_callback.host_unreachable.items():
print('{0} >>> {1}'.format(host, result._result['msg']))
time.sleep(args.padding)
# devices = get_interfaces()
# if args.interface_num:
# devices = devices[:args.interface_num]
# logging.info(f"Devices: {devices}")
# fault_values_sets = list(itertools.product(devices, *[fault["values"] for fault in FAULTS]))
# fault_values_sets = [fault_values for fault_values in fault_values_sets if sum(fault_values[1:]) != 0]
# print("The fault injection will take about", calculate_time(args.padding, args.duation, len(fault_values_sets)), "seconds.")
# try:
# for fault_values in fault_values_sets:
# device = fault_values[0]
# faults = [
# Delay(time=f"{fault_values[1]}ms", jitter="20ms"),
# Loss(rate=f"{fault_values[2]}%", correlation="25%"),
# # Duplicate(rate=f"{fault_values[3]}%")
# ]
# tc_netem = TcNetem(interface=device, faults=faults)
# if args.dry:
# tc_netem.dry_run()
# else:
# fault_event = FaultEvent(tc_netem=tc_netem, duation=args.duation)
# fault_event.run()
# time.sleep(args.padding)
# except KeyboardInterrupt:
# logging.info("KeyboardInterrupt")
# tc_netem.delete()
# exit(0)