home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / mgr / sparcmgr / demo3.zoo / demo / misc / ether2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-01-08  |  8.5 KB  |  382 lines

  1. /*                        Copyright (c) 1987 Bellcore
  2.  *                            All Rights Reserved
  3.  *       Permission is granted to copy or use this program, EXCEPT that it
  4.  *       may not be sold for profit, the copyright notice must be reproduced
  5.  *       on copies, and credit should be given to Bellcore where it is due.
  6.  *       BELLCORE MAKES NO WARRANTY AND ACCEPTS NO LIABILITY FOR THIS PROGRAM.
  7.  */
  8. /*    $Header: ether.c,v 4.2 88/06/22 14:37:31 bianchi Exp $
  9.     $Source: /tmp/mgrsrc/demo/misc/RCS/ether.c,v $
  10. */
  11. static char    RCSid_[] = "$Source: /tmp/mgrsrc/demo/misc/RCS/ether.c,v $$Revision: 4.2 $";
  12.  
  13. /* strip_chart vmstat output version II */
  14.  
  15. #include "term.h"
  16. #include <signal.h>
  17. #include <sgtty.h>
  18.  
  19. /* color stuff */
  20.  
  21. #define MAX_COLORS    20
  22. int colors[MAX_COLORS] = {
  23.     0,
  24.     9,
  25.     3,
  26.     4,
  27.     5,
  28.     2,
  29.     1,1,1,1,1,
  30.     };
  31.  
  32. int color=0;        /* true iff color mgr */
  33.  
  34. FILE *popen(), *file;
  35.  
  36. #define INTERVAL    60    /* bar interval (in secs) */
  37. #define SCROLL        4    /* # of scrolls per window */
  38. #define MAX        3    /* max number of plots */
  39. #define FREQ        3    /* update frequency (secs)*/
  40. #define DELTA        4    /* pixels/update */
  41.  
  42. #define Min(x,y)    ((x)>(y)?(y):(x))
  43. #define Max(x,y)    ((x)>(y)?(x):(y))
  44. #define dprintf        if (debug) fprintf
  45.  
  46. char *labels[] = {        /* graph labels */
  47.    "Input" , "Output", "Coll."
  48.    };
  49.  
  50. int indx[] = {            /* field indeces into netstat(1) */
  51.    0, 2, 4
  52.    };
  53.  
  54. int limit[] = {            /* default maximums */
  55.    15, 15, 3
  56.    };
  57.  
  58. main(argc,argv)
  59. int argc;
  60. char **argv;
  61.    {
  62.    register int i;
  63.    register int x;    /* current plot position */
  64.    int bar=0;        /* hash mark counter */
  65.    int back=0;        /* enable background writes */
  66.    int solid=1;        /* make solid lines */
  67.    int freq = FREQ;    /* update frequency (secs) */
  68.    int size;        /* size of label region */
  69.    int interval;    /* hash mark interval (secs) */
  70.  
  71.    char host[16];    /* hostname */
  72.    char title[255];    /* chart title */
  73.    char line[255];    /* vmstat input buffer */
  74.    char *fields[20];    /* place for raw input fields */
  75.  
  76.    int current[MAX];    /* current data value */
  77.    int old[MAX];    /* previous data value */
  78.    int first =1;    /* first time through */ 
  79.    int clean();
  80.  
  81.    int f_high, f_wide;        /* font size */
  82.    int high,wide;        /* window size */
  83.    int x1;            /* left margin */
  84.    int x2;            /* title */
  85.    int y1;            /* title line */
  86.    int y2;            /* first bar */
  87.    int scroll;            /* scroll amount */
  88.    int delta=DELTA;        /* pixels/line */
  89.    int item;
  90.    int dummy;
  91.    int debug=0;
  92.     int warn;
  93.  
  94.    ckmgrterm( *argv );
  95.  
  96.    for(i=1;i<argc;i++) {
  97.       if (strcmp(argv[i],"-d")==0) {
  98.          debug++;
  99.          }
  100.       if (strncmp(argv[i],"-c",2)==0) {
  101.          set_colors(argv[i]+2);
  102.          continue;
  103.          }
  104.       else if (strncmp(argv[i],"-f",2)==0) {
  105.          freq = atoi(argv[i]+2);
  106.          if (freq < 1) freq = 1;
  107.          if (freq > 120) freq = 120;
  108.          }
  109.       else if (strncmp(argv[i],"-m",2)==0) {
  110.          int max;
  111.          max = atoi(argv[i]+2);
  112.          if (max < 1) max = 1;
  113.          if (max > 999) max = 999;
  114.          limit[0] = limit[1] = max;
  115.          limit[2] = (max>=5) ? max/5 : 1;
  116.          
  117.          }
  118.       else
  119.          fprintf(stderr,"%s: unknown flag %s. Ignored\n",*argv,argv[i]);
  120.       }
  121.  
  122.    sprintf(line,"netstat %d",freq);
  123.    if ((file = popen(line,"r")) == NULL)
  124.       exit(1);
  125.  
  126.    m_setup(M_FLUSH);
  127.    m_ttyset();
  128.     color = is_color();
  129.    m_push(P_EVENT|P_FLAGS);
  130.    m_setmode(M_ABS);
  131.  
  132.    signal(SIGINT,clean);
  133.    signal(SIGTERM,clean);
  134.  
  135.    system("stty -ctlecho");
  136.    m_setevent(RESHAPE,"R\fRedrawing...\n");
  137.    m_setevent(REDRAW,"R\fRedrawing...\n");
  138.    first = 1;
  139.  
  140.    while (1) {
  141.  
  142.       for(size=0,i=0;i<MAX;i++)
  143.          size = Max(size,strlen(labels[i]));
  144.  
  145.       /* clear the screen, flush pending input */
  146.  
  147.       read_it(fileno(m_termin),line);
  148.  
  149.       /* get font size */
  150.  
  151.       get_font(&f_wide,&f_high);
  152.  
  153.       /* get window size */
  154.  
  155.       get_size(&dummy,&dummy,&wide,&high);
  156.    
  157.       if (wide==0 || high==0 || f_wide==0 || f_high==0) {
  158.          fprintf(stderr,"Can't get window info\n");
  159.          clean();
  160.          }
  161.  
  162.       /* get the title */
  163.  
  164.       gethostname(host,sizeof(host));
  165.  
  166.       sprintf(title,"Network statistics for %s in packets/%d seconds",
  167.               host,freq);
  168.  
  169.       if (strlen(title)*f_wide > wide)
  170.          sprintf(title,"%s (pkts/%ds.)",host,freq);
  171.  
  172.       /* make sure window is big enough */
  173.  
  174.       if (f_high * (2*MAX+1) > high) {
  175.          fprintf(stderr,"\fWindow isn't tall enough\n");
  176.          m_gets(line);
  177.          continue;
  178.          }
  179.       if (f_high * (3*MAX +1) > high)
  180.          size += 3;
  181.  
  182.       if (strlen(title)*f_wide > wide || 3*size*f_wide > wide*2) {
  183.          fprintf(stderr,"\fWindow isn't\nwide enough\n");
  184.          m_gets(line);
  185.          continue;
  186.          }
  187.  
  188.       /* calculate key positions */
  189.  
  190.       x1 = (size*f_wide+1);
  191.       x2 = (wide-strlen(title)*f_wide)/2;
  192.       y1 = f_high +1;
  193.       y2 = (high - y1) /MAX;
  194.       high--;
  195.  
  196.       m_func(B_SET);
  197.         bg_color(1);
  198.       m_clear();
  199.       x = x1;
  200.       scroll = Max((wide-x1)/SCROLL,10);
  201.       scroll += scroll%delta;
  202.  
  203.       if (freq >15)
  204.          interval = INTERVAL * 10 /freq;
  205.       else 
  206.          interval = INTERVAL / freq;
  207.  
  208.       /* draw form */
  209.  
  210.         fg_color(2);
  211.       m_moveprint(x2,y1,title);
  212.  
  213.         fg_color(3);
  214.         line_color(4);
  215.       for(i=0;i<MAX;i++) {
  216.          m_moveprint(x1-f_wide,high-i*y2,"0");
  217.          sprintf(line,"%3d",limit[i]);
  218.          m_moveprint(x1-f_wide*3,high-(i+1)*y2+f_wide*2+1,line);
  219.          m_moveprint(1,high-i*y2-(y2-f_high)/2,labels[i]);
  220.          m_line(x1,high-i*y2,wide,high-i*y2);
  221.          }
  222.    
  223.       m_line(0,y1,wide,y1);
  224.       m_line(x1,y1,x1,high);
  225.       m_movecursor(x1,0);
  226.       m_flush();
  227.  
  228.       /* read the data */
  229.  
  230.       while (fgets(line,sizeof(line),file) != NULL) {
  231.          i = parse(line,fields);
  232.          if (strcmp(*fields,"input")==0) {
  233.             fgets(line,sizeof(line),file);
  234.             fgets(line,sizeof(line),file);
  235.             continue;
  236.             }
  237.  
  238.          /* calculate new line position */
  239.  
  240.          for(i=0;i<MAX;i++) {
  241.                 line_color(6+i);
  242.             current[i] = atoi(fields[indx[i]]) *
  243.                          (y2-3)/limit[i];
  244.                 warn = current[i] > y2-3;
  245.             current[i] = Min(current[i],y2-3) + y2*i + 1;
  246.  
  247.             if (!first) {
  248.                m_line(x,high-old[i],x+delta,high-current[i]);
  249.                if (solid) {
  250.                         if (warn) line_color(5);
  251.                   m_line(x+delta,high-y2*i,x+delta,high-current[i]);
  252.                         }
  253.                }
  254.  
  255.             dprintf(stderr,"%s %d->%d, ",labels[i],
  256.                             high-old[i],high-current[i]);
  257.             old[i] = current[i];
  258.             }
  259.          dprintf(stderr," [%d]\n",high);
  260.    
  261.             line_color(4);
  262.          if (++bar  == interval) {
  263.             m_line(x,y1,x,high);
  264.             bar = 0;
  265.             dprintf(stderr,"---------\n");
  266.             }
  267.  
  268.          if (first)
  269.             first = 0;
  270.          else
  271.             x += delta;
  272.  
  273.          if (x > wide-delta) {
  274.  
  275.             /* scroll the display */
  276.  
  277.             x -= scroll;
  278.             m_func(B_COPY);
  279.             m_bitcopy(x1+1,y1+1,wide-x1-1,high-y1-1,x1+scroll+1,y1+1);
  280.                 line_color(1);
  281.                 if (color)
  282.                 m_func(B_SRC);
  283.                 else
  284.                 m_func(B_CLEAR);
  285.             m_bitwrite(wide-scroll,y1+1,scroll,high);
  286.             m_func(B_SET);
  287.          
  288.             dprintf(stderr,"scroll to %d,%d from %d,%d\n",
  289.                     x1+1,y1+1,x1+scroll+1,y1+1);
  290.                 line_color(4);
  291.             for(i=0;i<MAX;i++) 
  292.                m_line(wide-scroll,high-i*y2,wide,high-i*y2);
  293.             }
  294.          m_flush();
  295.          if (read_it(fileno(m_termin),line) && *line == 'R')
  296.             break;
  297.          }
  298.       }
  299.    }
  300.  
  301. clean()            /* clean up on SIGINT */
  302.    {
  303.    m_pop();
  304.    pclose(file);
  305.    m_clear();
  306.    m_flush();
  307.    m_ttyreset();
  308.    exit(1);
  309.    }
  310.  
  311. int read_it(fd,line)    /* non blocking read */
  312. int fd;
  313. char *line;
  314.    {
  315.    long rd;
  316.  
  317.    ioctl(fd,FIONREAD,&rd);
  318.    line[rd] = '\0';
  319.    if (rd > 0)  {
  320.       return(read(fd,line,rd));
  321.       }
  322.    else
  323.       return(0);
  324.    }
  325.  
  326. /* see if MGR is color */
  327.  
  328. int
  329. is_color()
  330.     {
  331.     int bits = 0;
  332.     m_getinfo(G_SYSTEM);
  333.     sscanf(m_get(),"%*s %*d %*d %*d %d",&bits);
  334.     return(bits>1);
  335.     }
  336.  
  337. /* set the bg color */
  338.  
  339. int
  340. bg_color(n)
  341. int n;
  342.     {
  343.     if (color) m_bcolor(colors[n]);
  344.     }
  345.  
  346. /* set the FG color */
  347.  
  348. int
  349. fg_color(n)
  350. int n;
  351.     {
  352.     if (color) m_fcolor(colors[n]);
  353.     }
  354.  
  355. /* set the line color */
  356.  
  357. int
  358. line_color(n)
  359. int n;
  360.     {
  361.     if (color) m_linecolor(B_SRC,colors[n]);
  362.     }
  363.  
  364. /* get colors from the command line */
  365.  
  366. int
  367. set_colors(s)
  368. register char *s;
  369.     {
  370.     register int i=0;
  371.     register unsigned char c;
  372.  
  373.     while((c = *s++) && i<MAX_COLORS) {
  374.         if (c>='0' && c <= '9')
  375.             colors[i++] = c - '0';
  376.         else if (c >= 'a' && c <= 'z')
  377.             colors[i++] = 16 + c - 'a';
  378.         else if (c >= 'A' && c <= 'Z')
  379.             colors[i++] = 16 + c - 'A';
  380.         }
  381.     }
  382.