summaryrefslogtreecommitdiffstats
path: root/scripts
diff options
context:
space:
mode:
authorSeth Vidal <skvidal@fedoraproject.org>2013-05-13 16:55:42 +0000
committerSeth Vidal <skvidal@fedoraproject.org>2013-05-13 16:55:42 +0000
commitd9308768074bfbf90cbbdad8b601c272c150e7d4 (patch)
tree5be254bf2d0bae7b1db424d49525a1e10d675dc7 /scripts
parent5a9399bcfe040c6f6f1ff2b2e57c6473c1e92203 (diff)
add ans-vhost-freemem
Diffstat (limited to 'scripts')
-rwxr-xr-xscripts/ans-vhost-freemem99
1 files changed, 99 insertions, 0 deletions
diff --git a/scripts/ans-vhost-freemem b/scripts/ans-vhost-freemem
new file mode 100755
index 000000000..890516282
--- /dev/null
+++ b/scripts/ans-vhost-freemem
@@ -0,0 +1,99 @@
+#!/usr/bin/python -tt
+# by skvidal
+# gplv2+
+# print out the ACTUAL freemem - not overcommitted value
+
+
+import sys
+import ansible.runner
+import os
+
+from optparse import OptionParser
+
+def parse_args(args):
+ parser = OptionParser(version = "1.0")
+ parser.add_option('--host', default=[], action='append',
+ help="hosts to act on, defaults to ALL")
+ parser.add_option('--timeout', default=30, type='int',
+ help='set the wait timeout for func commands')
+ parser.add_option('--hosts-from-file', default=None, dest="hostfile",
+ help="read list of hosts from this file, if '-' read from stdin")
+ (opts, args) = parser.parse_args(args)
+
+ if opts.hostfile:
+ hosts = []
+ if opts.hostfile == '-':
+ hosts = sys.stdin.readlines()
+ else:
+ hosts = open(opts.hostfile, 'r').readlines()
+
+ for hn in hosts:
+ hn = hn.strip()
+ if hn.startswith('#'):
+ continue
+ hn = hn.replace('\n', '')
+ opts.host.append(hn)
+
+ return opts, args, parser
+
+
+opts, args, parser = parse_args(sys.argv[1:])
+hosts ='*'
+if opts.host:
+ hosts = ';'.join(opts.host)
+
+if os.geteuid() == 0:
+ login = 'root'
+else:
+ login = os.getlogin()
+
+# get results of nodeinfo phymemory
+# "phymemory": "24018",
+
+
+errors = []
+
+mem_per_host = {}
+mem_used_in_vm = {}
+ans = ansible.runner.Runner(pattern=hosts, forks=25, transport='paramiko', timeout=10,
+ module_name='virt', module_args='command=nodeinfo', remote_user=login)
+res = ans.run()
+
+for hn in sorted(res['contacted']):
+ if 'failed' in res['contacted'][hn] and res['contacted'][hn]['failed']:
+ continue
+ mem_per_host[hn] = int(res['contacted'][hn]['phymemory'])
+
+
+
+ans = ansible.runner.Runner(pattern=hosts, forks=25, transport='paramiko', timeout=10,
+ module_name='virt', module_args='command=info', remote_user=login)
+res = ans.run()
+
+
+for hn in sorted(res['contacted']):
+ mem_used = 0
+ if 'failed' in res['contacted'][hn] and res['contacted'][hn]['failed']:
+ errors.append('Failed to contact/run virt lookups on %s' % hn)
+ continue
+ for vm in sorted(res['contacted'][hn]):
+ info = res['contacted'][hn][vm]
+
+ if vm == 'Domain-0':
+ continue
+ elif type(info) != dict:
+ continue
+ elif 'maxMem' not in info:
+ continue
+ mem_used += int(info['maxMem'])/1024
+
+ mem_used_in_vm[hn] = mem_used
+
+
+for hn in sorted(mem_per_host):
+ freemem = mem_per_host[hn] - mem_used_in_vm[hn]
+ print '%s : %s' % (hn, freemem)
+
+
+for err in errors:
+ print >> sys.stderr, err \ No newline at end of file