2 # ctdb event script for checking local file system utilization
4 [ -n "$CTDB_BASE" ] || \
5 export CTDB_BASE=$(cd -P $(dirname "$0") ; dirname "$PWD")
10 validate_percentage ()
13 "") return 1 ;; # A failure that doesn't need a warning
14 [0-9]|[0-9][0-9]|100) return 0 ;;
15 *) echo "WARNING: ${1} is an invalid percentage${2:+ in \"}${2}${2:+\"} check"
26 case "$_thresholds" in
28 _warn_threshold="${_thresholds%:*}"
29 _unhealthy_threshold="${_thresholds#*:}"
32 _warn_threshold="$_thresholds"
33 _unhealthy_threshold=""
36 if validate_percentage "$_unhealthy_threshold" "$_thing" ; then
37 if [ "$_usage" -ge "$_unhealthy_threshold" ] ; then
38 die "ERROR: ${_thing} utilization ${_usage}% >= threshold ${_unhealthy_threshold}%"
42 if validate_percentage "$_warn_threshold" "$_what" ; then
43 if [ "$_usage" -ge "$_warn_threshold" ] ; then
44 echo "WARNING: ${_thing} utilization ${_usage}% >= threshold ${_warn_threshold}%"
49 monitor_filesystem_usage ()
51 # Check each specified filesystem, specified in format
52 # <fs_mount>:<fs_warn_threshold>[:fs_unhealthy_threshold]
53 for _fs in $CTDB_MONITOR_FILESYSTEM_USAGE ; do
54 _fs_mount="${_fs%%:*}"
55 _fs_thresholds="${_fs#*:}"
57 if [ ! -d "$_fs_mount" ]; then
58 echo "WARNING: Directory ${_fs_mount} does not exist"
62 # Get current utilization
63 _fs_usage=$(df -kP "$_fs_mount" | \
64 sed -n -e 's@.*[[:space:]]\([[:digit:]]*\)%.*@\1@p')
65 if [ -z "$_fs_usage" ] ; then
66 echo "WARNING: Unable to get FS utilization for ${_fs_mount}"
70 check_thresholds "Filesystem ${_fs_mount}" \
76 monitor_memory_usage ()
78 if [ -z "$CTDB_MONITOR_FREE_MEMORY_WARN" -a \
79 -z "$CTDB_MONITOR_FREE_MEMORY" -a \
80 "$CTDB_CHECK_SWAP_IS_NOT_USED" != "yes" ] ; then
84 _meminfo=$(get_proc "meminfo")
85 set -- $(echo "$_meminfo" | awk '
86 $1 == "MemAvailable:" { memavail += $2 }
87 $1 == "MemFree:" { memfree += $2 }
88 $1 == "Cached:" { memfree += $2 }
89 $1 == "Buffers:" { memfree += $2 }
90 $1 == "MemTotal:" { memtotal = $2 }
91 $1 == "SwapFree:" { swapfree = $2 }
92 $1 == "SwapTotal:" { swaptotal = $2 }
94 if (memavail != 0) { memfree = memavail ; }
95 print int((memtotal - memfree) / memtotal * 100),
96 int((swaptotal - swapfree) / swaptotal * 100)
101 # Shutdown CTDB when memory is below the configured limit
102 if [ -n "$CTDB_MONITOR_FREE_MEMORY" ] ; then
103 if [ $_mem_usage -ge $CTDB_MONITOR_FREE_MEMORY ] ; then
104 echo "CRITICAL: OOM - ${_mem_usage}% usage >= ${CTDB_MONITOR_FREE_MEMORY}% (CTDB threshold)"
105 echo "CRITICAL: Shutting down CTDB!!!"
108 set_proc "sysrq-trigger" "m"
115 # Warn when low on memory
116 if [ -n "$CTDB_MONITOR_FREE_MEMORY_WARN" ] ; then
117 if [ $_mem_usage -ge $CTDB_MONITOR_FREE_MEMORY_WARN ] ; then
118 echo "WARNING: memory usage is excessive - ${_mem_usage}% >= ${CTDB_MONITOR_FREE_MEMORY_WARN}% (CTDB threshold)"
122 # We should never enter swap, so SwapTotal == SwapFree.
123 if [ "$CTDB_CHECK_SWAP_IS_NOT_USED" = "yes" ] ; then
124 if [ $_swap_usage -gt 0 ] ; then
125 echo We are swapping:
135 monitor_filesystem_usage
140 ctdb_standard_event_handler "$@"