TOMODACHI SHELL
Free Palestine !! - Free Gaza !!
Current File : //usr/src/../../opt/managed_servers/scripts/swap.sh |
readonly SWAP_CHECK_DATA="${BACKGROUND_DATA}/swap_check.data";
readonly SWAP_CHECK_ERRORS="${BACKGROUND_DATA}/swap_check.errors";
readonly FSTAB="/etc/fstab";
swap_innodb_vars_optimization(){
local innodb_buffer_pool_size=$(grep innodb_buffer_pool_size <<< "$innodb_vars" | tail -1);
local innodb_log_file_size=$(grep innodb_log_file_size <<< "$innodb_vars" | tail -1);
local cur_cpu_utilization=$(top -bn1 | head -5 | grep "^%Cpu(s)" | grep -oP "(?<=ni, ).+(?= id)" | tr -d ' ' |
awk '{printf "%d", (100 - $1)}');
local cpu_utilization_threshold=80; # percentage
if [[ $cur_cpu_utilization -lt $cpu_utilization_threshold ]]; then
set_new_innodb_vars "$innodb_buffer_pool_size $innodb_log_file_size" && {
swap_check_data[service_status]=$SERVICE_ACTIVE;
swap_check_data[auto_fix_status]=$AUTO_FIX_SUCCESS;
} || {
if [[ $? -eq 2 ]]; then
# optimization unavailable due to minimal innodb values limits
swap_check_data[recommendations]=$(printf "%s\n\n%s\n\n%s" \
"$(memory_recommendation_mysql_optimization_unavailable_minimal_innodb_vals)"\
"$(memory_recommendation_generic)"\
"$(plan_upgrade_recommendation_as_alternative)" | json_escape);
else
swap_check_data[service_status]=$SERVICE_DOWN;
swap_check_data[auto_fix_status]=$AUTO_FIX_FAIL;
fi;
};
else
# innodb values cannot be optimized due to high CPU usage
swap_check_data[recommendations]=$(printf "%s\n\n%s\n\n%s" \
"$(memory_recommendation_mysql_optimization_unavailable_high_cpu_usage)"\
"$(swap_recommendation_generic)"\
"$(plan_upgrade_recommendation_as_alternative)" | json_escape);
fi;
}
swap_recommendation_mysql(){
local mysql_config="/etc/my.cnf";
local innodb_vars=$(grep -e ^innodb_buffer_pool_size -e ^innodb_log_file_size $mysql_config 2>/dev/null);
if [[ $(management_type_check) == $COMPLETE && -n $innodb_vars ]]; then
# If swap_check_data[auto_fix_status] is unset, notifications will not be sent automatically.
#unset swap_check_data[auto_fix_status]; # Currently disabled, to sent notifications automatically.
swap_innodb_vars_optimization;
elif [[ $(management_type_check) == $BASIC && -n $innodb_vars ]]; then
swap_check_data[recommendations]=$(printf "%s\n\n%s\n\n%s" "$(memory_recommendation_mysql_body)"\
"$(swap_recommendation_generic)" "$(plan_upgrade_recommendation_as_alternative)" |
json_escape);
fi;
}
swap_check_recommendation(){
declare -a proc;
readarray -t proc <<< "$(ps -eo user,pid,%mem,rss,command)";
swap_check_data[swap_total]=$(bytes_to_human_readable "$total_swap_bytes");
swap_check_data[swap_free]=$(bytes_to_human_readable "$free_swap_bytes");
swap_check_data[free_percent]=$(( $free_swap_bytes * 100 / $total_swap_bytes ));
local ps_head=$(head -1 <<< "${proc[0]}");
# Processes under %1 ram usage will be filtered out
local processes_sorted=$(process_filter proc);
local top_proc=$(head -1 <<< "$processes_sorted");
local mysql_match="mysql|maria";
# Check if mysql is the top ram usage process
if [[ -n $(grep -iE "$mysql_match" <<< "$top_proc") ]]; then
swap_recommendation_mysql;
else
swap_check_data[recommendations]=$(printf "%s\n\n%s" "$(swap_recommendation_generic)"\
"$(plan_upgrade_recommendation_as_alternative)" | json_escape);
fi;
swap_check_data[details]=$(echo -e "${ps_head}\n${processes_sorted}" | json_escape);
}
swap_check_main(){
declare -A swap_check_data;
local swap_enabled=$(grep -i " swap " "$FSTAB" | grep -v "^#")
local threshold_crit=0.4;
#local threshold_warn=0.5; # The warning threshold is temporarily disabled
local total_swap_bytes=$(free -b | awk '{IGNORECASE = 1} /swap/ {print $2}');
local free_swap_bytes=$(free -b | awk '{IGNORECASE = 1} /swap/ {print $4}');
local total_swap_crit_threshold=$(awk '{printf "%d",$1 * $2}' <<< "$total_swap_bytes $threshold_crit");
#local total_swap_warn_threshold=$(awk '{printf "%d",$1 * $2}' <<< "$total_swap_bytes $threshold_warn");
swap_check_data[auto_fix_status]=$AUTO_FIX_NOPE;
if [[ ! $swap_enabled ]]; then
swap_check_data[service_status]=$SERVICE_DISABLED;
elif [[ $free_swap_bytes -lt $total_swap_crit_threshold ]]; then
swap_check_data[service_status]=$SERVICE_DOWN;
swap_check_recommendation;
#elif [[ $free_swap_bytes -lt $total_swap_warn_threshold ]]; then
# swap_check_data[service_status]=$SERVICE_WARNING;
# swap_check_recommendation;
else
swap_check_data[service_status]=$SERVICE_ACTIVE;
fi;
bash_arr_to_json swap_check_data ${!swap_check_data[@]} > "$SWAP_CHECK_DATA";
}
swap_check(){
run_check_in_background "swap_check_main" "$SWAP_CHECK_DATA" "$SWAP_CHECK_ERRORS"\
"${CHECK_LOCKS_DIR}/${FUNCNAME}.lock";
}
TOMODACHI | Tempest Hacker