|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +SPLUNK_HOST=localhost:8089 |
| 4 | +SPLUNK_USERNAME=admin |
| 5 | +SPLUNK_PASS=welcome1 |
| 6 | + |
| 7 | +AUTH_TOKEN="eyJraWQiOiJzcGx1bmsuc2VjcmV0IiwiYWxnIjoiSFM1MTIiLCJ2ZXIiOiJ2MiIsInR0eXAiOiJzdGF0aWMifQ.eyJpc3MiOiJhZG1pbiBmcm9tIHRtdXRoLW1icC1lNDFlOSIsInN1YiI6ImFkbWluIiwiYXVkIjoiUkVTVCBDYWxscyIsImlkcCI6IlNwbHVuayIsImp0aSI6IjA1MzU4ZjYzZjFjZjVkNzdhMjcwMzg1NzM0NDFmMTZiNTgxZWIxMGY1M2RlNzYyNWE4ZThkMTBhM2Y3YzY2OTMiLCJpYXQiOjE2MzEyODYyNzYsImV4cCI6MTY5NDM1ODI3NiwibmJyIjoxNjMxMjg2Mjc2fQ.RQdjHDkozi59yfn-Dh7BhaUmTuWrIT2y5textF7G3t3FTMk5npCUC2GzORyH4PdiKZUXvNb17UGAELOe6Nvifw" |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +function print_section { |
| 12 | + #printf "\n****** ${1} ************************************************\n" |
| 13 | + printf "\n****** %-.80s \n" "${1} ************************************************************************************************" |
| 14 | + if [[ "$1" = *"_END" ]];then |
| 15 | + printf "\n\n" |
| 16 | + fi |
| 17 | +} |
| 18 | + |
| 19 | +print_section "ONESHOT_BEGIN" |
| 20 | +print_section "ONESHOT_END" |
| 21 | + |
| 22 | + |
| 23 | +# exit 0 |
| 24 | + |
| 25 | +# URL escape codes used to pass special characters. DO NOT CHANGE! |
| 26 | +dqt="%22" |
| 27 | +pct="%25" |
| 28 | + |
| 29 | +function splunk_search_polling { |
| 30 | + SEARCH_LEVEL=${2:-verbose} |
| 31 | + curl_opts_common=( -s -k -d adhoc_search_level=${SEARCH_LEVEL} ) |
| 32 | + |
| 33 | + AUTH_OPTION=() |
| 34 | + if [ ! -z "$AUTH_TOKEN" ]; then |
| 35 | + AUTH_OPTION+=( -H "Authorization: Bearer ${AUTH_TOKEN}" ) |
| 36 | + #echo "Using Token Auth" |
| 37 | + else |
| 38 | + AUTH_OPTION+=( -u ${SPLUNK_USERNAME}:${SPLUNK_PASS} ) |
| 39 | + #echo "Using User:Pass Auth" |
| 40 | + fi |
| 41 | + |
| 42 | + curl_opts=( "${curl_opts_common[@]}" "${AUTH_OPTION[@]}" -d search="${1}" ) |
| 43 | + #echo ${curl_opts[@]} |
| 44 | + |
| 45 | + RESULTS=`curl "${curl_opts[@]}" -X POST https://${SPLUNK_HOST}/services/search/jobs` |
| 46 | + |
| 47 | + SID=`echo $RESULTS | sed -e 's,.*<sid>\([^<]*\)<\/sid>.*,\1,g' ` |
| 48 | + printf "\n" |
| 49 | + echo "SID: $SID" |
| 50 | + |
| 51 | + SEARCH_STATUS="" |
| 52 | + counter=30 # will check for status=DONE this many times, every ${wait_seconds} |
| 53 | + wait_seconds=1 |
| 54 | + while [ $counter -gt 0 ] |
| 55 | + do |
| 56 | + curl_opts=( "${curl_opts_common[@]}" "${AUTH_OPTION[@]}" ) |
| 57 | + OUTPUT=`curl "${curl_opts[@]}" -X POST https://${SPLUNK_HOST}/services/search/jobs/${SID}` |
| 58 | + #echo "$OUTPUT" |
| 59 | + STATUS=`echo $OUTPUT | sed -e 's,.*<s:key name=\"dispatchState\">\([^<]*\)<\/s\:key>.*,\1,g' ` |
| 60 | + #echo "$STATUS" |
| 61 | + if [[ "$STATUS" = "DONE" ]]; then |
| 62 | + SEARCH_STATUS="DONE" |
| 63 | + #echo "Leaving status loop" |
| 64 | + break 1 |
| 65 | + fi |
| 66 | + counter=$(( $counter - 1 )) |
| 67 | + sleep ${wait_seconds} |
| 68 | + done |
| 69 | + |
| 70 | + if [[ "$SEARCH_STATUS" = "DONE" ]]; then |
| 71 | + curl_opts=( "${curl_opts_common[@]}" "${AUTH_OPTION[@]}" -d output_mode=csv ) |
| 72 | + SEARCH_RESULTS=`curl "${curl_opts[@]}" -X GET https://${SPLUNK_HOST}/services/search/jobs/${SID}/results` |
| 73 | + echo "$SEARCH_RESULTS" | sed 's/,/ ,/g' | column -t -s, \ |
| 74 | + | awk 'NR == 1 {print $0;print $0}; NR > 1 {print $0}' \ |
| 75 | + | sed '2 s/[^[:space:]]/-/g' |
| 76 | + fi |
| 77 | +} |
| 78 | + |
| 79 | + |
| 80 | + |
| 81 | +function splunk_search_oneshot { |
| 82 | + #echo ${1} |
| 83 | + printf "\n\n" |
| 84 | + echo "splunk_search_oneshot - adhoc_search_level: ${2}" |
| 85 | + curl -s -k -u ${SPLUNK_USERNAME}:${SPLUNK_PASS} \ |
| 86 | + -X POST https://${SPLUNK_HOST}/services/search/jobs/ \ |
| 87 | + -d search="${1}" \ |
| 88 | + -d exec_mode=oneshot -d output_mode=csv -d adhoc_search_level=${2} |
| 89 | +} |
| 90 | + |
| 91 | + |
| 92 | +SEARCH_STRING="search index=_internal sourcetype=splunkd component=Metrics earliest=-5m | table _time, group, name | head 5 " |
| 93 | + |
| 94 | +#splunk_search_polling "${SEARCH_STRING}" "verbose" |
| 95 | +#splunk_search_polling "${SEARCH_STRING}" "fast" |
| 96 | + |
| 97 | +#splunk_search_oneshot "${SEARCH_STRING}" "verbose" |
| 98 | +#splunk_search_oneshot "${SEARCH_STRING}" "fast" |
| 99 | + |
| 100 | +INDEX=main |
| 101 | +SEARCH_STRING=" | walklex index=${dqt}${INDEX}${dqt} type=field | search NOT field=${dqt} *${dqt} | where NOT LIKE(field,${dqt}date_${pct}${dqt}) | stats sum(distinct_values) by field " |
| 102 | +#SEARCH_STRING=" search | walklex index=$INDEX earliest=1 " |
| 103 | +#splunk_search_polling "${SEARCH_STRING}" "verbose" |
| 104 | + |
| 105 | +splunk_search_polling "search index=main | stats count" |
| 106 | + |
0 commit comments