友链
导航
These are the good times in your life,
so put on a smile and it'll be alright
友链
导航
Monitorix Project by Jordi Sanfeliu (aka Mikaku)
安装配置最简单的图形化监控程序. 自带 web 服务器.
缺点是最高精度为 daily, 没有更细如 hourly 的图像. 且 top 经常会看到 perl 占满 CPU.
<list> 0 = /, swap, boot, home, /mnt/backup </list>
查看指定进程的内存占用情况
# 以 nginx 为例 # pmap [-x|-d] 2395 | head -n 10 2395: nginx: master process ./sbin/nginx 00110000 240K r-x-- /lib/libgssapi_krb5.so.2.2 0014c000 4K ----- /lib/libgssapi_krb5.so.2.2 0014d000 4K r---- /lib/libgssapi_krb5.so.2.2 0014e000 4K rw--- /lib/libgssapi_krb5.so.2.2 0014f000 12K r-x-- /lib/libcom_err.so.2.1 00152000 4K r---- /lib/libcom_err.so.2.1 00153000 4K rw--- /lib/libcom_err.so.2.1 00154000 48K r-x-- /lib/libnss_files-2.12.so 00160000 4K r---- /lib/libnss_files-2.12.so ... b78e4000 20K rw--- [ anon ] b78f3000 4K rw-s- /dev/zero (deleted) b78f4000 4K rw--- [ anon ] bfeaa000 84K rw--- [ stack ]
不加参数运行 iostat, iostat 返回的是 开机至当前 的 IO 数据
$ iostat Linux 3.10.25+ (piM) 03/24/2014 _armv6l_ (1 CPU) avg-cpu: %user %nice %system %iowait %steal %idle 9.93 0.00 1.60 0.06 0.00 88.41 Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn mmcblk0 0.02 0.06 0.00 768 0 sda 0.32 9.62 0.14 116869 1712
加一个参数 (interval) 使用 iostat, iostat 第一次输出是 开机至当前 的 IO 数据, 之后的输出为 间隔内 的 IO 数据
$ iostat 5 # 开机至当前 Linux 3.10.25+ (piM) 03/24/2014 _armv6l_ (1 CPU) avg-cpu: %user %nice %system %iowait %steal %idle 9.89 0.00 1.59 0.06 0.00 88.46 Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn mmcblk0 0.02 0.06 0.00 768 0 sda 0.32 9.58 0.14 116869 1712 # 前 5 秒 avg-cpu: %user %nice %system %iowait %steal %idle 0.20 0.00 0.60 0.00 0.00 99.20 Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn mmcblk0 0.00 0.00 0.00 0 0 sda 0.00 0.00 0.00 0 0 $ iostat 2 6 # 每 2s 输出一遍, 共输出 6 次
参考 man:
Theinterval
parameter specifies the amount of time in seconds between each report. The first report contains statistics for the time since system startup (boot). Each subsequent report contains statistics collected during the interval since the previous report.
TODO: 要明白 strace 中常见的系统调用: Polling (computer science) - Wikipedia, the free encyclopedia
mac 下用 dtrace 或 dtruss
如果对系统程序使用,报 'dtrace: system integrity protection is on, some features will not be available
'
则需 cp 要使用的程序到 /tmp 目录即可
The most common usage is to start a program using strace, which prints a list of system calls made by the program. This is useful if the program continually crashes, or does not behave as expected; for example using strace may reveal that the program is attempting to access a file which does not exist or cannot be read.
An alternative application is to use the -p flag to attach to a running process. This is useful if a process has stopped responding, and might reveal, for example, that the process is blocking whilst attempting to make a network connection.
一般 | |
---|---|
-p pid | 跟踪指定的进程 pid |
-o xxx.txt | 输出到某个文件 |
-c | Generate Statistics Report of System Calls |
-f/-F | 告诉strace同时跟踪 fork 和 vfork 出来的进程 |
-s strsize | 指定输出的字符串的最大长度.默认为 32.文件名一直全部输出 |
-u username | 以 username 的 UID 和GID 执行被跟踪的命令 |
显示时间 | |
-r | 打印出关于每一个系统调用的相对时间(微秒级, E-6) |
-t | 在输出中的每一行前加上时间信息(秒级) |
-t | 在输出中的每一行前加上时间信息(微秒级) |
-T | 显示每一调用所耗的时间 |
输出过滤 | |
-e trace=set | 只跟踪指定的系统调用.例如: -e trace=open,close,rean,write 表示只跟踪这四个系统调用.默认的为 set=all |
-e trace=file | 只跟踪有关文件操作的系统调用 |
-e trace=process | 只跟踪有关进程控制的系统调用 |
-e trace=network | 跟踪与网络有关的所有系统调用 |
-e strace=signal | 跟踪所有与系统信号有关的系统调用 |
-e trace=ipc | 跟踪所有与进程通讯有关的系统调用 |
-e abbrev=set | 设定 strace 输出的系统调用的结果集, 默认为 abbrev=all |
-v | 等于 abbrev=none |
-e raw=set | 将指定的系统调用的参数以十六进制显示 |
-e signal=set | 指定跟踪的系统信号. 默认为 all. 如 signal=!SIGIO (或者 signal=!io ), 表示不跟踪 SIGIO 信号 |
-e read=set | 输出从指定文件中读出的数据.例如 -e read=3,5 |
-e write=set | 输出写入到指定文件中的数据 |
top 界面分为两个部份,光标上面部份显示关于系统整体性能,光标下面部份显示各进程信息。光标所在处是用来输入操作命令的。
uptime | |
---|---|
01:47:56 | 系统当前时间 |
up 1:26 | 系统开机到现在经过了多少时间 |
2 users | 当前2用户在线 |
load average:0.00,0.00,0.00 | 系统1分钟、5分钟、15分钟的CPU负载信息 |
Tasks | |
38 total | 当前任务(进程)数 |
1 running | 1个进程正在运行 |
37 sleeping | 37个进程睡眠 |
0 stopped | 停止的进程数 |
0 zombie | 僵死的进程数 |
Cpu(s) | |
0.0%us | User CPU time, 用户态进程占用CPU时间百分比,不包含renice值为负的任务占用的CPU的时间。 |
0.7%sy | System CPU time, 内核占用CPU时间百分比 |
0.0%ni | Nice CPU time, renice值为负的任务的用户态进程的CPU时间百分比。nice是优先级的意思 |
99.3%id | idle, 空闲CPU时间百分比 |
0.0%wa | iowait, 等待I/O的CPU时间百分比 |
0.0%hi | Hardware IRQ, CPU硬件中断时间百分比 |
0.0%si | Software Interrupts, CPU软件中断时间百分比 |
0.0%st | Steal Time, 与虚拟机相关 |
Mem | |
256412k total | 物理内存总量 |
30156k used | 使用的物理内存量 |
226256 free | 空闲的物理内存量 |
8176k buffers | 用作内核缓存的物理内存量 |
Swap | |
337356k total | 交换区总量 |
0k used | 使用的交换区量 |
337356k free | 空闲的交换区量 |
12160k cached | 缓冲交换区总量 |
PID | Process Id, 进程的ID |
PPID | Parent Process Pid, 父进程的ID |
USER | User, 进程所有者 |
PR | Priority, 进程的优先级别, 越小越优先被执行 |
NI | Nice value, nice值 |
VIRT | Virtual Image (kb), 进程占用的虚拟内存 |
RES | Resident size (kb), 进程占用的物理内存 |
SHR | Shared Mem size (kb), 进程使用的共享内存 |
S | Process Status, 进程的状态。Sleeping,Running,Zombie, 'D' = uninterruptible sleep, 'T' = traced or stopped, N表示该进程优先值为负数 |
%CPU | 进程占用CPU的使用率 |
%MEM | 进程使用的物理内存和总内存的百分比 |
TIME+ | 该进程启动后占用的总的CPU时间,即占用CPU使用时间的累加值 |
COMMAND | 进程启动命令名称 |
参数 | |
---|---|
-u username | 指定显示用户进程 |
-p pid[,pid…] | 指定 PID 进程 |
-n | 显示 n 屏后退出 |
-b | 以非交互方式显示, 可配合 -n 输出到文件 |
命令 | |
O | 选择排序项 |
R | 按当前倒序显示 |
k | kill进程 |
r | renice 进程 |
u | 指定显示用户进程 |
1(one) | 显示所有CPU |
d/s | 设置刷新时间间隔 |
b/z | 高亮显示 running 进程 |
c | 显示命令完全模式 |
A | 按不同排序分栏显示, 分栏后 a 可显示各栏指标 |
n | 显示进程数 |
W | 保存当前对top的设置,下次top会按此启动 |
man top
标准 | 命令 | 意义 | 表头 |
---|---|---|---|
SUS/POSIX | ps -ef | every process, full infomation | UID, PID, PPID, C, STIME, TTY, TIME, CMD |
BSD | ps axu | all processes on a terminal, and all without a terminal, add user info | USER, PID, %CPU, %MEM, VSZ, RSS, TTY, STAT, START, TIME, COMMAND |
ps
has many options. On operating systems that support the SUS and POSIX standards, ps commonly runs with the options -ef
, where “-e” selects every process and “-f” chooses the “full” output format. Another common option on these systems is -l
, which specifies the “long” output format.
Most systems derived from BSD fail to accept the SUS and POSIX standard options because of historical conflicts (for example, the “e” or “-e” option will cause the display of environment variables). On such systems, ps commonly runs with the non-standard options aux
, where “a” lists all processes on a terminal, including those of other users, “x” lists all processes without controlling terminals and “u” adds a column for the controlling user for each process. Note that, for maximum compatibility when using this syntax, there is no “-” in front of the “aux”. Also you can add 'ww' after aux, like ps auxww
for complete information about the process including all parameters.
$ pstree # pstree -Hpid: highlight the specified process
$ pgrep lighttpd
1234
$ ps p 1234 o comm= lighttpd
$ ps auxf | sort -nr -k 4 | head -10
$ ps auxf | sort -nr -k 3 | head -10
Here are the different values that the s, stat and state output specifiers (header “STAT” or “S”) will display to describe the state of a process.
D | Uninterruptible sleep (usually IO) |
R | Running or runnable (on run queue) |
S | Interruptible sleep (waiting for an event to complete) |
T | Stopped, either by a job control signal or because it is being traced. |
W | paging (not valid since the 2.6.xx kernel) |
X | dead (should never be seen) |
Z | Defunct (“zombie”) process, terminated but not reaped by its parent. |
For BSD formats and when the stat keyword is used, additional characters may be displayed:
< | high-priority (not nice to other users) |
N | low-priority (nice to other users) |
L | has pages locked into memory (for real-time and custom IO) |
s | is a session leader |
l | is multi-threaded (using CLONE_THREAD, like NPTL pthreads do) |
+ | is in the foreground process group |
$ free total used free shared buffers cached Mem: 5905248 3353480 2551768 0 283332 1611712 -/+ buffers/cache: 1458436 4446812 Swap: 6081532 0 6081532 # 以 G 显示 $ free -g total used free shared buffers cached Mem: 5 3 2 0 0 1 -/+ buffers/cache: 1 4 Swap: 5 0 5
或者 less /proc/meminfo
查看系统的虚拟内存(见图)情况
与 free 的区别
# 每秒输出一次 # vmstat 1 procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu----- r b swpd free buff cache si so bi bo in cs us sy id wa st 2 0 0 57484 356864 861332 0 0 0 1 7 3 1 0 99 0 0 0 0 0 57468 356864 861360 0 0 0 0 336 145 6 1 94 0 0 0 0 0 57468 356864 861360 0 0 0 0 43 51 0 0 100 0 0 0 0 0 57468 356864 861360 0 0 0 16 51 62 0 0 100 0 0
$ w 00:33:06 up 1:06, 2 users, load average: 0.10, 0.08, 0.02 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT toksea tty1 - 23:28 1:04m 0.72s 0.56s -bash toksea pts/0 192.168.0.15 23:28 0.00s 1.05s 0.06s sshd: toksea [priv] # or $ w username
xiaopei@xiaopei-ThinkPad-X201:~$ last reboot system boot 3.0.0-15-generic Wed Apr 18 13:02 - 22:53 (2+09:51) reboot system boot 3.0.0-15-generic Sun Apr 15 16:47 - 18:13 (01:25) reboot system boot 3.0.0-15-generic Fri Apr 13 08:19 - 08:03 (1+23:43) reboot system boot 3.0.0-15-generic Thu Apr 12 22:46 - 01:06 (02:19) reboot system boot 3.0.0-15-generic Wed Apr 11 21:02 - 22:45 (1+01:42) reboot system boot 3.0.0-15-generic Tue Apr 10 00:18 - 01:23 (01:04) xiaopei pts/0 lis-imac.local Fri Apr 6 16:31 - 09:30 (2+16:59) reboot system boot 3.0.0-15-generic Thu Apr 5 14:11 - 00:18 (4+10:06)
Open files in the system include disk files, pipes, network sockets and devices opened by all processes. One use for this command is when a disk cannot be unmounted because (unspecified) files are in use. The listing of open files can be consulted (suitably filtered if necessary) to identify the process that is using the files.
# lsof COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME init 1 root cwd DIR 8,1 4096 2 / init 1 root txt REG 8,1 124704 917562 /sbin/init init 1 root 0u CHR 1,3 0t0 4369 /dev/null init 1 root 1u CHR 1,3 0t0 4369 /dev/null init 1 root 2u CHR 1,3 0t0 4369 /dev/null init 1 root 3r FIFO 0,8 0t0 6323 pipe ...
# lsof
不带任何参数运行lsof会列出所有进程打开的所有文件。
# lsof /path/to/file # lsof /path/to/file1 /path/to/file2
只需要执行文件的路径,lsof就会列出所有使用这个文件的进程,你也可以列出多个文件,lsof会列出所有使用这些文件的进程。
# lsof +D /usr/lib or # lsof | grep '/usr/lib'
加上+D参数,lsof会对指定目录进行递归查找,注意这个参数要比grep版本慢. 之所以慢是因为+D首先查找所有的文件,然后一次性输出。
# lsof -u pkrumins # lsof -u rms,root
-u选项限定只列出所有被用户pkrumins打开的文件,你可以通过逗号指定多个用户:
# lsof -c apache
-c选项限定只列出以apache开头的进程打开的文件:
所以你可以不用像下面这样写:
# lsof | grep foo
而使用下面这个更简短的版本:
# lsof -c foo
事实上,你可以只制定进程名称的开头:
# lsof -c apa
这会列出所有以apa开头的进程打开的文件
# lsof -p 1
-p选项让你可以使用进程id来过滤输出。
记住你也可以用都好来分离多个pid。
# lsof -p 450,980,333
列出所有进程打开的文件除了某个pid的
# lsof -p ^1
同前面的用户一样,你也可以对-p选项使用^来进行取反。
# lsof -i
lsof的-i选项可以列出所有打开了网络套接字(TCP和UDP)的进程。
列出所有TCP网络连接
# lsof -i tcp
也可以为-i选项加上参数,比如tcp,tcp选项会强制lsof只列出打开TCP sockets的进程。
列出所有UDP网络连接
# lsof -i udp
同样udp让lsof只列出使用UDP socket的进程。
找到使用某个端口的进程
# lsof -i :25
:25和-i选项组合可以让lsof列出占用TCP或UDP的25端口的进程。
你也可以使用/etc/services中制定的端口名称来代替端口号,比如:
# lsof -i :smtp
找到使用某个udp端口号的进程
# lsof -i udp:53
同样的,也可以找到使用某个tcp端口的进程:
# lsof -i tcp:80
不做 dns 和 service 转换:
# lsof -i -P
# lsof -t -i
-t选项输出进程的PID,你可以将它和-i选项组合输出使用某个端口的进程的PID,下面的命令将会杀掉所有使用网络的进程:
# kill -9 `lsof -t -i`
不如用 watch
# lsof -r 1
-r选项让lsof可以循环列出文件直到被中断,参数1的意思是每秒钟重复打印一次,这个选项最好同某个范围比较小的查询组合使用,比如用来监测网络活动:
# lsof -r 1 -u john -i -a
你可以像下面这样使用多个-u做同样的事情:
# lsof -u rms -u root
你同样可以制定多个-c参数:
# lsof -c apache -c python
这会列出所有由apache or python打开的文件
你也可以组合使用多个选项,这些选项默认进行或关联,也就是说下面的命令会输入由pkrumins用户或是apache进程打开的文件。
# lsof -u pkrumins -c apache
列出所有由一个用户与某个进程打开的文件
# lsof -a -u pkrumins -c bash
-a参数可以将多个选项的组合条件由或变为与,上面的命令会显示所有由pkrumins用户以及bash进程打开的文件。
找到某个用户的所有网络连接
# lsof -a -u hacker -i
使用-a将-u和-i选项组合可以让lsof列出某个用户的所有网络行为。
列出除root用户外的所有用户打开的文件
# lsof -u ^root
注意root前面的^符号,它执行取反操作,因此lsof会列出所有root用户之外的用户打开的文件。
# lsof -N
这个参数很好记,-N就对应NFS。
# lsof -U
这个选项也很好记,-U就对应UNIX。
# lsof -g 1234
进程组用来来逻辑上对进程进行分组,这个例子查找所有PGID为1234的进程打开的文件。
列出所有与某个描述符1)关联的文件
# lsof -d 2
这个命令会列出所有以描述符2打开的文件。
你也可以为描述符指定一个范围:
# lsof -d 0-2
这会列出所有描述符为0,1,2的文件。
-d选项还支持其它很多特殊值,下面的命令列出所有内存映射文件:
# lsof -d mem
txt则列出所有加载在内存中并正在执行的进程:
# lsof -d txt
查看 CPU 用量
$ mpstat Linux 3.2.0-34-generic (think.example.com) 12/23/2012 _x86_64_ (4 CPU) 10:39:01 PM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %idle 10:39:01 PM all 6.71 0.01 1.58 0.55 0.00 0.03 0.00 0.00 91.11 $ mpstat -P ALL Linux 3.2.0-34-generic (think.example.com) 12/23/2012 _x86_64_ (4 CPU) 10:38:38 PM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %idle 10:38:38 PM all 6.71 0.01 1.58 0.55 0.00 0.03 0.00 0.00 91.11 10:38:38 PM 0 9.97 0.01 2.54 1.36 0.00 0.12 0.00 0.00 86.00 10:38:38 PM 1 3.26 0.01 0.75 0.46 0.00 0.00 0.00 0.00 95.53 10:38:38 PM 2 10.53 0.01 2.36 0.26 0.00 0.00 0.00 0.00 86.84 10:38:38 PM 3 3.14 0.01 0.70 0.14 0.00 0.00 0.00 0.00 96.01