I am writing a nagios plugin that would exit based on number of users that are logged into my instance.
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)
however the issue is for my if statements the result I am getting from commands.getstatusoutput or os.popen are string. how can I get the actual number of users from a shell command.
python test.py -c
<type 'str'>
value of critical is 10
1
result from command is 1
<type 'str'>
we are critical