entrypoint-psacct.sh 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #!/bin/bash
  2. # Just die if not root.
  3. if [ ${UID} -ne 0 ]; then
  4. echo "FATAL: Need to run as root."
  5. exit 1
  6. fi
  7. # Die if not running with CAP_SYS_PACCT
  8. if [ -z "$(capsh --decode=$(cat /proc/self/status | grep ^CapEff | cut -f2) | grep sys_pacct)" ]; then
  9. echo "FATAL: Need CAP_SYS_PACCT to run."
  10. exit 1
  11. fi
  12. # Shutdown handler.
  13. trap_shutdown() {
  14. echo "Shutting down..."
  15. exit 0
  16. }
  17. trap trap_shutdown SIGHUP SIGINT SIGQUIT SIGABRT SIGTERM
  18. # Announce collection period.
  19. PERIOD=10
  20. if [ -n "$1" ]; then
  21. PERIOD=$1
  22. fi
  23. echo "Reporting on process activity every ${PERIOD} seconds."
  24. # Did someone ask for cumulative stats?
  25. case ${CUMULATIVE:-0} in
  26. 1|yes|true)
  27. echo "Gathering cumulative accounting data since container start."
  28. CUMULATIVE=1
  29. ;;
  30. *)
  31. echo "Clearing accounting data between snapshots."
  32. CUMULATIVE=0
  33. ;;
  34. esac
  35. # Clean up /var/account/pacct on startup if so requested.
  36. case ${STARTUP_SCRATCH:-0} in
  37. 1|yes|true)
  38. echo "Removing existing accounting data due to scratch-on-startup being ${STARTUP_SCRATCH}..."
  39. rm -rf /var/account
  40. ;;
  41. *)
  42. echo "Skipping scratch-on-startup..."
  43. ;;
  44. esac
  45. # Make sure we have everything
  46. echo "Making sure /var/account/pacct is ready..."
  47. /usr/libexec/psacct/accton-create
  48. # Start accounting
  49. /usr/sbin/accton /var/account/pacct
  50. # Do the loop
  51. while [ 1 ]; do
  52. sleep ${PERIOD}
  53. # Report after waking up.
  54. /usr/sbin/sa -ajlp > /var/account/psacct-dump-all
  55. /usr/sbin/dump-acct /var/account/pacct > /var/account/psacct-dump-raw
  56. # Could make a CSV out of the above "sa":
  57. # | tr -s' ' | sed 's/^ //; s/ /,/g'
  58. # Did we say NOT cumulative data?
  59. if [ ${CUMULATIVE} -eq 0 ]; then
  60. echo -n > /var/account/pacct
  61. fi
  62. done
  63. # End of entrypoint-psacct.sh