-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemoryusage.py
executable file
·42 lines (31 loc) · 1.33 KB
/
memoryusage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env python
"""
Display the system memory usage. Can be called on a remote server or use 'local' or 'localhost' for your computer
Usage:
./memoryusage.py MYSERVER
"""
import argparse
import subprocess
def getMemoryUsage(server):
"""
Returns the cpu load as a value from the interval [0.0, 1.0]
"""
if server in ['local', 'localhost']:
result = subprocess.check_output('free -m', shell=True)
else:
result = subprocess.check_output('ssh %s "free -m"' % server, shell=True)
lines = result.split('\n')
toks = lines[2].split() # split along whitespace
used = int(toks[2])
free = int(toks[3])
total = used + free
toks = lines[3].split()
swap = float(toks[2]) / float(toks[1]) if int(toks[1]) else 0
return used, total, swap
if __name__ == '__main__': # allow funcs above to be imported as a module
parser = argparse.ArgumentParser(description='Get memory usage for a server/computer.')
parser.add_argument("server", help='Enter server name as defined in ~/.ssh/config or user@ip. NB: public key should be uploaded to server. For local computer use either local or localhost')
args = parser.parse_args()
used, total, swap = getMemoryUsage(args.server)
print "Memory usage: {:.2f}% of {}Mb (swap: {:.2f}%)".format(100.0*used/total, total, swap*100)
exit()