Skip to content

Instantly share code, notes, and snippets.

@ninapavlich
Last active January 24, 2018 20:23
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 ninapavlich/5c7812ad90107d9e6c92941f794c1449 to your computer and use it in GitHub Desktop.
Save ninapavlich/5c7812ad90107d9e6c92941f794c1449 to your computer and use it in GitHub Desktop.
Bash script for sending a test result record to a password-protected elasticsearch endpoint
#!/bin/sh
# Example Usage:
# test.sh 'cluster-id' 'node-id' 'my-fancy-app' 'tag-a,tag-b' 'cluster-id-node-id-my-fancy-app-unit-test-a' 'false' 'Unit Test A is failing because of X'
# test.sh 'cluster-id' 'node-id' 'my-fancy-app' 'tag-a,tag-b' 'cluster-id-node-id-my-fancy-app-unit-test-a' 'true' 'Unit Test A is passing'
# Example Usage - same service but different UUID tests:
# test.sh 'cluster-id' 'node-id' 'nginx' 'tag-a,tag-b' 'cluster-id-node-id-nginx-https-test' 'false' 'HTTPS not working on NGINX'
# test.sh 'cluster-id' 'node-id' 'nginx' 'tag-a,tag-b' 'cluster-id-node-id-nginx-port-test' 'false' 'NGINX is not running on port 80'
# test.sh 'cluster-id' 'node-id' 'nginx' 'tag-a,tag-b' 'cluster-id-node-id-nginx-memory' 'false' 'NGINX is using too much memory'
# Inputs:
# $1 = cluster
# $2 = node
# $3 = service
# $4 = tags
# $5 = uuid
# $6 = okay
# $7 = message
#Fix for newlines
MESSAGE=$(echo $7)
DATE=`date +%Y.%m.%d`
TIMESTAMP=`date +"%Y-%m-%dT%T.%3N"`
ES_INDEX_URL="https://example.com:9200/test-$DATE"
ES_RECORD_URL="$ES_INDEX_URL/doc/"
STATUS=$(curl -s -o /dev/null -w '%{http_code}' $ES_INDEX_URL 'elastic_username':'elastic_password')
if [ $STATUS -eq 200 ]; then
#Index already exists...
printf "Test index exists"
else
#Create index and mapping if doesnt exist
curl -X PUT \
$ES_INDEX_URL \
-u 'elastic_username':'elastic_password' \
-H 'Cache-Control: no-cache' \
-H 'Content-Type: application/json' \
-d '{
"mappings" : {
"doc": {
"properties": {
"@timestamp": {
"type": "date"
},
"cluster": {
"type": "keyword",
"ignore_above": 256
},
"node": {
"type": "keyword",
"ignore_above": 256
},
"service": {
"type": "keyword",
"ignore_above": 256
},
"data": {
"type": "nested"
},
"tags": {
"type": "keyword",
"ignore_above": 256
},
"uuid": {
"type": "keyword",
"ignore_above": 256
},
"test_okay": {
"type": "boolean"
}
}
}
}
}'
fi
#Second post data to it
/usr/bin/curl \
-X POST \
$ES_RECORD_URL \
-u 'elastic_username':'elastic_password' \
-H 'Cache-Control: no-cache' \
-H 'Content-Type: application/json' \
-d "{
\"@timestamp\": \"$TIMESTAMP\",
\"cluster\": \"$1\",
\"node\": \"$2\",
\"service\":\"$3\",
\"tags\": \"$4\",
\"uuid\": \"$5\",
\"test_okay\": \"$6\",
\"data\":{
\"message\":\"$MESSAGE'\"
}
}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment