Skip to content

Commit

Permalink
add s3-bucket check
Browse files Browse the repository at this point in the history
  • Loading branch information
dmaes committed Sep 18, 2020
1 parent 4e3bbae commit 812a944
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 1 deletion.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,11 @@ An example Vagrant project has been included to get you started right away.
<td><a href="https://github.com/dmaes">dmaes</a></td>
<td></td>
</tr>
<tr>
<td>check_s3-bucket</td>
<td><a href="https://github.com/dmaes">dmaes</a></td>
<td></td>
</tr>
</table>

### Contributions
Expand Down
3 changes: 2 additions & 1 deletion build.txt
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,6 @@ check_vault.sh 1.2
check_consul.py 1.0
check_ftp.pl 1.0
check_sftp.sh 1.0
check_mysql-replication-configured 0.2
check_mysql-replication-configured 0.2
check_s3-bucket 0.1
# vim: set ts=2 sw=2 et : #
96 changes: 96 additions & 0 deletions check_s3-bucket
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#!/bin/sh
# vi: set shiftwidth=2 tabstop=2 :

STATUS_OK=0
STATUS_WARN=1
STATUS_CRIT=2
STATUS_UNK=3

parsecmd() {
while (($# > 0)); do
#CMDSTART
case "$1" in
-b|--bucket) # BUCKET
# bucket to connect to (required)
BUCKET=$2
shift;;
-e|--endpoint) # ENDPOINT
# endpoint to connect to (required)
ENDPOINT=$(echo $2 | sed -e 's/^.*:\/\///g')
shift;;
-f|--test-file) # TESTFILE
# file to use when testing (default: .testfile)
TESTFILE=$2
shift;;
-k|--s3-key) # S3_KEY
# S3 key to connect (required)
S3_KEY=$2
shift;;
-s|--s3-secret) # S3_SECRET
# S3 secret to create signature (required)
S3_SECRET=$2
shift;;
-o|--curl-opts) # CURL_OPTS
# Optional arguments to pass to curl (default: '')
CURL_OPTS=$2
shift;;
-h|--help) #
# this help
showhelp
exit 0
;;
*)
echo "UNK: Unknown parameter $1"
showhelp
exit $STATUS_UNK
;;
esac
#CMDEND
shift
done
}

showhelp() {
echo "Usage: $0 [OPTION] ..."
echo
echo "Command line arguments:"
echo
sed -rn '/CMDSTART/,/CMDEND/{/\) \#|^ +# /{s/\)? #//g;s/^ //;p}}' "$0"
echo
}

parsecmd "$@"

# Defaults
[ -z "$BUCKET" ] && echo "No bucket provided" && exit $STATUS_UNK
[ -z "$ENDPOINT" ] && echo "No endpoint provided" && exit $STATUS_UNK
[ -z "$S3_KEY" ] && echo "No S3 key provided" && exit $STATUS_UNK
[ -z "$S3_SECRET" ] && echo "No S3 secret provided" && exit $STATUS_UNK
[ -z "$TESTFILE" ] && TESTFILE=.testfile

resource="/$BUCKET/$TESTFILE"
content_type="text/html"
date="`date +'%a, %d %b %Y %H:%M:%S %z'`"
string_to_sign="GET\n\n${content_type}\n${date}\n${resource}"
signature=`/bin/echo -en "$string_to_sign" | openssl sha1 -hmac $S3_SECRET -binary | base64`

curl_output=$(mktemp "/tmp/${0##*/}.curl_output.XXXX") || exit $STATE_UNK
curl_stderr=$(mktemp "/tmp/${0##*/}.curl_stderr.XXXX") || exit $STATE_UNK
trap "rm -rf $curl_output >/dev/null 2>&1; rm -rf $curl_stderr >/dev/null 2>&1" EXIT

rc=$(curl --output $curl_output --write-out "%{http_code}" -H "Date:$date" -H "Content-Type:$content_type" -H "Authorization: AWS $S3_KEY:$signature" https://$BUCKET.$ENDPOINT/$TESTFILE $CURL_OPTS 2>$curl_stderr)

if [ $? -gt 0 ]; then
echo "UNK: $(cat $curl_output) $(cat $curl_stderr | sed -e 's/^.*curl: /curl: /g' | grep -E '^curl: ')"
exit $STATUS_UNK
elif [ $rc -eq 200 ]; then
echo "OK: HTTP STATUS 200 - Testfile '$TESTFILE' found"
exit $STATUS_OK
else
case "$(cat $curl_output)" in
*NoLoggingStatusForKey*)
echo "WARN: NoLoggingStatusForKey - Testfile '$TESTFILE' not found"; exit $STATUS_WARN;;
*)
echo "CRIT: $(cat $curl_output)"; exit $STATUS_CRIT;;
esac
fi

0 comments on commit 812a944

Please sign in to comment.