es_upload.sh script example

Last Updated : Jul 20, 2022 |
Prolog information
Create an es_upload.sh script, such as the following.
Note:
If the targeted Elasticsearch server is protected by a security mechanism, add the credentials or mechanism to this script.
#!/bin/bash
#set -x
function upload_to_es() {
    local _es_server=$1 # URL of Elasticsearch
    local _cust_name=$2 # E.g., example_company.  This will result in one or more indicies of example_companty-{date}
 
    echo "Uploading logs to $_es_server with index pre-fix $_cust_name..."
 
    _index_id=1
    _chunks=$(ls *log | sort)
    for _chunk in ${_chunks[@]}; do
        IFS=
        _es_index="$_cust_name"-$(echo $_chunk | awk -FT '{print $1}')
        echo ""
        echo "Converting ${_chunk} to ${_chunk}_es ..."
        while read -r entry; do
            echo "{\"index\":{\"_index\":\"$_es_index\", \"_id\":\"$_index_id\"}}"; echo $entry
            ((_index_id++))
        done < <(cat $_chunk | jq -c '.') > ${_chunk}_es
 
        echo "Uploading ${_chunk}_es to https://$_es_server/$_es_index ..."
        _rc=$(curl --insecure -s -o /dev/null --show-error --fail \
            -X PUT "https://$_es_server/$_es_index/_bulk?pretty" \
            -H 'Content-Type: application/x-ndjson' \
            --data-binary @${_chunk}_es)
 
        if [[ $_rc -ne 0 ]]; then
            echo "curl failed with ${_rc}...aborting upload"
        break
        fi
    done
}
 
#E.g., es_upload.sh flex188-54.dr.avaya.com/elasticsearch example_company
upload_to_es $1 $2