Here it is:

#!/bin/bash

# VERSION

XEN_VERSION=70


#Automatically Update script for Xen Servers.
#Start by downloading the xml file
#bash xeer1
#echo "Downloading updates.xml"
curl -s -# -L -R -o updates.xml http://updates.xensource.com/XenServer/updates.xml

#Grep the patches for version number XS62E, parse the data, and form the table
#Columns are 1-patch name 2 - url 3- timestamp and 4- uuid
#Each column is one variable and sorted by date and then by name

xmllint --shell updates.xml <<< 'cat //patch[contains(@name-label,"XS${XEN_VERSION}")]/@name-label'|grep -v ">\|<\|\ -"|cut -d"\"" -f2|while read line
do
lineUrl=$(xmllint --shell updates.xml <<< 'cat //patch[contains(concat(" ", normalize-space(@name-label), " ")," '$line' ")]/@patch-url'|grep "patch-url"|cut -d"\"" -f2)
lineTime=$(xmllint --shell updates.xml <<< 'cat //patch[contains(concat(" ", normalize-space(@name-label), " ")," '$line' ")]/@timestamp'|grep "timestamp"|cut -d"\"" -f2)
lineId=$(xmllint --shell updates.xml <<< 'cat //patch[contains(concat(" ", normalize-space(@name-label), " ")," '$line' ")]/@uuid'|grep "uuid"|cut -d"\"" -f2)
echo "$line $lineUrl $lineTime $lineId"
done > patchList


cat patchList|sort -k3,3 -k4|(i=0;while read line1 line2 line3 line4
do
#Save some typing
#export ShortUrl=`basename $line2`
# Check to see if the patch has been installed already
if [[ ! -n $(xe patch-list name-label=$line1 2> /dev/null) ]]
then
pathToApply[$((i++))]=$line1 
fi
done

#Output to NAGIOS
updates_id=''
for i in ${pathToApply[@]}; do updates_id="$updates_id $i"; done

total_updates=${#pathToApply[@]}

if [ $total_updates -gt 0 ]
then
echo "CRITICAL - You have $total_updates new updates to apply ($updates_id )"
exit 2
else
echo "OK - No new updates"
exit 0
fi
)

# Remove XML file
rm -rf updates.xml
Advertisements