#!/bin/bash
#
# This helper script is part of produceReport.sh

function check_hosts_health() {
    # Hosts health validation
    hosts_health_check=$(execute_SQL_from_file "${SQLS}"/hosts_query_check_health.sql)
    if [ $(echo "${hosts_health_check}" | wc -l) -gt 1 ]; then
        echo -e "{WARNING} The following host(s) are not in UP state, please" \
                " verify:"
        echo "${hosts_health_check}" | createAsciidocTable
        echo
    else
        echo -e "{OK} No blocker status found in the hosts\n"
    fi
}

function check_hosts_pretty_name() {
    # Hosts pretty name value validation
    hosts_pretty_name=$(execute_SQL_from_file "${SQLS}"/hosts_query_check_pretty_name.sql)
    if [ ${#hosts_pretty_name} -gt 0 ]; then
        echo "{WARNING} List of hosts with empty OS description value which " \
             "may cause Host type not to be recognized correctly. Such hosts" \
             " will need to be reinstalled after upgrade to work properly."
        echo "${hosts_pretty_name}" | createAsciidocTable "noheader"
    else
        echo -e "{OK} No hosts with empty OS description\n"
    fi
}

function check_vms_health() {
    # Virtual Machines health validation
    vms_health_check=$(execute_SQL_from_file "${SQLS}"/vms_query_health.sql)
    if [ $(echo "${vms_health_check}" | wc -l) -gt 1 ]; then
        echo "{WARNING} The following VMs are not ready for upgrade:"
        echo "${vms_health_check}" | createAsciidocTable
    else
        echo -e "{OK} No blocker status found in the VMs\n"
    fi
}

function check_cluster_no_dc() {
    # Clusters without datacenter validation
    cluster_no_dc=$(execute_SQL_from_file "${SQLS}"/cluster_query_check_datacenter.sql)
    if [ $(echo "${cluster_no_dc}" | wc -l) -gt 1 ]; then
        echo
        echo "{WARNING} The following cluster(s) have no datacenter assigned:"
        echo "${cluster_no_dc}" | createAsciidocTable
        echo
    else
        echo -e "{OK} All clusters contain datacenter assigned\n"
    fi
}

function check_third_party_certificate() {
    # Third party certificate validation
    pki_file_path=$(find "${SOS_REPORT_UNPACK_DIR}" -name ${ENGINE_PKI_FILE})
    if [[ $? != 0 ]]; then
        echo "Could not find ${ENGINE_PKI_FILE} in the sosreport, exiting"
        exit $?
    fi
    dir_pki_conf=$(dirname "${pki_file_path}")

    # Read the variables from conf files in /etc/ovirt-engine/engine.conf.d
    for file in ${dir_pki_conf}/*.conf
    do
        [ -f "$file" ] && source $file
    done

    if [ ! -z "${ENGINE_PKI_TRUST_STORE}" ] && [ "${ENGINE_PKI_TRUST_STORE}" != ${DEFAULT_PKI_TRUSTSTORE} ]; then
        echo
        echo "- PKI Trust Store:"
        echo "{WARNING} ENGINE_PKI_TRUST_STORE has non-default value"
        echo "ENGINE_PKI_TRUST_STORE defaults to ${DEFAULT_PKI_TRUSTSTORE}"
        echo "ENGINE_PKI_TRUST_STORE is currently ${ENGINE_PKI_TRUST_STORE}"
        echo
        echo "To change this value, use the files in ${ENGINE_PKI_CONF_DIR}"
        echo
        echo "For more information about this topic, see also:"
        echo "https://bugzilla.redhat.com/1336838"
    else
        echo -e "{OK} ENGINE_PKI_TRUST_STORE has default value\n"
    fi
}

function check_min_and_max_engine_heap() {
    # Based on
    # https://github.com/oVirt/ovirt-engine/blob/1e7d0dd7e11b1db035660491e37fbf0368614bd6/packaging/services/ovirt-engine/ovirt-engine.py#L311-L338
    # https://github.com/oVirt/ovirt-engine/blob/1e7d0dd7e11b1db035660491e37fbf0368614bd6/packaging/pythonlib/ovirt_engine/mem.py#L29-L85
    ENGINE_CONF_D_10_SETUP_JAVA=$(find "${SOS_REPORT_UNPACK_DIR}" -name 10-setup-java.conf | grep engine.conf.d | head -n 1 2> /dev/null)
    if [ -z ${ENGINE_CONF_D_10_SETUP_JAVA} ]; then
        return
    fi

    . ${ENGINE_CONF_D_10_SETUP_JAVA}
    ENGINE_HEAP_MIN="${ENGINE_HEAP_MIN//[!0-9]/}"
    ENGINE_HEAP_MAX="${ENGINE_HEAP_MAX//[!0-9]/}"

    MEMINFO_ENGINE=$(find "${SOS_REPORT_UNPACK_DIR}" -name meminfo)
    if [ -z ${MEMINFO_ENGINE} ]; then
        return
    fi
    TOTAL_MEM_MB=$(cat ${MEMINFO_ENGINE} | grep MemTotal | awk {'print int($2/1024)'})
    if [ -z ${TOTAL_MEM_MB} ]; then
        return
    fi

    ret=0
    if [ -z "${ENFORCE_ENGINE_HEAP_PARAMS}" ]; then
        if [ ! -z "${ENGINE_HEAP_MIN}" ] && [ ${ENGINE_HEAP_MIN} -gt ${TOTAL_MEM_MB} ]; then
            echo -e "{WARNING} ENGINE_HEAP_MIN ${ENGINE_HEAP_MIN} MB is higher than total memory available ${TOTAL_MEM_MB} MB\n"
            ret=-1
        fi
        if [ ! -z "${ENGINE_HEAP_MAX}" ] && [ ${ENGINE_HEAP_MAX} -gt ${TOTAL_MEM_MB} ]; then
            echo -e "{WARNING} ENGINE_HEAP_MAX ${ENGINE_HEAP_MAX} MB is higher than total memory available ${TOTAL_MEM_MB} MB\n"
            ret=-1
        fi
    fi

    if [ ${ret} = 0 ]; then
        echo -e "{OK} ENGINE_HEAP_MAX and ENGINE_HEAP_MIN are lower than total memory available\n"
    fi
}

function check_vms_running_obsolete_cluster() {
    # In oVirt Engine 4.0, the minimum cluster level supported is 3.6.
    # So we must inform in case there is vm in 3.5 cluster level
    vms_out_date=$(execute_SQL_from_file "${SQLS}"/vms_query_check_obsolete_cluster.sql)
    if [ ${vms_out_date} -gt 0 ]; then
        echo "{WARNING} There are ${vms_out_date} VMs in version 3.5 and prior clusters. These clusters need to be upgraded to version 3.6 prior upgrading to version 4.0."
        echo -e "https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Virtualization/3.6/html/Upgrade_Guide/Red_Hat_Enterprise_Virtualization_3.6_Upgrade_Considerations.html[See 3.6 Upgrade Considerations]\n"
    else
        echo -e "{OK} All virtual machines have cluster level >= 3.6\n"
    fi
}

function check_mixedrhelversion() {
    sql_ret=$(execute_SQL_from_file "${SQLS}"/vdc_options_query_checkmixedrhelversion.sql)
    if [ $(echo "${sql_ret}" | wc -l) -gt 1 ]; then
        echo -e "{WARNING} *After* upgrading all hosts to EL7 and *BEFORE* " \
                "upgrading the Manager to >= 4.y make sure " \
                "CheckMixedRhelVersions is true. More information about " \
                "this topic check https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Virtualization/3.6/html/Upgrade_Guide/Upgrading_Hosts_in_a_Red_Hat_Enterprise_Linux_6_Cluster_to_Red_Hat_Enterprise_Linux_7.html[upgrade guide]"
        echo "${sql_ret}" | createAsciidocTable
        echo
    else
        echo -e "{OK} CheckMixedRhelVersions validation\n"
    fi
}

function rpm_version() {
    find "${SOS_REPORT_UNPACK_DIR}" -name "installed-rpms" -exec grep '(engine\|ovirt\|postgresql\|rhevm\|spice\| )' '{}' \; | cut -f 1 -d " " | sort
}

function check_vms_windows_with_incorrect_timezone() {
   sql_ret=$(execute_SQL_from_file "${SQLS}"/vms_query_windows_vms_with_incorrect_timezone.sql)
    if [ $(echo "${sql_ret}" | wc -l) -gt 1 ]; then
        echo -e "{WARNING} The following VMs with **Windows OS** contain incorrect" \
                " timezone, please adjust the timezone via Web Admin. A list " \
                "of possible values can be found in https://github.com/oVirt/ovirt-engine/blob/fa401fb9a841bf1704588ed2bc9f7f0fbbf0d008/backend/manager/modules/compat/src/main/java/org/ovirt/engine/core/compat/WindowsJavaTimezoneMapping.java#L10[WindowsJavaTimezoneMapping.java], for more information https://bugzilla.redhat.com/show_bug.cgi?id=1386507[see bz#1386507]"
        echo "${sql_ret}" | createAsciidocTable
    else
        echo -e "{OK} All VMs with Windows OS have correct timezone\n"
    fi
}

function check_vms_linux_and_others_with_incorrect_timezone() {
   sql_ret=$(execute_SQL_from_file "${SQLS}"/vms_query_linux_and_others_vms_with_incorrect_timezone.sql)
    if [ $(echo "${sql_ret}" | wc -l) -gt 1 ]; then
        echo -e "{WARNING} The following VMs with **Linux OS or marked as " \
                "Other OS** contain incorrect timezone, please adjust the " \
                "timezone via Web Admin. A list of possible values can " \
                "be found in https://github.com/oVirt/ovirt-engine/blob/5ec729e5f8b5504b5c166c6d4217267a9bda7728/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/TimeZoneType.java#L20-L103[TimeZoneType.java], for more information https://bugzilla.redhat.com/show_bug.cgi?id=1386507[see bz#1386507]"
        echo "${sql_ret}" | createAsciidocTable
    else
        echo -e "{OK} All VMs with Linux or Other OS have correct timezone\n"
    fi
}

function check_vms_with_cluster_lower_3_6_with_virtio_serial_console() {
   sql_ret=$(execute_SQL_from_file "${SQLS}"/vms_with_cluster_lower_3_6_with_virtio_serial_console.sql)
    if [ $(echo "${sql_ret}" | wc -l) -gt 1 ]; then
        echo -e "{WARNING} The following VMs with VirtIO serial console " \
                "enabled in cluster levels 3.5 and below cannot be later " \
                "live migrated to 3.6 hosts (on RHEL7). Such attempt fails " \
                "and VM is terminated. You need to disable the VirtIO serial " \
                "console feature while the VM is still in a 3.5 cluster. " \
                "It can be re-enabled once the VM is in a 3.6 cluster." \
                "For more information, https://bugzilla.redhat.com/show_bug.cgi?id=1434401[see bz#1434401]"
        echo "${sql_ret}" | createAsciidocTable
    else
        echo -e "{OK} No VMs with VirtIO serial console enabled in cluster" \
                " levels 3.5 and below\n"
    fi
}

function check_async_tasks() {
    sql_ret=$(execute_SQL_from_file "${SQLS}"/async_tasks_query_running_tasks.sql)
    if [ $(echo "${sql_ret}" | wc -l) -gt 1 ]; then
        echo
        echo "{WARNING} The following async tasks are still running. " \
             "Please make sure that there are no running tasks before any " \
             "engine upgrade. You might use " \
             "https://github.com/oVirt/ovirt-engine/blob/bf3abca7848496f7cf60d44d9335e2ec0bd3f852/packaging/setup/dbutils/taskcleaner.sh[taskcleaner.sh] tool to stop the tasks"
        echo "${sql_ret}" | createAsciidocTable
        echo
    else
        echo -e "{OK} No async tasks running in Engine\n"
    fi
}

function check_runnning_commands() {
    sql_ret=$(execute_SQL_from_file "${SQLS}"/command_entities_query_get_running_commands.sql)
    if [ $(echo "${sql_ret}" | wc -l) -gt 1 ]; then
        echo
        echo "{WARNING} The following commands are still running. " \
             "Please make sure that there are no commands running before any " \
             "engine upgrade. You might use " \
             "https://github.com/oVirt/ovirt-engine/blob/bf3abca7848496f7cf60d44d9335e2ec0bd3f852/packaging/setup/dbutils/taskcleaner.sh[taskcleaner.sh] tool to stop the commands"
        echo "${sql_ret}" | createAsciidocTable
        echo
    else
        echo -e "{OK} No commands running in Engine\n"
    fi
}

function check_compensation_tasks() {
    sql_ret=$(execute_SQL_from_file "${SQLS}"/business_entity_snapshot_query_running_compensation_tasks.sql)
    if [ $(echo "${sql_ret}" | wc -l) -gt 1 ]; then
        echo
        echo "{WARNING} The following compensation tasks are still running. " \
             "Please make sure that there are no running tasks before any " \
             "engine upgrade. You might use " \
             "https://github.com/oVirt/ovirt-engine/blob/bf3abca7848496f7cf60d44d9335e2ec0bd3f852/packaging/setup/dbutils/taskcleaner.sh[taskcleaner.sh] tool to stop the tasks"
        echo "${sql_ret}" | createAsciidocTable
        echo
    else
        echo -e "{OK} No compensation tasks running in Engine\n"
    fi
}

function check_storage_domains_failing() {
    sql_ret=$(execute_SQL_from_file "${SQLS}"/storage_domains_query_get_failing_domains.sql)
    if [ $(echo "${sql_ret}" | wc -l) -gt 1 ]; then
        echo
        echo "{WARNING} The following storage domains are inactive. " \
             "Please make sure all storage domains required for the system " \
             "are available and active before upgrading the environment"
        echo "${sql_ret}" | createAsciidocTable
        echo
    else
        echo -e "{OK} No storage domain inactive\n"
    fi
}

function check_AAA_legacy() {
    sql_ret=$(execute_SQL_from_file "${SQLS}"/vdc_options_query_AAA_legacy.sql)
    if [ $(echo "${sql_ret}" | wc -l) -gt 1 ]; then
        echo
        echo "{WARNING} Legacy LDAP provider detected, **before upgrade** the environment use: " \
             "https://github.com/oVirt/ovirt-engine-kerbldap-migration/[ovirt-engine-kerbldap-migration tool]" \
             "to ease migrate into the new ovirt-engine-extension-aaa-ldap provider"
        echo "${sql_ret}" | createAsciidocTable
        echo
    else
        echo -e "{OK} No Legacy LDAP provider detected\n"
    fi
}

function check_custom_ip_tables_config() {
    sql_ret=$(execute_SQL_from_file "${SQLS}"/vdc_options_query_custom_ip_table_config.sql)
    if [ $(echo "${sql_ret}" | wc -l) -gt 1 ]; then
        echo
        echo "{WARNING} Custom iptables configuration for hosts has been detected. " \
             "Please be aware that iptables configuration for hosts has been deprecated in 4.2, " \
             "and such configuration should be migrated to firewalld for cluster version >= 4.0."
        echo "${sql_ret}" | createAsciidocTable
        echo
    else
        echo -e "{OK} No custom iptables rules detected\n"
    fi
}

function check_minimum_cluster_compat_version() {
    sql_ret=$(execute_SQL_from_file "${SQLS}"/cluster_query_minimum_3_6_compat_version.sql)
    if [ $(echo "${sql_ret}" | wc -l) -gt 1 ]; then
        echo
        echo "{WARNING} It's required to update the **Cluster Compatibility Version to 3.6" \
             " before** upgrading to 4.0 for the following cluster(s):"
        echo "${sql_ret}" | createAsciidocTable
        echo
    else
        echo -e "{OK} Cluster(s) are 3.6 or higher in Compatibility Version\n"
    fi
}

function check_minimum_datacenter_compat_version() {
    sql_ret=$(execute_SQL_from_file "${SQLS}"/datacenter_query_minimum_3_6_compat_version.sql)
    if [ $(echo "${sql_ret}" | wc -l) -gt 1 ]; then
        echo
        echo "{WARNING} It's required to update the **DataCenter Compatibility Version to 3.6" \
             " before** upgrading to 4.0 for the following DataCenter(s):"
        echo "${sql_ret}" | createAsciidocTable
        echo
    else
        echo -e "{OK} DataCenter(s) are 3.6 or higher in Compatibility Version\n"
    fi
}

function check_hosted_engine_environment() {
    sql_ret=$(execute_SQL_from_file "${SQLS}"/vdc_options_query_detect_hosted_engine_env.sql)
    if [ $(echo "${sql_ret}" | wc -l) -gt 1 ]; then
        echo -e "{OK} Detected Hosted Engine (HE) Environment"
        echo "${sql_ret}" | createAsciidocTable

        # Check for HA Global Maintenance mode
        sql_ret=$(execute_SQL_from_file "${SQLS}"/vds_statistics_query_ha_global_maintenance.sql)
        if [ $(echo "${sql_ret}" | wc -l) -gt 1 ]; then
            echo
            echo "{WARNING} Hosted Engine HA is in https://access.redhat.com/documentation/en-us/red_hat_virtualization/4.1/html-single/self-hosted_engine_guide/#chap-Maintenance_and_Upgrading_Resources[Global Maintenance mode]"
            echo "${sql_ret}" | createAsciidocTable
            echo
        else
            echo -e "{OK} Hosted Engine HA is **NOT IN** Global Maintenance mode\n"
            # Check for HA Global Maintenance mode
            sql_ret=$(execute_SQL_from_file "${SQLS}"/vds_statistics_query_hosted_engine_setup_with_hosts_non_up.sql)
            if [ $(echo "${sql_ret}" | wc -l) -gt 1 ]; then
                echo
                echo "{WARNING} There are Hosted Engine hypervisor(s) **NOT in UP state**. However, the maintenance modes https://access.redhat.com/documentation/en-us/red_hat_virtualization/4.1/html-single/self-hosted_engine_guide/#chap-Maintenance_and_Upgrading_Resources[local or global] are not set, this might affect HA"
                echo "${sql_ret}" | createAsciidocTable
                echo
            else
                echo -e "{OK} Hosted Engine HA is configured, active and all hypervisors are UP\n"
            fi
        fi

        # Check for non ovirtmgmt as management network
        sql_ret=$(execute_SQL_from_file "${SQLS}"/network_query_cluster_with_non_ovirtmgmt_as_management_network.sql)
        if [ $(echo "${sql_ret}" | wc -l) -gt 1 ]; then
            echo
            echo "{WARNING} There are cluster(s) with **NON ovirtmgmt as management network**." \
                 "Hosted Engine environments can be affected during upgrade. See " \
                 "https://bugzilla.redhat.com/show_bug.cgi?id=1449547[bz#1449547] and " \
                 "https://access.redhat.com/solutions/2292861[this knowledge article] for more information."
            echo "${sql_ret}" | createAsciidocTable
            echo
        else
            echo -e "{OK} All management network are ovirtmgmt\n"
        fi

    fi
}

function check_pinned_virtual_machines() {
    sql_ret=$(execute_SQL_from_file "${SQLS}"/vms_query_pinned_virtual_machines.sql)
    if [ $(echo "${sql_ret}" | wc -l) -gt 1 ]; then
        echo
        echo "{WARNING} There are virtual machine(s) pinned, it's not recommended upgrade" \
             " the environment with virtual machines pinned to run in specific hypervisors"
        echo "${sql_ret}" | createAsciidocTable
        echo
    else
        echo -e "{OK} No virtual machines pinned to specific hypervisor\n"
    fi
}

function check_images_locked_or_illegal() {
    sql_ret=$(execute_SQL_from_file "${SQLS}"/images_query_invalid_status.sql)
    if [ $(echo "${sql_ret}" | wc -l) -gt 1 ]; then
        echo
        echo "{WARNING} There are virtual machine(s) with invalid **image status** and" \
             " might not start in the next shutdown/start cycle"
        echo "${sql_ret}" | createAsciidocTable
        echo
    else
        echo -e "{OK} No virtual machines with invalid image status\n"
    fi
}

function check_vm_snapshot_id_zero() {
    sql_ret=$(execute_SQL_from_file "${SQLS}"/images_query_vm_snapshot_id_zero.sql)
    if [ $(echo "${sql_ret}" | wc -l) -gt 1 ]; then
        echo
        echo "{WARNING} There are image(s) with **vm_snapshot_id as zero**," \
             " it's recommended to not use zeros in vm_snapshot_id uuid " \
             "column to avoid potencial conflicts, you might use uuidgen" \
             " command to generate a new UUID. For more information check" \
             "https://access.redhat.com/solutions/130323[this article] " \
             "and https://bugzilla.redhat.com/show_bug.cgi?id=843157[bz#843157]"
        echo "${sql_ret}" | createAsciidocTable
        echo
    else
        echo -e "{OK} No images with zero vm_snapshot_id found\n"
    fi
}

function check_in_preview_snapshots() {
    sql_ret=$(execute_SQL_from_file "${SQLS}"/vms_query_snapshots_in_preview.sql)
    if [ $(echo "${sql_ret}" | wc -l) -gt 1 ]; then
        echo
        echo "{WARNING} There are virtual machine(s) with **IN PREVIEW** snapshots."
        echo "Either commit or undo the snapshot in-preview before upgrading.."
        echo "${sql_ret}" | createAsciidocTable
        echo
    else
        echo -e "{OK} No virtual machines with IN PREVIEW snapshots\n"
    fi
}

function check_legacy_apache_sso_config() {
    OVIRT_SSO_CONFIG=$(find "${SOS_REPORT_UNPACK_DIR}" -name ovirt-sso.conf 2> /dev/null)
    if [ -z ${OVIRT_SSO_CONFIG} ]; then
        echo -e "{OK} No Apache SSO config detected\n"
        return
    fi

    if [[ $(grep "<LocationMatch ^(/ovirt-engine/(webadmin|userportal|api)|/api)>" ${OVIRT_SSO_CONFIG}) ]]; then
        echo
        echo "{WARNING} Legacy Apache SSO configuration detected! **After upgrading to 4.0** is required to " \
             "replace the /etc/ovirt-engine/aaa/ovirt-sso.conf with https://github.com/oVirt/ovirt-engine-extension-aaa-ldap/blob/2db902ee5c9e392f97f7f4090d893b5eb6d29efb/README#L352-L374[this new configuration]," \
             " install mod_auth_gssapi mod_session packages and restart Apache."
        echo
    else
        echo -e "{OK} No legacy Apache SSO configuration found\n"
    fi
}

function check_dirty_network() {
    sql_ret=$(execute_SQL_from_file "${SQLS}"/vds_dynamic_check_for_net_config_dirty.sql)
    if [ $(echo "${sql_ret}" | wc -l) -gt 1 ]; then
        echo
        echo "{WARNING} Network configuration is not persisted on the " \
             "following hosts, which may lead to connectivity issues " \
             "after reboot. Make sure to use the button **Save Network Configuration** " \
             "on these host(s) to persist the network settings"
        echo "${sql_ret}" | createAsciidocTable
        echo
    else
        echo -e "{OK} No issues found in the hypervisor(s) network\n"
    fi
}

function check_ip_not_null_and_pm_user_null() {
    sql_ret=$(execute_SQL_from_file "${SQLS}"/vms_query_ip_set_power_management_null.sql)
    if [ $(echo "${sql_ret}" | wc -l) -gt 1 ]; then
        echo
        echo "{WARNING} The following hypervisor(s) have Power Manager disabled " \
             "but the address is still set with no username. This scenario might cause an upgrade " \
             "issue from 3.5 to 3.6 version. For more information see the https://access.redhat.com/solutions/3178621[solution]" \
             " and https://bugzilla.redhat.com/show_bug.cgi?id=1488630[bz#1488630]"
        echo "${sql_ret}" | createAsciidocTable
        echo
    else
        echo -e "{OK} No issues found in the Hypervisor(s) Power Management settings\n"
    fi
}

function check_number_of_hosts() {
    MAX_NUMBER_HOSTS=200
    sql_ret=$(execute_SQL_from_file "${SQLS}"/hosts_query_number_of_hosts.sql)
    if [ "${sql_ret}" -gt ${MAX_NUMBER_HOSTS} ]; then
        echo
        echo "{WARNING} The maximum number of hosts per Manager is ${MAX_NUMBER_HOSTS}, currently hosts " \
             " in the environment **${sql_ret}**. For more information about technical limitations, see " \
             "https://access.redhat.com/documentation/en-us/red_hat_virtualization/4.1/html-single/technical_reference/#chap-Minimum_Requirements_and_Technical_Limitations[here]. " \
             "This value can be adjusted by: engine-config -s MaxNumberOfHostsInStoragePool=<MaxNumber>"
        echo
    else
        echo -e "{OK} The number of hosts per Manager is ${sql_ret} and it is lower than maximum (${MAX_NUMBER_HOSTS})\n"
    fi
}

function check_for_cpu_model_Conroe_and_Penryn() {
    sql_ret=$(execute_SQL_from_file "${SQLS}"/hosts_cpu_Conroe_Penryn.sql)
    if [ $(echo "${sql_ret}" | wc -l) -gt 1 ]; then
        echo
        echo "{WARNING} The following hypervisor(s) have old CPU models like Conroe or Penryn." \
             "The oldest recommended is Nehalem which support IBRS. For more information see " \
             "https://access.redhat.com/solutions/3307851[solution]"
        echo "${sql_ret}" | createAsciidocTable
        echo
    else
        echo -e "{OK} No old CPU(s) detected (Conroe or Penryn) without https://access.redhat.com/solutions/3307851[IBRS] CPU for the guests.\n"
    fi
}

function check_vms_miminum_20_percent_memory_guaranteed() {
    sql_ret=$(execute_SQL_from_file "${SQLS}"/vms_query_minimum_memory_guaranteed.sql)
    if [ $(echo "${sql_ret}" | wc -l) -gt 1 ]; then
        echo
        echo "{WARNING} The following Virtual Machines are setup with less than 20% of total memory" \
             "as Physical Memory Guaranteed, it's recommended to review it via Admin Portal. " \
             "For more info see the https://access.redhat.com/solutions/136303[solution]."
        echo "${sql_ret}" | createAsciidocTable
        echo
    fi
}

function check_hosts_with_tls_disabled() {
    sql_ret=$(execute_SQL_from_file "${SQLS}"/hosts_query_tls_disabled.sql)
    if [ $(echo "${sql_ret}" | wc -l) -gt 1 ]; then
        echo
        echo "{WARNING} The following Hosts are running with TLS protocol disabled " \
             "It's recommended to enable the TLS protocol between Hosts and Engine." \
             "For more info see the https://access.redhat.com/solutions/122403[solution]."
        echo "${sql_ret}" | createAsciidocTable
        echo
    fi
}

function check_clusters_with_mixed_selinux_disabled() {
    # Look for clusters with mixed selinux configuration, example:
    # one host selinux is disabled and the others are enforcing.

    # We must check if the engine_db is > 3.4, selinux_enforce_mode was
    # was introduced in 3.5
    if [[ ${ENGINE_CURRENT_VERSION} > 3.4 ]]; then
        sql_ret=$(execute_SQL_from_file "${SQLS}"/cluster_query_check_mixed_selinux_configs.sql)
        if [ $(echo "${sql_ret}" | wc -l) -gt 1 ]; then
            echo
            echo "{WARNING} The following Host(s) are disabling SELinux but the **Cluster** contain" \
                 "others Host(s) with SELinux Enforcing/Permissive. Mixing such configuration will" \
                 "cause Virtual Machine(s) migration fail!"
            echo "${sql_ret}" | createAsciidocTable
            echo
        fi
    fi
}
