【问题标题】:Pass value grep command in python在python中传递值grep命令
【发布时间】:2013-08-12 14:51:42
【问题描述】:

我通过在 Linux Debian 7 机器上的 Python 脚本中运行以下命令来获取 openvpn 进程的 CPU 和 RAM 统计信息。

>ps aux | grep openvpn

解析输出并发送到zabbix监控服务器。

我目前使用名为 psperf.py 的以下 Python 脚本。

如果我想要 CPU% 统计信息,我会运行:psperf 2

>#!/usr/bin/env python
>
>import subprocess, sys, shlex
>
>psval=sys.argv[1] #ps aux val to extract such as CPU etc #2 = %CPU, 3 = %MEM, 4 = VSZ, 5 = RSS
>
>#https://stackoverflow.com/questions/6780035/python-how-to-run-ps-cax-grep-something-in-python
>proc1 = subprocess.Popen(shlex.split('ps aux'),stdout=subprocess.PIPE)
>proc2 = subprocess.Popen(shlex.split('grep  >openvpn'),stdin=proc1.stdout,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
>
>proc1.stdout.close() # Allow proc1 to receive a SIGPIPE if proc2 exits.
>out,err=proc2.communicate()
>
>#string stdout?
>output = (format(out))
>
>#create output list
>output = output.split()
>  
>#make ps val an integer to enable list location
>psval = int(psval)
>
>#extract value to send to zabbix from output list
>val = output[psval]
>
>#OUTPUT
>print val

这个脚本可以很好地获取与 openvpn 相关的数据。但是,我现在想通过传递从中提取数据的流程详细信息来重用脚本,而不必为每个单独的流程编写脚本。例如,我可能想要 zabbix 进程的 CPU 和 RAM 统计信息。

我尝试了各种解决方案,包括以下解决方案,但索引超出范围。

例如我运行:psperf 2 apache

>#!/usr/bin/env python
>
>import subprocess, sys, shlex
>
>psval=sys.argv[1] #ps aux val to extract such as CPU etc #2 = %CPU, 3 = %MEM, 4 = VSZ, 5 = RSS
>psname=sys.argv[2] #process details/name
>
>#https://stackoverflow.com/questions/6780035/python-how-to-run-ps-cax-grep-something-in-python
>proc1 = subprocess.Popen(shlex.split('ps aux'),stdout=subprocess.PIPE)
>proc2 = subprocess.Popen(shlex.split('grep', >psname),stdin=proc1.stdout,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
>
>proc1.stdout.close() # Allow proc1 to receive a SIGPIPE if proc2 exits.
>out,err=proc2.communicate()
>
>#string stdout?
>output = (format(out))
>
>#create output list
>output = output.split()
>
>#make ps val an integer to enable list location
>psval = int(psval)
>
>#extract value to send to zabbix from output list
>val = output[psval]
>
>#OUTPUT
>print val

错误:

>root@Deb764opVPN:~# python /usr/share/zabbix/externalscripts/psperf.py 4 openvpn
>Traceback (most recent call last):
>  File "/usr/share/zabbix/externalscripts/psperf.py", line 25, in <module>
>    val = output[psval]
>IndexError: list index out of range

过去我没有使用过对我来说是新的 shlex 类。这是将 ps aux 命令安全地通过管道传递给 grep 所必需的 - 避免 shell = true - 安全隐患 (http://docs.python.org/2/library/subprocess.html)。

我采用的脚本来自:How to run " ps cax | grep something " in Python?

我认为这与 shlex 如何处理我的请求有关,但我不确定如何继续。

你能帮忙吗?就像我如何成功地将值传递给 grep 命令一样。

我可以看到这对许多其他管道命令等是有益的。

问候

艾丹

【问题讨论】:

    标签: grep parameter-passing


    【解决方案1】:

    我使用以下方法进行了研究和解决:

    !/usr/bin/env python

    导入子进程、sys #、shlex

    psval=sys.argv[1] #ps aux val 要提取例如 CPU 等 #2 = %CPU, 3 = %MEM, 4 = VSZ, 5 = RSS psname=sys.argv[2] #进程详情/名称

    #http://www.cyberciti.biz/tips/grepping-ps-output-without-getting-grep.html
    proc1 = subprocess.Popen(['ps', 'aux'], stdout=subprocess.PIPE)
    proc2 = subprocess.Popen(['grep', psname], stdin=proc1.stdout,stdout=subprocess.PIPE)
    
    proc1.stdout.close() # Allow proc1 to receive a SIGPIPE if proc2 exits.
    
    stripres = proc2.stdout.read()
    
    #TEST RESULT
    print stripres
    
    #create output list
    output = stripres.split()
    
    #make ps val an integer to enable list location
    psval = int(psval)
    
    #extract value to send to zabbix from output list
    val = output[psval]
    
    #OUTPUT
    print val
    

    问候

    艾丹

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-16
      • 2017-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多