-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpmon.py
More file actions
executable file
·182 lines (156 loc) · 4.67 KB
/
pmon.py
File metadata and controls
executable file
·182 lines (156 loc) · 4.67 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
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
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
"""
A simple performance monitor for Unix processes that outputs scaled graphs.
"""
import argparse
import datetime
import time
try:
import matplotlib.pyplot as plt
except ImportError:
print("Missing 'matplotlib' package. Please refer to: http://matplotlib.org/users/installing.html")
plt = None
exit(1)
try:
import psutil
except ImportError:
print("Missing 'psutil' package. Please refer to: https://pypi.python.org/pypi/psutil")
psutil = None
exit(1)
__author__ = "Niccolò Maggioni"
__license__ = "MIT"
parser = argparse.ArgumentParser()
parser.add_argument("-v", "--verbose", help="Increase output verbosity.", action="store_true")
parser.add_argument("-t", "--timeout", help="The duration of the monitoring in seconds.", type=int, default=0)
parser.add_argument("PID", help="The PID to monitor.", type=int)
args = parser.parse_args()
p = None
try:
p = psutil.Process(args.PID)
except psutil.NoSuchProcess:
print("No process found with that PID.")
exit(1)
def min_or_zero(values):
min(values) - 1 if min(values) - 1 >= 0 else 0
def b_to_mb(b):
return round(b / (1024 * 1024))
if args.verbose:
print("Preparing data stores...")
cpu, threads, mem_pct, mem_vms, mem_rss = [], [], [], [], []
elapsed = 0
if args.verbose:
print("Harvesting process details...")
cmdline = " ".join(p.cmdline())
ppid = p.ppid()
cores = p.cpu_affinity()
create_time = datetime.datetime.fromtimestamp(p.create_time()).strftime("%d-%m-%Y %H:%M:%S")
niceness = p.nice()
if args.verbose:
print("Scrapping unstable values...")
for i in range(3):
p.cpu_percent() # First values should be discarded
time.sleep(0.1)
if args.verbose:
print()
print("Press CTRL+C to stop monitoring.")
while True:
try:
if args.timeout:
if elapsed == args.timeout:
break
print("Time left: " + str(args.timeout - elapsed) + "s ", end="\r")
if p.is_running():
cpu.append(round(p.cpu_percent(), -1))
mem_vms.append(b_to_mb(p.memory_info().vms))
mem_rss.append(b_to_mb(p.memory_info().rss))
mem_pct.append(round(p.memory_percent(), -1))
threads.append(p.num_threads())
elapsed += 1
time.sleep(1)
else:
print("Process is not running anymore.")
break
except KeyboardInterrupt:
break
# Graphspace configuration
if args.verbose:
print("\nPreparing graphspace...")
rows = 3
cols = 2
fig = plt.figure(1)
# CPU Percentage plot
if args.verbose:
print("Plotting CPU Percentage...")
plt.subplot(rows, cols, 1)
plt.ticklabel_format(useOffset=False)
plt.title("CPU")
plt.plot(cpu)
plt.ylabel("CPU (%)")
plt.xlabel("Time (s)")
plt.xlim(0, elapsed - 1)
plt.ylim(min_or_zero(cpu), max(cpu) + 1)
plt.grid(True)
# Threads Number plot
if args.verbose:
print("Plotting Threads Number...")
plt.subplot(rows, cols, 3)
plt.ticklabel_format(useOffset=False)
plt.title("Threads")
plt.plot(threads, '--')
plt.ylabel("Threads")
plt.xlabel("Time (s)")
plt.xlim(0, elapsed - 1)
plt.ylim(min(threads) - 1, max(threads) + 1)
plt.grid(True)
# Memory Percentage plot
if args.verbose:
print("Plotting Memory Percentage...")
plt.subplot(rows, cols, 2)
plt.ticklabel_format(useOffset=False)
plt.title("Memory (%)")
plt.plot(mem_pct)
plt.ylabel("Memory (%)")
plt.xlabel("Time (s)")
plt.xlim(0, elapsed - 1)
plt.ylim(min_or_zero(mem_pct), max(mem_pct) + 1)
plt.grid(True)
# Virtual Memory plot
if args.verbose:
print("Plotting Virtual Memory...")
plt.subplot(rows, cols, 4)
plt.ticklabel_format(useOffset=False)
plt.title("Memory (VMS)")
plt.plot(mem_vms, 'g-')
plt.ylabel("Memory (MB)")
plt.xlabel("Time (s)")
plt.xlim(0, elapsed - 1)
plt.ylim(min(mem_vms) - 1, max(mem_vms) + 1)
plt.grid(True)
# Resident Memory plot
if args.verbose:
print("Plotting Resident Memory...")
plt.subplot(rows, cols, 6)
plt.ticklabel_format(useOffset=False)
plt.title("Memory (RSS)")
plt.plot(mem_rss, 'r-')
plt.ylabel("Memory (MB)")
plt.xlabel("Time (s)")
plt.xlim(0, elapsed - 1)
plt.ylim(min(mem_rss) - 1, max(mem_rss) + 1)
plt.grid(True)
# Graphspace cleanup
if args.verbose:
print("Cleaning up graphspace...")
fig.tight_layout()
plt.subplots_adjust(left=0.1, right=0.95, top=0.95, bottom=0.2)
# Additional text
if args.verbose:
print("Overlaying process details...")
fig.text(0.005, 0.09, "Command: \"" + cmdline + "\"")
fig.text(0.005, 0.05, "Cores: " + str(cores) + " - Niceness: " + str(niceness) + " - Parent PID: " + str(ppid))
fig.text(0.005, 0.01, "Started at: " + create_time + " - Monitored for: " + str(elapsed) + "s")
if args.verbose:
print("Showing the graph...")
plt.show()