-
-
Save richcollier/5482702c7bef6de9a14ff29fa39ef21a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
HOST='localhost' | |
PORT=9200 | |
#CURL_AUTH="-u elastic:changeme" | |
JOB_ID="asamplejob" | |
ROOT="http://${HOST}:${PORT}/_xpack/ml" | |
JOBS="${ROOT}/anomaly_detectors" | |
DATAFEEDS="${ROOT}/datafeeds" | |
printf "\n== Script started for... $JOBS/$JOB_ID" | |
printf "\n\n== Stopping datafeed... " | |
curl $CURL_AUTH -s -X POST ${DATAFEEDS}/datafeed-${JOB_ID}/_stop | |
printf "\n\n== Deleting datafeed... " | |
curl $CURL_AUTH -s -X DELETE ${DATAFEEDS}/datafeed-${JOB_ID} | |
printf "\n\n== Closing job... " | |
curl $CURL_AUTH -s -X POST ${JOBS}/${JOB_ID}/_close | |
printf "\n\n== Deleting job... " | |
curl $CURL_AUTH -s -X DELETE ${JOBS}/${JOB_ID} | |
printf "\n\n== Creating job... \n" | |
curl $CURL_AUTH -s -X PUT -H 'Content-Type: application/json' ${JOBS}/${JOB_ID}?pretty -d '{ | |
"description" : "Unusual responsetimes by airlines", | |
"analysis_config" : { | |
"bucket_span": "5m", | |
"detectors" :[{"function":"metric", "field_name":"responsetime","by_field_name":"airline"}], | |
"influencers" : [ "airline" ] | |
}, | |
"data_description": { | |
"time_field": "@timestamp" | |
} | |
}' | |
printf "\n\n== Creating datafeed... \n" | |
curl $CURL_AUTH -s -X PUT -H 'Content-Type: application/json' ${DATAFEEDS}/datafeed-${JOB_ID}?pretty -d '{ | |
"job_id" : "'"$JOB_ID"'", | |
"indexes" : [ | |
"farequote-*" | |
], | |
"types" : [ | |
"responsetime" | |
], | |
"scroll_size" : 1000 | |
}' | |
printf "\n\n== Opening job for ${JOB_ID}... " | |
curl $CURL_AUTH -X POST ${JOBS}/${JOB_ID}/_open | |
printf "\n\n== Starting datafeed-${JOB_ID}... " | |
curl $CURL_AUTH -X POST "${DATAFEEDS}/datafeed-${JOB_ID}/_start?start=1970-01-02T10:00:00Z&end=2018-01-01T00:00:00Z" | |
printf "\n\n== Waiting for job to run ==\n\n" | |
while [ `curl -s -X GET "${JOBS}/${JOB_ID}/_stats?pretty" | grep state | cut -d: -f2 | sed 's/^[ \t]*//;s/[ \t]*$//' | sed 's/"//g'` != "closed" ]; do | |
printf "." | |
sleep 1 | |
done | |
printf "done\n" | |
printf "\n\n== Top Bucket Anomalies (over a score of 90) ==\n\n" | |
curl $CURL_AUTH -X POST "${JOBS}/${JOB_ID}/results/buckets?pretty&anomaly_score=90" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output: