home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / unix / large / 415 < prev    next >
Encoding:
Text File  |  1993-01-11  |  4.6 KB  |  184 lines

  1. Xref: sparky comp.unix.large:415 comp.unix.admin:7035
  2. Path: sparky!uunet!paladin.american.edu!howland.reston.ans.net!usc!news.cerf.net!nic.cerf.net!hardym
  3. From: hardym@nic.cerf.net (Mike Hardy)
  4. Newsgroups: comp.unix.large,comp.unix.admin
  5. Subject: Filesystem Activity Statistics
  6. Date: 12 Jan 1993 00:43:56 GMT
  7. Organization: CERFnet Dial n' CERF Customer Group
  8. Lines: 171
  9. Distribution: world
  10. Message-ID: <1it48cINN39o@news.cerf.net>
  11. NNTP-Posting-Host: nic.cerf.net
  12.  
  13.  
  14. The following program will traverse a directory tree to gather 
  15. statistics about all files in that tree.  The goal is to get
  16. an understanding of file activity on a server and to determine the
  17. utility of using hierarchical storage management for servers in
  18. general.  I am interested in anyone who would run this program
  19. against their server to help us gather information about the
  20. general state of file activity in the real world.  It has been
  21. tested on Sun, Ultrix, RS/6000, SGI and Unicos platforms.  I ask
  22. that it be run on your server(s) and the output be redirected to a file
  23. and sent to me via email.  I will gather the statistics and
  24. post a review of what I learn to comp.arch.storage.  Any suggestions
  25. on improvements are appreciated.  I would ask that it be run at a
  26. high enough level in your directory to provide information about
  27. user generated and stored data, as opposed to system files.
  28.  
  29. Run this at your own risk.  You might want to run it a night!
  30.  
  31. Thanks for any help given.  I will post a complete summary after
  32. responses come in.
  33.  
  34. Mike Hardy
  35. (619) 455-3757
  36. hardym@sdsc.edu
  37.  
  38. -------- cut here ----------
  39.  
  40. #define SECONDSINDAY 60 * 60 * 24    /* Seconds in a day */
  41. #define NOTIN30 SECONDSINDAY * 30     /* Seconds in 30 days */
  42. #define NOTIN90 SECONDSINDAY * 90    /* Seconds in 90 days */
  43. #define NEVERUSED 0            /* Time diff for files never used */
  44. #define calc_percent(a) (int)(((float)a/(float)totalfiles)*100)
  45.  
  46. #include <sys/types.h>
  47. #include <sys/stat.h>
  48. #include <dirent.h>
  49. #include <stdio.h>
  50.  
  51. #ifdef sun
  52. #include <sys/time.h>
  53. #endif
  54.  
  55. #ifdef mc68000
  56. #include <netat/compat.h>
  57. #endif
  58.  
  59. /* global counter to survive recursion */
  60.  
  61. int totalfiles = 0;
  62. long totalage = 0;
  63. int neverused = 0;
  64. int notin30 = 0;
  65. int notin90 = 0;
  66. int since30 = 0;
  67. int since90 = 0;
  68. int active = 0;
  69. int numdirs = 0;
  70.  
  71. main(argc, argv)
  72. int argc;
  73. char **argv;
  74. {
  75.     int i;
  76.  
  77.     if (argc == 1)
  78.         printf("usage: scan [directory_name]\n");
  79.     for(i=1;i<argc;i++){
  80.         walkdir(argv[i]);
  81.         }
  82.  
  83.     if (numdirs == 0) exit();
  84.  
  85.     printf("Number of directories scanned: %d\n", numdirs);
  86.     printf("Total files: %d\n",totalfiles);
  87.  
  88.     printf("Files never been used:\t\t\t%d\tpercent: %d\n", 
  89.         neverused, calc_percent(neverused));
  90.  
  91.     printf("Files that are active:\t\t\t%d\tpercent: %d\n",
  92.         active,calc_percent(active));
  93.  
  94.     printf("Files not used in last 30 days:\t\t%d\tpercent: %d\n",
  95.         notin30, calc_percent(notin30));
  96.  
  97.     printf("Files not used in last 90 days:\t\t%d\tpercent: %d\n", 
  98.         notin90, calc_percent(notin90));
  99.  
  100.     printf("Files not used after 30 days old:\t%d\tpercent: %d\n", 
  101.         since30, calc_percent(since30));
  102.  
  103.     printf("Files not used after 90 days old:\t%d\tpercent: %d\n", 
  104.         since90, calc_percent(since90));
  105.  
  106.     i = (int)(((float)totalage/(float)totalfiles));
  107.     printf("Average file age:\t%d days\n", i);
  108.     
  109. }
  110.  
  111. /* recursive function to traverse a directory tree while gathering
  112. file stats */    
  113.  
  114. int walkdir(dirname)
  115. char *dirname;
  116. {
  117.     int diff;
  118.     int diffnow;
  119.     DIR *dirp;
  120.     struct dirent *dp;
  121.     struct stat statbuf;
  122.     struct stat *bufaddr = & statbuf;
  123.     char pathname[256];
  124.  
  125.     if ((dirp = opendir(dirname)) == NULL){
  126.         perror("walkdir opendir");
  127.         printf("Bad directory: %s\n", dirname);
  128.         return;
  129.         }
  130.  
  131.     numdirs++;    
  132.  
  133.     while ((dp = readdir(dirp)) != NULL){
  134.         pathname[0] = NULL;
  135.  
  136.         if((!strcmp(dp->d_name, ".")) || (!strcmp(dp->d_name, "..")))
  137.             continue;
  138.         strcat(pathname,dirname);
  139.         strcat(pathname,"/");
  140.         strcat(pathname,dp->d_name);
  141.  
  142.         if (lstat(pathname, bufaddr) != NULL){
  143.             perror("walkdir stat");
  144.             printf("pathname is %s\n",pathname);
  145.             }
  146.             
  147.         if (S_ISDIR(bufaddr->st_mode))
  148.             walkdir(pathname);
  149.         else{
  150.             totalfiles++;
  151.  
  152.             /* determine time from last time data was
  153.                modified until current time         */
  154.  
  155.             diff = bufaddr->st_atime-bufaddr->st_mtime;
  156.  
  157.             /* determine time from last access to now */
  158.  
  159.             diffnow = time(0) - bufaddr->st_atime;
  160.  
  161.             if((diff == NEVERUSED) && (diffnow > NOTIN30))
  162.                 neverused++;
  163.             if(diffnow < NOTIN30) 
  164.                 active++;
  165.             if(diffnow > NOTIN30)
  166.                 notin30++;
  167.             if(diffnow > NOTIN90)
  168.                 notin90++;
  169.             if((diff < NOTIN30) && (diffnow > NOTIN30))
  170.                 since30++;
  171.             if((diff < NOTIN90) && (diffnow > NOTIN30))
  172.                 since90++;
  173.  
  174.             totalage+=(((float)(time(0)-bufaddr->st_mtime))/
  175.                   (float)(SECONDSINDAY));
  176.         }
  177.  
  178.     }
  179.  
  180.     closedir (dirp);
  181. }
  182.  
  183.  
  184.