diff --git a/conf/create_summary_websites.conf.sample b/conf/create_summary_websites.conf.sample new file mode 100644 index 0000000..c5fe7ca --- /dev/null +++ b/conf/create_summary_websites.conf.sample @@ -0,0 +1,17 @@ +## =================================================================== +## - Configuration File for Script "create_summary_websites.sh" +## =================================================================== + +# - DST_PATH_PHP +# - +# - Destination Path for statistics +# - +DST_PATH_PHP="/var/www/html/dummy/htdocs/website-stats + +# - DST_DIR_CSV +# - +# - Destination Path for generated csv-files +# - +# - Defaults to: ${DST_PATH_PHP}/csv-files-website-stats +# - +DST_DIR_CSV="${DST_PATH_PHP}/csv-files-website-stats" diff --git a/create_summary_websites.sh b/create_summary_websites.sh new file mode 100755 index 0000000..fdc3b1a --- /dev/null +++ b/create_summary_websites.sh @@ -0,0 +1,565 @@ +#!/usr/bin/env bash + +working_dir="$(dirname $(realpath $0))" +conf_file="${working_dir}/conf/create_summary_websites.conf" + + +# ------------- +# - Variable settings +# ------------- + +declare -a apache_vhost_dir_arr +declare -a _all_vhost_files_arr +declare -a _all_document_root_dirs_arr + + +# ------------- +# --- Some functions +# ------------- + +clean_up() { + + # Perform program exit housekeeping + rm -f $_tmp_filename_summary + rm -f $_tmp_filename_redirected + rm -f $_tmp_filename_mod_php + rm -f $_tmp_filename_php_fpm + rm -f $_tmp_filename_php_fcgid + rm -f $_tmp_filename_php_53 + rm -f $_tmp_filename_php_54 + rm -f $_tmp_filename_php_55 + rm -f $_tmp_filename_php_56 + rm -f $_tmp_filename_php_70 + rm -f $_tmp_filename_php_71 + exit $1 +} + +# - Check if a given array (parameter 2) contains a given string (parameter 1) +# - +containsElement () { + local e + for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done + return 1 +} + +stringContain() { + [ -z "${2##*$1*}" ] && [ -z "$1" -o -n "$2" ] +} + +# - Remove leading/trailling whitespaces +# - +trim() { + local var="$*" + var="${var#"${var%%[![:space:]]*}"}" # remove leading whitespace characters + var="${var%"${var##*[![:space:]]}"}" # remove trailing whitespace characters + echo -n "$var" +} + +fatal(){ + echo "" + echo -e " [ Fatal ] $*" + echo "" + echo -e "\tScript terminated.." + echo "" + clean_up 1 +} + + +trap clean_up SIGHUP SIGINT SIGTERM + + +# ------------- +# - Load Settings from configuration file get_summary_websites.conf +# ------------- + +if [[ -f "$conf_file" ]]; then + source "$conf_file" > /dev/null 2>&1 + if [[ $? -ne 0 ]]; then + fatal "Reading Configuration '$conf_file' failed!" + fi +fi + + +if [[ -z "$DST_PATH_PHP" ]]; then + if [[ -d "/var/www/html/dummy/htdocs" ]] ; then + DST_PATH_PHP="/var/www/html/dummy/htdocs" + elif [[ -d "/var/www/html/dummy" ]] ; then + DST_PATH_PHP="/var/www/html/dummy" + elif [[ -d "/var/www/default" ]] ; then + DST_PATH_PHP="$DST_PATH_PHP" + fi +else + if [[ ! -d "$DST_PATH_PHP" ]]; then + mkdir "$DST_PATH_PHP" > /dev/null 2>&1 + if [[ $? -ne 0 ]]; then + fatal "Creation of directory '$DST_PATH_PHP' failed!" + fi + fi +fi + +if [[ -z "$DST_PATH_PHP" ]]; then + fatal "Destination Path for statistics (parameter 'DST_PATH_PHP') not found!" +fi + +if [[ -z "$DST_DIR_CSV" ]] ; then + DST_DIR_CSV="${DST_PATH_PHP}/csv-files-website-stats" +fi + +if [[ ! -d "$DST_DIR_CSV" ]]; then + mkdir $DST_DIR_CSV > /dev/null 2>&1 + if [[ $? -ne 0 ]]; then + fatal "Creation of directory '$DST_DIR_CSV' failed!" + fi +fi + +_tmp_filename_summary=$(mktemp) +filename_summary="${DST_DIR_CSV}/WEBSITES_SUMMARY.csv" +cat < ${DST_PATH_PHP}/websites_summary.php + +EOF +echo "Website; ;PHP Version; ;PHP Engine; ;Database; ;CMS; ;DocumentRoot; ;VHost file; ;Server Alias(es)" > $filename_summary +echo ";" >> $filename_summary + +_tmp_filename_redirected=$(mktemp) +filename_redirected="${DST_DIR_CSV}/WEBSITES_REDIRECTED.csv" +cat < ${DST_PATH_PHP}/websites_redirected.php + +EOF +echo "Website; ;Redirected to; ;VHost file" > $filename_redirected +echo ";" >> $filename_redirected + +_tmp_filename_mod_php=$(mktemp) +filename_mod_php="${DST_DIR_CSV}/WEBSITES_MOD_PHP.csv" +cat < ${DST_PATH_PHP}/websites_mod_php.php + +EOF +echo "Website; ;PHP Version; ;PHP Engine; ;Database; ;CMS; ;DocumentRoot; ;VHost file; ;Server Alias(es)" > $filename_mod_php +echo ";" >> $filename_mod_php + +_tmp_filename_php_fpm=$(mktemp) +filename_php_fpm="${DST_DIR_CSV}/WEBSITES_PHP_FPM.csv" +cat < ${DST_PATH_PHP}/websites_php_fpm.php + +EOF +echo "Website; ;PHP Version; ;PHP Engine; ;Database; ;CMS; ;DocumentRoot; ;VHost file; ;Server Alias(es)" > $filename_php_fpm +echo ";" >> $filename_php_fpm + +_tmp_filename_php_fcgid=$(mktemp) +filename_php_fcgid="${DST_DIR_CSV}/WEBSITES_PHP_FCGID.csv" +cat < ${DST_PATH_PHP}/websites_php_fcgid.php + +EOF +echo "Website; ;PHP Version; ;PHP Engine; ;Database; ;CMS; ;DocumentRoot; ;VHost file; ;Server Alias(es)" > $filename_php_fcgid +echo ";" >> $filename_php_fcgid + +_tmp_filename_php_53=$(mktemp) +filename_php_53="${DST_DIR_CSV}/WEBSITES_PHP_53.csv" +cat < ${DST_PATH_PHP}/websites_php_53.php + +EOF +echo "Website; ;PHP Version; ;PHP Engine; ;Database; ;CMS; ;DocumentRoot; ;VHost file; ;Server Alias(es)" > $filename_php_53 +echo ";" >> $filename_php_53 + +_tmp_filename_php_54=$(mktemp) +filename_php_54="${DST_DIR_CSV}/WEBSITES_PHP_54.csv" +cat < ${DST_PATH_PHP}/websites_php_54.php + +EOF +echo "Website; ;PHP Version; ;PHP Engine; ;Database; ;CMS; ;DocumentRoot; ;VHost file; ;Server Alias(es)" > $filename_php_54 +echo ";" >> $filename_php_54 + +_tmp_filename_php_55=$(mktemp) +filename_php_55="${DST_DIR_CSV}/WEBSITES_PHP_55.csv" +cat < ${DST_PATH_PHP}/websites_php_55.php + +EOF +echo "Website; ;PHP Version; ;PHP Engine; ;Database; ;CMS; ;DocumentRoot; ;VHost file; ;Server Alias(es)" > $filename_php_55 +echo ";" >> $filename_php_55 + +_tmp_filename_php_56=$(mktemp) +filename_php_56="${DST_DIR_CSV}/WEBSITES_PHP_56.csv" +cat < ${DST_PATH_PHP}/websites_php_56.php + +EOF +echo "Website; ;PHP Version; ;PHP Engine; ;Database; ;CMS; ;DocumentRoot; ;VHost file; ;Server Alias(es)" > $filename_php_56 +echo ";" >> $filename_php_56 + +_tmp_filename_php_70=$(mktemp) +filename_php_70="${DST_DIR_CSV}/WEBSITES_PHP_70.csv" +cat < ${DST_PATH_PHP}/websites_php_70.php + +EOF +echo "Website; ;PHP Version; ;PHP Engine; ;Database; ;CMS; ;DocumentRoot; ;VHost file; ;Server Alias(es)" > $filename_php_70 +echo ";" >> $filename_php_70 + +_tmp_filename_php_71=$(mktemp) +filename_php_71="${DST_DIR_CSV}/WEBSITES_PHP_71.csv" +cat < ${DST_PATH_PHP}/websites_php_71.php + +EOF +echo "Website; ;PHP Version; ;PHP Engine; ;Database; ;CMS; ;DocumentRoot; ;VHost file; ;Server Alias(es)" > $filename_php_71 +echo ";" >> $filename_php_71 + + +cat < ${DST_PATH_PHP}/index.php + + + Statistics websites + + + + + +EOF + + +# ----- +# - Gathering global settings +# ----- + +# - Check if mod_php is installed. +# - +mod_php_installed=false +if $(httpd -M | grep -q -E "php[5-9]?_module" 2>/dev/null) ; then + mod_php_installed=true + if $(apachectl status | grep -q "PHP/" 2>/dev/null) ; then + MOD_PHP_VERSION="$(trim $(apachectl status | grep "PHP/" | cut -d '/' -f2))" + else + MOD_PHP_VERSION=" UNKNOWN" + warn "'mod_php' ist running, but i cannot dermin the PHP Version.\n 'apachectl status' does not work on this server." + fi +fi + + +# - Try to find apache vhost configuration directories +# - +if [[ -d "/usr/local/apache2/conf/vhosts" ]]; then + if [[ -d "/usr/local/apache2/conf/vhosts/0" ]]; then + while IFS='' read -r -d '' dirname ; do + if ! containsElement "$(realpath $dirname)" "${apache_vhost_dir_arr[@]}" ; then + apache_vhost_dir_arr+=("$(realpath $dirname)") + fi + done < <(find /usr/local/apache2/conf/vhosts -mindepth 1 -maxdepth 1 -type d -print0) + else + apache_vhost_dir_arr+=("/usr/local/apache2/conf/vhosts") + fi +elif [[ -d "/etc/apache2/sites-enabled" ]]; then + apache_vhost_dir_arr+=("/etc/apache2/sites-enabled") +fi + + +# - Create Array of all active VHost configurations +# - +for apache_vhost_dir in ${apache_vhost_dir_arr[@]} ; do + + + # - Collect all VHost files + # - + while IFS='' read -r -d '' filename ; do + if ! containsElement "$(realpath $filename)" "${_all_vhost_files_arr[@]}" ; then + _all_vhost_files_arr+=("$(realpath $filename)") + fi + done < <(find $apache_vhost_dir -mindepth 1 -maxdepth 1 -name '*.conf' -print0) +done + + +# - Create summary files +# - +for _vhost_file in ${_all_vhost_files_arr[@]} ; do + + # - Get Server Name + # - + _server_name=$(grep -i -E "^\s*ServerName" $_vhost_file | sed -e "s/ServerName//" | sed "s/^\s*//" | sed "s/\s*$//" | sort -u) + + + # - Get Server Aliase + # - + _server_aliases=$(grep -i -E "^\s*ServerAlias" $_vhost_file | sed -e "s/ServerAlias//" | sed "s/^\s*//" | sed "s/\s*$//" | sort -u) + + server_aliases="" + if [[ -n "$_server_aliases" ]] ; then + for _server_alias in $_server_aliases ; do + server_aliases="$server_aliases $_server_alias" + done + fi + + server_aliases="$(echo "$(trim $server_aliases)" | tr '\n' ' ')" + + + # - Get documetroot of current VHost configuration file + # - + _documentroot=$(grep -E "^\s*DocumentRoot" $_vhost_file 2> /dev/null | awk '{print$2}' | sed 's/"//g' | sed 's/\/$//' | sort -u) + + + if [[ -n $_documentroot ]] ; then + + # --- + # - Get databases amd type of CMS + # --- + + site_cms="Unknown" + database="Not found" + + file_to_check="$(dirname $_documentroot)/db.inc.php" + file_to_check_mediawiki="$(dirname $_documentroot)/htdocs/LocalSettings.php" + + _found=false + if [[ -r "$file_to_check_mediawiki" ]] && [[ ! -d "$file_to_check_mediawiki" ]] ; then + db_search_string='\$wgDBname' + #db_search_string='\$wgEnableUserEmail' + _found=false + if grep -i -q "$db_search_string" $file_to_check_mediawiki > /dev/null 2>&1 ; then + database="$(grep -i "$db_search_string" $file_to_check_mediawiki 2> /dev/null \ + | awk -F '=' '{print$2}' \ + | awk -F ';' '{print$1}'\ + | tail -1)" + + # - Remove leading whitespace characters + database="${database#"${database%%[![:space:]]*}"}" + # - Remove trailing whitespace characters + database="${database%"${database##*[![:space:]]}"}" + # - Remove leading single quote + database="${database#"${database%%[!\']*}"}" + # - Remove trailing single quote + database="${database%"${database##*[!\']}"}" + # - Remove leading double quote + database="${database#"${database%%[!\"]*}"}" + # - Remove trailing double quote + database="${database%"${database##*[!\"]}"}" + + + _found=true + fi + + site_cms="MediaWiki" + + elif [[ -r "$file_to_check" ]] && [[ ! -d "$file_to_check" ]]; then + db_search_strings=('\$db_name' '\$mysql_db') + _found=false + for _string in ${db_search_strings[@]} ; do + if grep -i -q -E "^\s*$_string" $file_to_check > /dev/null 2>&1 ; then + + database="$(grep -i -E "^\s*$_string" $file_to_check 2> /dev/null | awk -F '=' '{print$2}' | tail -1 )" + + # - Remove leading whitespace characters + database="${database#"${database%%[![:space:]]*}"}" + # - Remove trailing whitespace characters + database="${database%"${database##*[![:space:]]}"}" + # - Rmove trailing semicolon characters + database="${database%"${database##*[!;]}"}" + # - Remove trailing whitespace characters + database="${database#"${database%%[!\']*}"}" + # - Remove trailing inverted comma characters + database="${database%"${database##*[!\']}"}" + + _found=true + + break + fi + done + + if ! $_found ; then + file_to_check="${_documentroot}/includes/config.inc.php" + if [[ -f "$file_to_check" ]]; then + if grep -i -q -E "^\s*define\('DB_DATABASE'" $file_to_check > /dev/null 2>&1 ; then + database="$(grep -i -m 1 -E "^\s*define\('DB_DATABASE'" $file_to_check 2> /dev/null \ + | awk -F ',' '{print$2}' \ + | awk -F ')' '{print$1}' \ + | tail -1 )" + + # - Remove leading whitespace characters + database="${database#"${database%%[![:space:]]*}"}" + # - Remove trailing whitespace characters + database="${database%"${database##*[![:space:]]}"}" + # - Remove leading single quote + database="${database#"${database%%[!\']*}"}" + # - Remove trailing single quote + database="${database%"${database##*[!\']}"}" + # - Remove leading double quote + database="${database#"${database%%[!\"]*}"}" + # - Remove trailing double quote + database="${database%"${database##*[!\"]}"}" + + _found=true + fi + fi + fi + + site_cms="Kontext" + + fi + + + # - Get PHP version of current vhost configuration file + # - + _php_engine="" + _php_version="" + CUR_IFS=$IFS + IFS='' + if grep -q -E "^\s*(ProxyPassMatch|SetHandler).*php-[5-9]\.[0-9]+-" $_vhost_file 2> /dev/null ; then + _php_engine="PHP FPM" + + if grep -q -E "^\s*(SetHandler).*php-[5-9]\.[0-9]+-" $_vhost_file 2> /dev/null ; then + _php_fpm_socket="$(grep -E "^\s*(SetHandler).*php-[5-9]\.[0-9]+-" $_vhost_file 2> /dev/null \ + | head -1 | cut -d ':' -f3 | cut -d'|' -f1)" + else + _php_fpm_socket= + fi + if [[ -S "$_php_fpm_socket" ]] ; then + _php_version="$(exec /usr/local/php-$(echo "$_php_fpm_socket" | grep -o -E "[5-9]\.[0-9]")/bin/php-cgi -v \ + | head -1 | awk '{print$2}')" + else + _php_version="UNKNOWN" + fi + echo "$_server_name;;$_php_version;;$_php_engine;;$database;;$site_cms;;$_documentroot;;$_vhost_file;;$server_aliases" >> $_tmp_filename_php_fpm + + elif grep -d skip -q -E "^\s*FCGIWrapper\s+.*\.php" $_vhost_file 2> /dev/null ; then + _php_engine="PHP FastCGI" + + _fcgid_file="$(grep -E "^\s*FCGIWrapper\s+.*\.php" $_vhost_file 2> /dev/null \ + | head -1 | awk '{print$2}')" + if [[ -f "$_fcgid_file" ]] ; then + _php_version="$($(grep "exec" $_fcgid_file | awk '{print$2}') -v | head -1 | awk '{print$2}')" + [[ -z "$_php_version" ]] && _php_version="unknown Version" + else + _php_version="UNKNOWN" + fi + echo "$_server_name;;$_php_version;;$_php_engine;;$database;;$site_cms;;$_documentroot;;$_vhost_file;;$server_aliases" >> $_tmp_filename_php_fcgid + + elif $mod_php_installed ; then + _php_engine="Apache Handler" + _php_version="$MOD_PHP_VERSION" + echo "$_server_name;;$_php_version;;$_php_engine;;$database;;$site_cms;;$_documentroot;;$_vhost_file;;$server_aliases" >> $_tmp_filename_mod_php + else + _php_version="No PHP assigned" + _php_engine="" + fi + + echo "$_server_name;;$_php_version;;$_php_engine;;$database;;$site_cms;;$_documentroot;;$_vhost_file;;$server_aliases" >> $_tmp_filename_summary + + if [[ "$_php_version" =~ ^5.3 ]]; then + echo "$_server_name;;$_php_version;;$_php_engine;;$database;;$site_cms;;$_documentroot;;$_vhost_file;;$server_aliases" >> $_tmp_filename_php_53 + fi + + if [[ "$_php_version" =~ ^5.4 ]]; then + echo "$_server_name;;$_php_version;;$_php_engine;;$database;;$site_cms;;$_documentroot;;$_vhost_file;;$server_aliases" >> $_tmp_filename_php_54 + fi + + if [[ "$_php_version" =~ ^5.5 ]]; then + echo "$_server_name;;$_php_version;;$_php_engine;;$database;;$site_cms;;$_documentroot;;$_vhost_file;;$server_aliases" >> $_tmp_filename_php_55 + fi + + if [[ "$_php_version" =~ ^5.6 ]]; then + echo "$_server_name;;$_php_version;;$_php_engine;;$database;;$site_cms;;$_documentroot;;$_vhost_file;;$server_aliases" >> $_tmp_filename_php_56 + fi + + if [[ "$_php_version" =~ ^7.0 ]]; then + echo "$_server_name;;$_php_version;;$_php_engine;;$database;;$site_cms;;$_documentroot;;$_vhost_file;;$server_aliases" >> $_tmp_filename_php_70 + fi + + if [[ "$_php_version" =~ ^7.1 ]]; then + echo "$_server_name;;$_php_version;;$_php_engine;;$database;;$site_cms;;$_documentroot;;$_vhost_file;;$server_aliases" >> $_tmp_filename_php_71 + fi + + else + _redirect_site="" + _redirect_site=$(grep -E "^\s*RewriteRule" $_vhost_file 2> /dev/null | awk '{print$3}' | sed 's/"//g' | sed 's/\/$//' | sed 's/https\?:\/\///g' | sed 's/\$1//g' | sort -u) + if [[ -n "$_redirect_site" ]] ; then + echo "$_server_name;;$_redirect_site;;$_vhost_file" >> $_tmp_filename_redirected + else + echo "$_vhost_file:UNKNOWN" + fi + fi +done + +cat $_tmp_filename_summary | sort >> $filename_summary +cat $_tmp_filename_redirected | sort >> $filename_redirected +cat $_tmp_filename_mod_php | sort >> $filename_mod_php +cat $_tmp_filename_php_fpm | sort >> $filename_php_fpm +cat $_tmp_filename_php_fcgid | sort >> $filename_php_fcgid +cat $_tmp_filename_php_53 | sort >> $filename_php_53 +cat $_tmp_filename_php_54 | sort >> $filename_php_54 +cat $_tmp_filename_php_55 | sort >> $filename_php_55 +cat $_tmp_filename_php_56 | sort >> $filename_php_56 +cat $_tmp_filename_php_70 | sort >> $filename_php_70 +cat $_tmp_filename_php_71 | sort >> $filename_php_71 + +clean_up