entrypoint-psacct.sh 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. # Looks like the choice is between paging and I/O, can't have both.
  60. # This one prints paging info...
  61. /usr/sbin/sa -ajlp > /var/account/psacct-dump-all
  62. # ...and this one prints total I/O operations (which seems to be unsupported).
  63. #/usr/sbin/sa -ajlD > /var/account/psacct-dump-all
  64. # This is less than very useful.
  65. #/usr/sbin/dump-acct /var/account/pacct > /var/account/psacct-dump-raw
  66. # Could make a CSV out of the above "sa":
  67. # | tr -s' ' | sed 's/^ //; s/ /,/g'
  68. # Did we say NOT cumulative data?
  69. if [ ${CUMULATIVE} -eq 0 ]; then
  70. echo -n > /var/account/pacct
  71. fi
  72. done
  73. # End of entrypoint-psacct.sh