如何计算列表中的字符串数

时间:2012-06-05 09:37:43

标签: python

我需要检查Windows进程号。

import sys
import os
import commands

ip = sys.argv[5]
val = sys.argv[1]
oid = "HOST-RESOURCES-MIB::hrSWRunName"
cmd = "snmpwalk -v 2c -c public %s %s" % (ip,oid)
(r_c,r_e) = commands.getstatusoutput(cmd)
if r_c != 0:
    print "C - snmpwalk is Error."
else:
    for i in r_e.split('\n'):
    a  =  i.split(':')[-1].strip(' "')
    print a

结果:

conhost.exe
conhost.exe
conhost.exe
conhost.exe
fdhost.exe
cmd.exe
fdhost.exe

我希望结果是。我不知道如何实现它。

if sys.argv[1] <5:#count(conhost.exe)
    print "critical -"
else:
    print "OK - "

我的结果如何统计? conhost.exe 4 conhost.exe 1 conhost.exe 1

1 个答案:

答案 0 :(得分:1)

# replace here
else:
    processes = r_e.split('\n')
    programs = 0
    for program in processes:
        programFile = program.split(':')[-1].strip(' "')
        # the first argument you pass to the program should be conhost.exe
        if programFile == sys.argv[1]:
            programs = programs + 1
    if programs < 5 :#count(conhost.exe)
        print "critical: running less than 5 times:", sys.argv[1] 
    else:
        print "OK"

第二版

# replace here
else:
    processes = r_e.split('\n')
    processes = map(lambda program: program.split(':')[-1].strip(' "'), processes)
    if processes.count(sys.argv[1]) < 5 :#count(conhost.exe)
        print "critical: running less than 5 times:", sys.argv[1] 
    else:
        print "OK"