-
Notifications
You must be signed in to change notification settings - Fork 0
/
ddosw_baseline.py
158 lines (133 loc) · 6.47 KB
/
ddosw_baseline.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
import time
import optparse
import sysmon
import log_scraper
import os
if __name__ == '__main__':
description = """ddosw_baseline should be used to generate a baseline of the server's performance.
Ran without any options it displays the last report, if the -g option is passed a
new report is generated. It generates the mean and max values for CPU utilisation,
RAM utilisation and mb/s network usage. Make sure you run as root!!!"""
parser = optparse.OptionParser(description=description)
parser.add_option('-t', '--time', default=60, help="The number of minutes to run")
parser.add_option('-i', '--interval', default=10, help="The interval between polling server")
parser.add_option('-g', '--generate_report', action='store_true', default=False,
help="add this option to generate a new report")
parser.add_option('-a', '--adaptor', default='wlan0', help="The network interface to monitor")
parser.add_option('-r', '--rolling', default='1', help="Minutes rolling average should be calculated over")
opts, args = parser.parse_args()
RUNNING_TIME = opts.time
INTERVAL = opts.interval
GENERATE_REPORT = opts.generate_report
NETWORK_ADAPTOR = opts.adaptor
ROLLING_AVERAGE_PERIOD = opts.rolling
def create_baseline(interval, running_time, moving_average_time_period):
"""
This method calculates the average cpu utilisation, network usage and memory usage over the period that
ddow_baseline is ran for. It also calculates the maximum values for the cpu utilisation, network usage
and memory usage this is done however using a moving average over 1 minute (unless specified by user).
:param running_time: int, number of minutes it should run for
:param interval:int, number of seconds to wait between polling
:param moving_average_time_period: int, number of minutes moving average should be calculated over
:return:
"""
moving_average_time_period = int(moving_average_time_period)
running_time = int(running_time)
if moving_average_time_period >= running_time:
print("Running time must be more than rolling average period")
exit()
# get running time in minutes, div by interval plus 1 sec for network baseline
num_of_polls = int((running_time * 60) / (interval + 1))
moving_average_num_of_polls = int((moving_average_time_period * 60) / (interval + 1))
cpu_utilisation = list()
network_usage = list()
memory_usage = list()
cpu_utilisation_max = list()
network_usage_max = list()
memory_usage_max = list()
i = 0
max_cpu_average = 0
max_network_average = 0
max_memory_average = 0
while i < num_of_polls:
# create a list containing the CPU utilisation, memory usage and network usage over the entire period the
# baseline is ran for.
cpu = sysmon.get_cpu_utilisation()
network = sysmon.get_network_interface_traffic(NETWORK_ADAPTOR)
memory = sysmon.get_memory_usage()['memory_in_use']
cpu_utilisation.append(cpu)
network_usage.append(network)
memory_usage.append(memory)
# get the maximum values for the maximum cpu utilisation, network usage and memory usage. These values
# are calculated using a move average over a specified period of time
# first the minimum number of values are collected
if i <= moving_average_num_of_polls:
cpu_utilisation_max.append(cpu)
network_usage_max.append(network)
memory_usage_max.append(memory)
# once the minimum number of values have been collected to calculate the average over. The size of the
# list is maintained by dropping the first value and adding one to the end
else:
del cpu_utilisation_max[0]
cpu_utilisation_max.append(cpu)
cpu_average = get_mean(cpu_utilisation_max)
if cpu_average > max_cpu_average:
max_cpu_average = cpu_average
del network_usage_max[0]
network_usage_max.append(network)
network_average = get_mean(network_usage_max)
if network_average > max_network_average:
max_network_average = network_average
del memory_usage_max[0]
memory_usage_max.append(memory)
memory_average = get_mean(memory_usage_max)
if memory_average > max_memory_average:
max_memory_average = memory_average
time.sleep(interval)
i += 1
# calculate the mean average from all the values in the list
average_cpu_utilisation = get_mean(cpu_utilisation)
average_network_usage = get_mean(network_usage)
average_memory_usage = get_mean(memory_usage)
# print the results to the console and write them to server_stats.txt
resource_stats = "CPU Average: %0.2f %%\nCPU Max %0.2f %%\nNetwork Average: %0.2f bytes per second\n" \
"Network Max: %0.2f bytes per second\nMemory Average: %0.2f MB\nMemory Max: %0.2f MB" % \
(average_cpu_utilisation*100, max_cpu_average*100, average_network_usage,
max_network_average, average_memory_usage/1000, max_memory_average/1000)
directory = 'resources'
if not os.path.exists(directory):
os.makedirs(directory)
f = open('resources/server_stats.txt', 'w')
f.write(resource_stats)
f.close()
print_results()
def get_mean(stats):
"""
This method returns the mean value of all the numbers within the file or list
:param stats : A file or list containing numbers
:return: The means value of all the values in the file or list
"""
if type(stats) is not list:
f = open(stats, 'r')
num_list = f.read().splitlines()
else:
num_list = stats
total = 0.0
for num in num_list:
total += float(num)
mean = total / len(num_list)
return mean
def print_results():
f = open('resources/server_stats.txt', 'r')
stats = f.read()
print("\nDDOS_Wall system statics:\n-----------------\n%s\n-------------------" % stats)
f.close()
if __name__ == '__main__':
if GENERATE_REPORT:
print("Generating report this will take %s minutes plus some additional time to scrape the logs"
% RUNNING_TIME)
create_baseline(INTERVAL, RUNNING_TIME, ROLLING_AVERAGE_PERIOD)
log_scraper.get_maximum_download()
log_scraper.get_maximum_request_velocity()
else:
print_results()