newlisp 监控Linux进程 四

在前面用newlisp监控进程的时候,其实只是判断进程是否存活,如果不是,则重新启动。现在还要实现另一个功能,监控进程的cpu和memory占用情况。

如果用man ps查看ps命令,你会看到下面的参数:

CODE       HEADER  DESCRIPTION

       %cpu       %CPU    cpu utilization of the process in "##.#" format. Currently, it is the CPU time used divided by the time the process
                          has been running (cputime/realtime ratio), expressed as a percentage. It will not add up to 100% unless you are
                          lucky. (alias pcpu).

       %mem       %MEM    ratio of the process‘s resident set size  to the physical memory on the machine, expressed as a percentage.
                          (alias pmem).

       args       COMMAND command with all its arguments as a string. Modifications to the arguments may be shown. The output in this column
                          may contain spaces. A process marked <defunct> is partly dead, waiting to be fully destroyed by its parent.
                          Sometimes the process args will be unavailable; when this happens, ps will instead print the executable name in
                          brackets. (alias cmd, command). See also the comm format keyword, the -f option, and the c option.
                          When specified last, this column will extend to the edge of the display. If ps can not determine display width, as
                          when output is redirected (piped) into a file or another command, the output width is undefined (it may be 80,
                          unlimited, determined by the TERM variable, and so on). The COLUMNS environment variable or --cols option may be
                          used to exactly determine the width in this case. The w or -w option may be also be used to adjust width.

       blocked    BLOCKED mask of the blocked signals, see signal(7). According to the width of the field, a 32 or 64-bit mask in hexadecimal
                          format is displayed. (alias sig_block, sigmask).

....

%cpu 和 %mem就是我需要的。

通过下面的命令可以监控一个进程的cpu和memory使用情况:

ps -p <pid> -o %cpu,%mem,cmd

pid哪里来,通过pidof命令。现在用newlisp将这两个命令连接在一起:

#!/usr/bin/newlisp

(set ‘pid (exec "pidof data_service_d"))
(set ‘cmd (string "ps -p " (first pid) " -o %cpu,%mem,cmd"))
(println (exec cmd))
(exit)

执行一下:

root@jstc:/opt/detector# ./process_status.lsp 
("%CPU %MEM CMD" " 0.2  0.3 ./data_service_d ./config.xml")

效果不错。以后再加点代码发到我的风洞服务器上,就可以在网站上绘制折线图了。


关于top和ps反映出来的cpu占用率不一样,是个问题:

参考下面的帖子:

http://superuser.com/questions/643331/ps-and-top-give-different-cpu-usage

该帖提出用shift + I来通知top监控站全部cpu核的使用率,而不是单核。


http://unix.stackexchange.com/questions/58539/top-and-ps-not-showing-the-same-cpu-result

该贴答案指出ps不是最准确的cpu利用率跟踪工具,而应该使用top -p $pid的方式


因此ps的方法只能是一个不准确的用法。

newlisp 监控Linux进程 四,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。