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 ctdb_setup_service_state_dir "system-monitoring"
12 validate_percentage ()
15 "") return 1 ;; # A failure that doesn't need a warning
16 [0-9]|[0-9][0-9]|100) return 0 ;;
17 *) echo "WARNING: ${1} is an invalid percentage${2:+ in \"}${2}${2:+\"} check"
27 _unhealthy_callout="$4"
29 case "$_thresholds" in
31 _warn_threshold="${_thresholds%:*}"
32 _unhealthy_threshold="${_thresholds#*:}"
35 _warn_threshold="$_thresholds"
36 _unhealthy_threshold=""
39 _t=$(echo "$_thing" | sed -e 's@/@SLASH_@g' -e 's@ @_@g')
40 _cache="${service_state_dir}/cache_${_t}"
41 if validate_percentage "$_unhealthy_threshold" "$_thing" ; then
42 if [ "$_usage" -ge "$_unhealthy_threshold" ] ; then
43 echo "ERROR: ${_thing} utilization ${_usage}% >= threshold ${_unhealthy_threshold}%"
44 eval "$_unhealthy_callout"
45 echo "$_usage" >"$_cache"
50 if validate_percentage "$_warn_threshold" "$_what" ; then
51 if [ "$_usage" -ge "$_warn_threshold" ] ; then
52 if [ -r "$_cache" ] ; then
57 if [ "$_usage" != "$_prev" ] ; then
58 echo "WARNING: ${_thing} utilization ${_usage}% >= threshold ${_warn_threshold}%"
59 echo "$_usage" >"$_cache"
62 if [ -r "$_cache" ] ; then
63 echo "NOTICE: ${_thing} utilization ${_usage}% < threshold ${_warn_threshold}%"
70 monitor_filesystem_usage ()
72 # Check each specified filesystem, specified in format
73 # <fs_mount>:<fs_warn_threshold>[:fs_unhealthy_threshold]
74 for _fs in $CTDB_MONITOR_FILESYSTEM_USAGE ; do
75 _fs_mount="${_fs%%:*}"
76 _fs_thresholds="${_fs#*:}"
78 if [ ! -d "$_fs_mount" ]; then
79 echo "WARNING: Directory ${_fs_mount} does not exist"
83 # Get current utilization
84 _fs_usage=$(df -kP "$_fs_mount" | \
85 sed -n -e 's@.*[[:space:]]\([[:digit:]]*\)%.*@\1@p')
86 if [ -z "$_fs_usage" ] ; then
87 echo "WARNING: Unable to get FS utilization for ${_fs_mount}"
91 check_thresholds "Filesystem ${_fs_mount}" \
101 set_proc "sysrq-trigger" "m"
104 monitor_memory_usage ()
106 if [ -z "$CTDB_MONITOR_MEMORY_USAGE" -a \
107 -z "$CTDB_MONITOR_SWAP_USAGE" ] ; then
111 _meminfo=$(get_proc "meminfo")
112 set -- $(echo "$_meminfo" | awk '
113 $1 == "MemAvailable:" { memavail += $2 }
114 $1 == "MemFree:" { memfree += $2 }
115 $1 == "Cached:" { memfree += $2 }
116 $1 == "Buffers:" { memfree += $2 }
117 $1 == "MemTotal:" { memtotal = $2 }
118 $1 == "SwapFree:" { swapfree = $2 }
119 $1 == "SwapTotal:" { swaptotal = $2 }
121 if (memavail != 0) { memfree = memavail ; }
122 print int((memtotal - memfree) / memtotal * 100),
123 int((swaptotal - swapfree) / swaptotal * 100)
128 check_thresholds "System memory" \
129 "$CTDB_MONITOR_MEMORY_USAGE" \
133 check_thresholds "System swap" \
134 "$CTDB_MONITOR_SWAP_USAGE" \
142 monitor_filesystem_usage
147 ctdb_standard_event_handler "$@"