cpu_load_generic_mysql_recommendation(){
  echo "We recommend checking with your developer optimization of the SQL queries."
}

cpu_load_recommendation_mysql_optimization_unavailable(){
  echo "Due to the high memory usage on your server, the MySQL service cannot be optimized.";
}

cpu_load_wordpress_mysql_recommendations(){
  echo "Please note that optimization might not have much effect on the WordPress CMS, as its queries are mostly generated by plugins.
The following steps may reduce the WordPress CMS CPU load by MySQL queries:
- Install a caching plugin, such as WP Super Cache or W3 Total Cache;
- Use some debugging plugins, such as Query Monitor, that can help to pinpoint the plugins responsible for slow queries:
  - https://wordpress.org/plugins/query-monitor/

- Minimize plugins (especially, the ones that heavily contribute to the load) and use a lightweight theme;
- Enable the object-caching software such as Memcached or Redis:
  - https://blog.cpanel.com/memcached-php-applications-for-faster-web-apps/
  - https://tecadmin.net/install-redis-cpanel/

- Find and enable the Content Delivery Network specialized in caching dynamic content.
";
}

cpu_load_mysqltuner_recommendations(){
        local tuner_path="/root/mysqltuner.pl";

        wget http://mysqltuner.pl/ -O $tuner_path 1>/dev/null 2>&1;
        local tuner_recommendations=$(perl $tuner_path --buffers | grep -A500 "General recommendations" | tail -n +2 |
                        sed -E "s/^\s+/- /";);
        rm -f $tuner_path 2>/dev/null;

        local things_to_check="$(grep -B99 "Variables to adjust" <<< "$tuner_recommendations" | grep -v "Variables to adjust")";
        local vars_to_adjust="$(grep -A99 "Variables to adjust" <<< "$tuner_recommendations" | tail -n +1 | grep -v -e innodb_buffer_pool_size -e innodb_log_file_size)";
        local free_ram=$(free -b | grep Mem | tr -s '\t' ' ' | cut -d' ' -f4);
        local innodb_total_extra_ram=$(( $free_ram - $free_ram / 5 ));
        local innodb_buffer_pool_size_extra_ram=$(( $innodb_total_extra_ram - $innodb_total_extra_ram / 5 ));
        local innodb_log_file_size_extra_ram=$(( $innodb_total_extra_ram / 5 ));

        echo 'Regarding the MySQL configuration, we recommend checking the following:'
        echo "$things_to_check";
        if [[ -n $vars_to_adjust ]]; then
                echo "$vars_to_adjust";
                echo "- the innodb_buffer_pool_size and innodb_log_file_size values can be gradually increased as long as available memory on your server is above 20% of the maximum memory limit:";
                echo "  - innodb_buffer_pool_size can be increased additionally by $(( $innodb_buffer_pool_size_extra_ram / 1024 / 1024 )) MB";
                echo "  - innodb_log_file_size can be increased additionally by $(( $innodb_log_file_size_extra_ram / 1024 / 1024 )) MB";
        fi;
}
