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>
47 #define C1 0 /* 0-19 */
48 #define C2 20 /* 20-39 */
49 #define C3 40 /* 40-59 */
50 #define C4 60 /* 60-80 */
51 #define C5 80 /* Used for label positioning. */
53 static const int col0 = 0;
54 static const int col1 = C1;
55 static const int col2 = C2;
56 static const int col3 = C3;
57 static const int col4 = C4;
58 static const int col5 = C5;
61 SLIST_HEAD(, if_stat) curlist;
62 SLIST_HEAD(, if_stat_disp) displist;
65 SLIST_ENTRY(if_stat) link;
66 char if_name[IF_NAMESIZE];
67 struct ifmibdata if_mib;
69 struct timeval tv_lastchanged;
70 u_long if_in_curtraffic;
71 u_long if_out_curtraffic;
72 u_long if_in_traffic_peak;
73 u_long if_out_traffic_peak;
74 u_int if_row; /* Index into ifmib sysctl */
75 u_int if_ypos; /* 0 if not being displayed */
79 extern u_int curscale;
81 static void right_align_string(struct if_stat *);
82 static void getifmibdata(const int, struct ifmibdata *);
83 static void sort_interface_list(void);
84 static u_int getifnum(void);
86 #define IFSTAT_ERR(n, s) do { \
94 " Interface Traffic Peak Total"
96 #define STARTING_ROW (TOPLINE + 1)
97 #define ROW_SPACING (3)
99 #define CLEAR_LINE(y, x) do { \
104 #define IN_col2 (ifp->if_in_curtraffic)
105 #define OUT_col2 (ifp->if_out_curtraffic)
106 #define IN_col3 (ifp->if_in_traffic_peak)
107 #define OUT_col3 (ifp->if_out_traffic_peak)
108 #define IN_col4 (ifp->if_mib.ifmd_data.ifi_ibytes)
109 #define OUT_col4 (ifp->if_mib.ifmd_data.ifi_obytes)
111 #define EMPTY_COLUMN " "
112 #define CLEAR_COLUMN(y, x) mvprintw((y), (x), "%20s", EMPTY_COLUMN);
114 #define DOPUTRATE(c, r, d) do { \
115 CLEAR_COLUMN(r, c); \
116 mvprintw(r, (c), "%10.3f %s%s ", \
117 convert(d##_##c, curscale), \
118 get_string(d##_##c, curscale), \
122 #define DOPUTTOTAL(c, r, d) do { \
123 CLEAR_COLUMN((r), (c)); \
124 mvprintw((r), (c), "%12.3f %s ", \
125 convert(d##_##c, SC_AUTO), \
126 get_string(d##_##c, SC_AUTO)); \
129 #define PUTRATE(c, r) do { \
130 DOPUTRATE(c, (r), IN); \
131 DOPUTRATE(c, (r)+1, OUT); \
134 #define PUTTOTAL(c, r) do { \
135 DOPUTTOTAL(c, (r), IN); \
136 DOPUTTOTAL(c, (r)+1, OUT); \
139 #define PUTNAME(p) do { \
140 mvprintw(p->if_ypos, 0, "%s", p->if_name); \
141 mvprintw(p->if_ypos, col2-3, "%s", (const char *)"in"); \
142 mvprintw(p->if_ypos+1, col2-3, "%s", (const char *)"out"); \
149 return (subwin(stdscr, LINES-3-1, 0, MAINWIN_ROW, 0));
153 closeifstat(WINDOW *w)
155 struct if_stat *node = NULL;
157 while (!SLIST_EMPTY(&curlist)) {
158 node = SLIST_FIRST(&curlist);
159 SLIST_REMOVE_HEAD(&curlist, link);
177 wmove(wnd, TOPLINE, 0);
179 mvprintw(TOPLINE, 0, "%s", TOPLABEL);
187 struct if_stat *ifp = NULL;
188 SLIST_FOREACH(ifp, &curlist, link) {
189 if (ifp->display == 0)
192 PUTRATE(col2, ifp->if_ypos);
193 PUTRATE(col3, ifp->if_ypos);
194 PUTTOTAL(col4, ifp->if_ypos);
203 struct if_stat *p = NULL;
210 SLIST_INIT(&curlist);
212 for (i = 0; i < n; i++) {
213 p = (struct if_stat *)malloc(sizeof(struct if_stat));
215 IFSTAT_ERR(1, "out of memory");
216 memset((void *)p, 0, sizeof(struct if_stat));
217 SLIST_INSERT_HEAD(&curlist, p, link);
219 getifmibdata(p->if_row, &p->if_mib);
220 right_align_string(p);
223 * Initially, we only display interfaces that have
224 * received some traffic.
226 if (p->if_mib.ifmd_data.ifi_ibytes != 0)
230 sort_interface_list();
238 struct if_stat *ifp = NULL;
239 struct timeval tv, new_tv, old_tv;
240 double elapsed = 0.0;
241 u_int new_inb, new_outb, old_inb, old_outb = 0;
242 u_int we_need_to_sort_interface_list = 0;
244 SLIST_FOREACH(ifp, &curlist, link) {
246 * Grab a copy of the old input/output values before we
247 * call getifmibdata().
249 old_inb = ifp->if_mib.ifmd_data.ifi_ibytes;
250 old_outb = ifp->if_mib.ifmd_data.ifi_obytes;
251 ifp->tv_lastchanged = ifp->if_mib.ifmd_data.ifi_lastchange;
253 if (gettimeofday(&new_tv, (struct timezone *)0) != 0)
254 IFSTAT_ERR(2, "error getting time of day");
255 (void)getifmibdata(ifp->if_row, &ifp->if_mib);
258 new_inb = ifp->if_mib.ifmd_data.ifi_ibytes;
259 new_outb = ifp->if_mib.ifmd_data.ifi_obytes;
261 /* Display interface if it's received some traffic. */
262 if (new_inb > 0 && old_inb == 0) {
264 we_need_to_sort_interface_list++;
268 * The rest is pretty trivial. Calculate the new values
269 * for our current traffic rates, and while we're there,
270 * see if we have new peak rates.
273 timersub(&new_tv, &old_tv, &tv);
274 elapsed = tv.tv_sec + (tv.tv_usec * 1e-6);
276 ifp->if_in_curtraffic = new_inb - old_inb;
277 ifp->if_out_curtraffic = new_outb - old_outb;
280 * Rather than divide by the time specified on the comm-
281 * and line, we divide by ``elapsed'' as this is likely
282 * to be more accurate.
284 ifp->if_in_curtraffic /= elapsed;
285 ifp->if_out_curtraffic /= elapsed;
287 if (ifp->if_in_curtraffic > ifp->if_in_traffic_peak)
288 ifp->if_in_traffic_peak = ifp->if_in_curtraffic;
290 if (ifp->if_out_curtraffic > ifp->if_out_traffic_peak)
291 ifp->if_out_traffic_peak = ifp->if_out_curtraffic;
293 ifp->tv.tv_sec = new_tv.tv_sec;
294 ifp->tv.tv_usec = new_tv.tv_usec;
298 if (we_need_to_sort_interface_list)
299 sort_interface_list();
305 * We want to right justify our interface names against the first column
306 * (first sixteen or so characters), so we need to do some alignment.
309 right_align_string(struct if_stat *ifp)
311 int str_len = 0, pad_len = 0;
312 char *newstr = NULL, *ptr = NULL;
314 if (ifp == NULL || ifp->if_mib.ifmd_name == NULL)
317 /* string length + '\0' */
318 str_len = strlen(ifp->if_mib.ifmd_name)+1;
319 pad_len = IF_NAMESIZE-(str_len);
321 newstr = ifp->if_name;
322 ptr = newstr + pad_len;
323 (void)memset((void *)newstr, (int)' ', IF_NAMESIZE);
324 (void)strncpy(ptr, (const char *)&ifp->if_mib.ifmd_name,
332 * This function iterates through our list of interfaces, identifying
333 * those that are to be displayed (ifp->display = 1). For each interf-
334 * rface that we're displaying, we generate an appropriate position for
335 * it on the screen (ifp->if_ypos).
337 * This function is called any time a change is made to an interface's
341 sort_interface_list(void)
343 struct if_stat *ifp = NULL;
347 SLIST_FOREACH(ifp, &curlist, link) {
361 static int name[] = { CTL_NET,
367 datalen = sizeof(data);
368 if (sysctl(name, 5, (void *)&data, (size_t *)&datalen, (void *)NULL,
370 IFSTAT_ERR(1, "sysctl error");
375 getifmibdata(int row, struct ifmibdata *data)
378 static int name[] = { CTL_NET,
384 datalen = sizeof(*data);
387 if (sysctl(name, 6, (void *)data, (size_t *)&datalen, (void *)NULL,
389 IFSTAT_ERR(2, "sysctl error getting interface data");
393 cmdifstat(const char *cmd, const char *args)
397 retval = ifcmd(cmd, args);
398 /* ifcmd() returns 1 on success */