summaryrefslogtreecommitdiffstats
path: root/scripts
diff options
context:
space:
mode:
Diffstat (limited to 'scripts')
-rwxr-xr-xscripts/ans-vhost-freemem26
1 files changed, 16 insertions, 10 deletions
diff --git a/scripts/ans-vhost-freemem b/scripts/ans-vhost-freemem
index 7fd014717..b350f27df 100755
--- a/scripts/ans-vhost-freemem
+++ b/scripts/ans-vhost-freemem
@@ -56,14 +56,24 @@ else:
errors = []
+# Setup some dictionaries for storing intermediate results
mem_per_host = {}
mem_used_in_vm = {}
cpu_per_host = {}
cpu_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)
+# We end up running ansible twice here. These are the common arguments.
+# We'll use two different commands of the 'virt' ansible module.
+ansible_args = dict(
+ pattern=hosts,
+ module_name='virt',
+ forks=25,
+ transport='paramiko',
+ timeout=10,
+ remote_user=login,
+)
+
+ans = ansible.runner.Runner(module_args='command=nodeinfo', **ansible_args)
res = ans.run()
for hn in sorted(res['contacted']):
@@ -73,9 +83,7 @@ for hn in sorted(res['contacted']):
cpu_per_host[hn] = int(res['contacted'][hn]['cpus'])
-ans = ansible.runner.Runner(
- pattern=hosts, forks=25, transport='paramiko', timeout=10,
- module_name='virt', module_args='command=info', remote_user=login)
+ans = ansible.runner.Runner(module_args='command=info', **ansible_args)
res = ans.run()
for hn in sorted(res['contacted']):
@@ -92,11 +100,9 @@ for hn in sorted(res['contacted']):
continue
elif type(info) != dict:
continue
- elif 'maxMem' not in info:
- continue
- mem_used += int(info['maxMem'])/1024
- cpu_used += info['nrVirtCpu']
+ mem_used += int(info.get('maxMem', 0))/1024
+ cpu_used += info.get('nrVirtCpu', 0)
mem_used_in_vm[hn] = mem_used
cpu_used_in_vm[hn] = cpu_used