home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.3.4.17 [SPARC, PA-RISC] / nextstep33_risc.iso / private / etc / uucp / UUAIDS / uucp.day.sh < prev    next >
Linux/UNIX/POSIX Shell Script  |  1995-02-05  |  12KB  |  368 lines

  1. #! /bin/sh
  2. #
  3. #
  4. #    uucp.day [-x] [-l]
  5. #
  6. #    this script is, as most, continuously under developement. the
  7. # main reason for this script is to keep stats on uucp traffic in order to 
  8. # better understand who we talk to and to help justify alternate networking
  9. # ideas (x.25). 
  10. #    problems to Cal_Thixton@Next.COM
  11. #        cal thixton
  12. #
  13. # crontab:  20 23 * * * su uucp -c /usr/lib/uucp/uucp.day
  14. #
  15. # automatic logging to some master log site on the net....
  16. #
  17. # aliases: uucpstats: "|/usr/lib/uucp/uucp.day -l"
  18. #
  19. #    -l    log this report to STATSDIR
  20. #    -x    turn debugging on. do nothing damaging.
  21. #    
  22. #  
  23. #                            next
  24. #                       UUCP GENERAL ACCOUNTING
  25. #                          Mon Jun 15 1987
  26. # Site       Bytes   Time         Bytes      Calls    Cost ($ ATT)   Files  BPS
  27. #            Total hh:mm:ss    Out     In   Out Tot  Today  To_Date Out In
  28. # austsun    41191  0:06:37   24488   16703  0  3 $   2.25   65.80  10  10  103
  29. # cipric       901  0:00:11     410     491  0  2 $   0.00    0.00   1   1   75
  30. # convex   1391749  1:50:13 1390921     828  0  3 $   0.00    0.00 145   2  210
  31. # edsr     4011514 10:03:58 4011514       0  0  0 $   0.00    0.00 281   0  110
  32. # housun     20314  0:03:56   20314       0  0  4 $   1.57    1.57   8   0   85
  33. # kcsun        839  0:00:03     406     433  0  1 $   0.02    0.02   1   0  209
  34. # milano       919  0:00:15     410     509  0  2 $   0.09    0.09   1   1   57
  35. # sunarch      914  0:00:09     414     500  0  2 $   0.05    4.22   1   1   91
  36. # suntan      3109  0:00:30    1521    1588  0  2 $   0.00    0.00   2   3  100
  37. # ti-csl     18211  0:02:57    1533   16678  0  7 $   0.00    0.00   2   3  102
  38. # tulsun     76674  0:06:04   75215    1459  0 14 $   1.58   10.64  26   2  210
  39. # tundra     62273  0:04:34   61773     500  0  2 $   1.51    1.51  26   1  226
  40. # uokmax     81172  0:12:35   80334     838  0  4 $   0.00    0.00   3   2  107
  41. # | Days:   9 $/day: $   9.32            Total:   $   7.05   83.84 Bytes: 5709780
  42. #
  43. #    most of the colums should be obvious.
  44. # Call_Out: The number of calls we initiated.
  45. # Call_Tot: Total number of calls (in and out). call_tot - call_out = the # of calls
  46. #    we received.
  47. # Cost_today: an estimate of ATT charges for that day based on L-costs. call att and
  48. #    get a $/minute rate for day/direct_dial.
  49. # Cost_Todate: a running sum of the estimated att costs.
  50. # Files_Out: files sent
  51. # Files_In: files received
  52. # Days: number of days since the total was reset back to 0.
  53. # $/Day: average cost per day in $.
  54. # Total: first number is the estimated cost for today followed by running total for
  55. #    x days.
  56. # BPS - average bytes/sec
  57. #
  58. #    1/15/88 - add sed to clean up incoming email when logging.
  59. #        delete unnecessary headers. tj
  60. #    1/15/88 - do not run uucpanz. tj
  61. #
  62. # daily UUCP cleanup
  63. #
  64. PATH="/bin:/usr/bin:/usr/lib/uucp:/usr/ucb:/etc:/usr/local:" ; export PATH
  65. HOSTNAME=`hostname`
  66. LIBDIR=/usr/lib/uucp
  67. UUCLEAN=${LIBDIR}/uuclean
  68. CHOWN=/usr/etc/chown
  69. SPOOLDIR=/usr/spool/uucp
  70. STATSDIR=${SPOOLDIR}/STATS
  71. #
  72. #    if we are started up with -l, assume that we are being 
  73. # given an account report from some site and that we need to save it.
  74. #
  75. if [ "$1"x = -xx ]; then
  76.     DEBUG=true
  77.     shift
  78. fi
  79. if [ "$1"x = -lx ]; then
  80.     sed \
  81.         -e "/^Received: /d" \
  82.         -e "/^    id A[A-C][0-9]/d" \
  83.         -e "/^Return-Path: /d" \
  84.         -e "/^Posted-/d" \
  85.         -e "/^Message-Id: /d" \
  86.         -e "/^Status: /d" \
  87.         -e '/^UUCP ANALYZER:/,$d' > /tmp/uucpstats.$$
  88.     SUB=`grep '^Subject: .* UUCP GENERAL ACCOUNTING' /tmp/uucpstats.$$`
  89.     set ${SUB:=none}
  90.     if [ ! "$3" = UUCP -o ! "$4" = GENERAL -o ! "$5" = ACCOUNTING ]; then
  91.         cat /tmp/uucpstats.$$ >> ${STATSDIR}/trash
  92.         ${CHOWN} -f uucp ${STATSDIR}/trash
  93.         rm -f /tmp/uucpstats.$$
  94.         exit 0
  95.     fi
  96.     if [ ! -d ${STATSDIR} ]; then
  97.         mkdir ${STATSDIR}
  98.         chmod 777 ${STATSDIR}
  99.         ${CHOWN} -f uucp ${STATSDIR}
  100.     fi
  101.     DIR=${STATSDIR}/`echo $2 | sed 's,/.*,,'`
  102.     if [ ! -d ${DIR} ]; then
  103.         mkdir ${DIR}
  104.         chmod 777 ${DIR}
  105.         ${CHOWN} -f uucp ${DIR}
  106.     fi
  107.     FILE=`echo $2 | sed -e 's/Jan/01/' -e 's/Feb/02/' -e 's/Mar/03/' \
  108.         -e 's/Apr/04/' -e 's/May/05/' -e 's/Jun/06/' \
  109.         -e 's/Jul/07/' -e 's/Aug/08/' -e 's/Sep/09/' \
  110.         -e 's/Oct/10/' -e 's/Nov/11/' -e 's/Dec/12/' \
  111.         -e 's/\([0-9][0-9]\)\.\([0-9][0-9]\)\.\(19[0-9][0-9]\)/\3.\1.\2/'`
  112.     if [ -f ${STATSDIR}/${FILE} ]; then
  113.         mv /tmp/uucpstats.$$ ${STATSDIR}/${FILE}.$$
  114.         ${CHOWN} -f uucp ${STATSDIR}/${FILE}.$$
  115.     else
  116.         mv /tmp/uucpstats.$$ ${STATSDIR}/${FILE}
  117.         ${CHOWN} -f uucp ${STATSDIR}/${FILE}
  118.     fi
  119.     exit 0
  120. fi
  121. set `date`
  122. DAY=$1
  123. MON=$2
  124. YEAR=$6
  125. #
  126. #    DAYMON: Day of the month 1-31
  127. #
  128. DAYMON=$3
  129. #
  130. #    MONTH: Change Month name into a number
  131. #
  132. MONTH=`echo ${MON} | sed -e 's/Jan/01/' -e 's/Feb/02/' -e 's/Mar/03/' \
  133.     -e 's/Apr/04/' -e 's/May/05/' -e 's/Jun/06/' \
  134.     -e 's/Jul/07/' -e 's/Aug/08/' -e 's/Sep/09/' \
  135.     -e 's/Oct/10/' -e 's/Nov/11/' -e 's/Dec/12/' \
  136.     -e 's/\([0-9][0-9]\)\.\([0-9][0-9]\)\.\(19[0-9][0-9]\)/\3.\1.\2/'`
  137. #
  138. #    prefix 1-9 days with 0 so that ls(1)'s work on listing the 
  139. #    STATS directory and keep the files in chronological order.
  140. #
  141. if [ ${DAYMON} -lt 10 ]; then
  142.     DAYMON=0${DAYMON}
  143. fi
  144. #
  145. #    DATANAME: e.g. 1989.03.03       Year, Month, Day
  146. #
  147. DATENAME=${YEAR}.${MONTH}.${DAYMON}
  148. HOURS=720
  149. if [ ! -n "$DEBUG" ]; then
  150.     SYS=${SPOOLDIR}/SYSLOG.$$
  151. else
  152.     SYS=${SPOOLDIR}/SYSLOG
  153. fi
  154.  
  155. #
  156. #    STATSPERSON - place to email these stats to. this is really not
  157. #    a person but a archival script
  158. #
  159. STATSPERSON=uucpstats@localhost.com
  160. #
  161. #    L-costs has a list of sites and cost in $/minute. e.g.
  162. #        sun             cost    0.34
  163. #        housun          cost    0.40
  164. #    keyword 'cost' is required.
  165. #
  166. if [ -f /etc/uucp/L-costs ]; then
  167.     COSTS=/etc/uucp/L-costs
  168. elif [ -f /usr/lib/uucp/L-costs ]; then
  169.     COSTS=/usr/lib/uucp/L-costs
  170. else
  171.     COSTS=/dev/null
  172. fi
  173. #
  174. #
  175. #    LAST_REPORT - most recent report (yesterdays). used to 
  176. #    general cost_to_date estimates.
  177. #
  178. LASTREPORT=${STATSDIR}/LAST_REPORT
  179. if [ ! -f ${LASTREPORT} ]; then
  180.     touch ${LASTREPORT}
  181. fi
  182. #
  183. #    Figure out where the Logging directory is
  184. #
  185. echo hi
  186. if [ -d ${SPOOLDIR}/OLD ]; then
  187.     LOGDIR=${SPOOLDIR}/OLD
  188. elif [ -d ${SPOOLDIR}/LOG ]; then
  189.     LOGDIR=${SPOOLDIR}/LOG
  190. else
  191.     LOGDIR=/tmp
  192. fi
  193. LASTLOG=${LOGDIR}/LOGFILE.last
  194. #
  195. #    check to see if the stats directory is there and create it if not
  196. #
  197. if [ ! -d ${STATSDIR} ]; then
  198.     mkdir ${STATSDIR}
  199.     chmod 777 ${STATSDIR}
  200.     ${CHOWN} -f uucp ${STATSDIR}
  201. fi
  202. REPORTFILE=${DATENAME}
  203. NEWREPORT=${STATSDIR}/${REPORTFILE}
  204. if [ -f ${NEWREPORT} ]; then
  205.     NEWREPORT=${STATSDIR}/${REPORTFILE}.$$
  206. fi
  207. #
  208. #    UUclean a bunch of things
  209. #
  210. if [ ! -n "$DEBUG" ]; then 
  211.     for i in \
  212.         ${SPOOLDIR}/AUDIT \
  213.         ${SPOOLDIR}/CORRUPT \
  214.         ${SPOOLDIR}/LCK \
  215.         ${SPOOLDIR}/STST \
  216.         ${SPOOLDIR}/TM. \
  217.         ${SPOOLDIR}/X. \
  218.         ${SPOOLDIR}/XTMP \
  219.         ${SPOOLDIR}/C. \
  220.         ${SPOOLDIR}/D. \
  221.         ${SPOOLDIR}/D.${HOSTNAME} \
  222.         ${SPOOLDIR}/D.${HOSTNAME}X
  223.     do
  224.         if [ -d ${i} ]; then
  225.             $UUCLEAN -p -n${HOURS} -d${i}
  226.         fi
  227.     done
  228. fi
  229.  
  230. if [ ! -n "$DEBUG" ]; then $UUCLEAN -pSTST. -pTM. -pLTMP. -pLOG. -pX. -n4 -d${SPOOLDIR} ; fi
  231. if [ ! -n "$DEBUG" ]; then $UUCLEAN -pLCK. -n8 -d${SPOOLDIR} ; fi
  232. if [ ! -n "$DEBUG" ]; then $UUCLEAN -d${LIBDIR}/.XQTDIR -p -n72 ; fi
  233.  
  234. #
  235. # Begin new log at beginning of each month if either this is the first
  236. # day of the month or uucp.startnewmonth has something in it. (since /usr/adm
  237. # is owned by root and it is uncertain who cron runs as, then modify the file
  238. # instead of deleting/creating it to indicate change).
  239. #
  240. #    if uucp.newmonth has the keywords 'newmonth <some_month>',
  241. #    awk will assume this is a new month.
  242. #
  243. if [ ${DAYMON} = 01 ] ; then
  244.     NEWMONTH="newmonth ${MON}"
  245. else
  246.     NEWMONTH="${MON}"
  247. fi
  248.  
  249. # Old spool/log files are kept by the naming scheme:
  250. # LOGFILE.${YEAR}.${MONTH}.${DAYMON} where ${DAY} is the day of the week,
  251. # Sun to Sat, and ${DAYMON} is the numerical day of the month.  SYSLOG
  252. # files are kept by the scheme:  SYSLOG.week: the current week's: totals;
  253. # SYSLOG.month:  the current month's totals; SYSLOG.${month} where
  254. # ${month} is the first three letters of the month: the totals for that
  255. # month.
  256.  
  257. if [ ! -n "$DEBUG" ]; then
  258.     touch ${SPOOLDIR}/LOGFILE
  259.     ${CHOWN} -f uucp ${SPOOLDIR}/LOGFILE
  260.     touch ${LOGDIR}/LOGFILE.${DATENAME}
  261.     ${CHOWN} -f uucp ${LOGDIR}/LOGFILE.${DATENAME}
  262.     mv ${SPOOLDIR}/LOGFILE ${LOGDIR}/LOGFILE.${DATENAME}
  263.     rm -f ${LASTLOG} 
  264.     ln ${LOGDIR}/LOGFILE.${DATENAME} ${LASTLOG}
  265.     touch ${SPOOLDIR}/SYSLOG
  266.     ${CHOWN} -f uucp ${SPOOLDIR}/SYSLOG
  267.     touch ${SPOOLDIR}/SYSLOG.$$
  268.     ${CHOWN} -f uucp ${SPOOLDIR}/SYSLOG.$$
  269.     mv ${SPOOLDIR}/SYSLOG ${SPOOLDIR}/SYSLOG.$$
  270.     touch ${SPOOLDIR}/SYSLOG
  271.     ${CHOWN} -f uucp ${SPOOLDIR}/SYSLOG
  272.     touch ${SPOOLDIR}/LOGFILE
  273.     ${CHOWN} -f uucp ${SPOOLDIR}/LOGFILE
  274.     cat ${SPOOLDIR}/SYSLOG.$$ >> ${LOGDIR}/SYSLOG.week
  275.     if [ -f ${SPOOLDIR}/ERRLOG ]; then
  276.         touch ${SPOOLDIR}/ERRLOG
  277.         ${CHOWN} -f uucp ${SPOOLDIR}/ERRLOG
  278.         touch ${LOGDIR}/ERRLOG.${DATENAME}
  279.         ${CHOWN} -f uucp ${LOGDIR}/ERRLOG.${DATENAME}
  280.         mv ${SPOOLDIR}/ERRLOG ${LOGDIR}/ERRLOG.${DATENAME}
  281.         touch ${SPOOLDIR}/ERRLOG
  282.         ${CHOWN} -f uucp ${SPOOLDIR}/ERRLOG
  283.     fi
  284. fi
  285. #
  286. # make sure these are there when cat'ed below else the script could die
  287. #
  288. # send syslog stats to administrator.
  289. #
  290. # b - total bytes
  291. # c - costs in $ ATT
  292. # d - dialout to site
  293. # f - failed attempts to call site
  294. # o - total cost to date in $
  295. # r - bytes recieved
  296. # s - bytes sent
  297. # t - total time in seconds
  298. # v - conversations with site
  299. # x - files sent
  300. # y - files recieved
  301. #
  302. (
  303. echo " "
  304. echo "                             ${HOSTNAME}"
  305. echo "                      UUCP GENERAL ACCOUNTING"
  306. echo "                         ${DAY} ${MON} ${DAYMON} ${YEAR}"
  307. echo "Site       Bytes   Time         Bytes      Calls    Cost ($ ATT)   Files  BPS Fail"
  308. echo "           Total hh:mm:ss    Out     In   Out Tot  Today  To_Date Out In"
  309. (
  310. (
  311.     echo hostname ${HOSTNAME}
  312.     echo ${NEWMONTH}
  313.     cat ${LASTREPORT} ${LASTLOG} ${COSTS} ${SYS} ) | awk ' { \
  314.       if ( $1 == "hostname" )           { hostname = $2 } \
  315.       if ( $1 == "newmonth" )           { newmonth = 1 } \
  316.           if ( $6 == "data" )               { b[ $2 ] += $7 ; t[ $2 ] += $9 } \
  317.       if ( $5 == "sent" )               { s[ $2 ] += $7 ; x[ $2 ] += 1 } \
  318.       if ( $5 == "received" )           { r[ $2 ] += $7 ; y[ $2 ] += 1 } \
  319.       if ( $2 == "cost" && ( $4 == "" || $4 == hostname )) \
  320.                             { c[ $1 ] = $3 } \
  321.       if ( $8 == "$" && newmonth == 0 ) { o[ $1 ] = $10 } \
  322.       if ( $1 == "|" && newmonth == 0 ) { days = $3 } \
  323.       if ( $4 == "SUCCEEDED" && $5 == "(call" ) { d[ $2 ] += 1 } \
  324.       if ( $4 == "FAILED" && $5 == "(call" ) { f[ $2 ] += 1 } \
  325.       if ( $5 == "(conversation" && $6 == "complete)" ) { v[ $2 ] += 1 } \
  326.     }  \
  327.     END { for ( i in b ) {  \
  328.     printf("%-8.8s %7d %2d:%02d:%02d %7d %7d %2d %2d $ %6.2f %7.2f %3d %3d %4d %3d\n", \
  329.     i, b[i], t[i]/3600, (t[i]%3600)/60, (t[i]%3600)%60, s[i], r[i], \
  330.     d[i], v[i], (t[i]/60)*c[i], ((t[i]/60)*c[i])+o[i], x[i]/2, y[i]/2, \
  331.     b[i]/(t[i]+1), f[i]) ; \
  332.     totalbytes += b[i] ; day += (t[i]/60)*c[i] ; \
  333.     todate += ((t[i]/60)*c[i])+o[i] } \
  334.     printf("| Days: %3d $/day: $ %6.2f            Total:   $ %6.2f %7.2f Bytes: %d\n", \
  335.     (days + 1), todate/(days + 1), day, todate, totalbytes ) }' ) | \
  336.     sort ) > /tmp/.uucp.day.report
  337.  
  338. if [ ! -n "$DEBUG" ]; then
  339.     mv /tmp/.uucp.day.report ${NEWREPORT}
  340.     ${CHOWN} -f uucp ${NEWREPORT}
  341.     if [ -n "${STATSPERSON}" ]; then
  342.         Mail -s "${HOSTNAME}/${REPORTFILE} UUCP GENERAL ACCOUNTING" ${STATSPERSON} < ${NEWREPORT}
  343.     fi
  344.     rm -f ${LASTREPORT}
  345.     ln -s `basename ${NEWREPORT}` ${LASTREPORT}
  346.     rm -f ${SYS}
  347. fi
  348.  
  349. #
  350. #  Remove LOGFILEs older than one week; save SYSLOG
  351. #  files forever.  Remove them manually if desired.
  352. #
  353.  
  354. if [ ! -n "$DEBUG" ]; then find ${LOGDIR} -name 'LOGFILE.*' -mtime +7 -exec rm -f {} \; ; fi
  355.  
  356. #
  357. #  Poll morning systems
  358. #
  359. if [ ! -n "$DEBUG" ]; then
  360.     for i in `uuname`
  361.     do
  362.         touch ${SPOOLDIR}/C./C.${i}n0000
  363.     done
  364. fi
  365. exit 0
  366.  
  367.  
  368.