Skip to content

Instantly share code, notes, and snippets.

@nobuto-m
Last active August 29, 2015 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nobuto-m/10474463 to your computer and use it in GitHub Desktop.
Save nobuto-m/10474463 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# Nova create instance monitoring script for Sensu / Nagios
#
# Copyright © 2014 eNovance <licensing@enovance.com>
#
# Author: Florian Lambert <florian.lambert@enovance.com>
#
# This program is 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 3 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, see <http://www.gnu.org/licenses/>.
#
# Requirement: curl
#
# Derived from https://github.com/enovance/openstack-monitoring/blob/660c5a9bc5990390faca394fe0112da3a4bcccb2/scripts/horizon/check_horizon_login.sh
# by Nobuto MURATA <nobuto@ubuntu.com>
#
# Nagios/Sensu return codes
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3
STATE_DEPENDENT=4
# Script options
usage ()
{
echo "Usage: $0 [OPTIONS]"
echo " -h Get help"
echo " -E <Dashboard URL> URL for horizon dashboard. Ex: http://example.com/horizon"
echo " -U <username> Username to use to get an auth token. or env[OS_USERNAME]"
echo " -P <password> Password to use ro get an auth token. or env[OS_PASSWORD]"
echo " -c <cookieFile> Temporaire file to store cookie."
echo " -o <outputFile> File to write openrc.sh. default:./USERNAME-openrc.sh"
}
output_result () {
# Output check result & refresh cache if requested
MSG="$1"
RETCODE=$2
echo "$MSG"
exit $RETCODE
}
while getopts 'hH:U:P:E:c:o:' OPTION
do
case $OPTION in
h)
usage
exit 0
;;
E)
export ENDPOINT_URL=$OPTARG
;;
U)
export OS_USERNAME=$OPTARG
;;
P)
export OS_PASSWORD=$OPTARG
;;
c)
export COOKIE_FILE=$OPTARG
;;
o)
export OUTPUT_FILE=$OPTARG
;;
*)
usage
exit 1
;;
esac
done
if [ -z "$OS_USERNAME" ] || [ -z "$ENDPOINT_URL" ]
then
usage
exit 1
fi
if ! which curl > /dev/null 2>&1
then
output_result "UNKNOWN - curl is not installed." $STATE_UNKNOWN
fi
if [ -z "$OS_PASSWORD" ]
then
echo "Please enter ${OS_USERNAME}'s OpenStack Password: "
read -sr OS_PASSWORD
fi
COOKIE_FILE=${COOKIE_FILE:-`mktemp`}
OUTPUT_FILE=${OUTPUT_FILE:-"./${OS_USERNAME}-openrc.sh"}
# Get CSRFTOKEN and REGION on index
GET_INDEX=`curl -c $COOKIE_FILE -i "$ENDPOINT_URL" 2> /dev/null`
if [ -z "$GET_INDEX" ]
then
output_result "CRITICAL - $ENDPOINT_URL not respond." $STATE_CRITICAL
fi
CSRFTOKEN=$(echo $GET_INDEX | sed -r "s/.*csrfmiddlewaretoken['\"] value=['\"]([^'\"]+)['\"].*/\1/")
REGION=$(echo $GET_INDEX | sed -r "s/.*region['\"]( type=['\"]hidden['\"])* value=['\"]([^\"']+)['\"].*/\2/")
# Send POST login with CSRFTOKEN and REGION on /auth/login/
RESULT=`curl -L -c "$COOKIE_FILE" -b "$COOKIE_FILE" --referer "$ENDPOINT_URL" --data "username=$OS_USERNAME&password=$OS_PASSWORD&region=$REGION&csrfmiddlewaretoken=$CSRFTOKEN" "$ENDPOINT_URL/auth/login/" 2> /dev/null`
# If Auth work, find patterns Overview
if ! [[ $RESULT == *Overview* ]]
then
output_result "CRITICAL - Failed to log into $ENDPOINT_URL" $STATE_CRITICAL
fi
curl -s -L -b "$COOKIE_FILE" --referer "$ENDPOINT_URL" --output "${OUTPUT_FILE}" "$ENDPOINT_URL/project/access_and_security/api_access/openrc/" 2> /dev/null
# If Auth work, find patterns Overview
if grep 'OS_PASSWORD_INPUT' "$OUTPUT_FILE" >/dev/null
then
output_result "OK - Saved openrc.sh as $OUTPUT_FILE" $STATE_OK
else
output_result "CRITICAL - Failed to get openrc.sh" $STATE_CRITICAL
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment