From 9be48e952b1896255ecc5b811a41e2814e0a4fb4 Mon Sep 17 00:00:00 2001 From: Christian Korber Date: Mon, 5 Aug 2024 11:45:14 +0200 Subject: [PATCH] net-snmp: add logging This commit adds logging to syslog and to a logfile. Signed-off-by: Christian Korber --- net/net-snmp/files/snmpd.init | 63 +++++++++++++++++++++++++++++++++-- 1 file changed, 61 insertions(+), 2 deletions(-) diff --git a/net/net-snmp/files/snmpd.init b/net/net-snmp/files/snmpd.init index f910124e26e120..0e2b373bbaaa20 100644 --- a/net/net-snmp/files/snmpd.init +++ b/net/net-snmp/files/snmpd.init @@ -344,6 +344,64 @@ snmpd_setup_fw_rules() { HANDLED_SNMP_ZONES="$HANDLED_SNMP_ZONES $zone" } +snmpd_configure_logging() { + local cfg="$1" + local log_syslog + local log_syslog_facility + local log_syslog_priority + local log_file + local log_file_path + local log_file_priority + + config_get_bool log_syslog "$cfg" log_syslog 0 + + # d - LOG_DAEMON, + # u - LOG_USER, + # 0-7 - LOG_LOCAL0 through LOG_LOCAL7. + + # 0 or ! - LOG_EMERG + # 1 or a - LOG_ALERT + # 2 or c - LOG_CRIT + # 3 or e - LOG_ERR + # 4 or w - LOG_WARN + # 5 or n - LOG_NOTICE + # 6 or i - LOG_INFO + # 7 or d - LOG_DEBUG + + if [ "$log_syslog" -eq 1 ]; then + config_get log_syslog_facility "$cfg" log_syslog_facility "daemon" + config_get log_syslog_priority "$cfg" log_syslog_priority "info" + + if [ "$log_syslog_facility" = "daemon" ] || + [ "$log_syslog_facility" = "user" ]; then + log_syslog_facility=$(echo "$log_syslog_facility" | + cut -c 1) + else + log_syslog_facility=$(echo "$log_syslog_facility" | + cut -c 6) + fi + + [ "$log_syslog_priority" = "emerg" ] && log_syslog_priority="!" + log_syslog_priority=$(echo "$log_syslog_priority" | + cut -c 1) + + procd_append_param command "-LS ${log_syslog_priority} ${log_syslog_facility}" + fi + + config_get_bool log_file "$cfg" log_file 0 + + if [ "$log_file" -eq 1 ]; then + config_get log_file_path "$cfg" log_file_path "/var/log/snmpd.log" + config_get log_file_priority "$cfg" log_file_priority "info" + + [ "$log_file_priority" = "emerg" ] && log_file_priority="!" + log_file_priority=$(echo "$log_file_priority" | cut -c 1) + + mkdir -p "$(dirname "${log_file_path}")" + procd_append_param command "-LF ${log_file_priority} ${log_file_path}" + fi +} + start_service() { [ -f "$CONFIGFILE" ] && rm -f "$CONFIGFILE" @@ -381,8 +439,9 @@ start_service() { append_parm trapsess trapsess trapsess config_foreach snmpd_snmpv3_add v3 general - procd_set_param command $PROG -Lf /dev/null -f -r - procd_set_param file $CONFIGFILE + procd_set_param command $PROG -f -r -p "$pid_file" + config_foreach snmpd_configure_logging log + procd_append_param command -C -c $CONFIGFILE procd_set_param respawn for iface in $(ls /sys/class/net 2>/dev/null); do