home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume39 / enh-du2 / part02 / ngsizes < prev    next >
Encoding:
Text File  |  1993-08-21  |  5.9 KB  |  235 lines

  1. : '@(#) ngsizes 1.7 93/08/18 00:09:10'
  2. #
  3. # ngsizes - Generate disk usage summary for USENET newsgroups.
  4. #
  5. # Copyright 1990-1993, Unicom Systems Development.  All rights reserved.
  6. # See accompanying README file for terms of distribution and use.
  7. #
  8. # Usage:
  9. #
  10. #   ngsizes [-D] [-b breakdown_list] [-t threshold]
  11. #
  12. #    -t  Specifies only groups using "threshold" or more disk blocks should
  13. #        be reported.  The default is defined by the "threshold" parameter
  14. #        below.
  15. #
  16. #    -b  Specifies how usage should be broken down versus age.  For example,
  17. #        saying "-b 0,7,14" will report usage in three columns:  the total
  18. #        usage, the usage by articles a week or older, and the usage by
  19. #        articles two weeks or older.  The default is defined by the
  20. #        "breakdown" parameter below.
  21. #
  22. #    -D  For debugging, the temporary files will be maintained.
  23. #
  24.  
  25. USAGE="usage: $0 [-b breakdown_list] [-t threshold]"
  26.  
  27.  
  28. ##############################################################################
  29. #
  30. # Site-specific definitions.
  31. #
  32.  
  33. SPOOLDIR=/usenet/spool/news    # Pathname to the Usenet spool directory.
  34. ACTIVE=/usenet/lib/news/active    # Pathname to the list of active newsgroups.
  35. DU=du                # Pathname to the enhanced "du" command.
  36.  
  37. #
  38. # Pick one of the following.  It specifies how to account for the disk
  39. # space used by cross-posted articles.
  40. #
  41. DU_LINKOPTS=-l    # Count xposted article every time it appears.
  42. #DU_LINKOPTS=    # Count xposted article only first time it is encountered.
  43. #DU_LINKOPTS=-L    # Average usage across newsgroups in which it appears.
  44.  
  45. #
  46. # Pick one of the following.  It specifies how you want usage reported.
  47. #
  48. DU_BSIZE=-k    # Report usage in KB.
  49. #DU_BSIZE=    # Report usage however du normally does.
  50.  
  51. #
  52. # Specify the default reporting threshold.  Newsgroups with usage below
  53. # this value will not be reported.  The units for this number depend upon
  54. # whatever you specified above for DU_BSIZE.  This default can be overridden
  55. # by the "-t" command line option.
  56. #
  57. DFLT_THRESHOLD=0
  58.  
  59. #
  60. # Specify the default for the breakdown.  There will be one column in
  61. # the output for every number in the breakdown list.  Each value in the
  62. # list specifies a number of days, and the corresponding column will show
  63. # the disk usage by articles that are that many days or older.  This default
  64. # can be overridden by the "-b" command line option.
  65. #
  66. DFLT_BREAKDOWN=0,1,3,5,7,15
  67.  
  68. #
  69. # Work Files:
  70. #    $TMP.read    Readership statistics.
  71. #    $TMP.ngs    List of all newsgroups to check.
  72. #    $TMP.du        Disk usage for all directories in the news spool dir.
  73. #
  74. TMP=/tmp/ngsz$$
  75. trap 'rm -f $TMP.* ; exit 1' 1 2 3
  76.  
  77. #
  78. # End of site-specific customizations.
  79. #
  80. ##############################################################################
  81.  
  82.  
  83. #
  84. # Initialize.
  85. #
  86. debug=0
  87. threshold=$DFLT_THRESHOLD
  88. breakdown=$DFLT_BREAKDOWN
  89.  
  90. #
  91. # Crack the command line options.
  92. #
  93. set -- `getopt 'Db:t:' $*`
  94. if [ $? -ne 0 ] ; then
  95.     echo "$USAGE" 1>&2
  96.     exit 1
  97. fi
  98. while : ; do
  99.     case "$1" in
  100.     -D)  TMP=/tmp/ngsz debug=1 ; trap '' 1 2 3    ; shift ;;
  101.     -b)  breakdown="$2"                ; shift 2 ;;
  102.     -t)  threshold="$2"                ; shift 2 ;;
  103.     --)  shift ; break ;;
  104.     *)   echo "$USAGE" 1>&2                ; exit 1 ;;
  105.     esac
  106. done
  107. if [ $# -ne 0 ] ; then
  108.     echo "$USAGE" 1>&2
  109.     exit 1
  110. fi
  111.  
  112. #
  113. # Verify we can find the active file.
  114. #
  115. if [ ! -r $ACTIVE ] ; then
  116.     echo "$0: file '$ACTIVE' not found or unreadable" 1>&2
  117.     [ $debug -eq 0 ] && rm -f $TMP.*
  118.     exit 1
  119. fi
  120.  
  121. #
  122. # Get a count of the readers for each newsgroup.
  123. # Output format will be "readership_count newsgroup_name"
  124. #
  125. for newsrc in `awk -F: '{ print $6 "/.newsrc" }' /etc/passwd | sort -u` ; do
  126.     test -f $newsrc && sed -n 's/:.*//p' $newsrc
  127. done | sort | uniq -c > $TMP.read
  128.  
  129. #
  130. # Build a sorted list of all known newsgroups from the active file.
  131. #
  132. sed '
  133.     s/[     ].*//
  134.     /^$/d
  135. ' $ACTIVE | sort -u > $TMP.ngs
  136.  
  137. #
  138. # Scan the spool directory for disk usage.  Convert the newsgroup pathname
  139. # to a newsgroup name, and move it to the first field on the line.
  140. # Output format will be "newsgroup_name usage usage ..."
  141. #
  142. if [ $debug -ne 0 -a -f $TMP.du ] ; then
  143.     : suppress scan for debugging
  144. else
  145.     $DU -ir $DU_LINKOPTS $DU_BSIZE -c "$breakdown" $SPOOLDIR    \
  146.     | sed                                \
  147.         -e 's/^\(.*\)    \([^    ]*\)$/\2    \1/'        \
  148.         -e "s!$SPOOLDIR/!!"                    \
  149.         -e "s!/!.!g"                        \
  150.     | sort -u                            \
  151.         > $TMP.du
  152. fi
  153.  
  154. #
  155. # Generate the report.
  156. #
  157. (
  158.     echo "BREAKDOWN $breakdown" | sed 's/,/ /g'
  159.     echo "THRESHOLD $threshold"
  160.     sed 's/^/READERS /' $TMP.read
  161.     join $TMP.du $TMP.ngs | sort -rn +1 | sed 's/^/NEWSGROUP /'
  162. ) | awk '
  163.  
  164. BEGIN {
  165.     LINE_WIDTH = 79        # maximum length of a line
  166.     NG_WIDTH = 26        # width of field to print newsgroup in
  167.     READR_WIDTH = 4        # width of field to print number of readers in
  168.     FRONT_FMT = "%-" NG_WIDTH "." NG_WIDTH "s" "%" READR_WIDTH "s"
  169. }
  170.  
  171. #
  172. # Record "BREAKDOWN n1 n2 ..."
  173. #   Defines the format for the newsgroup usage lines.  Each "n" corresponds
  174. #   to one column in the newsgroup usage line, and specifies the age of
  175. #   articles which consume this amount of disk space.
  176. #
  177. $1 == "BREAKDOWN" {
  178.     num_breakdn = NF - 1
  179.     FIELD_WIDTH = ( LINE_WIDTH - (NG_WIDTH+READR_WIDTH) ) / num_breakdn
  180.     if ( FIELD_WIDTH > 8 )
  181.         FIELD_WIDTH = 8
  182.     FIELD_FMT = "%" FIELD_WIDTH "s"
  183.     printf(FRONT_FMT,"newsgroup","read")
  184.     for ( i = 0 ; i < num_breakdn ; ++i )
  185.         printf(FIELD_FMT,sprintf("%ddays",$(i+2)))
  186.     printf("\n")
  187.     next
  188. }
  189.  
  190. #
  191. # Record "THRESHOLD n"
  192. #   Indicates we only want to see newsgroups using "n" or more blocks.
  193. #
  194. $1 == "THRESHOLD" {
  195.     threshold = $2
  196.     next
  197. }
  198.  
  199. #
  200. # Record "READERS n ng"
  201. #   Indicates that newsgroup "ng" has "n" readers.
  202. #
  203. $1 == "READERS" {
  204.     num_readers[$3] = $2
  205.     next
  206. }
  207.  
  208. #
  209. # Record "NEWSGROUP ng n1 n2 ..."
  210. #   Indicates the disk usage of newsgroup "ng".  Each "n" specifies the
  211. #   diskspace used by articles "ndays" or older, where "ndays" is defined
  212. #   by the BREAKDOWN record.
  213. #
  214. $1 == "NEWSGROUP" {
  215.     if ( $3 >= threshold ) {
  216.         if ( num_readers[$2] == "" )
  217.             num_readers[$2] = 0
  218.         printf(FRONT_FMT,$2,num_readers[$2])
  219.         for ( i = 0 ; i < num_breakdn ; ++i )
  220.             printf(FIELD_FMT,$(i+3))
  221.         printf("\n")
  222.     }
  223.     next
  224. }
  225.  
  226. {
  227.     printf("ngsizes - bad line '%s'\n", $0) | "cat 1>&2"
  228. }
  229.  
  230. '
  231.  
  232. [ $debug -eq 0 ] && rm -f $TMP.*
  233. exit 0
  234.  
  235.