From 49810e77fdd386b57618d228b996dcc5c406d2c2 Mon Sep 17 00:00:00 2001 From: David Wahlstrom Date: Mon, 28 Sep 2015 09:46:45 -0700 Subject: [PATCH] orphaned_vms.sh: initial commit orphaned_vms.sh is a script that searches through the current VMs (as reported by nova) and retrieves their own. Then, it cross checks that list against a tenant listing (as reported by keystone). Any VMs whose owner does not exist in keystone is assumed to be orphaned. This can happen if a tenant is deleted while it still has VMs online. Change-Id: I880b66e6d303e3348ac1d7fde1762633ae9ac07a --- nova/orphaned_vms.sh | 47 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100755 nova/orphaned_vms.sh diff --git a/nova/orphaned_vms.sh b/nova/orphaned_vms.sh new file mode 100755 index 0000000..a1d2144 --- /dev/null +++ b/nova/orphaned_vms.sh @@ -0,0 +1,47 @@ +#!/bin/bash + +# OpenStack credentials are expected to be in your environment variables +if [ -z "$OS_AUTH_URL" -o -z "$OS_PASSWORD" -o -z "$OS_USERNAME" ]; then + echo "Please set OpenStack auth environment variables." + exit 1 +fi + +# temp files used for caching outputs +vm_tenants=$(mktemp) +keystone_tenants=$(mktemp) + +# get a list of all VMs in the cluster and who they belong to +echo -en "Retrieving list of all VMs...\r" +nova list --all-tenants --fields tenant_id | tail -n +4 | awk '{print $4}' | sort -u > $vm_tenants +total_vms=$(cat $vm_tenants | wc -l) +if [ $total_vms == 0 ]; then + echo "Zero VMs found. Exiting..." + rm -f $vm_tenants $keystone_tenants + exit 1 +fi + +# get a list of all tenants/projects in the cluster +echo -en "Retrieving list of all tenants...\r" +keystone tenant-list | tail -n +4 | awk '{print $2}' | sort -u > $keystone_tenants +total_tenants=$(cat $keystone_tenants | wc -l) +if [ $total_tenants == 0 ]; then + echo "Zero tenants found. Exiting..." + rm -f $vm_tenants $keystone_tenants + exit 1 +fi + +# compare all VM owners to all tenants as reported by keystone and print +# any VMs whose owner does not exist in keystone +echo -en "Comparing outputs to locate orphaned VMs....\r" +iter=0 +for tenant_id in `comm --nocheck-order -13 $keystone_tenants $vm_tenants`; do + if [[ $iter == 0 ]]; then + nova list --all-tenants --tenant=$tenant_id --fields tenant_id,name,status,created,updated | head -n -1 + let "iter++" + else + nova list --all-tenants --tenant=$tenant_id --fields tenant_id,name,status,created,updated | tail -n +4 | head -n -1 + fi +done + +# cleanup after ourself +rm $keystone_tenants $vm_tenants