diff --git a/logmngr/Config.in b/logmngr/Config.in index 9fae40e1e..d34e18257 100644 --- a/logmngr/Config.in +++ b/logmngr/Config.in @@ -9,12 +9,18 @@ choice config LOGMNGR_BACKEND_FLUENTBIT bool "Use fluent-bit for log management" help - Enable this option to use fluent-bit for log management. + Enable this option to use fluent-bit for log management. config LOGMNGR_BACKEND_SYSLOG_NG bool "Use syslog-ng for log management" help - Enable this option to use syslog-ng for log management. + 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 diff --git a/logmngr/Makefile b/logmngr/Makefile index fe865a859..9b4051b61 100644 --- a/logmngr/Makefile +++ b/logmngr/Makefile @@ -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)) diff --git a/logmngr/files/10-logmngr_config_generate b/logmngr/files/10-logmngr_config_generate index 5e4759128..42b701d53 100644 --- a/logmngr/files/10-logmngr_config_generate +++ b/logmngr/files/10-logmngr_config_generate @@ -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 diff --git a/logmngr/files/11-logmngr_logrotate_config_generate b/logmngr/files/11-logmngr_logrotate_config_generate new file mode 100644 index 000000000..c2d5ef9c4 --- /dev/null +++ b/logmngr/files/11-logmngr_logrotate_config_generate @@ -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 diff --git a/logmngr/files/lib/logmngr/fluent-bit.sh b/logmngr/files/lib/logmngr/fluent-bit.sh index 756ddbcef..37a2e504f 100644 --- a/logmngr/files/lib/logmngr/fluent-bit.sh +++ b/logmngr/files/lib/logmngr/fluent-bit.sh @@ -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 diff --git a/logmngr/files/lib/logmngr/logrotate.sh b/logmngr/files/lib/logmngr/logrotate.sh new file mode 100644 index 000000000..93a9614a6 --- /dev/null +++ b/logmngr/files/lib/logmngr/logrotate.sh @@ -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 +} diff --git a/logmngr/files/lib/logmngr/syslog-ng.sh b/logmngr/files/lib/logmngr/syslog-ng.sh index 077191fed..0f91d0e75 100644 --- a/logmngr/files/lib/logmngr/syslog-ng.sh +++ b/logmngr/files/lib/logmngr/syslog-ng.sh @@ -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