Skip to content

Commit

Permalink
net-snmp: add logging
Browse files Browse the repository at this point in the history
This commit adds logging to syslog and to a logfile.

Signed-off-by: Christian Korber <ckorber@tdt.de>
  • Loading branch information
Christian Korber committed Dec 3, 2024
1 parent 4ca5840 commit abd5697
Showing 1 changed file with 61 additions and 2 deletions.
63 changes: 61 additions & 2 deletions net/net-snmp/files/snmpd.init
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit abd5697

Please sign in to comment.