entrypoint-sysstat.sh 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. # Shutdown handler.
  8. trap_shutdown() {
  9. echo "Shutting down..."
  10. exit 0
  11. }
  12. trap trap_shutdown SIGHUP SIGINT SIGQUIT SIGABRT SIGTERM
  13. # Announce collection period.
  14. PERIOD=10
  15. if [ -n "$1" ]; then
  16. PERIOD=$1
  17. fi
  18. REPORT_LENGTH=$((2 * PERIOD - 1))
  19. echo "Collecting and reporting system activity statistics every ${PERIOD} seconds..."
  20. # Clean up /var/log/sa on startup if so requested.
  21. case ${STARTUP_SCRATCH:-0} in
  22. 1|yes|true)
  23. echo "Removing existing SA1 data due to scratch-on-startup being ${STARTUP_SCRATCH}..."
  24. rm -f /var/log/sa/sa*
  25. ;;
  26. *)
  27. echo "Skipping scratch-on-startup..."
  28. ;;
  29. esac
  30. # Add a record of rotation to existing logs if so requested.
  31. case ${STARTUP_ROTATE:-0} in
  32. 1|yes|true)
  33. echo "Marking existing SA1 data as rotated due to rotate-on-startup being ${STARTUP_ROTATE}..."
  34. /usr/lib64/sa/sa1 --rotate
  35. ;;
  36. *)
  37. echo "Skipping rotate-on-startup..."
  38. ;;
  39. esac
  40. # Mark a reboot.
  41. /usr/lib64/sa/sa1 --boot
  42. # Remember the hour-of-day.
  43. PREV_HOUR="$(date +%H)"
  44. # Do the loop.
  45. while [ 1 ]; do
  46. # Start sa1.
  47. /usr/lib64/sa/sa1 ${PERIOD} 1
  48. # Produce the report.
  49. REPORT_FROM=$(date +%H:%M:%S -d "${REPORT_LENGTH} seconds ago")
  50. REPORT_UNTIL=$(date +%H:%M:%S)
  51. /usr/bin/sadf -s ${REPORT_FROM} -e ${REPORT_UNTIL} -j -- -A > /var/log/sa/sysstat-dump.json
  52. # Have we awoken in a new day? Rotation record, please!
  53. NEW_HOUR="$(date +%H)"
  54. if [ ${NEW_HOUR} -lt ${PREV_HOUR} ]; then
  55. echo "Marking last saXX as rotated..."
  56. /usr/lib64/sa/sa1 --rotate
  57. # TODO: remove the old file
  58. fi
  59. PREV_HOUR=${NEW_HOUR}
  60. sleep ${PERIOD}
  61. done
  62. # End of entrypoint-sysstat.sh