2 * Copyright (c) 2003, Trent Nelson, <trent@arpa.com>.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTIFSTAT_ERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/types.h>
32 #include <sys/socket.h>
33 #include <sys/sysctl.h>
35 #include <net/if_mib.h>
49 #define C1 0 /* 0-19 */
50 #define C2 20 /* 20-39 */
51 #define C3 40 /* 40-59 */
52 #define C4 60 /* 60-80 */
53 #define C5 80 /* Used for label positioning. */
55 static const int col0 = 0;
56 static const int col1 = C1;
57 static const int col2 = C2;
58 static const int col3 = C3;
59 static const int col4 = C4;
60 static const int col5 = C5;
62 SLIST_HEAD(, if_stat) curlist;
63 SLIST_HEAD(, if_stat_disp) displist;
66 SLIST_ENTRY(if_stat) link;
67 char if_name[IF_NAMESIZE];
68 struct ifmibdata if_mib;
70 struct timeval tv_lastchanged;
71 uint64_t if_in_curtraffic;
72 uint64_t if_out_curtraffic;
73 uint64_t if_in_traffic_peak;
74 uint64_t if_out_traffic_peak;
75 uint64_t if_in_curpps;
76 uint64_t if_out_curpps;
77 uint64_t if_in_pps_peak;
78 uint64_t if_out_pps_peak;
79 u_int if_row; /* Index into ifmib sysctl */
80 int if_ypos; /* -1 if not being displayed */
86 extern char *matchline;
90 static int needclear = 0;
92 static void right_align_string(struct if_stat *);
93 static void getifmibdata(const int, struct ifmibdata *);
94 static void sort_interface_list(void);
95 static u_int getifnum(void);
97 #define IFSTAT_ERR(n, s) do { \
105 " Interface Traffic Peak Total"
107 #define STARTING_ROW (TOPLINE + 1)
108 #define ROW_SPACING (3)
110 #define IN_col2 (showpps ? ifp->if_in_curpps : ifp->if_in_curtraffic)
111 #define OUT_col2 (showpps ? ifp->if_out_curpps : ifp->if_out_curtraffic)
112 #define IN_col3 (showpps ? \
113 ifp->if_in_pps_peak : ifp->if_in_traffic_peak)
114 #define OUT_col3 (showpps ? \
115 ifp->if_out_pps_peak : ifp->if_out_traffic_peak)
116 #define IN_col4 (showpps ? \
117 ifp->if_mib.ifmd_data.ifi_ipackets : ifp->if_mib.ifmd_data.ifi_ibytes)
118 #define OUT_col4 (showpps ? \
119 ifp->if_mib.ifmd_data.ifi_opackets : ifp->if_mib.ifmd_data.ifi_obytes)
121 #define EMPTY_COLUMN " "
122 #define CLEAR_COLUMN(y, x) mvprintw((y), (x), "%20s", EMPTY_COLUMN);
124 #define DOPUTRATE(c, r, d) do { \
125 CLEAR_COLUMN(r, c); \
127 mvprintw(r, (c), "%10.3f %cp%s ", \
128 convert(d##_##c, curscale), \
129 *get_string(d##_##c, curscale), \
133 mvprintw(r, (c), "%10.3f %s%s ", \
134 convert(d##_##c, curscale), \
135 get_string(d##_##c, curscale), \
140 #define DOPUTTOTAL(c, r, d) do { \
141 CLEAR_COLUMN((r), (c)); \
143 mvprintw((r), (c), "%12.3f %cp ", \
144 convert(d##_##c, SC_AUTO), \
145 *get_string(d##_##c, SC_AUTO)); \
148 mvprintw((r), (c), "%12.3f %s ", \
149 convert(d##_##c, SC_AUTO), \
150 get_string(d##_##c, SC_AUTO)); \
154 #define PUTRATE(c, r) do { \
155 DOPUTRATE(c, (r), IN); \
156 DOPUTRATE(c, (r)+1, OUT); \
159 #define PUTTOTAL(c, r) do { \
160 DOPUTTOTAL(c, (r), IN); \
161 DOPUTTOTAL(c, (r)+1, OUT); \
164 #define PUTNAME(p) do { \
165 mvprintw(p->if_ypos, 0, "%s", p->if_name); \
166 mvprintw(p->if_ypos, col2-3, "%s", (const char *)"in"); \
167 mvprintw(p->if_ypos+1, col2-3, "%s", (const char *)"out"); \
173 return (subwin(stdscr, LINES-3-1, 0, MAINWIN_ROW, 0));
177 closeifstat(WINDOW *w)
179 struct if_stat *node = NULL;
181 while (!SLIST_EMPTY(&curlist)) {
182 node = SLIST_FIRST(&curlist);
183 SLIST_REMOVE_HEAD(&curlist, link);
200 wmove(wnd, TOPLINE, 0);
202 mvprintw(TOPLINE, 0, "%s", TOPLABEL);
210 struct if_stat *ifp = NULL;
212 SLIST_FOREACH(ifp, &curlist, link) {
213 if (ifp->if_ypos < LINES - 3 && ifp->if_ypos != -1)
214 if (ifp->display == 0 || ifp->match == 0) {
215 wmove(wnd, ifp->if_ypos, 0);
217 wmove(wnd, ifp->if_ypos + 1, 0);
222 PUTRATE(col2, ifp->if_ypos);
223 PUTRATE(col3, ifp->if_ypos);
224 PUTTOTAL(col4, ifp->if_ypos);
234 struct if_stat *p = NULL;
241 SLIST_INIT(&curlist);
243 for (i = 0; i < n; i++) {
244 p = (struct if_stat *)calloc(1, sizeof(struct if_stat));
246 IFSTAT_ERR(1, "out of memory");
247 SLIST_INSERT_HEAD(&curlist, p, link);
249 getifmibdata(p->if_row, &p->if_mib);
250 right_align_string(p);
254 * Initially, we only display interfaces that have
255 * received some traffic.
257 if (p->if_mib.ifmd_data.ifi_ibytes != 0)
261 sort_interface_list();
269 struct if_stat *ifp = NULL;
270 struct timeval tv, new_tv, old_tv;
271 double elapsed = 0.0;
272 uint64_t new_inb, new_outb, old_inb, old_outb = 0;
273 uint64_t new_inp, new_outp, old_inp, old_outp = 0;
275 SLIST_FOREACH(ifp, &curlist, link) {
277 * Grab a copy of the old input/output values before we
278 * call getifmibdata().
280 old_inb = ifp->if_mib.ifmd_data.ifi_ibytes;
281 old_outb = ifp->if_mib.ifmd_data.ifi_obytes;
282 old_inp = ifp->if_mib.ifmd_data.ifi_ipackets;
283 old_outp = ifp->if_mib.ifmd_data.ifi_opackets;
284 ifp->tv_lastchanged = ifp->if_mib.ifmd_data.ifi_lastchange;
286 (void)gettimeofday(&new_tv, NULL);
287 (void)getifmibdata(ifp->if_row, &ifp->if_mib);
289 new_inb = ifp->if_mib.ifmd_data.ifi_ibytes;
290 new_outb = ifp->if_mib.ifmd_data.ifi_obytes;
291 new_inp = ifp->if_mib.ifmd_data.ifi_ipackets;
292 new_outp = ifp->if_mib.ifmd_data.ifi_opackets;
294 /* Display interface if it's received some traffic. */
295 if (new_inb > 0 && old_inb == 0) {
301 * The rest is pretty trivial. Calculate the new values
302 * for our current traffic rates, and while we're there,
303 * see if we have new peak rates.
306 timersub(&new_tv, &old_tv, &tv);
307 elapsed = tv.tv_sec + (tv.tv_usec * 1e-6);
309 ifp->if_in_curtraffic = new_inb - old_inb;
310 ifp->if_out_curtraffic = new_outb - old_outb;
312 ifp->if_in_curpps = new_inp - old_inp;
313 ifp->if_out_curpps = new_outp - old_outp;
316 * Rather than divide by the time specified on the comm-
317 * and line, we divide by ``elapsed'' as this is likely
318 * to be more accurate.
320 ifp->if_in_curtraffic /= elapsed;
321 ifp->if_out_curtraffic /= elapsed;
322 ifp->if_in_curpps /= elapsed;
323 ifp->if_out_curpps /= elapsed;
325 if (ifp->if_in_curtraffic > ifp->if_in_traffic_peak)
326 ifp->if_in_traffic_peak = ifp->if_in_curtraffic;
328 if (ifp->if_out_curtraffic > ifp->if_out_traffic_peak)
329 ifp->if_out_traffic_peak = ifp->if_out_curtraffic;
331 if (ifp->if_in_curpps > ifp->if_in_pps_peak)
332 ifp->if_in_pps_peak = ifp->if_in_curpps;
334 if (ifp->if_out_curpps > ifp->if_out_pps_peak)
335 ifp->if_out_pps_peak = ifp->if_out_curpps;
337 ifp->tv.tv_sec = new_tv.tv_sec;
338 ifp->tv.tv_usec = new_tv.tv_usec;
343 sort_interface_list();
349 * We want to right justify our interface names against the first column
350 * (first sixteen or so characters), so we need to do some alignment.
353 right_align_string(struct if_stat *ifp)
355 int str_len = 0, pad_len = 0;
356 char *newstr = NULL, *ptr = NULL;
358 if (ifp == NULL || ifp->if_mib.ifmd_name == NULL)
361 /* string length + '\0' */
362 str_len = strlen(ifp->if_mib.ifmd_name)+1;
363 pad_len = IF_NAMESIZE-(str_len);
365 newstr = ifp->if_name;
366 ptr = newstr + pad_len;
367 (void)memset((void *)newstr, (int)' ', IF_NAMESIZE);
368 (void)strncpy(ptr, (const char *)&ifp->if_mib.ifmd_name,
376 check_match(const char *ifname)
378 char *p = matchline, *c, t;
381 if (matchline == NULL)
384 /* Strip leading whitespaces */
389 while ((mlen = strcspn(c, " ;,")) != 0) {
394 if (fnmatch(c, ifname, FNM_CASEFOLD) == 0) {
399 c = p + strspn(p, " ;,");
402 c = p + strspn(p, " ;,");
410 * This function iterates through our list of interfaces, identifying
411 * those that are to be displayed (ifp->display = 1). For each interf-
412 * rface that we're displaying, we generate an appropriate position for
413 * it on the screen (ifp->if_ypos).
415 * This function is called any time a change is made to an interface's
419 sort_interface_list(void)
421 struct if_stat *ifp = NULL;
425 SLIST_FOREACH(ifp, &curlist, link) {
426 if (matchline && !check_match(ifp->if_mib.ifmd_name))
430 if (ifp->display && ifp->match) {
448 static int name[] = { CTL_NET,
454 datalen = sizeof(data);
455 if (sysctl(name, 5, (void *)&data, (size_t *)&datalen, (void *)NULL,
457 IFSTAT_ERR(1, "sysctl error");
462 getifmibdata(int row, struct ifmibdata *data)
465 static int name[] = { CTL_NET,
471 datalen = sizeof(*data);
474 if ((sysctl(name, 6, (void *)data, (size_t *)&datalen, (void *)NULL,
475 (size_t)0) != 0) && (errno != ENOENT))
476 IFSTAT_ERR(2, "sysctl error getting interface data");
480 cmdifstat(const char *cmd, const char *args)
484 retval = ifcmd(cmd, args);
485 /* ifcmd() returns 1 on success */