KingbaseES R3 cluster log cutting and cleaning case

Posted by _off_axis_ on Thu, 10 Mar 2022 03:53:49 +0100

Case description:
For the cluster log of KingbaseES R3 cluster, the default system does not cut and clean up. With the increase of running time, the log will grow into a very large file, occupy a large disk space, and the efficiency is very low when analyzing problems and reading large files. Therefore, this case uses the Logrotate tool of Linux system to cut the cluster log, Cut the cluster log into small files as needed, and keep and clean it as needed. The crond process will automatically clean up the logs as needed according to the cleaned configuration file.

As shown in the figure below:

Logrotate Description:
logrotate is a log cutting tool that comes with Linux system. It can cut and compress log files by month, week and day, which solves the problem of log backup and readability of backup logs. It runs based on crontab and is automatically triggered by the system.

logrotate profile related:

  1)cron Run script/etc/cron.daily/logrotate

#!/bin/sh
/usr/sbin/logrotate /etc/logrotate.conf
EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
    /usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
fi
exit 0


2)configuration file/etc/logrotate.d/ lower
-rw-r--r--. 1 root root 178 Nov 24  2015 chrony
-rw-r--r--. 1 root root  71 Jul 21  2015 cups
-rw-r--r--. 1 root root 172 Nov 20  2015 iscsiuiolog
-rw-r--r--. 1 root root 165 Nov 21  2015 libvirtd
-rw-r--r--. 1 root root 163 Nov 21  2015 libvirtd.qemu
-rw-r--r--. 1 root root 893 Nov 21  2015 mariadb
-rw-r--r--. 1 root root 106 Mar  6  2015 numad
-rw-r--r--. 1 root root 136 Jun 10  2014 ppp
-rw-r--r--. 1 root root 408 Mar  6  2015 psacct
-rw-r--r--. 1 root root 115 Nov 21  2015 samba
-rw-r--r--. 1 root root 224 Sep  8  2015 syslog
-rw-r--r--. 1 root root 100 Jun 16  2015 wpa_supplicant
-rw-r--r--. 1 root root 100 Dec  3  2015 yum


3)Detailed explanation of configuration file parameters
compress: Compress the dumped log file;
nocompress: Do not compress the dumped log files;
delaycompress: And compress Used together, the dumped log files are compressed to the next dump;
nodelaycompress: And compress Used together, the dumped log files are compressed in this dump;

copytruncate: Back up the log file being opened, back up the file before the current node, and then empty the source file;
nocopytruncate: Directly back up the whole log file without cutting;
create mode owner group: Creates a new file using the specified properties;
nocreate: Do not create a new log file;

ifempty: Dump if the file is empty;
notifempty: If it is an empty file, it will not be dumped;
missingok: If the log file is lost, continue with the next log file dump without an error;

errors address: Send the error information during dump to the specified Email address;
mail address: Send the dumped log file to the specified E-mail address;
nomail: No email during dump;

olddir directory: The dumped log file is put into the specified directory and must be in the same file system as the current log file;
noolddir: The dumped log file and the current log file are placed in the same directory
prerotate/endscript: The commands to be executed before dumping can be put into this pair, and the two keywords must be on separate lines;
postrotate/endscript: The commands to be executed after dumping can be put into this pair, and the two keywords must be on separate lines;

daily: Specifies that the dump cycle is daily;
weekly: Specifies that the dump cycle is weekly;
monthly: Specifies that the dump cycle is monthly;

dateext: The current date is used as the naming format of the dump file. There is no such configuration. The dump file is named as 1, 3 and 4....Format;
dateformat: coordination dateext Use to define the format of dump file. Only supports%Y%m%d%s Four parameters;

rotate count: Specify the number of dumps before deleting the log file. 0 means no backup and 5 means 5 backups are reserved;
size size: The log file is dumped when it reaches the specified size, Size Can specify bytes as well as KB perhaps MB;

1, kingbaseES cluster log cutting and cleaning configuration file

1. View profile information

# Configuration file storage path
[root@node3 logrotate.d]# pwd
/etc/logrotate.d

# configuration file
[root@node3 logrotate.d]# ls -lh kingbase 
-rw-r--r-- 1 root root 727 Mar  2 12:27 kingbase

2. View profile content

=This host is a test environment. Kingbases v8r6 cluster is also deployed on this host. After deployment, kingbases v8r6 cluster pair hamgr Log and kbha Log is cut and cleaned, so hamgr. Is configured in the kingbase file Log and kbha Configuration information of log cutting and cleaning. Therefore, for the cluster of kingbaseES V8R3 Log configuration, just add it to the end of this file. For the production environment, only the kingbase es v8r3 cluster is deployed, so you only need to create the kingbase file, edit it, and put the cluster Log configuration can be written to the file=

[root@node3 logrotate.d]# cat kingbase 
# Generate by sys_monitor.sh at Mon Mar  1 17:26:29 CST 2021
/home/kingbase/cluster/R6HA/KHA/kingbase/bin/../kbha.log {
        weekly
        maxsize 100M
        su kingbase kingbase
        create 0600 kingbase kingbase
        rotate 3
        copytruncate
        dateext
}
/home/kingbase/cluster/R6HA/KHA/kingbase/hamgr.log {
        weekly
        maxsize 100M
        su kingbase kingbase
        create 0600 kingbase kingbase
        rotate 3
        copytruncate
        dateext
}

/home/kingbase/cluster/kha/log/cluster.log {
        daily
        maxsize 2M
        #su kingbase kingbase
        #create 0600 kingbase kingbase
        create 0600  root root
        rotate 3
        copytruncate
        dateext
}

3,cluster.log cleanup configuration file

[root@node3 logrotate.d]# cat kingbase 
# Generate by sys_monitor.sh at Mon Mar  1 17:26:29 CST 2021

/home/kingbase/cluster/kha/log/cluster.log {
        daily                   # Daily cleaning
        maxsize 2M              # The log file size is 2M
        #su kingbase kingbase
        #create 0600 kingbase kingbase    
        create 0600  root root   # The file belongs to the primary root
        rotate 3                # Keep only the last three files
        copytruncate
        dateext
}

2, Test cluster Log cleaning

1. View cluster Log information

[kingbase@node3 log]$ ls -lh cluster*
total 8.7M
-rw-r--r-- 1 root     root     3.2M Mar  2 12:27 cluster.log
-rw-r--r-- 1 root     root     266K Mar  2 12:33 cluster_restart.log
-rw-r--r-- 1 root     root      110 Mar  1 13:58 clusterstop

.......

2. Test logrotate manual cutting and cleaning
[root@node3 logrotate.d]# logrotate /etc/logrotate.d/kingbase

3. View the cut cluster Log information

=As shown below, 'cluster Log-20210302 ', current cluster The contents of the log file are cleared=

[kingbase@node3 log]$ ls -lh cluster*
-rw-r--r-- 1 root root  17K Mar  2 12:35 cluster.log
-rw-r--r-- 1 root root 3.2M Mar  2 12:27 cluster.log-20210302         #History file after cutting
-rw-r--r-- 1 root root 266K Mar  2 12:34 cluster_restart.log
-rw-r--r-- 1 root root  110 Mar  1 13:58 clusterstop

3, Summary

For log cleaning under Linux system, you can use logrotate tool. Automatic cleanup through the crond process. Users only need to configure the configuration file on demand and leave everything to the system for management.

Topics: PostgreSQL