Simplify ElasticSearch to make it more robust
[ta/caas-logging.git] / docker-build / elasticsearch / elasticsearch-logrotate.sh
1 #!/bin/bash
2 # Copyright 2019 Nokia
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 FS_LIMIT=80
17 DOCS_DROP=100000
18 ES_URL="http://localhost:$ELASTICSEARCH_SERVICE_PORT"
19
20 log () {
21     echo "LOGROTATE: $*" >/proc/1/fd/1
22 }
23
24 log "hourly job started"
25
26 # sleep to avoid concurrent runs across multiple ES Pods
27 let "SLEEP=($num * 600)+($RANDOM % 30)"
28 log "sleeping $SLEEP seconds..."
29 sleep $SLEEP
30
31 TODAY=`date -u +%Y.%m.%d`
32 declare -i pcent=100
33 while [ $pcent -ge $FS_LIMIT ]
34 do
35     pcent=`df --output=pcent /usr/share/elasticsearch/data | tail -n1 | tr -d '%'`
36     log "current filesystem usage: $pcent%"
37     if [ $pcent -le $FS_LIMIT ]; then break; fi
38     index_drop=`curl -sS -XGET "$ES_URL/_cat/indices?h=index" | egrep '^.+-[[:digit:]]{4}\.[[:digit:]]{2}\.[[:digit:]]{2}$' | grep -v "$TODAY" | sort -t'-' -k2 | head -n1`
39     if [ -n "$index_drop" ]; then
40         log "drop index: $index_drop"
41         curl -sS -XDELETE "$ES_URL/$index_drop" >/dev/null
42         sleep 5
43     else
44         log "drop oldest $DOCS_DROP log entries"
45         curl -sS -XPOST "$ES_URL/*-$TODAY/_delete_by_query?sort=@timestamp:asc&max_docs=$DOCS_DROP&refresh=true&q=*" >/dev/null
46         sleep 5
47         log "reclaim deleted space"
48         curl -sS -XPOST "$ES_URL/*-$TODAY/_forcemerge?only_expunge_deletes=true" >/dev/null
49         sleep 30
50     fi
51     log "flush"
52     curl -sS -XPOST "$ES_URL/*-$TODAY/_flush" >/dev/null
53     sleep 5
54 done
55
56 log "job exited"