flant/loghouse

Settings for log rotation (max_days, max_size)

z9r5 opened this issue · 4 comments

z9r5 commented

Old logs should be cleaned from time to time. 3 options will be introduced for that purpose:

  • max_days N — keep records not older than N days;
  • max_size M — store not more than M GBs of records;
  • max_days N + max_size M — if both options are in use, both limits (in days and GBs) should be always applied.
z9r5 commented

You can use the bash script below as a temporal solution.
It takes an oldest table and delete it if the value of TARGET_PERCENT more than current percent of use /var/lib/clickhouse directory.

#!/bin/bash 

TARGET_PERCENT=95
POD=$(/usr/local/bin/kubectl get po -n loghouse |awk '/clickhouse-server/{ print $1}')
CURRENT_PERCENT=$(/usr/local/bin/kubectl -n loghouse exec -ti $POD -- /bin/df -h |awk '/\/var\/lib\/clickhouse/{print $5}')
CURRENT_PERCENT=${CURRENT_PERCENT//%/}
if [ "$CURRENT_PERCENT" -gt "$TARGET_PERCENT" ]; then
  CL_PASS=$(/usr/local/bin/kubectl -n loghouse get po $POD -o yaml |grep CLICKHOUSE_PASS -A1 | awk '/value/{print $2}')
  echo "CURRENT_PERCENT $CURRENT_PERCENT -gt TARGET_PERCENT $TARGET_PERCENT"
  TBL=$(/usr/local/bin/kubectl -n loghouse exec -ti $POD -- clickhouse-client -h 127.0.0.1 -d logs --password $CL_PASS -q "show tables" |grep -E "logs[0-9]{10}" |sort |head -1)
  echo "drop table $TBL"
  /usr/local/bin/kubectl -n loghouse exec -ti $POD -- clickhouse-client -h 127.0.0.1 -d logs --password $CL_PASS -q "drop table $TBL"
fi

You can also use it in crontab.
Example:

*/15 * * * * root /opt/scripts/loghouse.cleaner.sh >> /var/log/loghouse.cleaner.log 2>&1

@zimmermann-russia you are right, but this patch makes you script better:

--- loghouse.cleaner.sh 2018-10-12 18:21:34.046754060 +0300
+++ loghouse.cleaner_new.sh     2018-10-12 18:22:27.719205599 +0300
@@ -4,10 +4,12 @@
 POD=$(/usr/local/bin/kubectl get po -n loghouse |awk '/clickhouse-server/{ print $1}')
 CURRENT_PERCENT=$(/usr/local/bin/kubectl -n loghouse exec -ti $POD -- /bin/df -h |awk '/\/var\/lib\/clickhouse/{print $5}')
 CURRENT_PERCENT=${CURRENT_PERCENT//%/}
-if [ "$CURRENT_PERCENT" -gt "$TARGET_PERCENT" ]; then
+while [ "$CURRENT_PERCENT" -gt "$TARGET_PERCENT" ]; do
   CL_PASS=$(/usr/local/bin/kubectl -n loghouse get po $POD -o yaml |grep CLICKHOUSE_PASS -A1 | awk '/value/{print $2}')
   echo "CURRENT_PERCENT $CURRENT_PERCENT -gt TARGET_PERCENT $TARGET_PERCENT"
   TBL=$(/usr/local/bin/kubectl -n loghouse exec -ti $POD -- clickhouse-client -h 127.0.0.1 -d logs --password $CL_PASS -q "show tables" |grep -E "logs[0-9]{10}" |sort |head -1)
   echo "drop table $TBL"
   /usr/local/bin/kubectl -n loghouse exec -ti $POD -- clickhouse-client -h 127.0.0.1 -d logs --password $CL_PASS -q "drop table $TBL"
-fi
+  CURRENT_PERCENT=$(/usr/local/bin/kubectl -n loghouse exec -ti $POD -- /bin/df -h |awk '/\/var\/lib\/clickhouse/{print $5}')
+  CURRENT_PERCENT=${CURRENT_PERCENT//%/}
+done
z9r5 commented

@broken-ufa Thanks!

z9r5 commented

Closed because of loghouse is no longer being actively developed.