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"
20 monitor_filesystem_usage
()
22 # Check each specified filesystem, specified in format
23 # <fs_mount>:<fs_warn_threshold>[:fs_unhealthy_threshold]
24 for _fs
in $CTDB_MONITOR_FILESYSTEM_USAGE ; do
25 _fs_mount
="${_fs%%:*}"
26 _fs_thresholds
="${_fs#*:}"
28 if [ ! -d "$_fs_mount" ]; then
29 echo "WARNING: Directory ${_fs_mount} does not exist"
33 # Get current utilization
34 _fs_usage
=$
(df
-kP "$_fs_mount" | \
35 sed -n -e 's@.*[[:space:]]\([[:digit:]]*\)%.*@\1@p')
36 if [ -z "$_fs_usage" ] ; then
37 echo "WARNING: Unable to get FS utilization for ${_fs_mount}"
41 case "$_fs_thresholds" in
43 _fs_warn_threshold
="${_fs_thresholds%:*}"
44 _fs_unhealthy_threshold
="${_fs_thresholds#*:}"
47 _fs_warn_threshold
="$_fs_thresholds"
48 _fs_unhealthy_threshold
=""
51 if validate_percentage
"$_fs_unhealthy_threshold" "$_fs" ; then
52 if [ "$_fs_usage" -ge "$_fs_unhealthy_threshold" ] ; then
53 die
"ERROR: Filesystem ${_fs_mount} utilization ${_fs_usage}% >= threshold ${_fs_unhealthy_threshold}%"
57 if validate_percentage
"$_fs_warn_threshold" "$_fs" ; then
58 if [ "$_fs_usage" -ge "$_fs_warn_threshold" ] ; then
59 echo "WARNING: Filesystem ${_fs_mount} utilization ${_fs_usage}% >= threshold ${_fs_warn_threshold}%"
65 monitor_memory_usage
()
67 if [ -z "$CTDB_MONITOR_FREE_MEMORY_WARN" -a \
68 -z "$CTDB_MONITOR_FREE_MEMORY" -a \
69 "$CTDB_CHECK_SWAP_IS_NOT_USED" != "yes" ] ; then
73 _meminfo
=$
(get_proc
"meminfo")
74 set -- $
(echo "$_meminfo" |
awk '
75 $1 == "MemAvailable:" { memavail += $2 }
76 $1 == "MemFree:" { memfree += $2 }
77 $1 == "Cached:" { memfree += $2 }
78 $1 == "Buffers:" { memfree += $2 }
79 $1 == "MemTotal:" { memtotal = $2 }
80 $1 == "SwapFree:" { swapfree = $2 }
81 $1 == "SwapTotal:" { swaptotal = $2 }
83 if (memavail != 0) { memfree = memavail ; }
84 print int((memtotal - memfree) / memtotal * 100),
85 int((swaptotal - swapfree) / swaptotal * 100)
90 # Shutdown CTDB when memory is below the configured limit
91 if [ -n "$CTDB_MONITOR_FREE_MEMORY" ] ; then
92 if [ $_mem_usage -ge $CTDB_MONITOR_FREE_MEMORY ] ; then
93 echo "CRITICAL: OOM - ${_mem_usage}% usage >= ${CTDB_MONITOR_FREE_MEMORY}% (CTDB threshold)"
94 echo "CRITICAL: Shutting down CTDB!!!"
97 set_proc
"sysrq-trigger" "m"
104 # Warn when low on memory
105 if [ -n "$CTDB_MONITOR_FREE_MEMORY_WARN" ] ; then
106 if [ $_mem_usage -ge $CTDB_MONITOR_FREE_MEMORY_WARN ] ; then
107 echo "WARNING: memory usage is excessive - ${_mem_usage}% >= ${CTDB_MONITOR_FREE_MEMORY_WARN}% (CTDB threshold)"
111 # We should never enter swap, so SwapTotal == SwapFree.
112 if [ "$CTDB_CHECK_SWAP_IS_NOT_USED" = "yes" ] ; then
113 if [ $_swap_usage -gt 0 ] ; then
114 echo We are swapping
:
124 monitor_filesystem_usage
129 ctdb_standard_event_handler
"$@"