File: //lib64/nagios/plugins/check_a2_httpdconf.shared
#!/bin/bash
# Script to ensure httpd.conf has no errors and can be rebuilt without any failure.
# BFENG-504
# Refactored May 2024 | BFENG-1045
cpversion=$(cat /usr/local/cpanel/version)
httpd_conf="/etc/apache2/conf/httpd-preview.conf"
httpd_conf_rebuild_log="/var/log/httpd_conf_errors.log"
# Excluding httpd.conf rebuild if it's not cPanel
if [ -z "$cpversion" ]; then
/usr/sbin/apachectl configtest &>/dev/null
if [ $? -eq 0 ]; then
echo "httpd.conf has passed syntax check."
exit 0
else
echo "httpd.conf isn't valid."
exit 2
fi
else
temp_output=$(mktemp)
max_attempts=3
attempt=1
retry_delay=5
while [ $attempt -le $max_attempts ]; do
/usr/local/cpanel/scripts/rebuildhttpdconf --preview &> "$temp_output"
if [ $? -eq 0 ]; then
echo "httpd.conf has passed syntax check and successfully rebuilt."
exit 0
rm -f "$temp_output"
exit
elif [ $attempt -lt $max_attempts ]; then
sleep $retry_delay
fi
attempt=$((attempt + 1))
done
echo "httpd.conf rebuild failed."
exit 2
echo -e "$(date '+%Y-%m-%d %H:%M:%S')\n$(cat "$temp_output")" >> "$httpd_conf_rebuild_log"
rm -f "$temp_output"
fi