-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory_monitor.py
More file actions
120 lines (94 loc) · 3.38 KB
/
Copy pathmemory_monitor.py
File metadata and controls
120 lines (94 loc) · 3.38 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
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
#!/usr/bin/env python3
# Author: Vladimir Novick , <vlad.novick@gmail.com>
#
# https://www.linkedin.com/in/vladimirnovick
#
# https://github.com/Vladimir-Novick/Monitoring-Memory-Usage
#
#
# Date: 09.07.2017
# Purpose: check max using memory and create log file
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
import subprocess
import sys
import threading
import time
import datetime
global logFile
logFile = "memory.log"
global test_time
test_time = 60 # seconds
global glb_minSize
glb_minSize = 999999999
def runProcess(exe):
p = subprocess.Popen(exe, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
while(True):
retcode = p.poll() #returns None while subprocess is running
line = p.stdout.readline()
line = line.decode("utf-8")
yield line
if(len(line) < 2):
break
if(retcode is not None):
break
def writeLog(fileName):
output = subprocess.run("ps axo rss,comm,pid | sort -n | tail -n 30 | sort -rn", shell=True, stdout=subprocess.PIPE,
universal_newlines=True)
with open(fileName, mode='a+') as out:
all_lines =output.stdout
records = all_lines.split('\n')
l1 = 'Memory'.rjust(8)
l2 = 'Task'.ljust(20)
l3 = 'PID'.rjust(10)
out.write(l1+ ' ' +l2+l3)
out.write('\n')
l1 = '------'.rjust(8)
l2 = '----------------------'.ljust(20)
l3 = '-----'.rjust(8)
out.write(l1+ ' ' +l2+l3)
out.write('\n')
for index in range(len(records)):
line = records[index]
if line != '':
l = line.split()
l1 = l[0].strip().rjust(8)
l2 = l[1].strip().ljust(20)
l3 = l[2].strip().rjust(10)
out.write(l1+ ' ' +l2+l3)
out.write('\n')
else:
break
out.close()
def getFreeSize():
p = subprocess.Popen('free -m'.split(), stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
retcode = p.poll() #returns None while subprocess is running
line = p.stdout.readline()
line = p.stdout.readline().decode("utf-8")
r = line.split()
return int(r[3].strip())
def writeMemoryLog(fileName):
with open(fileName, mode='w+') as out:
out.write(datetime.datetime.now().strftime("%I:%M%p on %B %d, %Y\n\n"))
for line in runProcess('free -m'.split()):
out.write(line)
out.write("_____________________________________________________________")
out.close()
def f():
global glb_minSize
currentSize = getFreeSize()
if currentSize < glb_minSize :
writeMemoryLog(logFile)
writeLog(logFile)
glb_minSize = currentSize
print('Free memory: '+ str(glb_minSize) + 'MB\n')
threading.Timer(test_time, f).start()
if __name__ == '__main__':
f()
time.sleep(20)