oom-monitor.sh 880 B

123456789101112131415161718192021222324252627282930313233343536
  1. #!/usr/bin/env bash
  2. #
  3. # Reports Linux OOM Killer activity
  4. #
  5. set -e
  6. cd "$(dirname "$0")"
  7. # shellcheck source=scripts/oom-score-adj.sh
  8. source oom-score-adj.sh
  9. # shellcheck source=scripts/configure-metrics.sh
  10. source configure-metrics.sh
  11. [[ $(uname) = Linux ]] || exit 0
  12. syslog=/var/log/syslog
  13. [[ -r $syslog ]] || {
  14. echo Unable to read $syslog
  15. exit 1
  16. }
  17. # Adjust OOM score to reduce the chance that this script will be killed
  18. # during an Out of Memory event since the purpose of this script is to
  19. # report such events
  20. oom_score_adj "self" -500
  21. while read -r victim; do
  22. echo "Out of memory event detected, $victim killed"
  23. ./metrics-write-datapoint.sh "oom-killer,victim=$victim,hostname=$HOSTNAME killed=1"
  24. done < <( \
  25. tail --follow=name --retry -n0 $syslog \
  26. | sed --unbuffered -n "s/^.* earlyoom\[[0-9]*\]: Killing process .\(.*\). with signal .*/\1/p" \
  27. )
  28. exit 1