home *** CD-ROM | disk | FTP | other *** search
/ ftp.muug.mb.ca / 2014.06.ftp.muug.mb.ca.tar / ftp.muug.mb.ca / pub / src / gopher / gopher1.01 / misc / Logging / glog.c < prev    next >
C/C++ Source or Header  |  1992-06-22  |  5KB  |  246 lines

  1.  
  2. /*** glog.c -- analysis tool for Unix gopherd logs ***/
  3.  
  4. /***
  5.  *** Usage : glog < logfile > reportfile
  6.  ***/
  7.  
  8. /*** Description: glog munges through a Unix gopherd log and extracts
  9.  *** important looking statistics. It catalogs all hosts that have connected
  10.  *** to the gopherd during the logging period and sorts them according to the
  11.  *** number of accesses. It does the same for each directory and file accessed,
  12.  *** and ranks them according to popularity.
  13.  ***/
  14.  
  15. /***
  16.  *** by: Chuck Shotton - U of Texas Health Science Center - Houston,
  17.  ***                     Office of Academic Computing
  18.  ***                     cshotton@oac.hsc.uth.tmc.edu
  19.  ***                     6/17/92
  20.  ***/
  21.  
  22.  
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <stdlib.h>
  26. #ifdef THINK_C
  27. #include <console.h>
  28. #endif
  29.  
  30. #define GLOG_VERSION "Gopher Log Analyzer v.1.0\n"
  31.  
  32. typedef struct node_rec {
  33.     char data[64];
  34.     int hits;
  35.     struct node_rec *left, *right;
  36. } NODE_REC;
  37.  
  38. typedef NODE_REC *NODE_PTR;
  39.  
  40. /*******************************/
  41.  
  42. NODE_PTR hosts, docs;
  43. char day[4], month[4], date[3], hours[9], year[5], pid[6],
  44.     hostname[64], message1[12], message2[20], path[64];
  45. char start_date[20], stop_date[20];
  46.  
  47. /*******************************/
  48.  
  49. main(argc, argv)
  50. int argc;
  51. char **argv;
  52. {
  53. char line[256];
  54.  
  55. #ifdef THINK_C
  56.     argc = ccommand(&argv);
  57. #endif
  58.  
  59.     printf(GLOG_VERSION);
  60.     
  61.     Initialize();
  62.     
  63.     /*process the first line here to get the date info*/
  64.     fgets(line, 256, stdin);
  65.     ProcessLine(line);
  66.     sprintf(start_date, "%s %s, %s", month, date, year);
  67.     
  68.     while (!feof(stdin)) {
  69.         fgets(line, 256, stdin);
  70.         ProcessLine(line);
  71.     }
  72.     
  73.     sprintf(stop_date, "%s %s, %s", month, date, year);
  74.     
  75.     ShowStats();
  76.         
  77.     exit(0);
  78. }
  79.  
  80. /*******************************/
  81.  
  82. Initialize()
  83. {
  84.     hosts = docs = NULL;
  85. }
  86.  
  87. /*******************************/
  88. /* Read a line from the log file, parse it up, and insert the */
  89. /* info into the appropriate tables.                          */
  90.  
  91. ProcessLine(line)
  92. char *line;
  93. {
  94. int i;
  95.     
  96.     path[0]='\0';
  97.     i = sscanf(line, "%s %s %s %s %s %s %s : %s %s %s\n",
  98.             day, month, date, hours, year, pid, hostname, 
  99.             message1, message2, path);
  100.  
  101.     Insert(&hosts, hostname);
  102.     
  103.     if (!strncmp(message1, "Root", 4))
  104.         Insert(&docs, "Root Connections");
  105.     else
  106.         Insert(&docs, path);
  107. }
  108.  
  109. /*******************************/
  110. /* Insert "str" into the appropriate symbol table. Increment the */
  111. /* number of hits if the "str" is already present.               */
  112.  
  113. Insert(tree, str)
  114. NODE_PTR *tree;
  115. char *str;
  116. {
  117. NODE_PTR temp;
  118. int i;
  119.     if (*tree == NULL) {
  120.         temp = (NODE_PTR) malloc(sizeof(NODE_REC));
  121.         if (temp) {
  122.             temp->left = temp->right = NULL;
  123.             temp->hits = 1;
  124.             strcpy (temp->data, str);
  125.             *tree = temp;
  126.         }
  127.         else
  128.             printf("Memory error\n");
  129.     }
  130.     else {
  131.         i=strcmp(str, (*tree)->data);
  132.         if (i > 0)
  133.             Insert(&((*tree)->right), str);
  134.         else if (i<0) 
  135.             Insert(&((*tree)->left), str);
  136.         else
  137.             (*tree)->hits += 1;
  138.     }
  139. }
  140.  
  141. /*******************************/
  142. int total_hits, total_nodes;
  143. NODE_PTR by_num;
  144.  
  145. /*******************************/
  146. /* Dump out the contents of the given symbol table and sort by */
  147. /* number of "hits" on the fly.                                */
  148.  
  149. DumpTree(tree)
  150. NODE_PTR tree;
  151. {
  152.     if (tree == NULL) 
  153.         return;
  154.     else {
  155.         DumpTree(tree->left);
  156.         printf("%-50.50s %5d\n", tree->data, tree->hits);
  157.         total_hits += tree->hits;
  158.         total_nodes++;
  159.         InsertByNum(tree);
  160.         DumpTree(tree->right);
  161.     }
  162. }
  163.  
  164. /*******************************/
  165.  
  166. DumpStats(tree)
  167. NODE_PTR tree;
  168. {
  169.     total_hits = 0;
  170.     total_nodes= 0;
  171.     by_num = NULL;
  172.     DumpTree(tree);
  173. }
  174.  
  175. /*******************************/
  176. /* Turn a tree node into an element in a linked list */
  177.  
  178. InsertByNum(node)
  179. NODE_PTR node;
  180. {
  181. NODE_PTR temp, temp2;
  182.     if (by_num == NULL) {
  183.         by_num = node;
  184.         node->left == NULL;
  185.     }
  186.     else {
  187.         temp = by_num;
  188.         temp2 = temp->left;
  189.         if (node->hits >= temp->hits) {
  190.             node->left = temp;
  191.             by_num = node;
  192.         }
  193.         else {
  194.             while (temp2 != NULL) {
  195.                 if (node->hits > temp2->hits) {
  196.                     temp->left = node;
  197.                     node->left = temp2;
  198.                     return;
  199.                 }
  200.                 else {
  201.                     temp = temp2;
  202.                     temp2 = temp->left;
  203.                 }
  204.             }
  205.             temp->left = node;
  206.             node->left = NULL;
  207.         }
  208.     }
  209. }
  210.  
  211. /*******************************/
  212. /* Dump out the linked list contents */
  213.  
  214. DumpByNum(tree)
  215. NODE_PTR tree;
  216. {
  217.     while (tree != NULL) {
  218.         printf("%-50.50s %5d (%0.2f%%)\n", tree->data, tree->hits, 
  219.             (float) tree->hits/(float) total_hits);
  220.         tree = tree->left;
  221.     }
  222. }
  223.  
  224. /*******************************/
  225. /* Show all the stats gleaned from the log file */
  226.  
  227. ShowStats()
  228. {
  229.     printf("Report Period: %s to %s\n", start_date, stop_date);
  230.     printf("\nAll Hosts:\n-----------------------------\n");
  231.     DumpStats(hosts);
  232.     printf("\nMost Active Hosts:\n-----------------------------\n");
  233.     DumpByNum(by_num);
  234.     printf("------------------------\n");
  235.     printf("Total Hosts:       %d\n", total_nodes);
  236.     printf("Total Connections: %d\n", total_hits);
  237.  
  238.     printf("\nAll Data Accesses:\n-----------------------------\n");
  239.     DumpStats(docs);
  240.     printf("\nMost Popular Data:\n-----------------------------\n");
  241.     DumpByNum(by_num);
  242.     printf("------------------------\n");
  243.     printf("Total Data Accesses: %d\n", total_hits);
  244. }
  245.  
  246.