modules/admin-full: track non-tcp or udp connections, use /proc/net/nf_conntrack...
authorJo-Philipp Wich <jow@openwrt.org>
Mon, 29 Nov 2010 14:14:17 +0000 (14:14 +0000)
committerJo-Philipp Wich <jow@openwrt.org>
Mon, 29 Nov 2010 14:14:17 +0000 (14:14 +0000)
modules/admin-full/htdocs/luci-static/resources/connections.svg
modules/admin-full/luasrc/view/admin_status/connections.htm
modules/admin-full/src/luci-bwc.c

index 1db9d6314962aa96c4daaf3b5288eddbf081c80b..816f7e6a751bed1485d37270125b6ac2564003bc 100644 (file)
@@ -11,6 +11,7 @@
        <line x1="0" y1="75%" x2="100%" y2="75%" style="stroke:black;stroke-width:0.1" />
        <text id="label_25" x="20" y="74%" style="fill:#999999; font-size:9pt"> </text>
 
-       <polyline id="udp" points="" style="fill:blue;fill-opacity:0.4;stroke:blue;stroke-width:1" />
        <polyline id="tcp" points="" style="fill:green;fill-opacity:0.4;stroke:green;stroke-width:1" />
+       <polyline id="udp" points="" style="fill:blue;fill-opacity:0.4;stroke:blue;stroke-width:1" />
+       <polyline id="other" points="" style="fill:red;fill-opacity:0.4;stroke:red;stroke-width:1" />
 </svg>
index 991eb676106861b8c136574db4f4eb0003c59d98..eb483985278aaad2913364aadd9d9278c7a7c928 100644 (file)
@@ -19,9 +19,10 @@ $Id$
        var bwxhr = new XHR();
 
        var G;
-       var TIME = 0;
-       var UDP  = 1;
-       var TCP  = 2;
+       var TIME  = 0;
+       var UDP   = 1;
+       var TCP   = 2;
+       var OTHER = 3;
 
        var width  = 760;
        var height = 300;
@@ -33,6 +34,7 @@ $Id$
 
        var data_udp = [ ];
        var data_tcp = [ ];
+       var data_otr = [ ];
 
        var line_udp;
        var line_tcp;
@@ -41,13 +43,17 @@ $Id$
        var label_50;
        var label_75;
 
-       var label_rx_cur;
-       var label_rx_avg;
-       var label_rx_peak;
+       var label_udp_cur;
+       var label_udp_avg;
+       var label_udp_peak;
 
-       var label_tx_cur;
-       var label_tx_avg;
-       var label_tx_peak;
+       var label_tcp_cur;
+       var label_tcp_avg;
+       var label_tcp_peak;
+
+       var label_otr_cur;
+       var label_otr_avg;
+       var label_otr_peak;
 
        var label_scale;
 
@@ -62,9 +68,11 @@ $Id$
 
                                var data_udp_avg = 0;
                                var data_tcp_avg = 0;
+                               var data_otr_avg = 0;
 
                                var data_udp_peak = 0;
                                var data_tcp_peak = 0;
+                               var data_otr_peak = 0;
 
                                for (var i = data_stamp ? 0 : 1; i < data.length; i++)
                                {
@@ -74,30 +82,36 @@ $Id$
 
                                        data_udp.push(data[i][UDP]);
                                        data_tcp.push(data[i][TCP]);
+                                       data_otr.push(data[i][OTHER]);
                                }
 
                                /* cut off outdated entries */
                                data_udp = data_udp.slice(data_udp.length - data_wanted, data_udp.length);
                                data_tcp = data_tcp.slice(data_tcp.length - data_wanted, data_tcp.length);
+                               data_otr = data_otr.slice(data_otr.length - data_wanted, data_otr.length);
 
                                /* find peak */
                                for (var i = 0; i < data_udp.length; i++)
                                {
                                        data_max = Math.max(data_max, data_udp[i]);
                                        data_max = Math.max(data_max, data_tcp[i]);
+                                       data_max = Math.max(data_max, data_otr[i]);
 
                                        data_udp_peak = Math.max(data_udp_peak, data_udp[i]);
                                        data_tcp_peak = Math.max(data_tcp_peak, data_tcp[i]);
+                                       data_otr_peak = Math.max(data_otr_peak, data_otr[i]);
 
                                        if (i > 0)
                                        {
                                                data_udp_avg = (data_udp_avg + data_udp[i]) / 2;
                                                data_tcp_avg = (data_tcp_avg + data_tcp[i]) / 2;
+                                               data_otr_avg = (data_otr_avg + data_otr[i]) / 2;
                                        }
                                        else
                                        {
                                                data_udp_avg = data_udp[i];
                                                data_tcp_avg = data_tcp[i];
+                                               data_otr_avg = data_otr[i];
                                        }
                                }
 
@@ -107,42 +121,51 @@ $Id$
 
 
                                /* plot data */
-                               var pt_rx = '0,' + height;
-                               var pt_tx = '0,' + height;
+                               var pt_udp = '0,' + height;
+                               var pt_tcp = '0,' + height;
+                               var pt_otr = '0,' + height;
 
-                               var y_rx = 0;
-                               var y_tx = 0;
+                               var y_udp = 0;
+                               var y_tcp = 0;
+                               var y_otr = 0;
 
                                for (var i = 0; i < data_udp.length; i++)
                                {
                                        var x = i * step;
 
-                                       y_rx = height - Math.floor(data_udp[i] * data_scale);
-                                       y_tx = height - Math.floor(data_tcp[i] * data_scale);
+                                       y_udp = height - Math.floor(data_udp[i] * data_scale);
+                                       y_tcp = height - Math.floor(data_tcp[i] * data_scale);
+                                       y_otr = height - Math.floor(data_otr[i] * data_scale);
 
-                                       pt_rx += ' ' + x + ',' + y_rx;
-                                       pt_tx += ' ' + x + ',' + y_tx;
+                                       pt_udp += ' ' + x + ',' + y_udp;
+                                       pt_tcp += ' ' + x + ',' + y_tcp;
+                                       pt_otr += ' ' + x + ',' + y_otr;
                                }
 
-                               pt_rx += ' ' + width + ',' + y_rx + ' ' + width + ',' + height;
-                               pt_tx += ' ' + width + ',' + y_tx + ' ' + width + ',' + height;
+                               pt_udp += ' ' + width + ',' + y_udp + ' ' + width + ',' + height;
+                               pt_tcp += ' ' + width + ',' + y_tcp + ' ' + width + ',' + height;
+                               pt_otr += ' ' + width + ',' + y_otr + ' ' + width + ',' + height;
 
 
-                               line_udp.setAttribute('points', pt_rx);
-                               line_tcp.setAttribute('points', pt_tx);
+                               line_udp.setAttribute('points', pt_udp);
+                               line_tcp.setAttribute('points', pt_tcp);
+                               line_otr.setAttribute('points', pt_otr);
 
                                label_25.firstChild.data = Math.floor(1.1 * 0.25 * data_max);
                                label_50.firstChild.data = Math.floor(1.1 * 0.50 * data_max);
                                label_75.firstChild.data = Math.floor(1.1 * 0.75 * data_max);
 
-                               label_rx_cur.innerHTML = Math.floor(data_udp[data_udp.length-1]);
-                               label_tx_cur.innerHTML = Math.floor(data_tcp[data_tcp.length-1]);
+                               label_udp_cur.innerHTML = Math.floor(data_udp[data_udp.length-1]);
+                               label_tcp_cur.innerHTML = Math.floor(data_tcp[data_tcp.length-1]);
+                               label_otr_cur.innerHTML = Math.floor(data_otr[data_otr.length-1]);
 
-                               label_rx_avg.innerHTML = Math.floor(data_udp_avg);
-                               label_tx_avg.innerHTML = Math.floor(data_tcp_avg);
+                               label_udp_avg.innerHTML = Math.floor(data_udp_avg);
+                               label_tcp_avg.innerHTML = Math.floor(data_tcp_avg);
+                               label_otr_avg.innerHTML = Math.floor(data_otr_avg);
 
-                               label_rx_peak.innerHTML = Math.floor(data_udp_peak);
-                               label_tx_peak.innerHTML = Math.floor(data_tcp_peak);
+                               label_udp_peak.innerHTML = Math.floor(data_udp_peak);
+                               label_tcp_peak.innerHTML = Math.floor(data_tcp_peak);
+                               label_otr_peak.innerHTML = Math.floor(data_otr_peak);
 
                                /* reset timer */
                                window.setTimeout(update_graph, 1000);
@@ -179,23 +202,29 @@ $Id$
                                {
                                        data_udp[i] = 0;
                                        data_tcp[i] = 0;
+                                       data_otr[i] = 0;
                                }
 
                                /* find svg elements */
                                line_udp = G.getElementById('udp');
                                line_tcp = G.getElementById('tcp');
+                               line_otr = G.getElementById('other');
 
                                label_25 = G.getElementById('label_25');
                                label_50 = G.getElementById('label_50');
                                label_75 = G.getElementById('label_75');
 
-                               label_rx_cur  = document.getElementById('lb_udp_cur');
-                               label_rx_avg  = document.getElementById('lb_udp_avg');
-                               label_rx_peak = document.getElementById('lb_udp_peak');
+                               label_udp_cur  = document.getElementById('lb_udp_cur');
+                               label_udp_avg  = document.getElementById('lb_udp_avg');
+                               label_udp_peak = document.getElementById('lb_udp_peak');
+
+                               label_tcp_cur  = document.getElementById('lb_tcp_cur');
+                               label_tcp_avg  = document.getElementById('lb_tcp_avg');
+                               label_tcp_peak = document.getElementById('lb_tcp_peak');
 
-                               label_tx_cur  = document.getElementById('lb_tcp_cur');
-                               label_tx_avg  = document.getElementById('lb_tcp_avg');
-                               label_tx_peak = document.getElementById('lb_tcp_peak');
+                               label_otr_cur  = document.getElementById('lb_otr_cur');
+                               label_otr_avg  = document.getElementById('lb_otr_avg');
+                               label_otr_peak = document.getElementById('lb_otr_peak');
 
                                label_scale   = document.getElementById('scale');
 
@@ -256,6 +285,16 @@ $Id$
                <td style="text-align:right; vertical-align:top"><strong><%:Peak:%></strong></td>
                <td id="lb_tcp_peak">0</td>
        </tr>
+       <tr>
+               <td style="text-align:right; vertical-align:top"><strong style="border-bottom:2px solid red"><%:Other:%></strong></td>
+               <td id="lb_otr_cur">0</td>
+
+               <td style="text-align:right; vertical-align:top"><strong><%:Average:%></strong></td>
+               <td id="lb_otr_avg">0</td>
+
+               <td style="text-align:right; vertical-align:top"><strong><%:Peak:%></strong></td>
+               <td id="lb_otr_peak">0</td>
+       </tr>
 </table>
 
 <%+footer%>
index d0f0e77144cacf788a12bbd4929dab2fa00a12ce..9f1b420adfb1a23ef966540fa73e057cdb3add1e 100644 (file)
@@ -66,6 +66,7 @@ struct conn_entry {
        uint64_t time;
        uint32_t udp;
        uint32_t tcp;
+       uint32_t other;
 };
 
 struct load_entry {
@@ -220,7 +221,7 @@ static int update_ifstat(
        return update_file(path, &e, sizeof(struct traffic_entry));
 }
 
-static int update_cnstat(uint32_t udp, uint32_t tcp)
+static int update_cnstat(uint32_t udp, uint32_t tcp, uint32_t other)
 {
        char path[1024];
 
@@ -240,9 +241,10 @@ static int update_cnstat(uint32_t udp, uint32_t tcp)
                }
        }
 
-       e.time = htonll(time(NULL));
-       e.udp  = htonl(udp);
-       e.tcp  = htonl(tcp);
+       e.time  = htonll(time(NULL));
+       e.udp   = htonl(udp);
+       e.tcp   = htonl(tcp);
+       e.other = htonl(other);
 
        return update_file(path, &e, sizeof(struct conn_entry));
 }
@@ -279,11 +281,14 @@ static int run_daemon(int nofork)
 {
        FILE *info;
        uint64_t rxb, txb, rxp, txp;
-       uint32_t udp, tcp;
+       uint32_t udp, tcp, other;
        float lf1, lf5, lf15;
        char line[1024];
        char ifname[16];
 
+       struct stat s;
+       const char *ipc = stat("/proc/net/nf_conntrack", &s)
+               ? "/proc/net/ip_conntrack" : "/proc/net/nf_conntrack";
 
        if (!nofork)
        {
@@ -331,29 +336,26 @@ static int run_daemon(int nofork)
                        fclose(info);
                }
 
-               if ((info = fopen("/proc/net/ip_conntrack", "r")) != NULL)
+               if ((info = fopen(ipc, "r")) != NULL)
                {
-                       udp = 0;
-                       tcp = 0;
+                       udp   = 0;
+                       tcp   = 0;
+                       other = 0;
 
                        while (fgets(line, sizeof(line), info))
                        {
-                               switch (line[0])
+                               if (sscanf(line, "%*s %*d %s", ifname) || sscanf(line, "%s %*d", ifname))
                                {
-                                       case 't':
+                                       if (!strcmp(ifname, "tcp"))
                                                tcp++;
-                                               break;
-
-                                       case 'u':
+                                       else if (!strcmp(ifname, "udp"))
                                                udp++;
-                                               break;
-
-                                       default:
-                                               break;
+                                       else
+                                               other++;
                                }
                        }
 
-                       update_cnstat(udp, tcp);
+                       update_cnstat(udp, tcp, other);
 
                        fclose(info);
                }
@@ -431,8 +433,9 @@ static int run_dump_conns(void)
                if (!e->time)
                        continue;
 
-               printf("[ %" PRIu64 ", %u, %u ]%s\n",
-                       ntohll(e->time), ntohl(e->udp), ntohl(e->tcp),
+               printf("[ %" PRIu64 ", %u, %u, %u ]%s\n",
+                       ntohll(e->time), ntohl(e->udp),
+                       ntohl(e->tcp), ntohl(e->other),
                        ((i + sizeof(struct conn_entry)) < m.size) ? "," : "");
        }