-
Notifications
You must be signed in to change notification settings - Fork 0
/
monit_process.py
executable file
·147 lines (127 loc) · 4.53 KB
/
monit_process.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import psutil, time
import argparse, sys
import numpy as np
from numpy import array
from datetime import datetime, timedelta
import funcs as ff
#--- retrieve args
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
description="""
Script to monitor memory info about processes that match
a given pattern.
""",
)
parser.add_argument(
'-fo', '--fname_out',
type=str,
default='test.txt',
help='output filename.',
)
parser.add_argument(
'-pn', '--proc_name',
type=str,
default='BINARY.exe',
help='pattern for the process command we seek for',
)
parser.add_argument(
'-pid', '--pid',
type=int,
default=None,
help='PID of the process',
)
parser.add_argument(
'-q', '--quiet',
action='store_true',
help='Don\'t show a "live" report while measuring. This \
is useful when running in background or piping the STDOUT \
to a file.',
)
pa = parser.parse_args()
t = []
mem_used = []
mem_tot = psutil.virtual_memory()[0] # [bytes] RAM capacity
start = time.time() # mediante argparse, cambiarle el valor inicial
print('\n [*] Starting measurement at ' + ff.utc2date(start).strftime('%d %b %Y %H:%M:%S') + ' (UTC) \n')
# template
VIRT = 0
SHR = 1
RES = 2
if pa.pid is not None:
PIDs = [str(pa.pid),]
NotFound = 0
else:
NotFound = 1
while True:
# look for target process...
if NotFound:
time.sleep(0.3)
# these are the PID numbers for the processes described by
# the pattern `proc_name`; except for those that match with
# this very same process (i.e. the childrens of this script):
PIDs, CMDs, raw = ff.look_for_process(pa.proc_name)
NotFound = 1 if PIDs is None else 0
continue
else:
print '\n [+] monitoring these PID(s):\n ', PIDs
break
# initialize data arrays for each of the processes
# listed in PIDs:
memdata = {} # keys are the PID numbers
for _pid in PIDs:
memdata[_pid] = {VIRT: [], SHR: [], RES: []}
ndata = 0
while True:
try:
time.sleep(1)
t += [ time.time() - start ] # [sec]
mem_avail = psutil.virtual_memory()[1] # [B]
mem_used += [ (mem_tot - mem_avail) ] # [B]
for _pid in PIDs:
pinfo = psutil.Process(pid=int(_pid))
res, virt, shr = pinfo.memory_info()[:3]
memdata[_pid][RES] += [ res ] # resident
memdata[_pid][VIRT] += [ virt ] # virtual
memdata[_pid][SHR] += [ shr ] # shared
# report on screen
_report = '> measure #%d (PID:%s); RES:%g, VIRT:%g, SHR:%g, TOT:%g'%(\
len(t), _pid, res/(2.**20), virt/(2.**20), shr/(2.**20), mem_used[-1]/(2.**20))
if not(pa.quiet):
sys.stdout.write(_report + '\r')
sys.stdout.flush()
# nmbr of **successful** measurements
ndata += 1
except (KeyboardInterrupt, psutil.NoSuchProcess):
# stop measuring either if:
# - we press Ctrl+C, or
# - processes terminate
if len(t)==0:
print("\n [-] No measurements made!\n")
else:
# truncate to a valid length
t = t[:ndata] # [sec]
mem_used = array(mem_used[:ndata])/(2.**20) # [MB]
for _pid in PIDs:
memdata[_pid][RES] = array(memdata[_pid][RES][:ndata])/(2.**20) # [MB]
memdata[_pid][VIRT] = array(memdata[_pid][VIRT][:ndata])/(2.**20) # [MB]
memdata[_pid][SHR] = array(memdata[_pid][SHR][:ndata])/(2.**20) # [MB]
# build header (reporting the PID numbers for the columns of data)
header = '\n For each PID, three columns of memory usage:'
header += '\n Virtual (VIRT), Shared (SHR), and Resident (RES)\n'
header += '\n time [sec], total RAM used [MB]'
for _pid in PIDs:
header += ', ' + _pid
header += '\n'
print " [*] we captured %d processes" % len(memdata.keys())
buff = [] # to save in ASCII
buff += [ mem_used ] # 2nd column (time is 1st column)
for _pid in memdata.keys():
for _dnm in [VIRT, SHR, RES]:
buff += [ memdata[_pid][_dnm] ]
print("\n [*] saving %d measurements...\n" % len(t))
ff.save_measure(pa.fname_out, start, header, t, *buff)
print('\n [+] success on saving:\n %s\n'%pa.fname_out)
break
#EOF