Skip to content
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 106 additions & 25 deletions net/nlbwmon/files/nlbwmon.init
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,75 @@ NAME=nlbwmon
PROG=/usr/sbin/nlbwmon
NICEPRIO=19

# ============================================
# [REFACTORED] Single Unified Logging Function
# ============================================
log_msg() {
local level="${1:-INFO}"
shift
local msg="$*"

case "$level" in
ERROR)
echo "[nlbwmon] ERROR: $msg" | logger -t nlbwmon -p user.err >&2
;;
WARN)
echo "[nlbwmon] WARN: $msg" | logger -t nlbwmon -p user.warn
;;
*)
echo "[nlbwmon] INFO: $msg" | logger -t nlbwmon -p user.info
;;
esac
}
Comment on lines +13 to +29

# ============================================
# [NEW] Health Check Function
# ============================================
health_check() {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

health_check() is defined but never called anywhere in the script, so it is dead code. If it is meant to back a status/health command, wire it into a status_service(); otherwise remove it.


Generated by Claude Code

local pidfile="/var/run/nlbwmon.pid"

if [ -f "$pidfile" ]; then
local pid=$(cat "$pidfile")
Comment on lines +34 to +38
if kill -0 "$pid" 2>/dev/null; then
return 0
fi
fi
return 1
}

add_subnet() {
local network="$1"
local range ranges

case "$network" in
*.*|*:*)
procd_append_param command '-s' "$network"
;;
*)
if network_get_subnets ranges "$network"; then
for range in $ranges; do
procd_append_param command '-s' "$range"
done
fi

if network_get_subnets6 ranges "$network"; then
for range in $ranges; do
procd_append_param command '-s' "$range"
done
fi
_process_subnets "$network"
;;
esac
}

# ============================================
# [NEW] Helper Function - Eliminated Code Duplication
# ============================================
_process_subnets() {
local network="$1"
local ranges range

if network_get_subnets ranges "$network"; then
for range in $ranges; do
procd_append_param command '-s' "$range"
done
fi

if network_get_subnets6 ranges "$network"; then
for range in $ranges; do
procd_append_param command '-s' "$range"
done
fi
}

add_option() {
local cfg="$1"
local flag="$2"
Expand All @@ -46,22 +91,55 @@ add_bool() {
local cfg="$1"
local flag="$2"
local option="$3"
local default="$4"
local default="${4:-0}"
local value

config_get_bool value "$cfg" "$option" "$default"
[ $value -eq 1 ] && procd_append_param command "$flag"
[ "$value" -eq 1 ] && procd_append_param command "$flag"
}

# ============================================
# [NEW] Performance Options
# ============================================
add_performance_options() {
local cfg="$1"
local thread_count
local memory_limit

config_get thread_count "$cfg" thread_count ""
config_get memory_limit "$cfg" memory_limit ""

[ -n "$thread_count" ] && procd_append_param command '--threads' "$thread_count"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nlbwmon does not accept these long options. Its argument parser is short-option only — getopt(argc, argv, "b:i:r:s:o:p:G:I:L:PZ") (no getopt_long), see https://github.com/jow-/nlbwmon/blob/29236be687927b0c81ee6e2642e7caa8787c4ad3/nlbwmon.c#L218 . Passing --threads/--memory-limit here (and --debug at line 124) will not enable any feature; getopt stops at the first --, so the values are treated as stray non-option args rather than tuning. nlbwmon is also single-threaded, so there is no thread-count knob to expose. Please drop add_performance_options/add_debug_mode unless you can point to the flags in nlbwmon's option table.


Generated by Claude Code

[ -n "$memory_limit" ] && procd_append_param command '--memory-limit' "$memory_limit"
Comment on lines +109 to +113
}

# ============================================
# [NEW] Debug Mode Support
# ============================================
add_debug_mode() {
local cfg="$1"
local debug_mode

config_get_bool debug_mode "$cfg" debug_mode 0
[ "$debug_mode" -eq 1 ] && procd_append_param command '--debug'
}

parse_config() {
. /lib/functions/network.sh
if ! . /lib/functions/network.sh; then
log_msg "ERROR" "Failed to source network functions"
return 1
fi

local cfg="$1"
local dir

config_get dir "$cfg" database_directory /var/lib/nlbwmon

mkdir -p "$dir"
if ! mkdir -p "$dir" 2>/dev/null; then
log_msg "ERROR" "Failed to create database directory: $dir"
return 1
fi

procd_append_param command -o "$dir"

add_option "$cfg" -b netlink_buffer_size 524288
Expand All @@ -75,6 +153,9 @@ parse_config() {
add_bool "$cfg" -P database_prealloc 0
add_bool "$cfg" -Z database_compress 1

add_performance_options "$cfg"
add_debug_mode "$cfg"

config_list_foreach "$cfg" local_network add_subnet
}

Expand All @@ -83,11 +164,16 @@ start_service() {
procd_set_param stderr 1
procd_set_param command "$PROG"
procd_set_param nice "$NICEPRIO"

procd_set_param limits nofile=4096
procd_set_param limits nproc=512

config_load nlbwmon
config_foreach parse_config nlbwmon

procd_close_instance

log_msg "INFO" "Service started successfully"
}

add_interface_trigger() {
Expand All @@ -96,13 +182,8 @@ add_interface_trigger() {
config_get interface "$1" interface
config_get_bool ignore "$1" ignore 0

[ -n "$interface" -a $ignore -eq 0 ] && procd_add_interface_trigger "interface.*" "$interface" /etc/init.d/nlbwmon reload
# [FIXED] Proper conditional block formatting without broken hyperlink syntax
if [ -n "$interface" ] && [ "$ignore" -eq 0 ]; then
procd_add_interface_trigger "interface.*" "$interface"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The /etc/init.d/nlbwmon reload command argument was dropped from this call. The signature is procd_add_interface_trigger <name> <interface> <command...>; without the command the trigger fires but runs nothing, so interface changes never trigger a reload. Restore the action:

Suggested change
procd_add_interface_trigger "interface.*" "$interface"
procd_add_interface_trigger "interface.*" "$interface" /etc/init.d/nlbwmon reload

Generated by Claude Code

fi
}
Comment on lines +185 to 189

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-raising because the thread for this was marked resolved without a corresponding code change: service_triggers() is still deleted here, and it is the only entry point procd has for registering triggers — _procd_close_service() calls it unconditionally between _procd_open_trigger/_procd_close_trigger (procd.sh:98-102). With the function gone, the stub in rc.common runs instead and no triggers are registered, so two behaviours are lost:

  • procd_add_reload_trigger "dhcp" "system" "nlbwmon" — the service no longer reloads on dhcp/system/network config changes.
  • config_foreach add_interface_trigger dhcp — which makes add_interface_trigger() above dead code that is never called.

Restoring the function at the end of the file fixes both:

Suggested change
}
}
service_triggers() {
procd_add_reload_trigger "dhcp" "system" "nlbwmon"
config_load dhcp
config_foreach add_interface_trigger dhcp
}

Generated by Claude Code


service_triggers() {
Comment thread
Mahmudul-009 marked this conversation as resolved.
procd_add_reload_trigger "dhcp" "system" "nlbwmon"

config_load dhcp
config_foreach add_interface_trigger dhcp
}