Nk_fpm_stats

Home

Description

nk_fpm_stats() is a function that generates a table with various statistics related to PHP-FPM (FastCGI Process Manager).

The script defines several functions:

find_max_requests(): retrieves the value of pm_max_requests from the configuration of PHP-FPM for a given domain.

find_max_children(): retrieves the value of pm_max_children from the configuration of PHP-FPM for a given domain.

check_opcache(): checks whether the opcache module is enabled for the PHP version currently being used.

check_memcached(): checks whether the memcached module is enabled for the PHP version currently being used. check_fpm_enabled: checks whether PHP-FPM is enabled for a given domain and user.

gen_fpm_stats(): generates a table with statistics for all domains on the server.

The gen_fpm_stats function calls the other functions to populate variables with the statistics needed for each domain. Then, it prints a row in the table for each domain, including the domain name, PHP version, whether PHP-FPM is enabled, the maximum number of child processes, the maximum number of requests per child process, the number of times that the maximum number of child processes has been exceeded, whether opcache is enabled, and whether memcached is enabled.

Example

[root@cloudvpsserver ~]# nk_fpm_stats
Domain     │  PHP-Version  FPM-Enabled  │  Max-Children  Max-Requests  Max-Children-Exceeded  │  OPCache  MemCached  │
nkern.net  │  ea-php81     Enabled      │  2             200           0                      │  Enabled  Disabled   │

Code

nk_fpm_stats() {
# This is basically just a wrapper for gen_fpm_stats that formats things nice.

  #### First we need to define some functions we'll use later. ####
find_max_requests() {
# Using the domain run whmapi php_fpm_config_get.
# Then filter out the value for max_requests
# About the whmapi command. https://api.docs.cpanel.net/openapi/whm/operation/php_fpm_config_get/
echo \{\"domain\":\""$domain"\"\} | whmapi1 --input=json  php_fpm_config_get | grep -A 2 "pm_max_requests" | awk '/\svalue:/ {print $NF}'
}


find_max_children() {
# Using the domain run whmapi php_fpm_config_get.
# Then filter out the value for max_children.
# About the whmapi command. https://api.docs.cpanel.net/openapi/whm/operation/php_fpm_config_get/
echo \{\"domain\":\""$domain"\"\} | whmapi1 --input=json  php_fpm_config_get | grep -A 2 "pm_max_children" | awk '/\svalue:/ {print $NF}'
}

check_opcache() {
# Check whether opcache is enabled.
# By the time this is called. $php_version has already been defined.
# If opcache is present in the results of running php -m.
if [ "$(/usr/local/bin/"$php_version" -m | grep -ic "opcache")" != "0" ]; then
    # Then print Enabled. and exit.
    echo "Enabled"
    return 0
fi
# Otherwise print Disabled.
echo "Disabled"
}

check_memcached() {
# Check whether opcache is enabled.
# By the time this is called. $php_version has already been defined.
# If memcached is present in the results of running php -m.
if [ "$(/usr/local/bin/"$php_version" -m | grep -ic "memcached")" != "0" ]; then
    # Then print Enabled. and exit.
    echo "Enabled"
    return 0
fi
# Otherwise print Disabled.
echo "Disabled"
}

check_fpm_enabled() {
# In order to determine if PHP-FPM is enabled for a domain we'll need to know the domain and it's username.
# They'll just be passed as arguments.
domain="$1"
user="$2"
# Given the domain and username we can figure out the path of the php yaml.
yaml_file="/var/cpanel/userdata/$user/$domain.php-fpm.yaml"

# If the php-fpm.yaml file doesn't exist for the domain, then we automatically know that PHP is not enabled.
if [ ! -f "$yaml_file" ]; then
    # So print Disabled and exit.
    echo "Disabled"
    return 0
fi

# Now that we know the file exists, check if "_is_present" is set to 1
if [ "$(grep -c "_is_present: 1" "$yaml_file")" = "0" ]; then
    # If it doesn't match Print Disabled. and exit.
    echo "Disabled"
    return 0
fi
# Otherwise If it does match then print Enabled.
echo "Enabled"
}

# Now we can start with gen_fpm_stats
gen_fpm_stats() {
# First print the header for the table we're creating.
echo "Domain │ PHP-Version FPM-Enabled │ Max-Children Max-Requests Max-Children-Exceeded │ OPCache MemCached │"
#echo "─── │ ─── ─── │ ─── ─── ─── │ ─── ─── │"
# For every domain on the server. Found with nk_list_all_domains.
for domain in $(nk_list_all_domains); do
    # Start populating variables for the table row.
    # php_version is the result of running nk_php_version $domain.
    # fpm_enabled is the result of running check_fpm_enabled when passed the domain and user
    # opcache is the result of running check_opcache
    # memcached is the result of running check_memcached.
    php_version="$(nk_php_version "$domain")"
    user="$(nk_user "$domain")"
    fpm_enabled="$(check_fpm_enabled "$domain" "$user")"
    opcache="$(check_opcache)"
    memcached="$(check_memcached)"

    # If FPM is not enabled, then set all the fpm related variables to 0
    if [ "$fpm_enabled" != "Enabled" ]; then
      max_children="0"
      max_requests="0"
      max_children_exceeded="0"
    else
      # Otherwise populate FPM related variable.
      # max_children is the result of running find_max_children
      # max_requests is the frulst of running find_max_requests
      # FPM error log is a path with php_version substituted in it.
      # pool_name is the domain with periods replaced with underscores.
      # max_children_exceeded is the number of times max_children errors appear for the pool inside of the fpm error log.
      max_children="$(find_max_children)"
      max_requests="$(find_max_requests)"
      fpm_error_log="/opt/cpanel/$php_version/root/usr/var/log/php-fpm/error.log"
      fpm_timestamp="$(date "+%d-%b-%Y")"
      pool_name="$(echo "$domain" | awk '{gsub("\\.","_"); print $0}' )"
      max_children_exceeded="$(grep "$pool_name" "$fpm_error_log" | grep "max_children" | grep -c "$fpm_timestamp")"
    fi
# Now that variables have been defined, we can print the row of the table.
echo "$domain$php_version $fpm_enabled$max_children $max_requests $max_children_exceeded$opcache $memcached │"
done | sort -rn -k 4
# After the full table has been populated, sort it by max_children.
}

# Finally run gen_fpm_stats and format it as a table.
gen_fpm_stats | column -t
}

Author: Nichole Kernreicht

Created: 2023-04-12 Wed 21:24