Created
July 21, 2016 15:43
-
-
Save forkbombe/fbed94474dd30ca2919e2b7feaf2437d to your computer and use it in GitHub Desktop.
Python Consol Args Example using psutil
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import sys | |
import getopt | |
import psutil | |
def main(argv): | |
# Define variables | |
cpuout = False | |
ramout = False | |
# Get arguments | |
try: | |
opts, args = getopt.getopt(argv, '', ['cpu=', 'ram=']) | |
# If no valid arguments, exit | |
except getopt.GetoptError: | |
sys.exit(2) | |
# Iterate over arguments | |
for opt, arg in opts: | |
# If one is CPU | |
if opt == "--cpu": | |
# These are accepted values | |
if arg == 'complete': | |
cpuout = psutil.cpu_percent(interval=1) | |
if arg == 'cores': | |
cpuout = psutil.cpu_percent(interval=1, percpu=True) | |
if opt == '--ram': | |
if arg == 'swap': | |
ramout = psutil.swap_memory() | |
if arg == 'virtual': | |
ramout = psutil.virtual_memory() | |
if ramout: | |
print 'Memory Usage = ', ramout | |
if cpuout: | |
print 'Percentage CPU = ', cpuout | |
if __name__ == "__main__": | |
main(sys.argv[1:]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment