Created
April 7, 2016 01:33
-
-
Save scollier/890159751cf04cee67c815d29284dc2b to your computer and use it in GitHub Desktop.
RHEV-M VM Report
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Set the variables for date, and argument | |
DATE=$(date +"%m_%d_%Y-%M") | |
INPUT="$1" | |
# Grab the password for RHEV-M, don't report it to std out. | |
echo | |
echo "Please provide the RHEVM password, password is not echoed out to stdout, enter password and press Enter." | |
read -p "Enter Password:" -s RHEVM_PASSWORD | |
echo | |
# Grab the xml report of all the VMs | |
curl -s -X GET -H "Accept: application/xml" -u "admin@internal:$RHEVM_PASSWORD" --cacert rhevm.cer https://your.domain.goes.here.com/api/vms > vm-output-$DATE.xml | |
# Parse the xml output and look for the name of the VM, and the stop time of the VM, put it in a separate file. | |
xpath vm-output-$DATE.xml '/vms/vm/name | /vms/vm/stop_time' > vm-output-$DATE-formatted.xml 2> /dev/null | |
# Clean up the file here. joherr helped out with this. Place line breaks after each </stop_time> xml tag, and format it so it's readable in two columns. | |
sed -e 's/<\/name><stop_time>/ /g' \ | |
-e 's/<\/stop_time><name>/\n/g' \ | |
-e 's/<name>//g' \ | |
-e 's/<\/stop_time>//g' vm-output-$DATE-formatted.xml | \ | |
sort -k 2 | \ | |
awk 'BEGIN { format = "%-60s %s\n" | |
printf format, "VMs", "Date Stopped" | |
printf format, "----------", "----------" } | |
{ printf format, $1, $2 }' > rhevm-vms-$DATE | |
# By default, output the number of VMs that are listed. | |
echo | |
echo "There are $(cat rhevm-vms-$DATE | wc -l) VMs now." | |
echo | |
# If it's run with a -p, ouput the entire list and sort by oldest first. | |
case $INPUT in | |
-p|--print) | |
cat rhevm-vms-$DATE | |
shift # past argument | |
;; | |
esac | |
shift # past argument or value |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment