read number of users from OS using python
<p>I am writing a nagios plugin that would exit based on number of users that are logged into my instance.</p>

<pre><code>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 &gt;=  args.critical:
                print "we are critical"
                sys.exit(2)
        elif a &lt;= args.warning:
                print " we are ok"
                sys.exit(0)
        elif (a &gt;= args.warning and a &lt; args.critical):
                print "we are warning"
                sys.exit(1)
        else:
                print "Unkown"
                sys.exit(3)
</code></pre>

<p>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.</p>

<pre><code>python test.py -c
&lt;type 'str'&gt;
value of critical is 10
1

result from command is 1
&lt;type 'str'&gt;
we are critical
</code></pre>

<p>To convert string to integer use function int(). For instance <code>res_integer = int(res)</code></p>

python shell python-os