Nk_base_stats

Home

Description

This script is a shell script for Linux servers. It generates basic server stats such as hostname, OS, CPU cores, memory usage, load, number of domains, PHP-FPM max children reached today, Apache max clients reached today, and MySQL database engine usage (MyISAM and InnoDB).

The script defines several functions to help with parsing log files and generating the output, such as “find_fpm_maxes()”, which finds the number of times that php_fpm has recorded max_children errors today, and nk_percent() which calculates a percentage.

The script then calls “gen_base_stats()”, which generates the body of the output by defining several variables and outputting them in a specific format using the “echo” command. The output is comma-separated, and formatted with column.

Overall, this script is useful for quickly obtaining basic server stats and could be used in a variety of ways, such as monitoring server health or generating reports.

Example

[root@cloudvpsserver ~]# nk_base_stats
Hostname:           cloudvpsserver.host.nkern.net
Operating-System:   AlmaLinux(8.7)
CPU-Cores:          2
Memory:             546Mi/1.8Gi (29.9%)
Load:               0.07 (3.5%)
Domains:            1
FPM-Maxes-Today:    2
Max-Clients-Today:  0
InnoDB:             800KB (99.9%)
MyISAM:             1B (0.00%)

Code

nk_base_stats() {
# This is basically just a wrapper around gen_base_stats that does some formatting.

# First we need to define some functions we'll use later.

find_fpm_maxes() {
# Find the number of times that php_fpm has recorded max_children errors today.
grep "max_children" /opt/cpanel/*/root/usr/var/log/php-fpm/error.log | grep -c "$fpm_timestamp"
}

find_max_clients() {
# Find the number of times that Apache has recorded max client errors today.
grep "MaxC" /usr/local/apache/logs/error_log | grep -c "$apache_timestamp"
}


find_myisam() {
# Find the total amount of MyISAM usage, in bytes.
mysql -e 'SELECT ENGINE,ROUND(SUM(data_length), 1) AS "Data", ROUND(SUM(index_length), 1) AS "Index", ROUND(SUM(data_length + index_length), 1) AS "Total", COUNT(*) "Num Tables" FROM  INFORMATION_SCHEMA.TABLES WHERE  table_schema not in ("information_schema", "performance_schema") GROUP BY ENGINE;' | awk '/MyISAM/ {print $4}'
}

find_innodb() {
# Find the total amount of InnoDB usage, in bytes.
mysql -e 'SELECT ENGINE,ROUND(SUM(data_length), 1) AS "Data", ROUND(SUM(index_length), 1) AS "Index", ROUND(SUM(data_length + index_length), 1) AS "Total", COUNT(*) "Num Tables" FROM  INFORMATION_SCHEMA.TABLES WHERE  table_schema not in ("information_schema", "performance_schema") GROUP BY ENGINE;' | awk '/InnoDB/ {print $4}'
}


gen_base_stats() {
# Generate the body of the output.
# First we need to delcare some variables.
# hostname : the hostname of the server
# os_distro : The Linux distro in use, ie CentOS or AlmaLinux
# os_version : The version of the OS, ex 7 for CentOS 7
# cores : The number of CPU cores on the system
# mem_total_human : Total amount of available ram in human readable.
# mem_use_human : The amoutn of memory used, in human readable.
# mem : printed out, formats mem_use_human and mem_total_human together.
# mem_use : Memory used, in bytes
# mem_total : Memory total, in bytes
# mem_percent : Percent of mem_used vs mem_total.
# load : The current 5 min load avg
# load_percent : The percent of load based on number of cores available.
# domain_num : The number of domains on the server.
# fpm_timestamp : The timestamp for today, in the format of fpm error logs.
# fpm_maxes : The number of times FPM has reached max_children today.
# apache_timestamp : Today's timestamp in apache error log format.
# max_clients : The number of times today apache has reached max_clients.
# innodb : The amount of innodb usage in bytes
# innodb_human : The amoutn of innodb usage in human readable.
# myisam : the amount of myisam usage in bytes.
# myisam_human : the amount of myisam data in human readable.
# total_db : The total amount of db used, with MyISAM and InnoDB combined.
# myisam_percent : The percent of total db data that myisam accounts for.
# innodb_percent : the percent of total db data that innodb accounts for.
hostname="$(hostname)"
os_distro="$(awk '{print $1}' /etc/redhat-release)"
os_version="$(awk '{print $3}' /etc/redhat-release)"
os="$os_distro($os_version)"
cores="$(nproc)"
mem_total_human="$(free -h | awk '/Mem:/ {print $2}')"
mem_use_human="$(free -h | awk '/Mem:/ {print $3}')"
mem="$mem_use_human/$mem_total_human"
mem_use="$(free | awk '/Mem:/ {print $3}')"
mem_total="$(free | awk '/Mem:/ {print $2}')"
mem_percent="$(nk_percent "$mem_use" "$mem_total")"
load="$(awk '{print $1}' /proc/loadavg)"
load_percent="$(nk_percent "$load" "$cores")"
domain_num="$(grep -Evc "^\*" /etc/userdomains)"
fpm_timestamp="$(date "+%d-%b-%Y")"
fpm_maxes="$(find_fpm_maxes)"
apache_timestamp="$(date "+%a %b %d")"
max_clients="$(find_max_clients)"
innodb="$(find_innodb)"
innodb_human="$(numfmt --to=iec --suffix=B "$innodb")"
myisam="$(find_myisam)"
# If myisam is empty, set it to one so we don't get percent errors.
if [ "$myisam" = "" ]; then
    myisam="1"
fi
myisam_human="$(numfmt --to=iec --suffix=B "$myisam")"
total_db="$(echo "$myisam" "$innodb" | awk '{print $1+$2}')"
myisam_percent="$(nk_percent "$myisam" "$total_db")"
innodb_percent="$(nk_percent "$innodb" "$total_db")"

echo "Hostname:,$hostname"
echo "Operating-System:,$os"
echo "CPU-Cores:,$cores"
echo "Memory:,$mem ($mem_percent)"
echo "Load:,$load ($load_percent)"
echo "Domains:,$domain_num"
echo "FPM-Maxes-Today:,$fpm_maxes"
echo "Max-Clients-Today:,$max_clients"
echo "InnoDB:,$innodb_human ($innodb_percent)"
echo "MyISAM:,$myisam_human ($myisam_percent)"
}

# Now print everything out, seperate by commas since we want spaces in fields.
gen_base_stats | column -t -s ","
}

Author: Nichole Kernreicht

Created: 2023-04-09 Sun 21:50