luci-mod-status: amend luci-bwc.c to use conntrack tool
authorjpfox156 <[email protected]>
Fri, 25 Feb 2022 13:25:58 +0000 (23:25 +1000)
committerPaul Donald <[email protected]>
Thu, 2 Oct 2025 13:42:52 +0000 (15:42 +0200)
Fall through to /usr/sbin/conntrack tool if /proc/net/nf_conntrack or
ip_conntrack is not available.

/proc/net/nf_conntrack has been obsoleted in recent kernels
(https://cateee.net/lkddb/web-lkddb/NF_CONNTRACK_PROCFS.html). This
change enables luci-bwc to collect conntrack information via the
/usr/sbin/conntrack tool (if installed) instead.

Populates the /luci/admin/status/realtime/connections chart.

Signed-off-by: James Fox <[email protected]>
[ format using tabs ]
Signed-off-by: Paul Donald <[email protected]>
modules/luci-mod-status/src/luci-bwc.c

index b7682f9e21c2c946082958116b13762e8349ec78..4eef2d38d1c241bc1eb66db35dbbb62b9706cef6 100644 (file)
@@ -442,8 +442,14 @@ static int run_daemon(void)
        struct dirent *e;
 
        struct stat s;
-       const char *ipc = stat("/proc/net/nf_conntrack", &s)
-               ? "/proc/net/ip_conntrack" : "/proc/net/nf_conntrack";
+       char *ipc;
+       char *ipc_command;
+       if(! stat("/proc/net/nf_conntrack", &s))
+               ipc = "/proc/net/nf_conntrack";
+       else if(! stat("/proc/net/ip_conntrack", &s))
+               ipc = "/proc/net/ip_conntrack";
+       else if(! stat("/usr/sbin/conntrack" , &s))
+               ipc_command = "/usr/sbin/conntrack -L -o extended";
 
        const struct {
                const char *file;
@@ -535,7 +541,8 @@ static int run_daemon(void)
                        closedir(dir);
                }
 
-               if ((info = fopen(ipc, "r")) != NULL)
+               if (((ipc != '\0') && ((info = fopen(ipc, "r")) != NULL)) ||
+                       ((ipc_command != '\0') && ((info=popen(ipc_command, "r")) != NULL)))
                {
                        udp   = 0;
                        tcp   = 0;
@@ -575,7 +582,10 @@ static int run_daemon(void)
                                                          (uint16_t)(lf15 * 100));
                        }
 
-                       fclose(info);
+                       if (ipc != '\0')
+                               fclose(info);
+                       if (ipc_command != '\0')
+                               pclose(info);
                }
 
                sleep(STEP_TIME);