2 # ctdb event script for checking local file system utilization
5 CTDB_BASE
=$
(d
=$
(dirname "$0") && cd -P "$d" && dirname "$PWD")
7 .
"${CTDB_BASE}/functions"
11 ctdb_setup_state_dir
"service" "system-monitoring"
16 "") return 1 ;; # A failure that doesn't need a warning
17 [0-9] |
[0-9][0-9] |
100) return 0 ;;
19 echo "WARNING: ${1} is an invalid percentage in \"${2}\" check"
30 _unhealthy_callout
="$4"
32 case "$_thresholds" in
34 _warn_threshold
="${_thresholds%:*}"
35 _unhealthy_threshold
="${_thresholds#*:}"
38 _warn_threshold
="$_thresholds"
39 _unhealthy_threshold
=""
43 _t
=$
(echo "$_thing" |
sed -e 's@/@SLASH_@g' -e 's@ @_@g')
44 # script_state_dir set by ctdb_setup_state_dir()
45 # shellcheck disable=SC2154
46 _cache
="${script_state_dir}/cache_${_t}"
47 if validate_percentage
"$_unhealthy_threshold" "$_thing"; then
48 if [ "$_usage" -ge "$_unhealthy_threshold" ]; then
49 printf 'ERROR: %s utilization %d%% >= threshold %d%%\n' \
52 "$_unhealthy_threshold"
53 eval "$_unhealthy_callout"
54 echo "$_usage" >"$_cache"
59 if validate_percentage
"$_warn_threshold" "$_thing"; then
60 if [ "$_usage" -ge "$_warn_threshold" ]; then
61 if [ -r "$_cache" ]; then
62 read -r _prev
<"$_cache"
66 if [ "$_usage" = "$_prev" ]; then
69 printf 'WARNING: %s utilization %d%% >= threshold %d%%\n' \
73 echo "$_usage" >"$_cache"
75 if [ ! -r "$_cache" ]; then
78 printf 'NOTICE: %s utilization %d%% < threshold %d%%\n' \
87 set_monitor_filsystem_usage_defaults
()
89 _fs_defaults_cache
="${script_state_dir}/cache_filsystem_usage_defaults"
91 if [ ! -r "$_fs_defaults_cache" ]; then
92 # Determine filesystem for each database directory, generate
93 # an entry to warn at 90%, de-duplicate entries, put all items
94 # on 1 line (so the read below gets everything)
95 for _t
in "${CTDB_DBDIR:-${CTDB_VARDIR}}" \
96 "${CTDB_DBDIR_PERSISTENT:-${CTDB_VARDIR}/persistent}" \
97 "${CTDB_DBDIR_STATE:-${CTDB_VARDIR}/state}"; do
98 df
-kP "$_t" |
awk 'NR == 2 { printf "%s:90\n", $6 }'
99 done |
sort -u |
xargs >"$_fs_defaults_cache"
102 read -r CTDB_MONITOR_FILESYSTEM_USAGE
<"$_fs_defaults_cache"
105 monitor_filesystem_usage
()
107 if [ -z "$CTDB_MONITOR_FILESYSTEM_USAGE" ]; then
108 set_monitor_filsystem_usage_defaults
111 # Check each specified filesystem, specified in format
112 # <fs_mount>:<fs_warn_threshold>[:fs_unhealthy_threshold]
113 for _fs
in $CTDB_MONITOR_FILESYSTEM_USAGE; do
114 _fs_mount
="${_fs%%:*}"
115 _fs_thresholds
="${_fs#*:}"
117 if [ ! -d "$_fs_mount" ]; then
118 echo "WARNING: Directory ${_fs_mount} does not exist"
122 # Get current utilization
123 _fs_usage
=$
(df
-kP "$_fs_mount" |
124 sed -n -e 's@.*[[:space:]]\([[:digit:]]*\)%.*@\1@p')
125 if [ -z "$_fs_usage" ]; then
126 printf 'WARNING: Unable to get FS utilization for %s\n' \
131 check_thresholds
"Filesystem ${_fs_mount}" \
141 set_proc
"sysrq-trigger" "m"
144 monitor_memory_usage
()
147 if [ -z "$CTDB_MONITOR_MEMORY_USAGE" ]; then
148 CTDB_MONITOR_MEMORY_USAGE
=80
151 _meminfo
=$
(get_proc
"meminfo")
152 # Intentional word splitting here
153 # shellcheck disable=SC2046
154 set -- $
(echo "$_meminfo" |
awk '
155 $1 == "MemAvailable:" { memavail += $2 }
156 $1 == "MemFree:" { memfree += $2 }
157 $1 == "Cached:" { memfree += $2 }
158 $1 == "Buffers:" { memfree += $2 }
159 $1 == "MemTotal:" { memtotal = $2 }
160 $1 == "SwapFree:" { swapfree = $2 }
161 $1 == "SwapTotal:" { swaptotal = $2 }
163 if (memavail != 0) { memfree = memavail ; }
164 if (memtotal + swaptotal != 0) {
165 usedtotal = memtotal - memfree + swaptotal - swapfree
166 print int(usedtotal / (memtotal + swaptotal) * 100)
173 check_thresholds
"System memory" \
174 "$CTDB_MONITOR_MEMORY_USAGE" \
181 # Load/cache database options from configuration file
185 monitor_filesystem_usage || rc
=$?
186 monitor_memory_usage || rc
=$?