kok*_*ito 4 linux monitoring sar
Can I get memory usage per process with Linux? we monitor our servers with sysstat/sar. But besides seeing that memory went off the roof at some point, we can't pinpoint which process was getting bigger and bigger. is there a way with sar (or other tools) to get memory usage per process? and look at it, later on?
sysstat包括pidstat其手册页说:
该
pidstat命令用于监视当前由 Linux 内核管理的单个任务。它为使用选项选择-p的每个任务或由 Linux 内核管理的每个任务写入标准输出活动[...]
Linux 内核任务包括用户空间进程和线程(还有内核线程,这里最不感兴趣)。
但不幸的sysstat是不支持从中收集历史数据,pidstat而且作者似乎没有兴趣提供此类支持(GitHub 问题):
pidstat话虽如此,pidstat可以将 的表格输出写入文件并稍后进行解析。通常感兴趣的是进程组,而不是系统上的每个进程。我将专注于一个进程及其子进程。
什么可以是一个例子?火狐。pgrep firefox返回其 PID,$(pgrep -d, -P $(pgrep firefox))返回其子项的逗号分隔列表。鉴于此,pidstat命令可能如下所示:
LC_NUMERIC=C.UTF-8 watch pidstat -dru -hl \
-p '$(pgrep firefox),$(pgrep -d, -P $(pgrep firefox))' \
10 60 '>>' firefox-$(date +%s).pidstat
Run Code Online (Sandbox Code Playgroud)
一些观察:
LC_NUMERIC设置为pidstat使用点作为小数点分隔符。watch用于pidstat每 600 秒重复一次,以防进程子树发生变化。-d至报告I / O统计,-r至报告页面错误和内存利用率,-u来report CPU utilization。-h使所有报告组被放置在一个行,-l以显示进程的命令名称和它的所有参数(当然,那种,因为它仍然在127个字符修剪它)。date 用于避免意外覆盖现有文件它产生类似的东西:
Linux kernel version (host) 31/03/20 _x86_64_ (8 CPU)
# Time UID PID %usr %system %guest %CPU CPU minflt/s majflt/s VSZ RSS %MEM kB_rd/s kB_wr/s kB_ccwr/s iodelay Command
1585671289 1000 5173 0.50 0.30 0.00 0.80 5 0.70 0.00 3789880 509536 3.21 0.00 29.60 0.00 0 /usr/lib/firefox/firefox
1585671289 1000 5344 0.70 0.30 0.00 1.00 1 0.50 0.00 3914852 868596 5.48 0.00 0.00 0.00 0 /usr/lib/firefox/firefox -contentproc -childID 1 ...
1585671289 1000 5764 0.10 0.10 0.00 0.20 1 7.50 0.00 9374676 363984 2.29 0.00 0.00 0.00 0 /usr/lib/firefox/firefox -contentproc -childID 2 ...
1585671289 1000 5852 6.60 0.90 0.00 7.50 7 860.70 0.00 4276640 1040568 6.56 0.00 0.00 0.00 0 /usr/lib/firefox/firefox -contentproc -childID 3 ...
1585671289 1000 24556 0.00 0.00 0.00 0.00 7 0.00 0.00 419252 18520 0.12 0.00 0.00 0.00 0 /usr/lib/firefox/firefox -contentproc -parentBuildID ...
# Time UID PID %usr %system %guest %CPU CPU minflt/s majflt/s VSZ RSS %MEM kB_rd/s kB_wr/s kB_ccwr/s iodelay Command
1585671299 1000 5173 3.40 1.60 0.00 5.00 6 7.60 0.00 3789880 509768 3.21 0.00 20.00 0.00 0 /usr/lib/firefox/firefox
1585671299 1000 5344 5.70 1.30 0.00 7.00 6 410.10 0.00 3914852 869396 5.48 0.00 0.00 0.00 0 /usr/lib/firefox/firefox -contentproc -childID 1 ...
1585671299 1000 5764 0.00 0.00 0.00 0.00 3 0.00 0.00 9374676 363984 2.29 0.00 0.00 0.00 0 /usr/lib/firefox/firefox -contentproc -childID 2 ...
1585671299 1000 5852 1.00 0.30 0.00 1.30 1 90.20 0.00 4276640 1040452 6.56 0.00 0.00 0.00 0 /usr/lib/firefox/firefox -contentproc -childID 3 ...
1585671299 1000 24556 0.00 0.00 0.00 0.00 7 0.00 0.00 419252 18520 0.12 0.00 0.00 0.00 0 /usr/lib/firefox/firefox -contentproc -parentBuildID ...
...
Run Code Online (Sandbox Code Playgroud)
请注意,每行数据都以空格开头,因此解析很容易:
import pandas as pd
def read_columns(filename):
with open(filename) as f:
for l in f:
if l[0] != '#':
continue
else:
return l.strip('#').split()
else:
raise LookupError
def get_lines(filename, colnum):
with open(filename) as f:
for l in f:
if l[0] == ' ':
yield l.split(maxsplit=colnum - 1)
filename = '/path/to/firefox.pidstat'
columns = read_columns(filename)
exclude = 'CPU', 'UID',
df = pd.DataFrame.from_records(
get_lines(filename, len(columns)), columns=columns, exclude=exclude
)
numcols = df.columns.drop('Command')
df[numcols] = df[numcols].apply(pd.to_numeric, errors='coerce')
df['RSS'] = df.RSS / 1024 # Make MiB
df['Time'] = pd.to_datetime(df['Time'], unit='s', utc=True)
df = df.set_index('Time')
df.info()
Run Code Online (Sandbox Code Playgroud)
数据帧的结构如下:
Data columns (total 15 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 PID 6155 non-null int64
1 %usr 6155 non-null float64
2 %system 6155 non-null float64
3 %guest 6155 non-null float64
4 %CPU 6155 non-null float64
5 minflt/s 6155 non-null float64
6 majflt/s 6155 non-null float64
7 VSZ 6155 non-null int64
8 RSS 6155 non-null float64
9 %MEM 6155 non-null float64
10 kB_rd/s 6155 non-null float64
11 kB_wr/s 6155 non-null float64
12 kB_ccwr/s 6155 non-null float64
13 iodelay 6155 non-null int64
14 Command 6155 non-null object
dtypes: float64(11), int64(3), object(1)
Run Code Online (Sandbox Code Playgroud)
它可以在依靠什么监控的重点,但许多方面进行可视化%CPU,并RSS是最常见的指标来看待。所以这是一个例子。
import matplotlib.pyplot as plt
fig, axes = plt.subplots(len(df.PID.unique()), 2, figsize=(12, 8))
x_range = [df.index.min(), df.index.max()]
for i, pid in enumerate(df.PID.unique()):
subdf = df[df.PID == pid]
title = ', '.join([f'PID {pid}', str(subdf.index.max() - subdf.index.min())])
for j, col in enumerate(('%CPU', 'RSS')):
ax = subdf.plot(
y=col, title=title if j == 0 else None, ax=axes[i][j], sharex=True
)
ax.legend(loc='upper right')
ax.set_xlim(x_range)
plt.tight_layout()
plt.show()
Run Code Online (Sandbox Code Playgroud)
它产生一个像:
这纯粹是偏好,但我会保持它的美好和简单,直到你知道你在寻找什么。我会创建一个cronjob首先显示可用内存、磁盘和 CPU 使用情况的管道,然后显示前十个罪魁祸首。
#!/bin/sh
free -m | awk 'NR==2{printf "Memory Usage: %s/%sMB (%.2f%%)\n", $3,$2,$3*100/$2 }'
df -h | awk '$NF=="/"{printf "Disk Usage: %d/%dGB (%s)\n", $3,$2,$5}'
top -bn1 | grep load | awk '{printf "CPU Load: %.2f\n", $(NF-2)}'
ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head
Run Code Online (Sandbox Code Playgroud)
找到罪魁祸首后,您可以进一步磨练并深入研究一些细节。
| 归档时间: |
|
| 查看次数: |
5028 次 |
| 最近记录: |