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_SERVICE_PORT"
21 echo "LOGROTATE: $*" >/proc/1/fd/1
24 log "hourly job started"
26 # sleep to avoid concurrent runs across multiple ES Pods
27 let "SLEEP=($num * 600)+($RANDOM % 30)"
28 log "sleeping $SLEEP seconds..."
31 TODAY=`date -u +%Y.%m.%d`
33 while [ $pcent -ge $FS_LIMIT ]
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
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
47 log "reclaim deleted space"
48 curl -sS -XPOST "$ES_URL/*-$TODAY/_forcemerge?only_expunge_deletes=true" >/dev/null
52 curl -sS -XPOST "$ES_URL/*-$TODAY/_flush" >/dev/null