entrypoint-psacct.sh 1.9 KB

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