home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 15 / 15.iso / s / s053 / 9.ddi / usr / lib / acct / runacct < prev    next >
Encoding:
Text File  |  1990-12-08  |  12.5 KB  |  427 lines

  1. #!/sbin/sh
  2. #    Copyright (c) 1990 UNIX System Laboratories, Inc.
  3. #    Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T
  4. #      All Rights Reserved
  5.  
  6. #    THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF
  7. #    UNIX System Laboratories, Inc.
  8. #    The copyright notice above does not evidence any
  9. #    actual or intended publication of such source code.
  10.  
  11.  
  12.  
  13. #ident    "@(#)/usr/lib/acct/runacct.sl 1.1 4.0 12/08/90 59498 AT&T-USL"
  14. #       "nitely accounting shell, should be run from cron (adm) at 4am"
  15. #    "does process, connect, disk, and fee accounting"
  16. #    "prepares command summaries"
  17. #    "shell is restartable and provides reasonable diagnostics"
  18. _adm=/var/adm
  19. _nite=/var/adm/acct/nite
  20. _sum=/var/adm/acct/sum
  21. _wtmp=/var/adm/wtmp
  22. export PATH
  23. PATH=/usr/lib/acct:/usr/bin:/usr/sbin
  24. _statefile=${_nite}/statefile
  25. _active=${_nite}/active
  26. _lastdate=${_nite}/lastdate
  27. _errormsg="\n\n************ ACCT ERRORS : see  ${_active}${_date}********\n\n"
  28. _MIN_BLKS=500
  29.  
  30. cd ${_adm}
  31. #        "make sure that 2 crons weren't started, or leftover problems"
  32. date  > ${_nite}/lock1
  33. chmod 400 ${_nite}/lock1
  34. ln ${_nite}/lock1 ${_nite}/lock
  35. if test $? -ne 0; then
  36.     _lnkerr="\n\n*********** 2 CRONS or ACCT PROBLEMS***********\n\n\n"
  37.     (date ; echo "$_lnkerr" ) >/dev/sysmsg
  38.     echo "$_lnkerr" | mail adm root
  39.     echo "ERROR: locks found, run aborted" >> ${_active}
  40.     rm -f ${_nite}/lock*
  41.     exit 1
  42. fi
  43.  
  44. # Check to see if there is enough space in /var to do nitely accounting
  45. #
  46. _dev=`devnm /var | awk '{print $1}' | sed -e "s;/dev/;;"`
  47. _blocks=`df /dev/$_dev | sed "s/.*:  *\([0-9][0-9]*\) blocks.*/\1/"`
  48. if [ "$_blocks" -le $_MIN_BLKS ];then
  49.     echo "runacct: Insufficient space in /var ($_blocks blks); \c"
  50.     echo "Terminating procedure"
  51.     ( echo "runacct: Insufficient space in /var ($_blocks blks); \c"
  52.     echo "Terminating procedure" ) | \
  53.          tee /dev/sysmsg ${_active} | mail root adm
  54.     rm -f ${_nite}/lock*
  55.     exit 1
  56. fi
  57.  
  58.  
  59. case $# in
  60. 0)
  61. #    "as called by the cron each day"
  62.     _date="`date +%m%d`"
  63.     if test ! -r ${_lastdate} ; then
  64.         echo "0000" > ${_lastdate}
  65.     fi
  66.     if test "${_date}" = "`cat ${_lastdate}`"; then
  67.         (date; echo "${_errormsg}") > /dev/sysmsg
  68.         echo "${_errormsg}" | mail root adm
  69.         echo "ERROR: acctg already run for `date`: check ${_lastdate}" >> ${_active}
  70.         rm -f ${_nite}/lock*
  71.         mv ${_active} ${_active}${_date}
  72.         exit 1
  73.     fi
  74.     echo ${_date} > ${_lastdate}
  75.     echo "SETUP" > ${_statefile}
  76.     nulladm ${_active}
  77.     echo ${_date} > ${_active}    # debuging
  78.     echo "\n\n\n\n\n**********  SYSTEM ACCOUNTING STARTED `date`  **********\n\n\n\n\n" > /dev/sysmsg
  79.     echo ${_date} > ${_active}    # debuging
  80.     ;;
  81.  
  82. 1)
  83. #    "runacct MMDD  (date)  will restart at current state"
  84.     _date=$1
  85.     echo "restarting acctg for ${_date} at `cat ${_statefile}`" >> ${_active}
  86.     echo "\n\n\n\n\n********** SYSTEM ACCOUNTING RESTARTED `date` **********\n\n\n\n\n" > /dev/sysmsg
  87.     ;;
  88.  
  89. 2)
  90. #    "runacct MMDD STATE  restart at specified state"
  91.     _date=$1
  92.     echo "restarting acctg for ${_date} at $2" >> ${_active}
  93.     echo "previous state was `cat ${_statefile}`" >> ${_active}
  94.     echo "$2" > ${_statefile}
  95.     echo "\n\n\n\n\n********** SYSTEM ACCOUNTING RESTARTED `date` **********\n\n\n\n\n" > /dev/sysmsg
  96.     ;;
  97. *)
  98.     (date; echo "${_errormsg}") > /dev/sysmsg
  99.     echo "${_errormsg}" | mail root adm
  100.     echo "ERROR: runacct called with invalid arguments" > ${_active}
  101.     rm -f ${_nite}/lock*
  102.     mv ${_active} ${_active}${_date}
  103.     exit 1
  104.     ;;
  105. esac
  106.  
  107.  
  108. #    "processing is broken down into seperate, restartable states"
  109. #    "the statefile is updated at the end of each state so that the"
  110. #    "next loop through the while statement switches to the next state"
  111. while [ 1 ]
  112. do
  113. case "`cat ${_statefile}`" in
  114. SETUP)
  115.  
  116. cd ${_adm}
  117.  
  118. (date ; ls -l fee pacct* ${_wtmp}* ) >> ${_active}
  119.  
  120. #    "switch current pacct file"
  121. turnacct switch
  122. _rc=$?
  123. if test ${_rc} -ne 0; then
  124.     (date ; echo "${_errormsg}" ) > /dev/sysmsg
  125.     echo "${_errormsg}" | mail root adm
  126.     echo "ERROR: turnacct switch returned rc=${_rc}" >> ${_active}
  127.     rm -f ${_nite}/lock*
  128.     mv ${_active} ${_active}${_date}
  129.     exit 1
  130. fi
  131.  
  132. #    " give pacct files unique names for easy restart "
  133. for _i in pacct?*
  134. do
  135.     if test -r S${_i}.${_date} ; then
  136.          (date ; echo "${_errormsg}" ) > /dev/sysmsg
  137.         echo "${_errormsg}" | mail root adm
  138.         echo "ERROR: S${_i}.${_date} already exists" >> ${_active}
  139.         echo "file setups probably already run" >> ${_active}
  140.         rm -f ${_nite}/lock*
  141.         mv ${_active} ${_active}${_date}
  142.         exit 1
  143.     fi
  144.     mv ${_i} S${_i}.${_date}
  145. done
  146.  
  147.  
  148. #    "add current time on end"
  149. if test -r ${_nite}/wtmp.${_date} ; then
  150.     (date ; echo "${_errormsg}" ) > /dev/sysmsg
  151.     echo "${_errormsg}" | mail root adm
  152.     echo "ERROR: ${_nite}/wtmp.${_date} already exists: run setup manually" > ${_active}
  153.     rm -f ${_nite}/lock*
  154.     mv ${_active} ${_active}${_date}
  155.     exit 1
  156. fi
  157. closewtmp    # fudge a DEAD_PROCESS for /var/wtmp
  158. cp ${_wtmp} ${_nite}/wtmp.${_date}
  159. acctwtmp "runacct" >> ${_nite}/wtmp.${_date}
  160. nulladm ${_wtmp}
  161. utmp2wtmp    # fudge active user from utmp to wtmp
  162.  
  163. echo "files setups complete" >> ${_active}
  164. echo "WTMPFIX" > ${_statefile}
  165. ;;
  166.  
  167. WTMPFIX)
  168. #    "verify the integrity of the wtmp file"
  169. #    "wtmpfix will automatically fix date changes"
  170. cd ${_nite}
  171. nulladm tmpwtmp wtmperror
  172. wtmpfix < wtmp.${_date} > tmpwtmp 2>wtmperror
  173. if test $? -ne 0 ; then
  174.     (date ; echo "${_errormsg}") | mail root adm
  175.     echo "${_errormsg}" > /dev/sysmsg
  176.     echo "ERROR: wtmpfix errors see ${_nite}/wtmperror${_date}" >> ${_active}
  177.     rm -f ${_nite}/lock*
  178.     mv ${_active} ${_active}${_date}
  179.     mv wtmperror wtmperror${_date}
  180.     exit 1
  181. fi
  182.  
  183. echo "wtmp processing complete" >> ${_active}
  184. echo "CONNECT" > ${_statefile}
  185. ;;
  186.  
  187.  
  188. CONNECT)
  189. #    "produce connect records"
  190. #    "the lineuse and reboots files are used by prdaily"
  191. cd ${_nite}
  192. nulladm lineuse reboots log ctacct.${_date}
  193. acctcon -l lineuse -o reboots < tmpwtmp  2> log > ctacct.${_date}
  194.  
  195. # if the following test is true, then pnpsplit complained about
  196. # the year and holidays not being up to date.  This used to be
  197. # a fatal error, but now it will continue to process the accounting.
  198. if test -s log ; then 
  199.     (date ; cat ${_nite}/log) | mail adm root
  200.     echo "${_errormsg}" > /dev/sysmsg
  201.     cat ${_nite}/log >> ${_active}${_date}
  202. fi
  203.  
  204. echo "connect acctg complete" >> ${_active}
  205. echo "PROCESS" > ${_statefile}
  206. ;;
  207.  
  208.  
  209. PROCESS)
  210. #    "correlate Spacct and ptacct files by number"
  211. #    "will not process Spacct file if corresponding ptacct exists"
  212. #    "remove the ptacct file to rurun the Spacct file"
  213. #    "if death occurs here, rerunacct should remove last ptacct file"
  214.  
  215. cd ${_nite}
  216. for _Spacct in ${_adm}/Spacct*.${_date}
  217. do
  218.     _ptacct=`basename ${_Spacct} | sed 's/Sp/pt/'`
  219.     if test -s ${_ptacct}; then
  220.         echo "WARNING: accounting already run for ${_Spacct}" \
  221.             >> ${_active}
  222.         echo "WARNING: remove ${_nite}/${_ptacct} to rerun" \
  223.             >> ${_active}
  224.     else
  225.         nulladm ${_ptacct}
  226.         acctprc < ${_Spacct} > ${_ptacct}
  227.     
  228.         echo "process acctg complete for ${_Spacct}" >> ${_active}
  229.     fi
  230. done
  231. echo "all process actg complete for ${_date}" >> ${_active}
  232. echo "MERGE" > ${_statefile}
  233. ;;
  234.  
  235.  
  236. MERGE)
  237. cd ${_nite}
  238. #    "merge ctacct and ptacct files together"
  239. acctmerg ptacct*.${_date} < ctacct.${_date} > daytacct
  240.  
  241. echo "tacct merge to create daytacct complete" >> ${_active}
  242. echo "FEES" > ${_statefile}
  243. ;;
  244.  
  245.  
  246. FEES)
  247. cd ${_nite}
  248. #    "merge in fees"
  249. if test -s ${_adm}/fee; then
  250.     cp daytacct tmpdayt
  251.     sort +0n +2 ${_adm}/fee | acctmerg -i | acctmerg tmpdayt  > daytacct
  252.     echo "merged fees" >> ${_active}
  253.     rm -f tmpdayt
  254. else
  255.     echo "no fees" >> ${_active}
  256. fi
  257. echo "DISK" > ${_statefile}
  258. ;;
  259.  
  260.  
  261. DISK)
  262. cd ${_nite}
  263. #    "the last act of any disk acct procedure should be to mv its"
  264. #    "entire output file to disktacct, where it will be picked up"
  265. if test -r disktacct; then
  266.     cp daytacct tmpdayt
  267.     acctmerg disktacct  < tmpdayt > daytacct
  268.     echo "merged disk records" >> ${_active}
  269.     rm -f tmpdayt
  270.     mv disktacct /tmp/disktacct.${_date}
  271. else
  272.     echo "no disk records" >> ${_active}
  273. fi
  274. echo "MERGETACCT" > ${_statefile}
  275. ;;
  276.  
  277. MERGETACCT)
  278. cd ${_adm}/acct
  279. #    "save each days tacct file in sum/tacct.${_date}"
  280. #    "if sum/tacct gets corrupted or lost, could recreate easily"
  281. #    "the monthly acctg procedure should remove all sum/tacct files"
  282. cp nite/daytacct sum/tacct${_date}
  283. if test ! -r sum/tacct; then
  284.     echo "WARNING: recreating ${_adm}/sum/tacct " >> ${_active}
  285.     nulladm sum/tacct
  286. fi
  287.  
  288. #    "merge in todays tacct with the summary tacct"
  289. cp sum/tacct sum/tacctprev
  290. acctmerg sum/tacctprev  < sum/tacct${_date} > sum/tacct
  291.  
  292. echo "updated sum/tacct" >> ${_active}
  293. echo "CMS" > ${_statefile}
  294. ;;
  295.  
  296.  
  297. CMS)
  298. cd ${_adm}/acct
  299. #    "do command summaries"
  300. nulladm sum/daycms
  301. if test ! -r sum/cms; then
  302.     nulladm sum/cms
  303.     echo "WARNING: recreating ${_adm}/sum/cms " >> ${_active}
  304. fi
  305. cp sum/cms sum/cmsprev
  306. acctcms ${_adm}/Spacct*.${_date}  > sum/daycms
  307. acctcms -s sum/daycms sum/cmsprev  > sum/cms
  308. acctcms -a -s sum/daycms | sed -n 1,56p  > nite/daycms
  309. acctcms -a -s sum/cms | sed -n 1,56p  > nite/cms
  310. lastlogin 
  311. echo "command summaries complete" >> ${_active}
  312. echo "USEREXIT" > ${_statefile}
  313. ;;
  314.  
  315.  
  316. USEREXIT)
  317. #    "any installation dependant accounting programs should be run here"
  318. [ -s /usr/lib/acct/runacct.local ] && /usr/lib/acct/runacct.local
  319.  
  320. echo "CLEANUP" > ${_statefile}
  321. ;;
  322.  
  323.  
  324. CLEANUP)
  325. cd ${_adm}/acct
  326. #    " finally clear files; could be done next morning if desired"
  327. nulladm ${_adm}/fee
  328. rm -f ${_adm}/Spacct*.${_date}
  329. #    "put reports onto a file"
  330. prdaily >> sum/rprt${_date};
  331. rm -f nite/lock*
  332. rm -f nite/ptacct*.${_date} nite/ctacct.${_date}
  333. mv nite/wtmp.${_date} nite/owtmp
  334. rm -f nite/wtmperror${_date} nite/active${_date} nite/tmpwtmp
  335. echo "system accounting completed at `date`" >> ${_active}
  336. echo "********** SYSTEM ACCOUNTING COMPLETED `date` **********" > /dev/sysmsg
  337. echo "COMPLETE" > ${_statefile}
  338. exit 0
  339. ;;
  340.  
  341. *)
  342.     (date;echo "${_errormsg}") > /dev/sysmsg
  343.     echo "${_errormsg}" | mail adm root
  344.     echo "ERROR: invalid state, check ${_statefile}" >> active
  345.     rm -f ${_nite}/lock*
  346.     mv ${_active} ${_active}${_date}
  347.     exit 1
  348.     ;;
  349. esac
  350. done
  351.  
  352.  
  353. #    " runacct is normally called with no arguments from the cron"
  354. #    " it checks its own locks to make sure that 2 crons or previous"
  355. #    " problems have not occured"
  356.  
  357. #    " runacct uses the statefile to record its progress"
  358. #    " each state updates the statefile upon completion"
  359. #    " then the next loop though the while picks up the new state"
  360.  
  361. #    " to restart this shell,  check the active file for diagnostics"
  362. #    " fix up any corrupted data (ie. bad pacct or wtmp files)"
  363. #    " if runacct detected the error it removes the locks"
  364. #    " remove the locks if necessary, otherwise runacct will complain"
  365. #    " the lastdate file should be removed or changed"
  366. #    " restart runacct at current state with:  runacct MMDD"
  367. #    " to override the statefile: runacct MMDD STATE"
  368.  
  369.  
  370. #    " if runacct has been executed after the latest failure"
  371. #    " ie. it ran ok today but failed yesterday"
  372. #    " the statefile will not be correct"
  373. #    " check the active files and restart properly"
  374.  
  375. #    " if runacct failed in the PROCESS state, remove the last"
  376. #    " ptacct file because it may not be complete"
  377.  
  378. #    " if shell has failed several days, do SETUP manually"
  379. #    " then rerun runacct once for each day failed"
  380. #    " could use fwtmp here to split up wtmp file correctly"
  381.  
  382. #    " normally not a good idea to restart the SETUP state"
  383. #    " should be done manually, or just cleanup first"
  384.  
  385.  
  386. #    " FILE USAGE:    all files in /var/adm/acct/nite unless specified"
  387.  
  388. #    " statefile    records progess of runacct"
  389. #    " lastdate    last day runacct ran in date +%m%d format"
  390. #    " lock lock1    controls serial use of runacct"
  391. #    " active    place for all descriptive and error messages"
  392. #    " fd2log    fd2 output for runacct ( see cron entry ) "
  393. #    " wtmp.MMDD owtmp yesterdays wtmp file"
  394. #    " tmpwtmp    yesterdays wtmp corrected by wtmpfix"
  395. #    " wtmperror    place for wtmpfix error messages"
  396. #    " lineuse    lineusage report used in prdaily"
  397. #    " reboots    reboots report used in prdaily"
  398. #    " log        place for error messages from acctcon1"
  399. #    " ctacct.MMDD    connect tacct records for MMDD"
  400. #    " ptacct.n.MMDD    process tacct records n files for MMDD"
  401. #    " daytacct    total tacct records for this days accounting"
  402. #    " disktacct    disk tacct records produced by disk shell"
  403. #    " daycms    ascii daily command summary used by prdaily"
  404. #    " cms        acsii total command summary used by prdaily"
  405.  
  406. #    " following files in /var/adm directory"
  407.  
  408. #    " fee        output from chargefee program"
  409. #    " pacct        active pacct file"
  410. #    " pacctn    switched pacct files"
  411. #    " Spacctn.MMDD    pacct files for MMDD after SETUP state"
  412. #    " wtmp        active wtmp file"
  413.  
  414. #    " following files in /var/adm/acct/sum"
  415.  
  416. #    " loginlog    output of lastlogin used in prdaily"
  417. #    " tacct        total tacct file for current fiscal"
  418. #    " tacct.MMDD    tacct file for day MMDD"
  419. #    " cms        total cms file for current fiscal"
  420. #    " rprt.MMDD    output of prdaily program"
  421. #    " wtmp.MMDD    saved copy of wtmp for MMDD"
  422. #    " pacct.MMDD    concatenated version of all pacct files for MMDD"
  423. #    " cmsprev    total cms file without latest update"
  424. #    " tacctprev    total tacct file without latest update"
  425. #    " daycms    cms files for todays usage"
  426.