From 4d9b427bde24551bd6691eb5d10bfcd33fe32c72 Mon Sep 17 00:00:00 2001 From: Yevhen Fastiuk Date: Tue, 23 Aug 2022 01:52:06 +0300 Subject: [PATCH 1/7] Add log rotation configuration mechanism * Separate debug log rotation config * Separate syslog log rotation config * Add CONFIG_DB LOGGING table to hold logging configuration Signed-off-by: Yevhen Fastiuk --- files/build_templates/defaults.j2 | 13 ++ files/build_templates/init_cfg.json.j2 | 14 ++ .../build_templates/sonic_debian_extension.j2 | 5 +- .../logrotate/logrotate-config.sh | 46 ++++- .../logrotate-common.j2} | 107 +++++------ .../logrotate/templates/logrotate-debug.j2 | 29 +++ .../logrotate/templates/logrotate-syslog.j2 | 29 +++ src/sonic-yang-models/setup.py | 2 + .../tests/files/sample_config_db.json | 6 + .../tests/yang_model_tests/tests/logging.json | 53 ++++++ .../tests_config/logging.json | 168 ++++++++++++++++++ .../yang-models/sonic-logging.yang | 62 +++++++ 12 files changed, 470 insertions(+), 64 deletions(-) create mode 100644 files/build_templates/defaults.j2 rename files/image_config/logrotate/{rsyslog.j2 => templates/logrotate-common.j2} (65%) create mode 100644 files/image_config/logrotate/templates/logrotate-debug.j2 create mode 100644 files/image_config/logrotate/templates/logrotate-syslog.j2 create mode 100644 src/sonic-yang-models/tests/yang_model_tests/tests/logging.json create mode 100644 src/sonic-yang-models/tests/yang_model_tests/tests_config/logging.json create mode 100644 src/sonic-yang-models/yang-models/sonic-logging.yang diff --git a/files/build_templates/defaults.j2 b/files/build_templates/defaults.j2 new file mode 100644 index 000000000000..a50224cd40c2 --- /dev/null +++ b/files/build_templates/defaults.j2 @@ -0,0 +1,13 @@ +{# Don't use that file to generate something, it is just for holding defaults #} +{% set def_logging = { + 'common': { + 'frequency': 'daily', + }, + 'debug': { + 'max_number': 1, + 'size': 0.001, + }, + 'syslog': { + 'max_number': 5000, + } +} %} diff --git a/files/build_templates/init_cfg.json.j2 b/files/build_templates/init_cfg.json.j2 index ae382263bb18..14068925287b 100644 --- a/files/build_templates/init_cfg.json.j2 +++ b/files/build_templates/init_cfg.json.j2 @@ -152,6 +152,20 @@ "logout": "" } }, +{% if sonic_asic_platform == 'mellanox' %} + "LOGGING": { + "debug": { + "frequency": "daily", + "max_number": 20, + "size": 10 + }, + "syslog": { + "frequency": "daily", + "max_number": 10, + "size": 20 + } + }, +{% endif %} "SYSTEM_DEFAULTS" : { {%- if include_mux == "y" %} "mux_tunnel_egress_acl": { diff --git a/files/build_templates/sonic_debian_extension.j2 b/files/build_templates/sonic_debian_extension.j2 index cd22584c942d..f04182788f39 100644 --- a/files/build_templates/sonic_debian_extension.j2 +++ b/files/build_templates/sonic_debian_extension.j2 @@ -445,7 +445,7 @@ echo "system-health.service" | sudo tee -a $GENERATED_SERVICE_FILE # Copy logrotate.d configuration files sudo cp -f $IMAGE_CONFIGS/logrotate/logrotate.d/* $FILESYSTEM_ROOT/etc/logrotate.d/ -sudo cp $IMAGE_CONFIGS/logrotate/rsyslog.j2 $FILESYSTEM_ROOT_USR_SHARE_SONIC_TEMPLATES/ +sudo cp -f $IMAGE_CONFIGS/logrotate/templates/* $FILESYSTEM_ROOT_USR_SHARE_SONIC_TEMPLATES/ sudo cp $IMAGE_CONFIGS/logrotate/logrotate-config.service $FILESYSTEM_ROOT_USR_LIB_SYSTEMD_SYSTEM sudo cp $IMAGE_CONFIGS/logrotate/logrotate-config.sh $FILESYSTEM_ROOT/usr/bin/ sudo mkdir -p $FILESYSTEM_ROOT/etc/systemd/system/logrotate.timer.d @@ -521,6 +521,9 @@ sudo LANG=C chroot $FILESYSTEM_ROOT systemctl enable systemd-bootchart sudo https_proxy=$https_proxy LANG=C chroot $FILESYSTEM_ROOT pip3 install azure-storage==0.36.0 sudo https_proxy=$https_proxy LANG=C chroot $FILESYSTEM_ROOT pip3 install watchdog==0.10.3 +# Copy defaults template +sudo cp $BUILD_TEMPLATES/defaults.j2 $FILESYSTEM_ROOT_USR_SHARE_SONIC_TEMPLATES/ + {% if include_kubernetes == "y" %} # Point to kubelet to /etc/resolv.conf # diff --git a/files/image_config/logrotate/logrotate-config.sh b/files/image_config/logrotate/logrotate-config.sh index 76caec6f035d..ef30105aaf6f 100755 --- a/files/image_config/logrotate/logrotate-config.sh +++ b/files/image_config/logrotate/logrotate-config.sh @@ -1,4 +1,48 @@ #!/bin/bash -sonic-cfggen -d -t /usr/share/sonic/templates/rsyslog.j2 -a "{\"var_log_kb\":$(df -k /var/log | sed -n 2p | awk '{ print $2 }') }" > /etc/logrotate.d/rsyslog +VAR_LOG_SIZE=$(df -k /var/log | sed -n 2p | awk '{ print $2 }') +# Adjust NUM_LOGS_TO_ROTATE to reflect number of common log files (auth, cron, +# teamd, telemetry, etc.) to rotate. +NUM_LOGS_TO_ROTATE=10 + +# Detect common log files (auth, cron, teamd, telemetry, etc.) size. 720M is +# required to hold extra space to rotate all files (16M each) two times. +if [[ "$VAR_LOG_SIZE" < "737280" ]]; then + LOG_FILE_ROTATE_SIZE_MB=1 +elif [[ "$VAR_LOG_SIZE" < "1474560" ]]; then + LOG_FILE_ROTATE_SIZE_MB=2 +else + LOG_FILE_ROTATE_SIZE_MB=16 +fi + +# Reserve space for btmp, wtmp, dpkg.log, monit.log, etc., as well as logs that +# should be disabled, just in case they get created and rotated +RESERVED_SPACE_KB=4096 + +# Limit usable space to 90 to define percentage of the partition minus the +# reserved space for other logs +USABLE_SPACE_KB=$(((VAR_LOG_SIZE * 90 / 100) - RESERVED_SPACE_KB)) + +# Set our threshold so as to maintain enough space to write all logs from empty +# to full. Most likely, some logs will have non-zero size when this is called, +# so this errs on the side of caution, giving us a bit of a cushion if a log +# grows quickly and passes its rotation size. USABLE_SPACE_KB should be at least +# twice as big as THRESHOLD_KB value. +THRESHOLD_KB=$((USABLE_SPACE_KB - \ + (LOG_FILE_ROTATE_SIZE_MB * 1024 * 2 * NUM_LOGS_TO_ROTATE))) + +# Append data to template +APPEND_DATA="{\"max_logs_size_kb\":$THRESHOLD_KB, + \"common_file_size_mb\":\"$LOG_FILE_ROTATE_SIZE_MB\"}" + +# Generic log files +sonic-cfggen -d -t /usr/share/sonic/templates/logrotate-common.j2 \ + -a "$APPEND_DATA" > /etc/logrotate.d/rsyslog + +# Specific log files +sonic-cfggen -d -t /usr/share/sonic/templates/logrotate-debug.j2 \ + -a "$APPEND_DATA" > /etc/logrotate.d/debug + +sonic-cfggen -d -t /usr/share/sonic/templates/logrotate-syslog.j2 \ + -a "$APPEND_DATA" > /etc/logrotate.d/syslog diff --git a/files/image_config/logrotate/rsyslog.j2 b/files/image_config/logrotate/templates/logrotate-common.j2 similarity index 65% rename from files/image_config/logrotate/rsyslog.j2 rename to files/image_config/logrotate/templates/logrotate-common.j2 index 77d950eb4fd4..7f858af1cc15 100644 --- a/files/image_config/logrotate/rsyslog.j2 +++ b/files/image_config/logrotate/templates/logrotate-common.j2 @@ -1,3 +1,44 @@ +{% from "defaults.j2" import def_logging %} + +{% macro postrotate_action() -%} + /bin/kill -HUP $(cat /var/run/rsyslogd.pid) +{%- endmacro -%} + +{% macro postrotate() -%} + postrotate + {{ postrotate_action() }} + endscript +{%- endmacro -%} + +{% macro disk_percentage_treshold_rotation() -%} + firstaction + # Add threshold + THRESHOLD_KB={{ max_logs_size_kb }} + + # First, delete any *.1.gz files that might be left around from a prior incomplete + # logrotate execution, otherwise logrotate will fail to do its job + find /var/log/ -name '*.1.gz' -type f -exec rm -f {} + + + while true; do + USED_KB=$(du -s /var/log | awk '{ print $1; }') + + if [ $USED_KB -lt $THRESHOLD_KB ]; then + break + else + OLDEST_ARCHIVE_FILE=$(find /var/log -type f -printf '%T+ %p\n' | grep -E '.+\.[0-9]+(\.gz)?$' | sort | head -n 1 | awk '{ print $2; }') + + if [ -z "$OLDEST_ARCHIVE_FILE" ]; then + logger -p syslog.err -t "logrotate" "No archive file to delete -- potential for filling up /var/log partition!" + break + fi + + logger -p syslog.info -t "logrotate" "Deleting archive file $OLDEST_ARCHIVE_FILE to free up space" + rm -rf "$OLDEST_ARCHIVE_FILE" + fi + done + endscript +{%- endmacro -%} + # These logs should no longer get created. However, in case they do get created, # we should keep them to a small size and rotate them also. /var/log/mail.info @@ -8,7 +49,6 @@ /var/log/kern.log /var/log/user.log /var/log/lpr.log -/var/log/debug /var/log/messages { size 10k @@ -18,14 +58,11 @@ compress delaycompress sharedscripts - postrotate - /bin/kill -HUP $(cat /var/run/rsyslogd.pid) - endscript + {{ postrotate() }} } /var/log/auth.log /var/log/cron.log -/var/log/syslog /var/log/teamd.log /var/log/telemetry.log /var/log/gnmi.log @@ -35,68 +72,14 @@ /var/log/swss/swss*.rec /var/log/swss/responsepublisher.rec { -{% if var_log_kb <= 204800 %} - size 1M -{% elif var_log_kb <= 409600 %} - size 2M -{% else %} - size 16M -{% endif %} + size {{ ((common_file_size_mb | float) * 1024 * 1024) | int }} rotate 5000 missingok notifempty compress delaycompress nosharedscripts - firstaction - # Adjust NUM_LOGS_TO_ROTATE to reflect number of log files that trigger this block specified above - NUM_LOGS_TO_ROTATE=8 - - # Adjust LOG_FILE_ROTATE_SIZE_KB to reflect the "size" parameter specified above, in kB -{% if var_log_kb <= 204800 %} - LOG_FILE_ROTATE_SIZE_KB=1024 -{% elif var_log_kb <= 409600 %} - LOG_FILE_ROTATE_SIZE_KB=2048 -{% else %} - LOG_FILE_ROTATE_SIZE_KB=16384 -{% endif %} - - # Reserve space for btmp, wtmp, dpkg.log, monit.log, etc., as well as logs that - # should be disabled, just in case they get created and rotated - RESERVED_SPACE_KB=4096 - - VAR_LOG_SIZE_KB={{var_log_kb}} - - # Limit usable space to 90% of the partition minus the reserved space for other logs - USABLE_SPACE_KB=$(( (VAR_LOG_SIZE_KB * 90 / 100) - RESERVED_SPACE_KB)) - - # Set our threshold so as to maintain enough space to write all logs from empty to full - # Most likely, some logs will have non-zero size when this is called, so this errs on the side - # of caution, giving us a bit of a cushion if a log grows quickly and passes its rotation size - THRESHOLD_KB=$((USABLE_SPACE_KB - (NUM_LOGS_TO_ROTATE * LOG_FILE_ROTATE_SIZE_KB * 2))) - - # First, delete any *.1.gz files that might be left around from a prior incomplete - # logrotate execution, otherwise logrotate will fail to do its job - find /var/log/ -name '*.1.gz' -type f -exec rm -f {} + - - while true; do - USED_KB=$(du -s /var/log | awk '{ print $1; }') - - if [ $USED_KB -lt $THRESHOLD_KB ]; then - break - else - OLDEST_ARCHIVE_FILE=$(find /var/log -type f -printf '%T+ %p\n' | grep -E '.+\.[0-9]+(\.gz)?$' | sort | head -n 1 | awk '{ print $2; }') - - if [ -z "$OLDEST_ARCHIVE_FILE" ]; then - logger -p syslog.err -t "logrotate" "No archive file to delete -- potential for filling up /var/log partition!" - break - fi - - logger -p syslog.info -t "logrotate" "Deleting archive file $OLDEST_ARCHIVE_FILE to free up space" - rm -rf "$OLDEST_ARCHIVE_FILE" - fi - done - endscript + {{ disk_percentage_treshold_rotation() }} postrotate if [ $(echo $1 | grep -c "/var/log/swss/") -gt 0 ]; then # for multi asic platforms, there are multiple orchagents @@ -116,7 +99,7 @@ pgrep -x orchagent | xargs /bin/kill -HUP 2>/dev/null || true fi else - /bin/kill -HUP $(cat /var/run/rsyslogd.pid) + {{ postrotate_action() }} fi endscript } diff --git a/files/image_config/logrotate/templates/logrotate-debug.j2 b/files/image_config/logrotate/templates/logrotate-debug.j2 new file mode 100644 index 000000000000..4b9af9a3eba4 --- /dev/null +++ b/files/image_config/logrotate/templates/logrotate-debug.j2 @@ -0,0 +1,29 @@ +{% from "logrotate-common.j2" import postrotate with context %} +{% from "logrotate-common.j2" + import disk_percentage_treshold_rotation with context -%} +{% from "defaults.j2" import def_logging -%} + +{% set debug = (LOGGING | d({})).get('debug', {}) -%} +{% set frequency = debug.frequency | d(def_logging.common.frequency) -%} +{% set max_number = debug.max_number | d(def_logging.debug.max_number) -%} + +{% if debug.disk_percentage -%} + {% set size_mb = (max_logs_size_kb | int) * + (debug.disk_percentage | float) / 100 / 1024 -%} +{% else -%} + {% set size_mb = (debug.size | d(def_logging.debug.size)) | float -%} +{%- endif %} +{% set size = (size_mb * 1024 * 1024) | int %} +/var/log/debug +{ + {{ frequency }} + size {{ size }} + rotate {{ max_number }} + missingok + notifempty + compress + delaycompress + nosharedscripts + {{ disk_percentage_treshold_rotation() }} + {{ postrotate() }} +} diff --git a/files/image_config/logrotate/templates/logrotate-syslog.j2 b/files/image_config/logrotate/templates/logrotate-syslog.j2 new file mode 100644 index 000000000000..a8172f92d453 --- /dev/null +++ b/files/image_config/logrotate/templates/logrotate-syslog.j2 @@ -0,0 +1,29 @@ +{% from "logrotate-common.j2" import postrotate with context %} +{% from "logrotate-common.j2" + import disk_percentage_treshold_rotation with context -%} +{% from "defaults.j2" import def_logging -%} + +{% set syslog = (LOGGING | d({})).get('syslog', {}) -%} +{% set frequency = syslog.frequency | d(def_logging.common.frequency) -%} +{% set max_number = syslog.max_number | d(def_logging.syslog.max_number) -%} + +{% if syslog.disk_percentage -%} + {% set size_mb = (max_logs_size_kb | int) * + (syslog.disk_percentage | float) / 100 / 1024 -%} +{% else -%} + {% set size_mb = (syslog.size | d(common_file_size_mb)) | float -%} +{%- endif %} +{% set size = (size_mb * 1024 * 1024) | int -%} +/var/log/syslog +{ + {{ frequency }} + size {{ size }} + rotate {{ max_number }} + missingok + notifempty + compress + delaycompress + nosharedscripts + {{ disk_percentage_treshold_rotation() }} + {{ postrotate() }} +} diff --git a/src/sonic-yang-models/setup.py b/src/sonic-yang-models/setup.py index bf0372b8f61c..e908c910d9b2 100644 --- a/src/sonic-yang-models/setup.py +++ b/src/sonic-yang-models/setup.py @@ -135,6 +135,7 @@ def run(self): './yang-models/sonic-interface.yang', './yang-models/sonic-kdump.yang', './yang-models/sonic-kubernetes_master.yang', + './yang-models/sonic-logging.yang', './yang-models/sonic-loopback-interface.yang', './yang-models/sonic-lossless-traffic-pattern.yang', './yang-models/sonic-mgmt_interface.yang', @@ -235,6 +236,7 @@ def run(self): './cvlyang-models/sonic-interface.yang', './cvlyang-models/sonic-kdump.yang', './cvlyang-models/sonic-kubernetes_master.yang', + './cvlyang-models/sonic-logging.yang', './cvlyang-models/sonic-loopback-interface.yang', './cvlyang-models/sonic-mgmt_interface.yang', './cvlyang-models/sonic-mgmt_port.yang', diff --git a/src/sonic-yang-models/tests/files/sample_config_db.json b/src/sonic-yang-models/tests/files/sample_config_db.json index 4cf42f535c60..d18d70cb2b78 100644 --- a/src/sonic-yang-models/tests/files/sample_config_db.json +++ b/src/sonic-yang-models/tests/files/sample_config_db.json @@ -2643,6 +2643,12 @@ }, "dpu1": { "midplane_interface": "dpu1" + "LOGGING": { + "debug": { + "disk_percentage": "46.0", + "frequency": "daily", + "max_number": "100", + "size": "20.0" } }, "BANNER_MESSAGE": { diff --git a/src/sonic-yang-models/tests/yang_model_tests/tests/logging.json b/src/sonic-yang-models/tests/yang_model_tests/tests/logging.json new file mode 100644 index 000000000000..70541b059374 --- /dev/null +++ b/src/sonic-yang-models/tests/yang_model_tests/tests/logging.json @@ -0,0 +1,53 @@ +{ + "LOGGING_NAME_TEST": { + "desc": "Valid log file name test" + }, + "LOGGING_INVALID_NAME_TEST": { + "desc": "Invalid log file name test", + "eStrKey": "Pattern" + }, + "LOGGING_DISK_PERCENTAGE_TEST": { + "desc": "Valid disk percentage test" + }, + "LOGGING_INVALID_DISK_PERCENTAGE_TEST": { + "desc": "Invalid disk percentage test", + "eStrKey": "Range" + }, + "LOGGING_INVALID_DISK_PERCENTAGE_TEST_2": { + "desc": "Invalid disk percentage test 2", + "eStrKey": "Range" + }, + "LOGGING_FREQUENCY_TEST": { + "desc": "Valid rotation frequency test" + }, + "LOGGING_INVALID_FREQUENCY_TEST": { + "desc": "Invalid rotation frequency test", + "eStrKey": "Pattern" + }, + "LOGGING_INVALID_FREQUENCY_TEST_2": { + "desc": "Invalid rotation frequency test 2", + "eStrKey": "Pattern" + }, + "LOGGING_MAX_NUMBER_TEST": { + "desc": "Valid rotation max number test" + }, + "LOGGING_INVALID_MAX_NUMBER_TEST": { + "desc": "Invalid rotation max number test", + "eStrKey": "Range" + }, + "LOGGING_INVALID_MAX_NUMBER_TEST_2": { + "desc": "Invalid rotation max number test 2", + "eStrKey": "Range" + }, + "LOGGING_SIZE_TEST": { + "desc": "Rotation file size test" + }, + "LOGGING_INVALID_SIZE_TEST": { + "desc": "Invalid rotation file size test", + "eStrKey": "Range" + }, + "LOGGING_INVALID_SIZE_TEST_2": { + "desc": "Invalid rotation file size test 2", + "eStrKey": "Range" + } +} diff --git a/src/sonic-yang-models/tests/yang_model_tests/tests_config/logging.json b/src/sonic-yang-models/tests/yang_model_tests/tests_config/logging.json new file mode 100644 index 000000000000..413612724551 --- /dev/null +++ b/src/sonic-yang-models/tests/yang_model_tests/tests_config/logging.json @@ -0,0 +1,168 @@ +{ + "LOGGING_NAME_TEST": { + "sonic-logging:sonic-logging": { + "sonic-logging:LOGGING": { + "LOGGING_LIST": [ + { + "name": "someloggingfile.log" + } + ] + } + } + }, + "LOGGING_INVALID_NAME_TEST": { + "sonic-logging:sonic-logging": { + "sonic-logging:LOGGING": { + "LOGGING_LIST": [ + { + "name": "" + } + ] + } + } + }, + "LOGGING_DISK_PERCENTAGE_TEST": { + "sonic-logging:sonic-logging": { + "sonic-logging:LOGGING": { + "LOGGING_LIST": [ + { + "name": "syslog", + "disk_percentage": "90.0" + } + ] + } + } + }, + "LOGGING_INVALID_DISK_PERCENTAGE_TEST": { + "sonic-logging:sonic-logging": { + "sonic-logging:LOGGING": { + "LOGGING_LIST": [ + { + "name": "syslog", + "disk_percentage": "101.0" + } + ] + } + } + }, + "LOGGING_INVALID_DISK_PERCENTAGE_TEST_2": { + "sonic-logging:sonic-logging": { + "sonic-logging:LOGGING": { + "LOGGING_LIST": [ + { + "name": "syslog", + "disk_percentage": "0.0" + } + ] + } + } + }, + "LOGGING_FREQUENCY_TEST": { + "sonic-logging:sonic-logging": { + "sonic-logging:LOGGING": { + "LOGGING_LIST": [ + { + "name": "syslog", + "frequency": "weekly" + } + ] + } + } + }, + "LOGGING_INVALID_FREQUENCY_TEST": { + "sonic-logging:sonic-logging": { + "sonic-logging:LOGGING": { + "LOGGING_LIST": [ + { + "name": "syslog", + "frequency": "blabla" + } + ] + } + } + }, + "LOGGING_INVALID_FREQUENCY_TEST_2": { + "sonic-logging:sonic-logging": { + "sonic-logging:LOGGING": { + "LOGGING_LIST": [ + { + "name": "syslog", + "frequency": "" + } + ] + } + } + }, + "LOGGING_MAX_NUMBER_TEST": { + "sonic-logging:sonic-logging": { + "sonic-logging:LOGGING": { + "LOGGING_LIST": [ + { + "name": "debug", + "max_number": "5000" + } + ] + } + } + }, + "LOGGING_INVALID_MAX_NUMBER_TEST": { + "sonic-logging:sonic-logging": { + "sonic-logging:LOGGING": { + "LOGGING_LIST": [ + { + "name": "debug", + "max_number": "1000000" + } + ] + } + } + }, + "LOGGING_INVALID_MAX_NUMBER_TEST_2": { + "sonic-logging:sonic-logging": { + "sonic-logging:LOGGING": { + "LOGGING_LIST": [ + { + "name": "debug", + "max_number": "-1" + } + ] + } + } + }, + "LOGGING_SIZE_TEST": { + "sonic-logging:sonic-logging": { + "sonic-logging:LOGGING": { + "LOGGING_LIST": [ + { + "name": "debug", + "size": "10.0" + } + ] + } + } + }, + "LOGGING_INVALID_SIZE_TEST": { + "sonic-logging:sonic-logging": { + "sonic-logging:LOGGING": { + "LOGGING_LIST": [ + { + "name": "debug", + "size": "0.0" + } + ] + } + } + }, + "LOGGING_INVALID_SIZE_TEST_2": { + "sonic-logging:sonic-logging": { + "sonic-logging:LOGGING": { + "LOGGING_LIST": [ + { + "name": "debug", + "size": "10000.001" + } + ] + } + } + } +} diff --git a/src/sonic-yang-models/yang-models/sonic-logging.yang b/src/sonic-yang-models/yang-models/sonic-logging.yang new file mode 100644 index 000000000000..c4545a2974ed --- /dev/null +++ b/src/sonic-yang-models/yang-models/sonic-logging.yang @@ -0,0 +1,62 @@ +module sonic-logging { + + yang-version 1.1; + + namespace "http://github.com/Azure/sonic-logging"; + prefix logging; + + description "LOGGING YANG Module for SONiC-based OS"; + + revision 2022-08-27 { + description "First Revision"; + } + + container sonic-logging { + + container LOGGING { + + description "LOGGING part of config_db.json"; + + list LOGGING_LIST { + + key "name"; + + leaf name { + type string { + length 1..255; + } + } + + leaf disk_percentage { + type decimal64 { + fraction-digits 3; + range 0.001..100; + } + } + + leaf frequency { + type string { + pattern "daily|weekly|monthly|yearly"; + } + } + + leaf max_number { + type int32 { + range "0..999999"; + } + } + + leaf size { + type decimal64 { + fraction-digits 3; + range 0.001..3500.0; + } + } + } + /* end of list LOG_FILES_LIST */ + } + /* end of container LOGGING */ + } + /* end of top level container */ +} +/* end of module sonic-logging */ \ No newline at end of file From 7f89174e4b7d3cf642e485c9b07bcb7c3aa919b5 Mon Sep 17 00:00:00 2001 From: Yevhen Fastiuk Date: Sun, 25 Dec 2022 02:34:16 +0200 Subject: [PATCH 2/7] Pin max size of logging to static value Signed-off-by: Yevhen Fastiuk --- files/image_config/logrotate/logrotate-config.sh | 4 ++-- files/image_config/logrotate/templates/logrotate-debug.j2 | 2 +- files/image_config/logrotate/templates/logrotate-syslog.j2 | 2 +- src/sonic-yang-models/tests/files/sample_config_db.json | 2 ++ 4 files changed, 6 insertions(+), 4 deletions(-) diff --git a/files/image_config/logrotate/logrotate-config.sh b/files/image_config/logrotate/logrotate-config.sh index ef30105aaf6f..dce28f7084e7 100755 --- a/files/image_config/logrotate/logrotate-config.sh +++ b/files/image_config/logrotate/logrotate-config.sh @@ -8,9 +8,9 @@ NUM_LOGS_TO_ROTATE=10 # Detect common log files (auth, cron, teamd, telemetry, etc.) size. 720M is # required to hold extra space to rotate all files (16M each) two times. -if [[ "$VAR_LOG_SIZE" < "737280" ]]; then +if [[ "$VAR_LOG_SIZE" < "204800" ]]; then LOG_FILE_ROTATE_SIZE_MB=1 -elif [[ "$VAR_LOG_SIZE" < "1474560" ]]; then +elif [[ "$VAR_LOG_SIZE" < "409600" ]]; then LOG_FILE_ROTATE_SIZE_MB=2 else LOG_FILE_ROTATE_SIZE_MB=16 diff --git a/files/image_config/logrotate/templates/logrotate-debug.j2 b/files/image_config/logrotate/templates/logrotate-debug.j2 index 4b9af9a3eba4..4494b718aac8 100644 --- a/files/image_config/logrotate/templates/logrotate-debug.j2 +++ b/files/image_config/logrotate/templates/logrotate-debug.j2 @@ -7,7 +7,7 @@ {% set frequency = debug.frequency | d(def_logging.common.frequency) -%} {% set max_number = debug.max_number | d(def_logging.debug.max_number) -%} -{% if debug.disk_percentage -%} +{% if debug.disk_percentage and not debug.size -%} {% set size_mb = (max_logs_size_kb | int) * (debug.disk_percentage | float) / 100 / 1024 -%} {% else -%} diff --git a/files/image_config/logrotate/templates/logrotate-syslog.j2 b/files/image_config/logrotate/templates/logrotate-syslog.j2 index a8172f92d453..cdab80a1f072 100644 --- a/files/image_config/logrotate/templates/logrotate-syslog.j2 +++ b/files/image_config/logrotate/templates/logrotate-syslog.j2 @@ -7,7 +7,7 @@ {% set frequency = syslog.frequency | d(def_logging.common.frequency) -%} {% set max_number = syslog.max_number | d(def_logging.syslog.max_number) -%} -{% if syslog.disk_percentage -%} +{% if syslog.disk_percentage and not syslog.size -%} {% set size_mb = (max_logs_size_kb | int) * (syslog.disk_percentage | float) / 100 / 1024 -%} {% else -%} diff --git a/src/sonic-yang-models/tests/files/sample_config_db.json b/src/sonic-yang-models/tests/files/sample_config_db.json index d18d70cb2b78..0198c9f1611e 100644 --- a/src/sonic-yang-models/tests/files/sample_config_db.json +++ b/src/sonic-yang-models/tests/files/sample_config_db.json @@ -2643,6 +2643,8 @@ }, "dpu1": { "midplane_interface": "dpu1" + } + }, "LOGGING": { "debug": { "disk_percentage": "46.0", From 6868b21d4be21adbd2c34c1a5ada62da5d10d620 Mon Sep 17 00:00:00 2001 From: Yevhen Fastiuk Date: Tue, 13 Aug 2024 02:58:42 +0300 Subject: [PATCH 3/7] Logrotate: Fix according to GitHub comments Signed-off-by: Yevhen Fastiuk --- files/build_templates/defaults.j2 | 13 ------------- files/build_templates/init_cfg.json.j2 | 2 -- files/image_config/logrotate/logrotate-config.sh | 2 +- .../logrotate/templates/logrotate-common.j2 | 2 -- .../logrotate/templates/logrotate-debug.j2 | 7 +++---- .../logrotate/templates/logrotate-syslog.j2 | 5 ++--- .../yang-models/sonic-logging.yang | 5 +++++ 7 files changed, 11 insertions(+), 25 deletions(-) delete mode 100644 files/build_templates/defaults.j2 diff --git a/files/build_templates/defaults.j2 b/files/build_templates/defaults.j2 deleted file mode 100644 index a50224cd40c2..000000000000 --- a/files/build_templates/defaults.j2 +++ /dev/null @@ -1,13 +0,0 @@ -{# Don't use that file to generate something, it is just for holding defaults #} -{% set def_logging = { - 'common': { - 'frequency': 'daily', - }, - 'debug': { - 'max_number': 1, - 'size': 0.001, - }, - 'syslog': { - 'max_number': 5000, - } -} %} diff --git a/files/build_templates/init_cfg.json.j2 b/files/build_templates/init_cfg.json.j2 index 14068925287b..3d67373cbc2b 100644 --- a/files/build_templates/init_cfg.json.j2 +++ b/files/build_templates/init_cfg.json.j2 @@ -152,7 +152,6 @@ "logout": "" } }, -{% if sonic_asic_platform == 'mellanox' %} "LOGGING": { "debug": { "frequency": "daily", @@ -165,7 +164,6 @@ "size": 20 } }, -{% endif %} "SYSTEM_DEFAULTS" : { {%- if include_mux == "y" %} "mux_tunnel_egress_acl": { diff --git a/files/image_config/logrotate/logrotate-config.sh b/files/image_config/logrotate/logrotate-config.sh index dce28f7084e7..1b56f1512d58 100755 --- a/files/image_config/logrotate/logrotate-config.sh +++ b/files/image_config/logrotate/logrotate-config.sh @@ -4,7 +4,7 @@ VAR_LOG_SIZE=$(df -k /var/log | sed -n 2p | awk '{ print $2 }') # Adjust NUM_LOGS_TO_ROTATE to reflect number of common log files (auth, cron, # teamd, telemetry, etc.) to rotate. -NUM_LOGS_TO_ROTATE=10 +NUM_LOGS_TO_ROTATE=8 # Detect common log files (auth, cron, teamd, telemetry, etc.) size. 720M is # required to hold extra space to rotate all files (16M each) two times. diff --git a/files/image_config/logrotate/templates/logrotate-common.j2 b/files/image_config/logrotate/templates/logrotate-common.j2 index 7f858af1cc15..285087333995 100644 --- a/files/image_config/logrotate/templates/logrotate-common.j2 +++ b/files/image_config/logrotate/templates/logrotate-common.j2 @@ -1,5 +1,3 @@ -{% from "defaults.j2" import def_logging %} - {% macro postrotate_action() -%} /bin/kill -HUP $(cat /var/run/rsyslogd.pid) {%- endmacro -%} diff --git a/files/image_config/logrotate/templates/logrotate-debug.j2 b/files/image_config/logrotate/templates/logrotate-debug.j2 index 4494b718aac8..2b8cf1242436 100644 --- a/files/image_config/logrotate/templates/logrotate-debug.j2 +++ b/files/image_config/logrotate/templates/logrotate-debug.j2 @@ -1,17 +1,16 @@ {% from "logrotate-common.j2" import postrotate with context %} {% from "logrotate-common.j2" import disk_percentage_treshold_rotation with context -%} -{% from "defaults.j2" import def_logging -%} {% set debug = (LOGGING | d({})).get('debug', {}) -%} -{% set frequency = debug.frequency | d(def_logging.common.frequency) -%} -{% set max_number = debug.max_number | d(def_logging.debug.max_number) -%} +{% set frequency = debug.frequency | 'daily' -%} +{% set max_number = debug.max_number | 1 -%} {% if debug.disk_percentage and not debug.size -%} {% set size_mb = (max_logs_size_kb | int) * (debug.disk_percentage | float) / 100 / 1024 -%} {% else -%} - {% set size_mb = (debug.size | d(def_logging.debug.size)) | float -%} + {% set size_mb = (debug.size | 0.001) | float -%} {%- endif %} {% set size = (size_mb * 1024 * 1024) | int %} /var/log/debug diff --git a/files/image_config/logrotate/templates/logrotate-syslog.j2 b/files/image_config/logrotate/templates/logrotate-syslog.j2 index cdab80a1f072..bb98afa20f54 100644 --- a/files/image_config/logrotate/templates/logrotate-syslog.j2 +++ b/files/image_config/logrotate/templates/logrotate-syslog.j2 @@ -1,11 +1,10 @@ {% from "logrotate-common.j2" import postrotate with context %} {% from "logrotate-common.j2" import disk_percentage_treshold_rotation with context -%} -{% from "defaults.j2" import def_logging -%} {% set syslog = (LOGGING | d({})).get('syslog', {}) -%} -{% set frequency = syslog.frequency | d(def_logging.common.frequency) -%} -{% set max_number = syslog.max_number | d(def_logging.syslog.max_number) -%} +{% set frequency = syslog.frequency | 'daily' -%} +{% set max_number = syslog.max_number | 5000 -%} {% if syslog.disk_percentage and not syslog.size -%} {% set size_mb = (max_logs_size_kb | int) * diff --git a/src/sonic-yang-models/yang-models/sonic-logging.yang b/src/sonic-yang-models/yang-models/sonic-logging.yang index c4545a2974ed..4c2deb2980f7 100644 --- a/src/sonic-yang-models/yang-models/sonic-logging.yang +++ b/src/sonic-yang-models/yang-models/sonic-logging.yang @@ -22,12 +22,14 @@ module sonic-logging { key "name"; leaf name { + description "Name of the file to configure log rotation"; type string { length 1..255; } } leaf disk_percentage { + description "Define rotation size as disk percentage"; type decimal64 { fraction-digits 3; range 0.001..100; @@ -35,18 +37,21 @@ module sonic-logging { } leaf frequency { + description "Log file rotation frequency"; type string { pattern "daily|weekly|monthly|yearly"; } } leaf max_number { + description "Max number of rotated files to keep"; type int32 { range "0..999999"; } } leaf size { + description "Log files are rotated only if they grow bigger then size MiB"; type decimal64 { fraction-digits 3; range 0.001..3500.0; From 91179d7ce10e635b27b86e8a69f05d4a88e5adc9 Mon Sep 17 00:00:00 2001 From: Yevhen Fastiuk Date: Tue, 13 Aug 2024 03:02:18 +0300 Subject: [PATCH 4/7] Logrotate: Don't copy defaults file anymore Signed-off-by: Yevhen Fastiuk --- files/build_templates/sonic_debian_extension.j2 | 3 --- 1 file changed, 3 deletions(-) diff --git a/files/build_templates/sonic_debian_extension.j2 b/files/build_templates/sonic_debian_extension.j2 index ff230f3729f2..ec92e432b8b1 100644 --- a/files/build_templates/sonic_debian_extension.j2 +++ b/files/build_templates/sonic_debian_extension.j2 @@ -540,9 +540,6 @@ sudo LANG=C chroot $FILESYSTEM_ROOT systemctl enable systemd-bootchart sudo https_proxy=$https_proxy LANG=C chroot $FILESYSTEM_ROOT pip3 install azure-storage==0.36.0 sudo https_proxy=$https_proxy LANG=C chroot $FILESYSTEM_ROOT pip3 install watchdog==0.10.3 -# Copy defaults template -sudo cp $BUILD_TEMPLATES/defaults.j2 $FILESYSTEM_ROOT_USR_SHARE_SONIC_TEMPLATES/ - {% if include_kubernetes == "y" %} # Point to kubelet to /etc/resolv.conf # From b30a3feaf8644d3460a54a06b940d728ae697592 Mon Sep 17 00:00:00 2001 From: Yevhen Fastiuk Date: Wed, 21 Aug 2024 02:13:03 +0300 Subject: [PATCH 5/7] Logrotate: Fix logical mistake in template Signed-off-by: Yevhen Fastiuk --- files/image_config/logrotate/templates/logrotate-debug.j2 | 6 +++--- files/image_config/logrotate/templates/logrotate-syslog.j2 | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/files/image_config/logrotate/templates/logrotate-debug.j2 b/files/image_config/logrotate/templates/logrotate-debug.j2 index 2b8cf1242436..d0b44fbd0779 100644 --- a/files/image_config/logrotate/templates/logrotate-debug.j2 +++ b/files/image_config/logrotate/templates/logrotate-debug.j2 @@ -3,14 +3,14 @@ import disk_percentage_treshold_rotation with context -%} {% set debug = (LOGGING | d({})).get('debug', {}) -%} -{% set frequency = debug.frequency | 'daily' -%} -{% set max_number = debug.max_number | 1 -%} +{% set frequency = debug.frequency | d('daily') -%} +{% set max_number = debug.max_number | d(1) -%} {% if debug.disk_percentage and not debug.size -%} {% set size_mb = (max_logs_size_kb | int) * (debug.disk_percentage | float) / 100 / 1024 -%} {% else -%} - {% set size_mb = (debug.size | 0.001) | float -%} + {% set size_mb = (debug.size | d(0.001)) | float -%} {%- endif %} {% set size = (size_mb * 1024 * 1024) | int %} /var/log/debug diff --git a/files/image_config/logrotate/templates/logrotate-syslog.j2 b/files/image_config/logrotate/templates/logrotate-syslog.j2 index bb98afa20f54..a4db282a1d75 100644 --- a/files/image_config/logrotate/templates/logrotate-syslog.j2 +++ b/files/image_config/logrotate/templates/logrotate-syslog.j2 @@ -3,8 +3,8 @@ import disk_percentage_treshold_rotation with context -%} {% set syslog = (LOGGING | d({})).get('syslog', {}) -%} -{% set frequency = syslog.frequency | 'daily' -%} -{% set max_number = syslog.max_number | 5000 -%} +{% set frequency = syslog.frequency | d('daily') -%} +{% set max_number = syslog.max_number | d(5000) -%} {% if syslog.disk_percentage and not syslog.size -%} {% set size_mb = (max_logs_size_kb | int) * From 780746871bd8a9c582cfe5aaf9bee93e8bed59a6 Mon Sep 17 00:00:00 2001 From: Yevhen Fastiuk Date: Wed, 21 Aug 2024 16:19:48 +0300 Subject: [PATCH 6/7] Logrotate: Decrease syslog size to 16M Signed-off-by: Yevhen Fastiuk --- files/build_templates/init_cfg.json.j2 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/files/build_templates/init_cfg.json.j2 b/files/build_templates/init_cfg.json.j2 index 59fd782f685e..e4f3f1023c21 100644 --- a/files/build_templates/init_cfg.json.j2 +++ b/files/build_templates/init_cfg.json.j2 @@ -164,7 +164,7 @@ "syslog": { "frequency": "daily", "max_number": 10, - "size": 20 + "size": 16 } }, "SYSTEM_DEFAULTS" : { From 544937b658dfac49f9015895d782a252895eaf8c Mon Sep 17 00:00:00 2001 From: Yevhen Fastiuk Date: Mon, 21 Oct 2024 19:08:51 +0300 Subject: [PATCH 7/7] Rename namespace in YANG model --- src/sonic-yang-models/yang-models/sonic-logging.yang | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/sonic-yang-models/yang-models/sonic-logging.yang b/src/sonic-yang-models/yang-models/sonic-logging.yang index 4c2deb2980f7..531741b495e0 100644 --- a/src/sonic-yang-models/yang-models/sonic-logging.yang +++ b/src/sonic-yang-models/yang-models/sonic-logging.yang @@ -2,7 +2,7 @@ module sonic-logging { yang-version 1.1; - namespace "http://github.com/Azure/sonic-logging"; + namespace "http://github.com/sonic-net/sonic-logging"; prefix logging; description "LOGGING YANG Module for SONiC-based OS"; @@ -64,4 +64,4 @@ module sonic-logging { } /* end of top level container */ } -/* end of module sonic-logging */ \ No newline at end of file +/* end of module sonic-logging */