#!/bin/bash # # Script written by lej 2020-12-09 Usage() { echo "Usage $0 -H -U -P [-E exclude]" exit 3 } while getopts "hH:U:P:E:" arg; do case $arg in h) Usage ;; H) hostname=$OPTARG #echo $hostname ;; U) username=$OPTARG #echo $username ;; P) password=$OPTARG #echo $password ;; E) exclude=$OPTARG #echo $password ;; *) Usage ;; esac done cookie_jar="/var/tmp/${hostname}.$$.cookie-jar.txt" if [ -z "$hostname" ] | [ -z "$username" ] | [ -z "$password" ]; then Usage fi # First we authenticate and put the resulting cookie in our jar. result=$(curl -X POST "https://${hostname}/rest/com/vmware/cis/session" --insecure --user "${username}:${password}" -k -i -c $cookie_jar -s) # The result code is on the first line of output firstline=$(echo "$result" | head -1) # If authentication suceeded, we get the cluster list. That includes if each cluster has HA enabled. if [[ "$firstline" =~ "200 OK" || "$firstline" =~ "HTTP/2 200" ]]; then # We cut up the JSON into one line for each cluster clusters="$(curl -X GET "https://${hostname}/rest/vcenter/cluster" -b $cookie_jar --insecure -s | \ sed -e 's/^.*\[{//' -e 's/}\].*$//' -e 's/},{/\n/g' |cut -f 3,4 -d ',')" else echo "$result" fi # Delete the cookie jar when we're done with it # If we failed authentication, no jar is created. Therefore we use the -f flag rm -f $cookie_jar # Did we find any clusters at all? allclusters=$(echo "$clusters" |grep '"ha_enabled"') if [ ! -z "$exclude" ]; then excludedclusters=$(echo "$allclusters" | grep "$exclude") allclusters=$(echo "$allclusters" | grep -v "$exclude") fi if [ -z "$allclusters" ]; then echo "We didn't find any clusters" exitstatus=3 else # Does any of the clusters have HA disabled falseclusters=$(echo "$allclusters" |grep '"ha_enabled":false') if [ -z "$falseclusters" ]; then echo "All clusters have HA enabled" exitstatus=0 else echo "At least one cluster doesn't have HA enabled" exitstatus=2 fi fi echo "$allclusters" if [ ! -z "$exclude" ]; then echo "These clusters were excluded" echo "$excludedclusters" fi exit $exitstatus