#!/usr/bin/bash
#
# A collector to prepare virsh info in XML format
#
# Copyright (c) 2020-2021 Virtuozzo International GmbH. All rights reserved.
#
# This file is part of vzreport. It's free software; you can redistribute
# it and/or modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the License,
# or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
#
# Our contact details: Virtuozzo International GmbH, Vordergasse 59, 8200
# Schaffhausen, Switzerland.
#

REPORT_DIR=$1
if [ -z "$REPORT_DIR" ]; then
    echo "Usage: $0 <REPORT_DIR>"
    exit 0
fi

virsh sysinfo > "$REPORT_DIR/Sysinfo.xml"

echo '<networks>' > "$REPORT_DIR/NetConfig.xml"
virsh net-list | grep -v 'Name\|---' | sed 's/^[[:space:]]*//' | cut -f1 -d\  | grep -v '^[[:space:]]*$' | while read net; do
    virsh net-dumpxml $net >>  "$REPORT_DIR/NetConfig.xml"
done
echo '</networks>' >> "$REPORT_DIR/NetConfig.xml"

echo '<nwfilters>' > "$REPORT_DIR/NwFilters.xml"
virsh nwfilter-list | grep -v 'Name\|---' | sed 's/^[[:space:]]*//' | cut -f1 -d\  | grep -v '^[[:space:]]*$' | while read net; do
    virsh nwfilter-dumpxml $net >>  "$REPORT_DIR/NwFilters.xml"
done
echo '</nwfilters>' >> "$REPORT_DIR/NwFilters.xml"

echo '<ifaces>' > "$REPORT_DIR/LibvirtIfaces.xml"
virsh iface-list | grep -v 'Name\|---' | sed 's/^[[:space:]]*//' | cut -f1 -d\  | grep -v '^[[:space:]]*$' | while read net; do
    virsh iface-dumpxml $net >>  "$REPORT_DIR/LibvirtIfaces.xml"
done
echo '</ifaces>' >> "$REPORT_DIR/LibvirtIfaces.xml"

echo '<pools>' > "$REPORT_DIR/LibvirtPools.xml"
virsh pool-list | grep -v 'Name\|---' | sed 's/^[[:space:]]*//' | cut -f1 -d\  | grep -v '^[[:space:]]*$' | while read net; do
    virsh pool-dumpxml $net >>  "$REPORT_DIR/LibvirtPools.xml"

    echo "<pool_vols pool='$net'>" >> "$REPORT_DIR/LibvirtPools.xml"
    virsh vol-list $net | grep -v 'Name\|---' | sed 's/^[[:space:]]*//' | cut -f1 -d\  | grep -v '^[[:space:]]*$' | while read vol; do
        virsh vol-dumpxml --pool $net $vol >> "$REPORT_DIR/LibvirtPools.xml"
    done
    echo '</pool_vols>' >> "$REPORT_DIR/LibvirtPools.xml"
done
echo '</pools>' >> "$REPORT_DIR/LibvirtPools.xml"

echo '<secrets>' > "$REPORT_DIR/LibvirtSecrets.xml"
virsh secret-list | grep -v 'UUID\|---' | sed 's/^[[:space:]]*//' | cut -f1 -d\  | grep -v '^[[:space:]]*$' | while read net; do
    virsh secret-dumpxml $net >>  "$REPORT_DIR/LibvirtSecrets.xml"
done
echo '</secrets>' >> "$REPORT_DIR/LibvirtSecrets.xml"

echo '<nodedevs>' > "$REPORT_DIR/LibvirtNodedev.xml"
virsh nodedev-list | grep -v '^[[:space:]]*$' | while read net; do
    virsh nodedev-dumpxml $net >>  "$REPORT_DIR/LibvirtNodedev.xml"
done
echo '</nodedevs>' >> "$REPORT_DIR/LibvirtNodedev.xml"

echo '<domains>' > "$REPORT_DIR/LibvirtDomains.xml"
virsh list --all | grep -v 'Name\|---' | awk '{ print $2; }'  | grep -v '^[[:space:]]*$' | while read net; do
    virsh dumpxml $net >>  "$REPORT_DIR/LibvirtDomains.xml"
done
virsh -c vzct:///system list --all | grep -v 'Name\|---' | awk '{ print $2; }'  | grep -v '^[[:space:]]*$' | while read net; do
    virsh -c vzct:///system dumpxml $net >>  "$REPORT_DIR/LibvirtDomains.xml"
done
echo '</domains>' >> "$REPORT_DIR/LibvirtDomains.xml"


virsh capabilities > "$REPORT_DIR/capabilities.xml"
virsh nodeinfo > "$REPORT_DIR/nodeinfo.txt"
virsh nodecpustats > "$REPORT_DIR/nodecpustats.txt"
virsh nodememstats > "$REPORT_DIR/nodememstats.txt"
