[BACK]Return to if.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / systat

Diff for /src/usr.bin/systat/if.c between version 1.11 and 1.12

version 1.11, 2007/09/05 20:31:34 version 1.12, 2008/06/12 22:26:01
Line 26 
Line 26 
 #include <string.h>  #include <string.h>
   
 #include "systat.h"  #include "systat.h"
 #include "extern.h"  
   
 static  enum state { BOOT, TIME, RUN } state = TIME;  static  enum state { BOOT, TIME, RUN } state = TIME;
   
Line 50 
Line 49 
 } *ifstats;  } *ifstats;
   
 static  int nifs = 0;  static  int nifs = 0;
   static int num_ifs = 0;
   
 const char      *showlinkstate(int);  void print_if(void);
   int read_if(void);
   int select_if(void);
   int if_keyboard_callback(int);
   
 WINDOW *  static void fetchifstat(void);
 openifstat(void)  static void showifstat(struct ifstat *);
 {  static void showtotal(void);
   
         return (subwin(stdscr, LINES-1-1, 0, 1, 0));  
 }  
   
 void  /* Define fields */
 closeifstat(WINDOW *w)  field_def fields_if[] = {
 {          {"IFACE", 8, 16, 1, FLD_ALIGN_LEFT, -1, 0, 0, 0},
           {"STATE", 10, 16, 1, FLD_ALIGN_LEFT, -1, 0, 0, 0},
           {"IPKTS", 5, 8, 1, FLD_ALIGN_RIGHT, -1, 0, 0, 0},
           {"IBYTES", 5, 8, 1, FLD_ALIGN_RIGHT, -1, 0, 0, 0},
           {"IERRS", 5, 8, 1, FLD_ALIGN_RIGHT, -1, 0, 0, 0},
           {"OPKTS", 5, 8, 1, FLD_ALIGN_RIGHT, -1, 0, 0, 0},
           {"OBYTES", 5, 8, 1, FLD_ALIGN_RIGHT, -1, 0, 0, 0},
           {"OERRS", 5, 8, 1, FLD_ALIGN_RIGHT, -1, 0, 0, 0},
           {"COLLS", 5, 8, 1, FLD_ALIGN_RIGHT, -1, 0, 0, 0},
   };
   
         if (w == NULL)  
                 return;  
         wclear(w);  
         wrefresh(w);  
         delwin(w);  
 }  
   
   #define FIELD_ADDR(x) (&fields_if[x])
   
   #define FLD_IF_IFACE    FIELD_ADDR(0)
   #define FLD_IF_STATE    FIELD_ADDR(1)
   #define FLD_IF_IPKTS    FIELD_ADDR(2)
   #define FLD_IF_IBYTES   FIELD_ADDR(3)
   #define FLD_IF_IERRS    FIELD_ADDR(4)
   #define FLD_IF_OPKTS    FIELD_ADDR(5)
   #define FLD_IF_OBYTES   FIELD_ADDR(6)
   #define FLD_IF_OERRS    FIELD_ADDR(7)
   #define FLD_IF_COLLS    FIELD_ADDR(8)
   
   
   /* Define views */
   field_def *view_if_0[] = {
           FLD_IF_IFACE, FLD_IF_STATE, FLD_IF_IPKTS, FLD_IF_IBYTES,
           FLD_IF_IERRS, FLD_IF_OPKTS, FLD_IF_OBYTES, FLD_IF_OERRS,
           FLD_IF_COLLS, NULL
   };
   
   /* Define view managers */
   
   struct view_manager ifstat_mgr = {
           "Ifstat", select_if, read_if, NULL, print_header,
           print_if, if_keyboard_callback, NULL, NULL
   };
   
   field_view views_if[] = {
           {view_if_0, "ifstat", '1', &ifstat_mgr},
           {NULL, NULL, 0, NULL}
   };
   
   
 int  int
 initifstat(void)  initifstat(void)
 {  {
           field_view *v;
           read_if();
           for (v = views_if; v->name != NULL; v++)
                   add_view(v);
   
         fetchifstat();  
         return(1);          return(1);
 }  }
   
Line 105 
Line 145 
         }          }
 }  }
   
   
   
   int
   select_if(void)
   {
           num_disp = num_ifs + 1;
           return (0);
   }
   
   int
   read_if(void)
   {
           fetchifstat();
           num_disp = num_ifs + 1;
   
           return 0;
   }
   
 void  void
   print_if(void)
   {
           int n, i, count = 0;
   
           for (n = 0, i = 0; n < nifs; n++) {
                   if (ifstats[n].ifs_name[0] == '\0')
                           continue;
                   if (i++ < dispstart)
                           continue;
                   if (i == num_disp)
                           break;
                   showifstat(ifstats + n);
                   if (maxprint > 0 && ++count >= maxprint)
                           return;
           }
           showtotal();
   }
   
   
   static void
 fetchifstat(void)  fetchifstat(void)
 {  {
         struct ifstat *newstats, *ifs;          struct ifstat *newstats, *ifs;
Line 133 
Line 211 
         }          }
   
         bzero(&sum, sizeof(sum));          bzero(&sum, sizeof(sum));
           num_ifs = 0;
   
         lim = buf + need;          lim = buf + need;
         for (next = buf; next < lim; next += ifm.ifm_msglen) {          for (next = buf; next < lim; next += ifm.ifm_msglen) {
Line 141 
Line 220 
                    !(ifm.ifm_addrs & RTA_IFP))                     !(ifm.ifm_addrs & RTA_IFP))
                         continue;                          continue;
                 if (ifm.ifm_index >= nifs) {                  if (ifm.ifm_index >= nifs) {
                         if ((newstats = realloc(ifstats, (ifm.ifm_index + 4) *                          if ((newstats = realloc(ifstats, (ifm.ifm_index + 4)
                             sizeof(struct ifstat))) == NULL)                              * sizeof(struct ifstat))) == NULL)
                                 continue;                                  continue;
                         ifstats = newstats;                          ifstats = newstats;
                         for (; nifs < ifm.ifm_index + 4; nifs++)                          for (; nifs < ifm.ifm_index + 4; nifs++)
                                 ifstats[nifs].ifs_name[0] = '\0';                                  bzero(&ifstats[nifs], sizeof(*ifstats));
                 }                  }
                 ifs = &ifstats[ifm.ifm_index];                  ifs = &ifstats[ifm.ifm_index];
                 if (ifs->ifs_name[0] == '\0') {                  if (ifs->ifs_name[0] == '\0') {
Line 165 
Line 244 
                         if (ifs->ifs_name[0] == '\0')                          if (ifs->ifs_name[0] == '\0')
                                 continue;                                  continue;
                 }                  }
                   num_ifs++;
                 UPDATE(ifc_ip, ifm_data.ifi_ipackets);                  UPDATE(ifc_ip, ifm_data.ifi_ipackets);
                 UPDATE(ifc_ib, ifm_data.ifi_ibytes);                  UPDATE(ifc_ib, ifm_data.ifi_ibytes);
                 UPDATE(ifc_ie, ifm_data.ifi_ierrors);                  UPDATE(ifc_ie, ifm_data.ifi_ierrors);
Line 178 
Line 258 
         free(buf);          free(buf);
 }  }
   
 #define INSET 0  
   
 void  static void
 labelifstat(void)  showifstat(struct ifstat *ifs)
 {  {
           print_fld_str(FLD_IF_IFACE, ifs->ifs_name);
   
         wmove(wnd, 0, 0);          tb_start();
         wclrtobot(wnd);          tbprintf("%s", ifs->ifs_cur.ifc_flags & IFF_UP ?
                    "up" : "dn");
   
         mvwaddstr(wnd, 1, INSET, "Iface");          switch (ifs->ifs_cur.ifc_state) {
         mvwaddstr(wnd, 1, INSET+9, "State");  
         mvwaddstr(wnd, 1, INSET+19, "Ibytes");  
         mvwaddstr(wnd, 1, INSET+29, "Ipkts");  
         mvwaddstr(wnd, 1, INSET+36, "Ierrs");  
         mvwaddstr(wnd, 1, INSET+48, "Obytes");  
         mvwaddstr(wnd, 1, INSET+58, "Opkts");  
         mvwaddstr(wnd, 1, INSET+65, "Oerrs");  
         mvwaddstr(wnd, 1, INSET+74, "Colls");  
 }  
   
 #define FMT "%-8.8s %2s%2s  %10llu %8llu %6llu   %10llu %8llu %6llu   %6llu "  
   
 const char *  
 showlinkstate(int state)  
 {  
         switch (state) {  
         case LINK_STATE_UP:          case LINK_STATE_UP:
         case LINK_STATE_HALF_DUPLEX:          case LINK_STATE_HALF_DUPLEX:
         case LINK_STATE_FULL_DUPLEX:          case LINK_STATE_FULL_DUPLEX:
                 return (":U");                  tbprintf(":U");
                   break;
         case LINK_STATE_DOWN:          case LINK_STATE_DOWN:
                 return (":D");                  tbprintf (":D");
         case LINK_STATE_UNKNOWN:                  break;
         default:  
                 return ("");  
         }          }
   
           print_fld_tb(FLD_IF_STATE);
   
           print_fld_size(FLD_IF_IBYTES, ifs->ifs_cur.ifc_ib);
           print_fld_size(FLD_IF_IPKTS, ifs->ifs_cur.ifc_ip);
           print_fld_size(FLD_IF_IERRS, ifs->ifs_cur.ifc_ie);
   
           print_fld_size(FLD_IF_OBYTES, ifs->ifs_cur.ifc_ob);
           print_fld_size(FLD_IF_OPKTS, ifs->ifs_cur.ifc_op);
           print_fld_size(FLD_IF_OERRS, ifs->ifs_cur.ifc_oe);
   
           print_fld_size(FLD_IF_COLLS, ifs->ifs_cur.ifc_co);
   
           end_line();
 }  }
   
 void  static void
 showifstat(void)  showtotal(void)
 {  {
         int row;          print_fld_str(FLD_IF_IFACE, "Totals");
         struct ifstat *ifs;  
   
         row = 2;          print_fld_size(FLD_IF_IBYTES, sum.ifc_ib);
         wmove(wnd, 0, 0);          print_fld_size(FLD_IF_IPKTS, sum.ifc_ip);
         wclrtoeol(wnd);          print_fld_size(FLD_IF_IERRS, sum.ifc_ie);
         for (ifs = ifstats; ifs < ifstats + nifs; ifs++) {  
                 if (ifs->ifs_name[0] == '\0')          print_fld_size(FLD_IF_OBYTES, sum.ifc_ob);
                         continue;          print_fld_size(FLD_IF_OPKTS, sum.ifc_op);
                 mvwprintw(wnd, row++, INSET, FMT,          print_fld_size(FLD_IF_OERRS, sum.ifc_oe);
                     ifs->ifs_name,  
                     ifs->ifs_cur.ifc_flags & IFF_UP ? "up" : "dn",          print_fld_size(FLD_IF_COLLS, sum.ifc_co);
                     showlinkstate(ifs->ifs_cur.ifc_state),  
                     ifs->ifs_cur.ifc_ib,          end_line();
                     ifs->ifs_cur.ifc_ip,  
                     ifs->ifs_cur.ifc_ie,  
                     ifs->ifs_cur.ifc_ob,  
                     ifs->ifs_cur.ifc_op,  
                     ifs->ifs_cur.ifc_oe,  
                     ifs->ifs_cur.ifc_co);  
         }  
         mvwprintw(wnd, row++, INSET, FMT,  
             "Totals",  
             "", "",  
             sum.ifc_ib,  
             sum.ifc_ip,  
             sum.ifc_ie,  
             sum.ifc_ob,  
             sum.ifc_op,  
             sum.ifc_oe,  
             sum.ifc_co);  
 }  }
   
 int  int
 cmdifstat(char *cmd, char *args)  if_keyboard_callback(int ch)
 {  {
         struct ifstat *ifs;          struct ifstat *ifs;
   
         if (prefix(cmd, "run")) {          switch (ch) {
                 if (state != RUN)          case 'r':
                         for (ifs = ifstats; ifs < ifstats + nifs; ifs++)                  for (ifs = ifstats; ifs < ifstats + nifs; ifs++)
                                 ifs->ifs_old = ifs->ifs_now;                          ifs->ifs_old = ifs->ifs_now;
                 state = RUN;                  state = RUN;
                 return (1);                  gotsig_alarm = 1;
         }  
         if (prefix(cmd, "boot")) {                  break;
           case 'b':
                 state = BOOT;                  state = BOOT;
                 for (ifs = ifstats; ifs < ifstats + nifs; ifs++)                  for (ifs = ifstats; ifs < ifstats + nifs; ifs++)
                         bzero(&ifs->ifs_old, sizeof(ifs->ifs_old));                          bzero(&ifs->ifs_old, sizeof(ifs->ifs_old));
                 return (1);                  gotsig_alarm = 1;
         }                  break;
         if (prefix(cmd, "time")) {          case 't':
                 state = TIME;                  state = TIME;
                 return (1);                  gotsig_alarm = 1;
         }                  break;
         if (prefix(cmd, "zero")) {          default:
                 if (state == RUN)                  return keyboard_callback(ch);
                         for (ifs = ifstats; ifs < ifstats + nifs; ifs++)          };
                                 ifs->ifs_old = ifs->ifs_now;  
                 return (1);          return 1;
         }  
         return (1);  
 }  }
   

Legend:
Removed from v.1.11  
changed lines
  Added in v.1.12