newlisp 监控Linux进程 四

用ps命令查看进程使用的cpu和内存

在前面用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占用率不一样


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

Irix和Solaris模式

参考下面的帖子:

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

该帖提出用shift + I来通知top监控站全部cpu核的使用率,而不是单核。默认采用的是Irix mode,按一次shift + I会取消,切换到Solaris mode, 再按一次会回到Irix mode。

简单来说:

Irix mode 就是只计算一个cpu核心,100%代表一个cpu核心达到100%负载,此时如果你有4个核心,就需要除以4.

参考:

http://stackoverflow.com/questions/1032357/comprehending-top-cpu-usage

默认启用Solaris模式

可以用shift + I键切换到Solaris mode,然后用Shift + W键保存到~/.toprc中,这样下次就不需要再手动切换,top命令启动时将读取~/.toprc文件中的配置,自动使用solaris模式。

将toprc文件复制到/etc/toprc文件下,则系统全局使用该文件。


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

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


用top来查看cpu

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

下面的这个newlisp代码使用top而不是ps来获得cpu负载:

#!/usr/bin/newlisp

(set 'pid (first (exec "pidof data_service_d")))
;;(set 'cmd (string "ps -p " (first pid) " -o %cpu,%mem,cmd"))                                                                                    
(set 'cmd (format "top -p %s -n 1" pid))
(set 'result (exec cmd))
(dolist (element result)
;;  (println element)                                                                                                                             
  (set 'l (parse element))
  (unless (empty? l)
    (if (find pid l)
        (println (l 10))
    ))
)
(exit)

运行一下:

root@jstc:/opt/detector# ./process_status.lsp
0



你可能感兴趣的:(newlisp 监控Linux进程 四)