-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitor_system.py
More file actions
63 lines (48 loc) · 1.95 KB
/
Copy pathmonitor_system.py
File metadata and controls
63 lines (48 loc) · 1.95 KB
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
import subprocess
import sys
import time
def get_cpu_utilization():
p = subprocess.Popen("grep 'cpu ' /proc/stat | awk '{usage=($2+$4)*100/($2+$4+$5)} END {print usage}'",
stdout=subprocess.PIPE, shell=True)
(output, err) = p.communicate()
return output.strip()
def get_memory_utilization():
p = subprocess.Popen("free | awk 'FNR == 3 {print $3/($3+$4)*100}'",
stdout=subprocess.PIPE, shell=True)
(output, err) = p.communicate()
return output.strip()
def get_disk_utilization():
p = subprocess.Popen("df --total -hl | tail -n1 | awk '{print $5}' | sed 's/%//'",
stdout=subprocess.PIPE, shell=True)
(output, err) = p.communicate()
return output.strip()
def get_average(array):
return reduce(lambda x, y: x + y, array) / len(array)
def main():
minutes = sys.argv[1]
cpu_threshold = sys.argv[2]
mem_threshold = sys.argv[3]
disk_threshold = sys.argv[4]
cpu_utilization = []
mem_utilization = []
disk_utilization = []
t_end = time.time() + 60 * int(minutes)
while time.time() < t_end:
cpu_utilization.append(float(get_cpu_utilization()))
mem_utilization.append(float(get_memory_utilization()))
disk_utilization.append(float(get_disk_utilization()))
time.sleep(60)
cpu_utilization_avg = str(get_average(cpu_utilization))
mem_utilization_avg = str(get_average(mem_utilization))
disk_utilization_avg = str(get_average(disk_utilization))
print "CPU Utilization: " + cpu_utilization_avg + "\n"
print "Mem Utilization: " + mem_utilization_avg + "\n"
print "Disk Utilization: " + disk_utilization_avg + "\n"
if (float(cpu_utilization_avg) < cpu_threshold and
float(mem_utilization_avg) < mem_threshold and
float(disk_utilization_avg) < disk_threshold):
print("Under Utilized")
else:
print("Over Utilized")
if __name__ == '__main__':
main()