-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProject_Backend_Main.py
207 lines (168 loc) · 8.15 KB
/
Project_Backend_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
# -*- coding: utf-8 -*-
"""
Created on Mon Nov 1 17:52:30 2021
@author: sbgadhwa
"""
import psutil
import os
import time
import shutil
import csv
import pandas as pd
import datetime as dt
#class Project_Backend_Main:
def get_pname(id):
return os.system("ps -o cmd= {}".format(id))
def getListOfProcessSortedByMemory():
'''
Get list of running process sorted by Memory Usage
'''
listOfProcObjects = []
# Iterate over the list
for proc in psutil.process_iter():
try:
# Fetch process details as dict
pinfo = proc.as_dict(attrs=['pid', 'name', 'username'])
pinfo['vms'] = proc.memory_info().vms / (1024 * 1024)
pinfo['cpu']=proc.cpu_percent()
# Append dict to list
listOfProcObjects.append(pinfo);
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
pass
# Sort list of dict by key vms i.e. memory usage
listOfProcObjects = sorted(listOfProcObjects, key=lambda procObj: procObj['vms'], reverse=True)
return listOfProcObjects
def getUsersCount():
userList = []
for i in getListOfProcessSortedByMemory():
if str(type(i['username']))!='NoneType':
userList.append(i['username'])
#print("Yes")
userList = set(userList)
return userList
def runCheck():
t = time.localtime()
current_time = time.strftime("%H:%M:%S", t)
total, used, free = shutil.disk_usage("/")
usedPer = 100*used/total
return current_time, psutil.cpu_percent(), psutil.virtual_memory().percent, usedPer
def takeFinalAction(component, rule, app, val, thr):
if rule.lower() == "kill":
appList = app.split(",")
for i in appList:
if not i.endswith(".exe"):
i = i +".exe"
i = i.lower()
try:
for process in (process for process in psutil.process_iter() if process.name().lower() == i):
process.kill()
print(f"{process.name()} was killed")
appendRowData = [str(dt.datetime.now()), component, str(val), str(thr), 'ATaM ' + rule + 'ed ' + app]
if not os.path.exists(r'ATam_Actions_Data.csv'):
with open(r'ATam_Actions_Data.csv', 'w') as file:
writer = csv.writer(file)
writer.writerow(['Timestamp', 'Component', 'Observed Value', 'Threshold', 'Action'])
writer.writerow(appendRowData)
else:
with open(r'ATam_Actions_Data.csv', 'a') as file:
writer = csv.writer(file)
writer.writerow(appendRowData)
except:
print(f"Issue wihle killing {i} ")
if rule.lower() == "restart":
count = 0
appList = app.split(",")
for i in appList:
j = i.split("\\")[-1]
if not j.endswith(".exe") :
if count < 1:
j = i.split("\\")[-1]
j = j +".exe"
j = j.lower()
try:
for process in (process for process in psutil.process_iter() if process.name().lower() == j):
process.kill()
print(f"{process.name()} was killed")
print("restarting ", i.split("\\")[-1])
os.startfile(fr"{i}")
appendRowData = [str(dt.datetime.now()), component, str(val), str(thr), 'ATaM ' + rule + 'ed ' + app]
if not os.path.exists(r'ATam_Actions_Data.csv'):
with open(r'ATam_Actions_Data.csv', 'w') as file:
writer = csv.writer(file)
writer.writerow(['Timestamp', 'Component', 'Observed Value', 'Threshold', 'Action'])
writer.writerow(appendRowData)
else:
with open(r'ATam_Actions_Data.csv', 'a') as file:
writer = csv.writer(file)
writer.writerow(appendRowData)
count = count + 1
except:
print(f"Issue wihle restarting {j} ")
if rule.lower()=="delete":
try:
shutil.rmtree(fr"{app}")
print(f"{app} folder was deleted")
appendRowData = [str(dt.datetime.now()), component, str(val), str(thr), 'ATaM ' + rule + 'ed ' + app]
if not os.path.exists(r'ATam_Actions_Data.csv'):
with open(r'ATam_Actions_Data.csv', 'w') as file:
writer = csv.writer(file)
writer.writerow(['Timestamp', 'Component', 'Observed Value', 'Threshold', 'Action'])
writer.writerow(appendRowData)
else:
with open(r'ATam_Actions_Data.csv', 'a') as file:
writer = csv.writer(file)
writer.writerow(appendRowData)
except:
print(f"Error while deleteing {app}")
def takeInputFromUI(cpuVal, ramVal, diskVal, cpu_dropdown, ram_dropdown, disk_dropdown, actionCpu, actionRam, actionDisk):
curTime, cpuUtil, ramUtil, diskUtil = runCheck()
latest_cpu_thr = 0
latest_ram_thr = 0
latest_disk_thr = 0
latest_cpu_rule = ''
latest_ram_rule = ''
latest_disk_rule = ''
latest_cpu_action = ''
latest_ram_action = ''
latest_disk_action = ''
if str(cpuVal)!='None':
latest_cpu_thr = cpuVal
if str(cpu_dropdown)!='None':
latest_cpu_rule = cpu_dropdown
if str(actionCpu)!='None':
latest_cpu_action = actionCpu
if str(ramVal)!='None':
latest_ram_thr = ramVal
if str(ram_dropdown)!='None':
latest_ram_rule = ram_dropdown
if str(actionRam)!='None':
latest_ram_action = actionRam
if str(diskVal)!='None':
latest_disk_thr = diskVal
if str(disk_dropdown)!='None':
latest_disk_rule = disk_dropdown
if str(actionDisk)!='None':
latest_disk_action = actionDisk
if latest_cpu_thr > 0 and cpuUtil > latest_cpu_thr:
if latest_cpu_rule != '':
if latest_cpu_action != '':
#method (rule, and action item)
print("satisfied condition for CPU")
takeFinalAction("CPU", latest_cpu_rule, latest_cpu_action, cpuUtil, latest_cpu_thr)
if latest_ram_thr > 0 and ramUtil > latest_ram_thr:
if latest_ram_rule != '':
if latest_ram_action != '':
print("sending to take action.....")
takeFinalAction("RAM", latest_ram_rule, latest_ram_action, ramUtil, latest_ram_thr)
#print("Satisfied Condition for RAM")
if latest_disk_thr > 0 and diskUtil > latest_disk_thr:
if latest_disk_rule != '':
if latest_disk_action != '':
print("Satisfied conditon for Disk")
takeFinalAction("Disk", latest_disk_rule, latest_disk_action, diskUtil, latest_disk_thr)
def createDataFrameFromUserInput(latest_cpu_thr, latest_cpu_rule, latest_cpu_action, latest_ram_thr, latest_ram_rule, latest_ram_action, latest_disk_thr, latest_disk_rule, latest_disk_action):
data = [['CPU', latest_cpu_thr, latest_cpu_rule, latest_cpu_action], ['RAM', latest_ram_thr, latest_ram_rule, latest_ram_action], ['Disk', latest_disk_thr, latest_disk_rule, latest_disk_action]]
df = pd.DataFrame(data, columns = ['Monitoring Component', 'Threshold', 'Action', 'Action Item'])
df.set_index('Monitoring Component', inplace=True)
print(df)
df.to_csv("User_Input.csv")