/**************************************************************************
                             node_report.c
                              RW Salnick
                                2/2/92
                              Version 1.0
                             Public Domain
                             
   This program opens uuspool:timelog and reads it, compiling a report
   (node.report), listing various statistics for each node found
   in the timelog for the month passed on the command line.  If no month
   is passed on the command line, then the report is run for the month
   previous to the one returned from the system time.  The report is placed
   in the current directory, and contains mail header information (suitable
   for direct feed to sendmail) for 'Postmaster' at each node for which
   statistics are found in the timelog.
*************************************************************************/

# include <stdio.h>
# include <string.h>
# include <time.h>

# define MAXNODES 64

main(argc, argv)
int	argc;
char	*argv[];
{
   char copyright[] = "Copyright 1992, RW Salnick";
	FILE	*infile, *outfile;
   char datestring[9],node_name[16],date[28];
	int	i,node_count=0, node_no, dummytime1, dummytime2;
   int   connect_minutes, connect_seconds, bytes_in, bytes_out;
   char *months[] = {"January","February","March",
                     "April","May","June",
                     "July","August","September",
                     "October","November","December","January"};
                     
   struct _node {
            char name[16];
            int bytes_recv;
            int bytes_sent;
            int connect_seconds;
            int avg_cps;
          } *node;  

   time(&i);
   strncpy(date,ctime(&i)+4,3);

   switch(argc)
      {

         case 1:
            {
              for(i=0;i<12;i++)
                  {
                   if((strnicmp(date,months[i],3)) == 0 )
                       {
                        if (i==0) i=12;
                        strcpy(date,months[i]);
                        break;
                       } 
                  }   
             break;
            }
         
         case 2:
            {
             if(strcmp(argv[1],"-l") == 0)
                 {
                   for(i=0;i<12;i++)
                       {
                        if((strnicmp(date,months[i],3)) == 0 )
                           {
                            if (i==0) i=12;
                            strcpy(date,months[i-1]);
                            break;
                           }
                       }    
                   break;     
                 }
                  
             for(i=0;i<12;i++)
                 {
                  if((strnicmp(argv[1],months[i],3)) == 0 )
                     {
                       if (i==0) i=12;
                       strcpy(date,months[i]);
                       i=0;
                       break;
                     }
                 }    
             if (i<1) break;    
            }
                     
            
         default:
            {
             printf("%s.\n",copyright);
             printf("Usage:  node_report [mon|-l]\n");
             printf("\twhere mon is a 3-letter month abbreviation,\n");
             printf("\t      -l produces a report for last month\n");
             printf("\nSupplying no options generates a report for this month.\n");
             exit(1);
            }       
	   }
   

   printf("Reporting for month: %s.\n",date);

   if ((node = (struct _node *)calloc(MAXNODES,sizeof(node)))==NULL)
      {
       printf("Out of memory - allocation of node structure.\n");
       exit();
      }

	if ((infile = fopen("uuspool:timelog", "r")) == (FILE *)NULL)  
     {
		printf("Unable to open uuspool:timelog\n");
	  }

   else /*  #1  - infile opened ok */
      {
       if((outfile = fopen("node.report", "w")) == (FILE *)NULL)
          {
           printf("Unable to open node.report for output.\n");
           fclose(infile); 
          }
          
       else /*  #2   - outfile opened ok */
         {  
          strcpy(node[0].name,"(Total)");
          while (fscanf(infile,"%s %d:%d %d:%d in=%d out=%d %s",\
                 datestring,
                 &dummytime1,
                 &dummytime2,
                 &connect_minutes,
                 &connect_seconds,
                 &bytes_in,
                 &bytes_out,
                 node_name) != EOF)
              {

               /* choose only entries from the month as input...  */
               if((strnicmp(date,datestring+3,3)) == 0 )
                 {
                  /* do we know this node yet?  */
                  node_no = -1;
                  for(i=1;i<node_count+1;i++)
                     {
                      if(strcmp(node_name,node[i].name) == 0)
                        {
                         node_no = i;
                         break;
                        }  
                     }
                  if (node_no ==-1)
                     {
                      /* new node - add it to the structure array 
                             - if there is room! */
                             
                      if (node_count < MAXNODES)
                         {
                          node_count++;
                          node_no = node_count;
                          /* node[0] contains grand totals */
                          strcpy(node[node_no].name,node_name);
                         }
                      else node_no = -1;    
                     }
                              
                  if (node_no >= 0)
                     {
                      node[node_no].bytes_recv += bytes_in;
                      node[node_no].bytes_sent  += bytes_out;
                      node[node_no].connect_seconds += (connect_minutes*60
                                                        + connect_seconds);
                          
                      node[0].bytes_recv += bytes_in;
                      node[0].bytes_sent  += bytes_out;
                      node[0].connect_seconds += (connect_minutes*60
                                                        + connect_seconds);
                     }
                     
                 } /* end of if correct month  */ 
              } /* we are done scanning uuspool:timelog */
          
          fprintf(outfile,"To: Postmaster");
          for (i=1; i<node_count+1; i++)
            {
             fprintf(outfile,", Postmaster@%s",node[i].name);
            }
          fprintf(outfile,
              "\nSubject: Activity Report For %s\n\n",date);
                 
          fprintf(outfile,
"Remote\t\t----------kBytes----------\t---Minutes---\t-Avg-\n");
          fprintf(outfile,
" Host \t\tRecv     Sent    Total \t\t  Connected\t CPS\n");
          fprintf(outfile,
"--------\t--------------------------\t-------------\t------\n");
          for(i=1;i<node_count+1;i++)
            {
               strncat(node[i].name,"             ",
                        10-strlen(node[i].name));
               fprintf(outfile,   
               "%s\t%-lu\t%-lu\t%-lu\t\t%-lu\t\t%-lu\n",
               node[i].name,
               node[i].bytes_recv/1000,
               node[i].bytes_sent/1000,
               (node[i].bytes_recv + node[i].bytes_sent)/1000,
               node[i].connect_seconds/60,
               (node[i].bytes_recv + node[i].bytes_sent)/node[i].connect_seconds);
            }

          fprintf(outfile,"\n\n\n\t\t-------------SUMMARY----------------\n");
          fprintf(outfile,"\t\tTotal Active UUCP Sites:\t%u\n",node_count);
          fprintf(outfile,"\t\tTotal K-Bytes Recvd:    \t%u\n",
                           node[0].bytes_recv/1000);
          fprintf(outfile,"\t\tTotal K-Bytes Sent:     \t%u\n",
                           node[0].bytes_sent/1000);
          fprintf(outfile,"\t\tTotal K-Bytes Handled:  \t%u\n",
                           (node[0].bytes_recv+node[0].bytes_sent)/1000);
          fprintf(outfile,"\t\tTotal Min. Connected:   \t%u\n",
                           node[0].connect_seconds/60);
          fprintf(outfile,"\t\tAverage CPS:            \t%u\n",
                 (node[0].bytes_recv+node[0].bytes_sent)/
                  node[0].connect_seconds);
          fclose(outfile);  

         } /* end of else #2  */
         
       fclose(infile);  
      }  /*  end of else #1  */    

}  /* end of main  */
