#!/bin/bash
# Query VMDB for MIQ Server hostnames and associated zones and dynamically build the content of miqhosts.
# Results printed to STDOUT.  Redirect to miqhosts to save.

echo 1>&2
echo "Redirect standard output of this command to your miqhosts file if desired." 1>&2
echo "Enter VMDB database password if prompted." 1>&2
echo 1>&2
echo "# miqhosts content generated by miqhosts-gen.  See miqhosts-gen to regenerate this file by querying the VMDB if desired."
echo "#"
echo "# hostname_or_ip  <white space> groups to assign host to separated by commas."
echo "#"
echo "# Lines starting with a # are ignored."
echo "#"


cd /var/www/miq/vmdb

#echo 'select miq_servers.hostname, zones.name from miq_servers join zones on zones.id = miq_servers.zone_id;' | rails db | awk '$1!="hostname" && !/^---/ && !/^\(/ {print}' | sed -e 's/ //g' -e 's/|/ /' -e '/^$/d'
psql -d vmdb_production -c 'select miq_servers.hostname, zones.name from miq_servers join zones on zones.id = miq_servers.zone_id;' | awk '$1!="hostname" && !/^---/ && !/^\(/ {print}' | sed -e 's/ //g' -e 's/|/ /' -e '/^$/d'

cd - > /dev/null
