entrypoint-psacct.sh 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. # Announce collection period.
  13. PERIOD=10
  14. if [ -n "$1" ]; then
  15. PERIOD=$1
  16. fi
  17. echo "Reporting on process activity every ${PERIOD} seconds."
  18. # Did someone ask for cumulative stats?
  19. case ${CUMULATIVE:-0} in
  20. 1|yes|true)
  21. echo "Gathering cumulative accounting data since container start."
  22. CUMULATIVE=1
  23. ;;
  24. *)
  25. echo "Clearing accounting data between snapshots."
  26. CUMULATIVE=0
  27. ;;
  28. esac
  29. # Clean up /var/account/pacct on startup if so requested.
  30. case ${STARTUP_SCRATCH:-0} in
  31. 1|yes|true)
  32. echo "Removing existing accounting data due to scratch-on-startup being ${STARTUP_SCRATCH}..."
  33. rm -rf /var/account
  34. ;;
  35. *)
  36. echo "Skipping scratch-on-startup..."
  37. ;;
  38. esac
  39. # Make sure we have everything
  40. echo "Making sure /var/account/pacct is ready..."
  41. /usr/libexec/psacct/accton-create
  42. # Start accounting
  43. /usr/sbin/accton /var/account/pacct
  44. # Do the loop
  45. while [ 1 ]; do
  46. sleep ${PERIOD}
  47. # Report after waking up.
  48. /usr/sbin/sa -ajlp > /var/account/dump-all
  49. /usr/sbin/dump-acct /var/account/pacct > /var/account/dump-raw
  50. # Could make a CSV out of the above "sa":
  51. # | tr -s' ' | sed 's/^ //; s/ /,/g'
  52. # Did we say NOT cumulative data?
  53. if [ ${CUMULATIVE} -eq 0 ]; then
  54. echo -n > /var/account/pacct
  55. fi
  56. done
  57. # End of entrypoint-psacct.sh