0
我正在編寫一個將基於登錄到我的實例的用戶數退出的nagios插件。使用python從操作系統讀取用戶數量
import argparse
import subprocess
import os
import commands
import sys
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='checks number of logged in users')
parser.add_argument('-c','--critical', help='enter the value for critical limit', nargs='?', const=10)
parser.add_argument('-w','--warning', help='enter the value for warning limit', nargs='?', const=5)
args = parser.parse_args()
x= commands.getstatusoutput("users | tr ' ' '\n' | sort -u | wc -l")
a= os.popen("users | tr ' ' '\n' | sort -u | wc -l").read()
print type(a)
print "value of critical is %s" % (args.critical)
print a
(co, res) = x
print "result from command is %s" % (res)
print type(res)
if a >= args.critical:
print "we are critical"
sys.exit(2)
elif a <= args.warning:
print " we are ok"
sys.exit(0)
elif (a >= args.warning and a < args.critical):
print "we are warning"
sys.exit(1)
else:
print "Unkown"
sys.exit(3)
但是問題是我的if語句的結果我從commands.getstatusoutput獲取或os.popen是字符串。我如何從shell命令中獲得實際的用戶數量。
python test.py -c
<type 'str'>
value of critical is 10
1
result from command is 1
<type 'str'>
we are critical
確保您可能需要先手動清除空白字符串,因爲空格可能會導致錯誤。 logins = int(str(res).strip()) –
如果答案對您有幫助,請註冊並標記爲已接受我的答案。 –