home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume1 / 8711 / 9 < prev    next >
Text File  |  1990-07-13  |  11KB  |  437 lines

  1. Path: uunet!husc6!rutgers!clyde!cbosgd!mandrill!hal!ncoast!allbery
  2. From: jv@mh.nl.UUCP (Johan Vromans)
  3. Newsgroups: comp.sources.misc
  4. Subject: submission for comp.sources.misc
  5. Message-ID: <5642@ncoast.UUCP>
  6. Date: 14 Nov 87 20:34:49 GMT
  7. Sender: allbery@ncoast.UUCP
  8. Lines: 425
  9. Approved: allbery@ncoast.UUCP
  10. X-Archive: comp.sources.misc/8711/9
  11.  
  12. Some time ago, someone posted somewhere a shell script to generate UUCP
  13. reports from UUCP files. I don't know who, when and where, but I made the
  14. script running on our systems. The script took too long to complete, so I
  15. rewrote the script in C.
  16. This program has not been adapted to SVR3 (not having such a system). It is
  17. not perfect either.
  18. I added some code to generate weekly reports (using SYSLOG.WEEK).
  19.  
  20. Take it for what it is worth -- it generates fine reports on our systems.
  21.  
  22. Whoever wrote the shell script deserves the honour - I only converted it.
  23.  
  24. Remarks and fixes are welcome.
  25.  
  26. --
  27. Real-Name: Johan Vromans                 | jv@mh.nl via European backbone
  28. Multihouse N.V., Gouda, The Netherlands  | uucp: ..{uunet!}mcvax!mh.nl!jv
  29. "Its better to light a candle than to curse the darkness"
  30.  
  31. ---------------- CUT HERE ----------------
  32. /* uucpstat.c - to print the UUCP transaction statistics (aap 7/9/87)
  33.  * for System V Release 2 or 3 formatted UUCP transaction files
  34.  * Version 1.0
  35.  *
  36.  * This program will process the uucp transactions for each node and report
  37.  * total transaction statistics. Processing is performed on the file $UUCP_LOG
  38.  * which defaults to:
  39.  * /usr/spool/uucp/SYSLOG (Rlse 2) or /usr/spool/uucp/.Admin/xferstats (Rlse 3).
  40.  * Processing defaults to statistics for the current day. Options allow 
  41.  * processing of all transactions found (usually the current week's 
  42.  * transactions are maintained) or a specific day's transactions.  
  43.  * Output is directed to standard output.
  44.  *
  45.  * INSTALLATION NOTES -
  46.  *
  47.  * uucp.stat is non-intrusive and should be installed with 755 permissions.
  48.  * Set the variable: $RLSE_LVL to the System V major release level that
  49.  * you are running prior to installation.
  50.  *
  51.  * syntax:           uucp.stat [<option>] 
  52.  *
  53.  *  where <option> is one or more of the following:
  54.  *
  55.  *                 -a     :  process all transactions found
  56.  *                 -u     :  do not display user statistics
  57.  *                 -n     :  do not display node statistics
  58.  *                 -t     :  do not display total statistics
  59.  *                 mm/dd  :  process transactions for month/day.
  60.  *                           (do not use leading zeroes)
  61.  *                 -h     :  display syntax only
  62.  *                 -w     :  use logfile.WEEK, implies -a
  63.  *                 -v     :  display program parameters
  64.  *                 
  65.  *   If no <option> is specified, process today's transactions only.
  66.  */
  67.  
  68. #define PROG_VERSION    "1.0"      /* Program Version number */
  69. #define RLSE_LVL    2    /* major system V release level */
  70.  
  71. #include <stdio.h>
  72. #define EOS    '\0'
  73.  
  74. #if RLSE_LVL == 2
  75. char UUCP_log[] = "/usr/spool/uucp/SYSLOG\0\0\0\0\0\0\0";
  76. #else
  77. char UUCP_log[] = "/usr/spool/uucp/.Admin/xferstats";
  78. #endif
  79.  
  80. int D_month;
  81. int D_day;
  82. int D_year;
  83. char local_node[20];
  84. char cur_node[20];
  85. char cur_login[32];
  86. char *wrkname;
  87. int uulog;
  88. char *my_name;
  89.  
  90. /* Print mode flags */
  91. int PF_user = 1;
  92. int PF_node = 1;
  93. int PF_grand = 1;
  94. int PF_param = 0;
  95. int PF_weekly = 0;
  96. int f_date = 1;            /* default is current day's */
  97.  
  98. int nrecs;
  99. int user_xact;
  100. long user_bytes;
  101. int user_time;
  102. int node_xact;
  103. long node_bytes;
  104. int node_time;
  105. int tot_xact;
  106. long tot_bytes;
  107. int tot_time;
  108.  
  109. main (argc, argv)
  110. int argc;
  111. char *argv[];
  112. {
  113.     init (argc, argv);
  114.     pre_process ();
  115.     sort ();
  116.     process ();
  117.     return (0);
  118. }
  119.  
  120. #include <sys/utsname.h>
  121. #include <time.h>
  122. #include <sys/types.h>
  123. #include <sys/stat.h>
  124.  
  125. init (argc, argv)
  126. int argc;
  127. char *argv[];
  128. {
  129.     extern char *optarg;
  130.     extern int optind;
  131.     struct utsname u;
  132.     struct stat st;
  133.     long time();
  134.     int c;
  135.     long now = time (&now);
  136.     char *tempnam ();
  137.     struct tm *lt;
  138.     
  139.     wrkname = tempnam (NULL, "UU");
  140.     my_name = argv[0];
  141.     uname (&u);
  142.     strncpy (local_node, u.nodename, UTSLEN);
  143.     local_node[UTSLEN] = '\0';
  144.     lt = localtime (&now);
  145.     D_day = lt->tm_mday;
  146.     D_month = lt->tm_mon + 1;
  147.     D_year = lt->tm_year;
  148.     
  149.     while ((c = getopt (argc, argv, "aunthwv")) != EOF) {
  150.     switch (c) {
  151.     case '?':
  152.     case 'h':
  153.         fprintf (stderr, " syntax:  %s [<option>]\n", my_name);
  154.         fprintf (stderr,  "        where <option> is one or more of the following:\n");
  155.         fprintf (stderr,  "        -h     :  display syntax \n");
  156.         fprintf (stderr,  "        -a     :  process statistics for all days found \n");
  157.         fprintf (stderr,  "        -u     :  do not display user statistics\n");
  158.         fprintf (stderr,  "        -n     :  do not display node statistics\n");
  159.         fprintf (stderr,  "        -t     :  do not display total statistics\n");
  160.         fprintf (stderr,  "        -w      :  display weekly stats\n");
  161.         fprintf (stderr,  "        mo/day :  process statistics for specific month/day\n");
  162.         fprintf (stderr,  "        -v     :  display program characteristics\n");
  163.         fprintf (stderr,  "        default is to process today's transactions only\n");
  164.         break;
  165.     case 'u':
  166.         PF_user = 0;
  167.         break;
  168.     case 'n':
  169.         PF_node = 0;
  170.         break;
  171.     case 't':
  172.         PF_grand = 0;
  173.         break;
  174.     case 'v':
  175.         PF_param = 1;
  176.         break;
  177.     case 'a':
  178.         f_date = 0;
  179.         break;
  180. #if RLSE_LVL == 2
  181.     case 'w':
  182.         PF_weekly = 1;
  183.         f_date = 0;
  184.         strcat (UUCP_log, ".WEEK");
  185.         break;
  186. #endif
  187.     }
  188.     }
  189.  
  190.     if (argc > optind) {
  191.     c = atoi (argv[optind]);
  192.     D_day = c % 100;
  193.     D_month = c / 100;
  194.     if (D_day == 0 || D_day > 31 || D_month == 0 || D_month > 12) {
  195.         fprintf (stderr, "%s: invalid date spec: %s\n",
  196.         my_name, argv[optind]);
  197.     }
  198.     else
  199.         f_date = 1;
  200.     }
  201.  
  202.     print_params ();
  203.  
  204.     if (((uulog = open (UUCP_log, 0)) < 0) ||
  205.     (fstat (uulog, &st) < 0) ||
  206.     (st.st_size == 0)) {
  207.     fprintf (stderr, "File: %s unaccessible or empty\n", UUCP_log);
  208.         exit (1);
  209.     }
  210. }
  211.  
  212. /* print routines */
  213.  
  214. /* Display Individual login User Stats */
  215.  
  216. printit (node, login, xact, bytes, time)
  217. char *node, *login;
  218. int xact, time;
  219. long bytes;
  220. {
  221.     printf ("%-8s %-8s %6d %9ld %8d", node, login, xact, bytes, time);
  222.     if (time != 0)
  223.     printf ("%10d\n", bytes / time);
  224.     else
  225.     printf ("\n\n");
  226. }
  227.  
  228. user_stats ()
  229. {
  230.     if (PF_user) {
  231.     printit (cur_node, cur_login, user_xact, user_bytes, user_time);
  232.     }
  233. }
  234.  
  235. /* Display cur_node Statistics */
  236.  
  237. node_stats ()
  238. {
  239.     if (PF_node) {
  240.     printit (cur_node, "TOTAL:", node_xact, node_bytes, node_time);
  241.     printf ("\n");
  242.     }
  243. }
  244.  
  245. /* Display Grand Totals */
  246.  
  247. grand_totals ()
  248. {
  249.     if (PF_grand) {
  250.         printf ("_________________________________________________________________\n\n");
  251.     printit (local_node, "", tot_xact, tot_bytes, tot_time);
  252.     printf ("\n");
  253.    }
  254. }
  255.  
  256. print_params ()
  257. {
  258.     if (PF_param) {
  259.     printf ("[%s]  Version: %s   O/S Rlse Level: %d\n",
  260.         my_name, PROG_VERSION, RLSE_LVL);
  261.     printf ("[%s]  Transaction File: %s\n", my_name, UUCP_log);
  262.     printf ("[%s]  Print Flags:%s%s%s\n\n\n", my_name,
  263.         PF_user ? " User" : "",
  264.         PF_node ? " Node" : "",
  265.         PF_grand ? " Total" : "");
  266.     }
  267. }
  268.  
  269. /* Print header */
  270.  
  271. print_header ()
  272. {
  273.     long time();
  274.     long now = time (&now);
  275.     char *ctime ();
  276.     char *cp = ctime (&now);
  277.     static char *m_tab = "JanFebMarAprMayJunJulAugSepOctNovDec";
  278.     cp[24] = '\0';
  279.  
  280.     if (PF_param)
  281.     printf ("\n\n");
  282.  
  283.     printf ("\tUUCP Network Transaction Statistics\n");
  284.     printf ("\tNode: %s  %s\n", local_node, cp);
  285.  
  286.     if (f_date)
  287.     printf ("\t(Transactions of: %.3s %d 19%d)\n\n",
  288.         &m_tab[3*(D_month-1)], D_day, D_year);
  289.     else
  290.     printf ("\t(All Transactions)\n\n");
  291.  
  292.     printf ("Node     User       Xact     Bytes     Time      Rate (Bytes/Sec)\n");
  293.     printf ("_________________________________________________________________\n\n");
  294. }
  295.  
  296. /* phase 2 - pre-processing of UUCP_log file
  297.  
  298.  * by making one pass at the UUCP_log file and extracting just the
  299.  * fields for cur_node & username, byte count and transaction time,
  300.  * then sorting them by node and login, we can process the sub-totals
  301.  * and totals synchronously and process more quickly then by mutiple passes. 
  302.  */
  303. pre_process ()
  304. {
  305.     char buf [BUFSIZ];
  306.     char *cp;
  307.     FILE *uu = fdopen (uulog, "r");
  308.     FILE *wrkfile = fopen (wrkname, "w");
  309.     int mtmp, dtmp;
  310.     int etime;
  311.     long bytes;
  312.  
  313.     if (PF_param)
  314.     printf ("Selecting ...\n");
  315.     nrecs = 0;
  316.     while ((cp = fgets (buf, BUFSIZ, uu)) != NULL) {
  317.     while (*cp == ' ') cp++;
  318.     if (*cp == '[') continue;
  319.     if (sscanf (cp, "%[^!]!%s %*s (%d/%d%*s %*s %*s %*s %ld / %d",
  320.         cur_node, cur_login, &mtmp, &dtmp, &bytes, &etime) != 6) {
  321.         fprintf (stderr, "** fmt ** %s", buf);
  322.         continue;
  323.     }
  324.  
  325.     /* if a date option was specified then extract only the records */
  326.     /* for that date */
  327.     if (f_date) {
  328.         if (mtmp != D_month || dtmp != D_day)
  329.         continue;
  330.     }
  331.  
  332. #if RLSE_LVL == 3
  333.           # Rlse 3 xaction log file has time as fractional number
  334. #endif
  335.     fprintf (wrkfile, "%s %s %ld %d\n", cur_node, cur_login, bytes, etime);
  336.     nrecs++;
  337.     }
  338.     fclose (wrkfile);
  339.     fclose (uu);
  340. }
  341.  
  342. /* now sort the stream in order of node login bytes time */
  343. sort ()
  344. {
  345.     char buf [BUFSIZ];
  346.  
  347.     if (PF_param)
  348.     printf ("Sorting %d records ...\n", nrecs);
  349.     sprintf (buf, "sort -o %s %s", wrkname, wrkname);
  350.     system (buf);
  351. }
  352.  
  353. /* phase 3 - process each transaction record and calculate/display stats */
  354.  
  355. process ()
  356. {
  357.     FILE *wrkfile = fopen (wrkname, "r");
  358.     char buf [BUFSIZ];
  359.     char node [64];
  360.     char login [64];
  361.     long bytes;
  362.     int etime;
  363.  
  364.     *node = *cur_node = EOS;
  365.     *login = *cur_login = EOS;
  366.     tot_xact = tot_bytes = tot_time = 0;
  367.     user_xact = user_bytes = user_time = 0;
  368.     node_xact = node_bytes = node_time = 0;
  369.  
  370.     print_header ();
  371.     while (fgets (buf, BUFSIZ, wrkfile) != NULL) {
  372.     sscanf (buf, "%s %s %ld %d", node, login, &bytes, &etime);
  373.  
  374.     if  (!strcmp (node, cur_node) || *node == EOS) {
  375.     /* first entry or same node as previous entry */
  376.  
  377.         if (!strcmp (login, cur_login) || *login == EOS) {
  378.         /* first entry or same cur_login as previous entry */
  379.         user_xact++;
  380.         user_bytes += bytes;
  381.         user_time += etime;
  382.         }
  383.         else {
  384.         /* cur_login is different than previous login. */
  385.         /* print current user's summaries. */
  386.         if (*cur_login)
  387.             user_stats ();
  388.  
  389.         /* init counters for new cur_login */
  390.         user_xact = 1;
  391.         user_bytes = bytes;
  392.         user_time = etime;
  393.         strcpy (cur_login, login);
  394.         }
  395.  
  396.         /* update node statistics */
  397.         node_xact++;
  398.         node_bytes += bytes;
  399.         node_time += etime;
  400.         strcpy (cur_node, node);
  401.     }
  402.     else {
  403.         /* node name is different than previous entry */
  404.         /* print previous user and cur_node totals */
  405.         if (*cur_node) {
  406.         user_stats ();
  407.         node_stats ();
  408.         }
  409.  
  410.         /* init counters for new cur_node */
  411.         strcpy (cur_node, node);
  412.         strcpy (cur_login, login);
  413.         user_xact = 1;
  414.         user_bytes = bytes;
  415.         user_time = etime;
  416.         node_xact = 1;
  417.         node_bytes = bytes;
  418.         node_time = etime;
  419.     }
  420.  
  421.     /* update grand totals */
  422.     tot_xact++;
  423.     tot_bytes += bytes;
  424.     tot_time += etime;
  425.     }
  426.     if (*cur_node) {
  427.     user_stats ();
  428.     node_stats ();
  429.     grand_totals ();
  430.     }
  431.     else
  432.     printf ("No transactions found\n");
  433.  
  434.     fclose (wrkfile);
  435.     unlink (wrkname);
  436. }
  437.