在python中传递值grep命令

时间:2013-08-12 14:51:43

标签: grep parameter-passing

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

>ps aux | grep openvpn

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

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

如果我想要CPU%stats,我运行: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命令。

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

此致

1 个答案:

答案 0 :(得分:0)

我继续研究并使用以下方法解决:

!/ usr / bin / env python

import subprocess,sys#,shlex

psval = sys.argv [1] #ps aux val提取诸如CPU等#2 =%CPU,3 =%MEM,4 = VSZ,5 = RSS psname = sys.argv [2] #process details / name

#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

此致