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
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
18 ES_URL="http://localhost:$ELASTICSEARCH_LOGGING_SERVICE_PORT"
21 echo "LOGROTATE: $*" >/proc/1/fd/1
24 log "hourly job started"
26 IFS='-' read es type num <<< "$HOSTNAME"
27 if [[ "$type" != "data" ]]; then
28 log "non-data node -> exiting"
32 # sleep to avoid concurrent runs across multiple ES data Pods
33 let "SLEEP=($num * 600)+($RANDOM % 30)"
34 log "sleeping $SLEEP seconds..."
37 TODAY=`date -u +%Y.%m.%d`
39 while [ $pcent -ge $FS_LIMIT ]
41 pcent=`df --output=pcent /usr/share/elasticsearch/data | tail -n1 | tr -d '%'`
42 log "current filesystem usage: $pcent%"
43 if [ $pcent -le $FS_LIMIT ]; then break; fi
44 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`
45 if [ -n "$index_drop" ]; then
46 log "drop index: $index_drop"
47 curl -sS -XDELETE "$ES_URL/$index_drop" >/dev/null
50 log "drop oldest $DOCS_DROP log entries"
51 curl -sS -XPOST "$ES_URL/*-$TODAY/_delete_by_query?sort=@timestamp:asc&max_docs=$DOCS_DROP&refresh=true&q=*" >/dev/null
53 log "reclaim deleted space"
54 curl -sS -XPOST "$ES_URL/*-$TODAY/_forcemerge?only_expunge_deletes=true" >/dev/null
58 curl -sS -XPOST "$ES_URL/*-$TODAY/_flush" >/dev/null