-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmemory_calc.py
35 lines (27 loc) · 1.25 KB
/
memory_calc.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
#!/usr/bin/python
#filename: memory_calc.py
#description: checks processes in tree and calculated memory use.
#written for Python 2
import subprocess
import re
def get_procs():
proc=subprocess.Popen("ps efux",shell=True,stdout=subprocess.PIPE) #get memory fields
running_procs=proc.stdout.readlines()
return running_procs
total_resident_memory_used=0
total_virt_memory_used=0
running_procs=get_procs()
for line in running_procs:
array_of_ps_output=[]
array_of_ps_output=line.split()
matchObj = re.search(r'\d',array_of_ps_output[5]) #look for digits on 5th column of output Resident memory
matchObj1 = re.search(r'\d',array_of_ps_output[4]) #look for digits on 4th column of output VSV
if matchObj:
resident_mem=array_of_ps_output[5]
total_resident_memory_used = ( total_resident_memory_used + int(resident_mem))
#if matchObj1:
# vmem=array_of_ps_output[4]
# total_virt_memory_used = ( total_virt_memory_used + int(vmem))
print "total resident memory used by applications from process tree: " + str(total_resident_memory_used/1024) + " Mb"
#VSV is the total virtual memory a process has available so this is not correct
#print "total virtual memory used by applications from process tree: " + str(total_virt_memory_used/1024) + " Mb"