From 35ab4759e1d8a29ad2bb9d789f0bd1eb8d104b34 Mon Sep 17 00:00:00 2001 From: Ralph Bean Date: Tue, 30 Jul 2013 02:00:21 +0000 Subject: [PATCH] Some enhancements, simplifications to that vhost script. --- scripts/ans-vhost-freemem | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/scripts/ans-vhost-freemem b/scripts/ans-vhost-freemem index 7fd0147172..b350f27df8 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