-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcpu_stats.py
149 lines (122 loc) · 6.59 KB
/
cpu_stats.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
#!/usr/bin/env python3
''' Author: Nikita Yadav
This script will calculate the cpu and memory utilization by the system during runtime the output of which is a graph on html page also the second tab of html gives the system logs
steps to run script:
1- On mate terminal type – python3 (file_name) -t (your duration in minutes for how much time you want to run the script).
2- Example – python3 cpu_stat.py -t 1
3- Wait for the time provided to calculate statistics.
4- After the script ends check for log.html file.
5- Log.html file file show results in tab format selected.
'''
import argparse
import os
import matplotlib.pyplot as plt
import datetime
import base64
import logging
import threading
import time
fh = ''
fh_1 = ''
def cpu_percent(cmd):
global fh
fh = os.popen(cmd)
def memory_percent(cmd):
global fh_1
fh_1 = os.popen(cmd)
def htmlimage(data_4, data):
html = open("log.html", 'w')
img = data_4 + " " + data
html.write(img)
def main():
global duration
parser = argparse.ArgumentParser()
parser.add_argument("-t", "--duration", type=int, help="Enter the Time for which you want to run test (in minutes)")
try:
args = parser.parse_args()
if (args.duration is not None):
duration = args.duration
except Exception as e:
logging.exception(e)
exit(2)
endTime = datetime.datetime.now() + datetime.timedelta(seconds=duration)
now = datetime.datetime.now()
starttime = datetime.datetime(now.year, now.month, now.day, now.hour, now.minute, now.second)
delta = datetime.timedelta(seconds=10)
cpu_stats_data = {"system": [], "kernel": []}
memory_stats_data = {"Total": [], "Used": []}
iterations = duration * 60
cmd_1 = "top -bn1 -d 1 -n " + str(iterations) + " | grep '%Cpu(s)' "
cmd_2 = "top -bn1 -d 1 -n " + str(iterations) + " | grep 'MiB Mem'"
t1 = threading.Thread(target=cpu_percent, args=(cmd_1,))
t2 = threading.Thread(target=memory_percent, args=(cmd_2,))
t1.start()
t2.start()
t1.join()
t2.join()
time.sleep(10)
output = fh.read() + fh.readline()
output_1 = fh_1.read() + fh_1.readline()
data = output.split('\n')
data_1 = output_1.split('\n')
for i in data:
# print(i.split(','))
if len(i) > 3:
cpu_stats_data["system"].append(float(i.split(',')[0].split()[1]))
cpu_stats_data["kernel"].append(float(i.split(',')[1].split()[0]))
# print(cpu_stats_data)
for i in data_1:
if len(i) > 3:
memory_stats_data["Total"].append(float(i.split(',')[0].split()[3]))
memory_stats_data["Used"].append(float(i.split(',')[2].split()[0]))
# print(memory_stats_data)
sample_times = [starttime + i * delta for i in range(len(cpu_stats_data["system"]))]
label_locations = [d for d in sample_times if d.minute % 1 == 0]
labels = [d.strftime('%Y-%m-%d %H:%M:%S') for d in label_locations]
# print(labels)
#print(sample_times)
# thread creation
# graphs
#plot1
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.plot(sample_times, cpu_stats_data['system'], '-', lw=1, color='r', label="system cpu%")
ax.plot(sample_times, cpu_stats_data['kernel'], '-', lw=1, color='b', label="kernel cpu%")
ax.set_ylabel('CPU (%)', color='r')
ax.set_xlabel('time (s)')
plt.tight_layout()
ax.set_ylim(0., max(cpu_stats_data['system']) + 20)
plt.xticks(rotation='vertical')
fig.legend(["System CPU Utilization", "Kernel CPU Utilization"], loc='upper center')
ax.grid()
fig.savefig("cpu.png")
#plot2
fig_1 = plt.figure()
ax_1 = fig_1.add_subplot(1, 1, 1)
ax_1.plot(sample_times, memory_stats_data["Total"], '-', lw=1, color='r', label="Total MEMORY AVAILABLE")
ax_1.plot(sample_times, memory_stats_data["Used"], '-', lw=1, color='b', label="Total MEMORY USED")
ax_1.set_ylabel('Total available', color='r')
ax_1.set_xlabel('time (s)')
plt.tight_layout()
ax_1.set_ylim(0., max(memory_stats_data["Total"]) + 2000)
plt.xticks(rotation='vertical')
fig_1.legend(["TOTAL MEMORY AVAILABLE", "TOTAL MEMORY USED"], loc='upper center')
ax_1.grid()
fig_1.savefig("MEMORY.png")
cmd_1 = "timeout 2s journalctl -p err --since '24 hour ago' > syslog.txt"
fh_2 = os.system(cmd_1)
fi_open = open("syslog.txt", "r+")
out = fi_open.read()
data_3 = out.split(" ")
data_uri = base64.b64encode(open('cpu.png', 'rb').read()).decode('utf-8')
data_uri_1 = base64.b64encode(open('MEMORY.png', 'rb').read()).decode('utf-8')
data_a = '<img title="CPU utilization" src="data:image/png;base64,{0}">'.format(
data_uri) + " " + '<img title="CPU utilization" src="data:image/png;base64,{0}">'.format(data_uri_1)
data_4 = "<!DOCTYPE html><html><head> <meta name='viewport' content='width=device-width, initial-scale=1''><style>.accordion {background-color: #eee;color: #444;cursor: pointer;padding: 18px;width: 50%; border: none;text-align: left;outline: none;font-size: 15px;transition: 0.4s;}.active, .accordion:hover {background-color: #ccc; }.panel {padding: 0 18px;display: none;background-color: white;overflow: hidden;}</style></head><body> <button class='accordion''>CPU UTILIZATION</button> <div class='panel'> <p>" + data_a + "</p></div><script>acc = document.getElementsByClassName('accordion');var i;for (i = 0; i < acc.length; i++) {acc[i].addEventListener('click', function() {this.classList.toggle('active');var panel = this.nextElementSibling;if (panel.style.display === 'ock') {panel.style.display = 'none;} else {panel.style.display = 'block';}});}</script></body></html>"
logs = " "
for i in data_3:
logs = logs + i
data = "<head> <meta name='viewport' content='width=device-width, initial-scale=1'> <style>.accordion { background-color: #eee; color: #444; cursor: pointer;padding: 18px; width: 50%; border: none;text-align: left;outline: none;font-size: 10px;transition: 0.4s;} .active, .accordion:hover { background-color: #ccc;} .panel { padding: 0 18px; display: none; background-color: white; overflow: hidden;} </style> </head> <button class='accordion'>SYSTEM LOGS</button> <div class='panel'> <font face = 'Courier New' size = '2' color='#ff0000'>" + logs + "</font><br /> </div> <script> var acc = document.getElementsByClassName('accordion'); var i; for (i = 0; i < acc.length; i++) {acc[i].addEventListener('click', function() { this.classList.toggle('active');var panel = this.nextElementSibling;if (panel.style.display === 'block') {panel.style.display = 'none';} else {panel.style.display = 'block';}});} </script> "
htmlimage(data_4, data)
if __name__ == '__main__':
main()