logmngr: add logrotate support

* add support for logrotate functionality to logmngr
This commit is contained in:
Rahul Thakur 2024-07-25 16:27:41 +05:30
parent 0884ecd15e
commit d1f16dc432
7 changed files with 138 additions and 6 deletions

View file

@ -17,4 +17,10 @@ config LOGMNGR_BACKEND_SYSLOG_NG
Enable this option to use syslog-ng for log management.
endchoice
config LOGMNGR_LOGROTATE
bool "Logrotate support"
depends on PACKAGE_logmngr
default y
help
It adds support for logrotate functionality.
endif

View file

@ -3,7 +3,6 @@
#
include $(TOPDIR)/rules.mk
include $(INCLUDE_DIR)/kernel.mk
PKG_NAME:=logmngr
PKG_VERSION:=1.0.1
@ -11,7 +10,7 @@ LOCAL_DEV:=0
ifneq ($(LOCAL_DEV),1)
PKG_SOURCE_PROTO:=git
PKG_SOURCE_URL:=https://dev.iopsys.eu/system/logmngr.git
PKG_SOURCE_VERSION:=d107b8b65dd63709c85a1b907108bb0b6a13572c
PKG_SOURCE_VERSION:=ec10abb3cc0f3b96eb806c9c67e18d9d134287e9
PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.gz
PKG_MIRROR_HASH:=skip
endif
@ -25,9 +24,10 @@ include ../bbfdm/bbfdm.mk
MAKE_PATH:=bbf_plugin
define Package/logmngr
SECTION:=utils
CATEGORY:=Utilities
TITLE:=Logging Manager
DEPENDS:=+libbbfdm-api +LOGMNGR_BACKEND_FLUENTBIT:fluent-bit
DEPENDS:=+libbbfdm-api +LOGMNGR_BACKEND_FLUENTBIT:fluent-bit +LOGMNGR_LOGROTATE:logrotate
DEPENDS+=+LOGMNGR_BACKEND_SYSLOG_NG:syslog-ng
endef
@ -63,7 +63,12 @@ endif
ifeq ($(CONFIG_LOGMNGR_BACKEND_SYSLOG_NG),y)
$(INSTALL_DATA) ./files/lib/logmngr/syslog-ng.sh $(1)/lib/logmngr/.
endif
$(BBFDM_INSTALL_MS_DM) $(PKG_BUILD_DIR)/bbf_plugin/libsyslog.so $(1) $(PKG_NAME)
$(BBFDM_INSTALL_CORE_PLUGIN) $(PKG_BUILD_DIR)/bbf_plugin/libbbfsyslog.so $(1)
ifeq ($(CONFIG_LOGMNGR_LOGROTATE),y)
$(INSTALL_BIN) ./files/11-logmngr_logrotate_config_generate $(1)/etc/uci-defaults/
$(INSTALL_DATA) ./files/lib/logmngr/logrotate.sh $(1)/lib/logmngr/.
$(BBFDM_INSTALL_CORE_PLUGIN) $(PKG_BUILD_DIR)/bbf_plugin/libbbflogrotate.so $(1)
endif
endef
$(eval $(call BuildPackage,logmngr))

View file

@ -20,3 +20,4 @@ uci set logmngr.lf1=log_file
uci set logmngr.lf1.enable=1
uci set logmngr.lf1.action="ac1"
uci set logmngr.lf1.file="/var/log/messages"
uci commit logmngr

View file

@ -0,0 +1,14 @@
#!/bin/sh
if [ -s "/etc/config/logmngr" ]; then
if uci -q get logmngr.@log_rotate[0] >/dev/null; then
# return if there is any valid content
exit
fi
uci set logmngr.lro1=log_rotate
uci set logmngr.lro1.enable=1
uci set logmngr.lro1.file_name="/var/log/messages"
uci set logmngr.lro1.file_count=1
uci set logmngr.lro1.max_file_size=1000000
uci commit logmngr
fi

View file

@ -1,6 +1,7 @@
#!/bin/sh
. /lib/functions.sh
. /lib/logmngr/logrotate.sh
CONF_FILE=/etc/fluent-bit/fluent-bit.conf
TMP_CONF_FILE=/tmp/fluent-bit/fluent-bit.conf
@ -306,6 +307,10 @@ logmngr_init() {
handle_action_section
apply_config_file
if [ -f /lib/logmngr/logrotate.sh ]; then
logrotate_init
fi
procd_open_instance logmngr
procd_set_param command $PROG -c $CONF_FILE
procd_set_param file $CONF_FILE

View file

@ -0,0 +1,96 @@
#!/bin/sh
. /lib/functions.sh
LOGROTATE_FILE=/etc/logrotate.conf
LOGROTATE_TMP_FILE=/tmp/logrotate/logrotate.conf
create_logrotate_file() {
mkdir -p /tmp/logrotate
rm -f ${LOGROTATE_TMP_FILE}
touch ${LOGROTATE_FILE}
}
handle_logrotate() {
local section="$1"
local enabled
config_get enabled $section enable
if [ "$enabled" == "0" ]; then
return
fi
local file_name
config_get file_name $section file_name
if [ -z "$file_name" ]; then
# no file to rotate, return
return
fi
echo -e "$file_name {" >> ${LOGROTATE_TMP_FILE}
echo -e "\tcreate" >> ${LOGROTATE_TMP_FILE}
echo -e "\tmissingok" >> ${LOGROTATE_TMP_FILE}
echo -e "\tnotifempty" >> ${LOGROTATE_TMP_FILE}
local file_count
config_get file_count $section file_count
if [ -n "$file_count" ]; then
echo -e "\trotate $file_count" >> ${LOGROTATE_TMP_FILE}
fi
local max_file_size
config_get max_file_size $section max_file_size
if [ -n "$max_file_size" ]; then
echo -e "\tmaxsize $max_file_size" >> ${LOGROTATE_TMP_FILE}
fi
local duration
config_get duration $section duration
if [ -n "$duration" ]; then
echo -e "\tminutes $duration" >> ${LOGROTATE_TMP_FILE}
fi
local retention
config_get retention $section retention
if [ -n "$retention" ]; then
echo -e "\tmaxage $retention" >> ${LOGROTATE_TMP_FILE}
fi
local compression
config_get compression $section compression
if [ -n "$compression" ]; then
echo -e "\tcompress" >> ${LOGROTATE_TMP_FILE}
echo -e "\tcompresscmd $compression" >> ${LOGROTATE_TMP_FILE}
fi
echo -e "\tpostrotate" >> ${LOGROTATE_TMP_FILE}
echo -e "\t\tservice logmngr restart" >> ${LOGROTATE_TMP_FILE}
echo -e "\t\tsleep 1" >> ${LOGROTATE_TMP_FILE}
echo -e "\tendscript" >> ${LOGROTATE_TMP_FILE}
echo -e "}" >> ${LOGROTATE_TMP_FILE} # close the logfile section
}
apply_logrotate_file() {
cp ${LOGROTATE_TMP_FILE} ${LOGROTATE_FILE}
}
config_cron_job() {
# taking the liberty to configure the cron job hourly, that is, at the end
# of each hour, check if logrotation is needed. The logrotate daemon, when
# triggered hourly, will still honour the configure log rotation duration,
# the only slight different being that if the minutes for log rotation
# are configured in such a way that it falls within the hour, then the
# log rotation will be done at the completion of hour and not before. I do
# not think this is a drawback in the interest of keeping things simple.
sed -i '/logrotate/d' /etc/crontabs/root
echo "0 * * * * logrotate ${LOGROTATE_FILE}" >> /etc/crontabs/root
/etc/init.d/cron restart
}
logrotate_init() {
create_logrotate_file
config_foreach handle_logrotate log_rotate
apply_logrotate_file
config_cron_job
}

View file

@ -1,6 +1,7 @@
#!/bin/sh
. /lib/functions.sh
. /lib/logmngr/logrotate.sh
CONF_FILE=/etc/syslog-ng.conf
TMP_CONF_FILE=/tmp/syslog-ng/syslog-ng.conf
@ -334,6 +335,10 @@ logmngr_init() {
handle_action_section
apply_config_file
if [ -f /lib/logmngr/logrotate.sh ]; then
logrotate_init
fi
procd_open_instance logmngr
procd_set_param command $PROG reload
procd_close_instance