diff --git a/recipes/newrelic/apm/php/awslinux.yml b/recipes/newrelic/apm/php/awslinux.yml new file mode 100644 index 000000000..04031f949 --- /dev/null +++ b/recipes/newrelic/apm/php/awslinux.yml @@ -0,0 +1,560 @@ +# Visit our schema definition for additional information on this file format +# https://github.com/newrelic/open-install-library/blob/main/docs/recipe-spec/recipe-spec.md#schema-definition + +name: php-agent-installer +displayName: PHP Agent +description: New Relic install recipe for instrumenting PHP applications on Redhat systems with php-fpm, nginx, or apache. +repository: https://github.com/newrelic/newrelic-php-agent + +installTargets: + - type: host + os: linux + platform: amazon + platformFamily: rhel + platformVersion: "(2023\\.*)" + +keywords: + - Apm + - php + - fpm + - Amazon Linux 2023 + +processMatch: + # several possible configurations we can match on: + # - older apache and no php-fpm (process named httpd) + # - newer apache and no php-fpm (process named apache2) + # - nginx and php-fpm (process named php-fpm) + # in the preInstall we will check if php is installed and further filter down the possible scenarios + - php-fpm + - apache2 + - httpd + +preInstall: + requireAtDiscovery: | + # verify running with sudo + if [ -z "$SUDO_USER" ]; then + echo "This installation recipe for the New Relic PHP agent must be run under sudo." >&2 + exit 3 + fi + + IS_PHP_INSTALLED=$(which php | wc -l) + if [ "$IS_PHP_INSTALLED" -eq 0 ]; then + # PHP not installed, no detection + echo "Error: PHP is not installed. Please install PHP, configure and enable your web server (Apache or NGINX), then retry the installation." >&2 + exit 1 + fi + + # Map of tool names to the associated error code + required_tools_and_error_codes="grep:132 sed:132 awk:132 egrep:132 curl:132 mount:132 yum:132" + + for tuple in $required_tools_and_error_codes; do + tool=$(echo ${tuple} |cut -d':' -f1) + code=$(echo ${tuple} |cut -d':' -f2) + + IS_TOOL_INSTALLED=$(which ${tool} | wc -l) + if [ "$IS_TOOL_INSTALLED" -eq 0 ] + then + echo "This installation recipe for the New Relic PHP Agent on Linux requires '${tool}' to be installed." >&2 + exit ${code} + fi + done + + IS_FPM_RUNNING=$(ps aux | grep php-fpm | grep -v grep | wc -l) + IS_APACHE_RUNNING=$(ps -eo comm,etime,user | grep root | grep apache2 | grep -v grep| wc -l) + if [ "$IS_FPM_RUNNING" -eq 0 ]; then + # No FPM running, recipe cannot install, either signal DETECTED or skip + if [ "$IS_APACHE_RUNNING" -eq 0 ]; then + # No Apache or FPM running, no detection + echo "Error: Neither Apache nor php-fpm is running. Please start the relevant services then retry the installation." >&2 + exit 4 + else + # No FPM, Apache is running, let's check if Apache PHP module is enabled + IS_APACHE_PHP_ENABLED=$(apachectl -t -D DUMP_MODULES | grep php | grep -v grep | wc -l) + if [ "$IS_APACHE_PHP_ENABLED" -eq 0 ]; then + # No FPM, Apache is running, but not PHP module enabled, no detection + echo "Error: Apache is running, but the PHP module is still disabled. Please enable the PHP module and retry the installation." >&2 + exit 5 + else + echo "No FPM, Apache running with PHP module, Ok to install" >&2 + fi + fi + else + echo "PHP-FPM present, Ok to install" >&2 + fi + + # Either FPM or Apache present with PHP enabled, will signal DETECTED or ok to install + + IS_SYSTEMCTL_INSTALLED=$(which systemctl | wc -l) + if [ "$IS_SYSTEMCTL_INSTALLED" -eq 0 ]; then + echo "This installation recipe for the New Relic PHP agent only supports services managed by 'systemd'." >&2 + exit 132 + fi + + # check if /tmp is mounted noexec - this causes problems with how the recipe works + tmp_is_noexec=$(mount| egrep ".*on /tmp.*,noexec,.*$") + if [ ! -z "${tmp_is_noexec}" ]; then + echo "This installation recipe for the New Relic PHP Agent on Linux requires /tmp to not be mounted noexec." >&2 + exit 132 + fi + + # check if we have permissions to manipulate /tmp + mkdir -p /tmp/newrelic-php-agent.test-dir 2>/dev/null + MKDIR_STATUS=$? + + rm -rf /tmp/newrelic-php-agent.test-dir 2>/dev/null + RM_STATUS=$? + + if [ $RM_STATUS -ne 0 ] || [ $MKDIR_STATUS -ne 0 ]; then + echo "This installation recipe for the New Relic PHP Agent on Linux requires write permissions to /tmp. " + exit 132 + fi + + # everything looks good! + exit 0 + +validationNrql: "SELECT count(*) from Transaction WHERE host like '{{.HOSTNAME}}%' facet entityGuid since 10 minutes ago" + +install: + version: "3" + silent: true + + vars: + TMP_INSTALL_DIR: + sh: mktemp -d /tmp/newrelic-php-agent.XXXXXX + WHITE: '\033[0;97m' + RED: '\033[0;31m' + GRAY: '\033[38;5;248m' + CYAN: '\033[0;36m' + NOCOLOR: '\033[0m' + YELLOW: '\033[0;33m' + ARROW: '\033[0;36m===> \033[0;97m' + NEW_RELIC_APPLICATION_NAME: '{{.NEW_RELIC_APPLICATION_NAME}}' + NEW_RELIC_APPLICATION_RESTART: 'y' + USER_INPUT: 'user_input.txt' + + tasks: + default: + cmds: + - task: collect_metadata + - task: user_input + - task: verify_continue + - task: clean_slate + - task: detect_services + - task: install_tarball + - task: configure + - task: restart_services + - task: send_transaction + - task: cleanup_temp_files + + collect_metadata: + cmds: + - | + phpVersion=$(php -r "echo PHP_VERSION;") + echo "{\"Metadata\":{\"phpVersion\":\"${phpVersion}\"}}" | tee -a {{.NR_CLI_OUTPUT}} > /dev/null + + user_input: + cmds: + - | + # Interactive install with NEW_RELIC_APPLICATION_NAME envar unset, prompts user input + if [[ "{{.NEW_RELIC_ASSUME_YES}}" != "true" && -z "$NEW_RELIC_APPLICATION_NAME" ]]; then + printf "What is the name of your PHP application? (Double-quote (\") is not a valid character in the name) " + read -r APPLICATION_NAME + + printf "Restart the PHP web service after PHP agent installation? Selecting 'y' will automatically restart php-fpm, nginx, or apache depending on the service that is detected. Selecting 'n' will require you to manually restart the process when prompted. (y/n)" + read -r APPLICATION_RESTART + APPLICATION_RESTART=${APPLICATION_RESTART:-y} + + echo "$APPLICATION_NAME" > "{{.TMP_INSTALL_DIR}}/{{.USER_INPUT}}" + echo "$APPLICATION_RESTART" >> "{{.TMP_INSTALL_DIR}}/{{.USER_INPUT}}" + fi + + # Interactive install with NEW_RELIC_APPLICATION_NAME envar set, uses envar and also defaults to service restart + if [[ "{{.NEW_RELIC_ASSUME_YES}}" != "true" && ! -z $NEW_RELIC_APPLICATION_NAME ]]; then + echo "$NEW_RELIC_APPLICATION_NAME" > "{{.TMP_INSTALL_DIR}}/{{.USER_INPUT}}" + echo "y" >> "{{.TMP_INSTALL_DIR}}/{{.USER_INPUT}}" + fi + + verify_continue: + cmds: + - | + if [[ "{{.NEW_RELIC_ASSUME_YES}}" != "true" ]]; then + read -r APPLICATION_NAME < "{{.TMP_INSTALL_DIR}}/{{.USER_INPUT}}" + read -r APPLICATION_RESTART < <(tail -n 1 "{{.TMP_INSTALL_DIR}}/{{.USER_INPUT}}") + else + APPLICATION_NAME="{{.NEW_RELIC_APPLICATION_NAME}}" + APPLICATION_RESTART="{{.NEW_RELIC_APPLICATION_RESTART}}" + + # Remove '{{.TMP_INSTALL_DIR}}/{{.USER_INPUT}}' and re-create it again with + # new values to make them available in next tasks. + if [ -f "{{.TMP_INSTALL_DIR}}/{{.USER_INPUT}}" ]; then + rm -f "{{.TMP_INSTALL_DIR}}/{{.USER_INPUT}}" 2>/dev/null + fi + echo "$APPLICATION_NAME" > "{{.TMP_INSTALL_DIR}}/{{.USER_INPUT}}" + echo "$APPLICATION_RESTART" >> "{{.TMP_INSTALL_DIR}}/{{.USER_INPUT}}" + fi + + echo -e "{{.WHITE}}Confirmation: The following options were selected. If you wish to change an option, please select 'n' at the prompt and restart the installation." + echo -e "* The following application name was selected: $APPLICATION_NAME" + if [ "$APPLICATION_RESTART" = "y" ]; then + echo -e "{{.YELLOW}}* Automatic process restart was selected. This installation WILL restart the php-fpm or nginx process depending on the application architecture detected.{{.NOCOLOR}}" + else + echo -e "{{.YELLOW}}* Manual process restart was selected. This installation WILL NOT automatically restart the apache, php-fpm, or nginx process depending on the application architecture detected. You will be prompted to restart manually in order to complete a successful installation of the PHP agent.{{.NOCOLOR}}" + fi + echo -e "{{.GRAY}} + Note: If you are hosting your PHP application on something other than apache, nginx, or php-fpm, please select 'n' and check out our other installation options: + https://docs.newrelic.com/docs/agents/php-agent/installation/php-agent-installation-overview/{{.NOCOLOR}} + " + if [[ "{{.NEW_RELIC_ASSUME_YES}}" != "true" ]]; then + while :; do + echo -n -e "{{.WHITE}}Do you want to install the PHP Agent Y/N (default: Y)? {{.NOCOLOR}}" + read answer + echo "" + NEW_RELIC_CONTINUE=$(echo "${answer^^}" | cut -c1-1) + if [[ -z "$NEW_RELIC_CONTINUE" ]]; then + NEW_RELIC_CONTINUE="Y" + fi + if [[ "$NEW_RELIC_CONTINUE" == "n" ]] || [[ "$NEW_RELIC_CONTINUE" == "N" ]]; then + echo -e "{{.WHITE}}Exiting the installation{{.NOCOLOR}}" + exit 130 + fi + if [[ "$NEW_RELIC_CONTINUE" == "y" ]] || [[ "$NEW_RELIC_CONTINUE" == "Y" ]]; then + break + fi + echo -e "{{.WHITE}}Please type Y or N only.{{.NOCOLOR}}" + done + fi + + clean_slate: + cmds: + - | + # + # The `|| true` is required else if there isn't a daemon running, + # the killall command will always exit 1 and terminate the recipe. + # + killall -q newrelic-daemon || true + rm -rf /var/log/newrelic 2>/dev/null + + detect_services: + cmds: + - | + echo -e "{{.ARROW}}Detecting services{{.NOCOLOR}}" + cd {{.TMP_INSTALL_DIR}} + rm -f {{.TMP_INSTALL_DIR}}/nonpriv_users.txt 2>/dev/null + rm -f {{.TMP_INSTALL_DIR}}/processes_to_restart.txt 2>/dev/null + # + # For guided installs, we currently support `php-fpm`, `apache`, and `nginx`. + # There is a reason for the order. According our install script: + # If both Apache and FPM are installed, we want FPM to win: + # while there are many ways to end up with libapache2-mod-php5 installed, + # it's unlikely php5-fpm will be installed unless FPM is actually in use. + # + declare -a guided_support=("(php-fpm)" "(httpd|apache2|apache)" "(nginx)") + current_user=$SUDO_USER + for PROCESS in ${guided_support[@]} + do + # + # First look for the full process name with a minimal set of other values so + # that there is no chance of filtering off of those (for instance, when the + # username is `apache` or something similar to the filters. Then use the name + # for other searches. + # + full_process_name=$(ps xf | egrep $PROCESS | grep -v grep | grep -v yml | head -n1 | awk '{print $5}') + if [ -z $full_process_name ]; then + continue + fi + priv_user=$(ps auxf | egrep $full_process_name | grep -v grep | grep -v yml | head -n1 | awk '{print $1}') + + pid=$(ps auxf | egrep $full_process_name | grep -v grep | grep -v yml | head -n1 | awk '{print $2}') + # Use the pid to find the systemctl service name + service_name=$(systemctl status $pid | head -n1 | awk '{print $2}') + # Check that systemctl found a .service + if [[ ! $service_name =~ '.service' ]] + then + echo -e "{{.RED}}Found service-process \"${full_process_name}\" but failed to find associated systemctl service{{.NOCOLOR}}" + continue + fi + # Remove .service from the end of the service name + service_name=${service_name%".service"} + + # Check that service name is somewhat expected + if [[ ! $service_name =~ 'php.*fpm' ]] && \ + [[ ! $service_name =~ 'apache' ]] && \ + [[ ! $service_name =~ 'httpd' ]] && \ + [[ ! $service_name =~ 'nginx' ]] + then + echo -e "{{.RED}}Found systemctl service \"${service_name}\" that was assumed to be a php service ${PROCESS}, but the name suggests otherwise{{.NOCOLOR}}" + continue + fi + + echo -e "{{.WHITE}}Found service: {{.CYAN}}$service_name{{.NOCOLOR}}" + + nonpriv_user= + if [ -n "${priv_user}" ]; then + nonpriv_user=$(ps auxf | egrep $full_process_name | grep -v grep | grep -v yml | grep -v $priv_user | head -n1 | awk '{print $1}') + fi + if [ -n "${service_name}" ]; then + if [ -z "${nonpriv_user}" ]; then + nonpriv_user=$current_user + fi + # + # Output all processes to be restarted to a file with the following format: + # service_name privileged_user + # When the process is restarted, it will be used with the associated + # privileged user. + # + echo "$service_name $priv_user" >> {{.TMP_INSTALL_DIR}}/processes_to_restart.txt + # + # Save all non-privileged users associated with detected processes. + # When initiating the transaction, it will ONLY be used with + # the first associated non-privileged user because due the reasoning above + # that the installation logic adheres to, between FPM and Apache, + # FPM will be the one to use and `newrelic-install.sh` only + # installs the `newrelic.ini` in one web location. + # + echo "$nonpriv_user" >> {{.TMP_INSTALL_DIR}}/nonpriv_users.txt + fi + done + + install_tarball: + cmds: + - | + echo -e "{{.ARROW}}Installing New Relic PHP Agent{{.GRAY}}" + echo -e "{{.WHITE}}Downloading newest PHP Agent Release{{.GRAY}}" + cd {{.TMP_INSTALL_DIR}} + # Remove old log tarballs to ensure we don't accidentally use it later + rm -f /tmp/nrinstall* 2>/dev/null + + # Versions of the form MAJ.MIN.PATCH.BUILD + VERSION_REGEX='[1-9][0-9]\?\(\.[0-9]\+\)\{3\}' + # PHP Agent current release tarball listing. '/' is crucial + # for treating directory as an HTML directory listing + RELEASE_URL='https://download.newrelic.com/php_agent/release/' + AGENT_VERSION="$(curl -s "$RELEASE_URL" | grep --only-match "$VERSION_REGEX" | head -n1)" + echo -e "{{.RED}}AGENT VERSION: $AGENT_VERSION{{.NOCOLOR}}" + if [ -z "${AGENT_VERSION}" ]; then + echo -e "{{.RED}}Unable to determine current PHP Agent version from New Relic's downloads site.{{.NOCOLOR}}" + # Exit with Agent install failure code exit(16) + exit 16 + else + echo -e "{{.WHITE}}Found agent version: {{.CYAN}}$AGENT_VERSION{{.GRAY}}" + fi + + AGENT="newrelic-php5-$AGENT_VERSION-linux" + AGENT_TARBALL="$AGENT.tar.gz" + # Lack of '/' is important. See above comment where $RELEASE_URL is + # defined. An extraneous '/' here treats the URL as an HTML doc. + curl -s "$RELEASE_URL$AGENT_TARBALL" -o "$AGENT_TARBALL" + gzip -dc "$AGENT_TARBALL" | tar xf - + pushd "$AGENT" > /dev/null + echo -e "{{.WHITE}}Running PHP Agent installer{{.GRAY}}" + NR_INSTALL_USE_CP_NOT_LN=1 NR_INSTALL_SILENT=true NR_INSTALL_KEY="{{.NEW_RELIC_LICENSE_KEY}}" ./newrelic-install install + popd > /dev/null + + configure: + cmds: + - | + echo -e "{{.ARROW}}Configuring New Relic PHP Agent{{.GRAY}}" + cd {{.TMP_INSTALL_DIR}} + NR_INSTALL_LOG={{.TMP_INSTALL_DIR}}/nrinstall.log + # Get path of most recent agent install log + tar_file=$(ls -t /tmp/nrinstall*.tar 2>/dev/null | head -1) + # + # Expand the log files from the most recent installation attempt, + # and get the name of the most recent log file. + # + if [ -f "${tar_file}" ]; then + tar xvf $tar_file + log_file=$(ls -t nrinstall*.log | head -1) + if [ -f $log_file ]; then + ln -s $log_file $NR_INSTALL_LOG + else + # + # We really shouldn't get here if we found the tar file. + # Exit with Agent install failure code exit(16) + echo -e "{{.RED}}Unable to find agent install log.{{.NOCOLOR}}" + exit 16 + fi + else + # + # If tar file doesn't exist, something went wrong with installation. + # Exit with Agent install failure code exit(16) + echo -e "{{.RED}}Unable to find agent install log tarfile.{{.NOCOLOR}}" + exit 16 + fi + + WEB_INI_DIR=($(sed -n 's/.*final pi_inidir_dso=\(.*\/\)/\1/p' $NR_INSTALL_LOG)) + CLI_INI_DIR=($(sed -n 's/.*final pi_inidir_cli=\(.*\/\)/\1/p' $NR_INSTALL_LOG)) + + echo + + # + # Loop through all the directories where the installation placed `newrelic.ini` + # files. + # + read -r APPLICATION_NAME < "{{.TMP_INSTALL_DIR}}/{{.USER_INPUT}}" + for ini in $WEB_INI_DIR $CLI_INI_DIR; do + ini_full_name="${ini}/newrelic.ini" + if [ ! -f "$ini_full_name" ]; then + continue + fi + + # Default application name, if APPLICATION_NAME have not been set so far + if [ -z "$APPLICATION_NAME" ]; then + APPLICATION_NAME="PHP Application" + else + # '\' replacement needs to be first so as to not replace the escaping '\'s added later + APPLICATION_NAME=${APPLICATION_NAME//\\/\\} # Replace every occurrence of '\' with "\\" + APPLICATION_NAME=${APPLICATION_NAME//\//\/} # Replace every occurrence of '/' with "\/" + APPLICATION_NAME=${APPLICATION_NAME//\&/\&} # Replace every occurrence of '&' with "\&" + fi + + + sed -i "s/newrelic\.appname = \"[^\"]*\"/newrelic.appname = \"${APPLICATION_NAME}\"/" $ini_full_name + if [ "{{.NEW_RELIC_REGION}}" = "STAGING" ]; then + sed -i 's/;newrelic.daemon.collector_host = ""/newrelic.daemon.collector_host = "staging-collector.newrelic.com"/' $ini_full_name + fi + HTTPS_PROXY="{{.HTTPS_PROXY}}" + if [ ! -z "$HTTPS_PROXY" ]; then + sed -i 's,;newrelic.daemon.proxy = "",newrelic.daemon.proxy = \"'"$HTTPS_PROXY"'\",g' $ini_full_name + fi + + TAGS='{{.NEW_RELIC_CLI_TAGS}}' + if [ -n "$TAGS" ]; then + sed -i "s/;newrelic.labels = \"\"/newrelic.labels = \"${TAGS}\"/" $ini_full_name + fi + done; + + restart_services: + cmds: + - | + echo -e "{{.ARROW}}Restarting the services, as needed{{.GRAY}}" + echo -e "{{.WHITE}}This step causes the changes to newrelic.ini to be picked up." + # + # Case: php-fpm/apache or php-fpm/nginx, only php-fpm needs to be restarted. + # Case: apache only or nginx only, they respective service needs to be restarted. + # This means only restart the first process in processes_to_restart.txt. + # + read -r APPLICATION_RESTART < <(tail -n 1 "{{.TMP_INSTALL_DIR}}/{{.USER_INPUT}}") + if [ "$APPLICATION_RESTART" = "y" ] && [ -f "{{.TMP_INSTALL_DIR}}/processes_to_restart.txt" ]; then + cd {{.TMP_INSTALL_DIR}} + read -r process user_name < "{{.TMP_INSTALL_DIR}}/processes_to_restart.txt" + echo -e "{{.YELLOW}}Restarting $process as privileged user $user_name{{.GRAY}}" + sudo -u $user_name service $process restart + echo -e "{{.WHITE}}Sleeping for 10 seconds to give process time to recover.{{.GRAY}}" + sleep 10 + else + if [ -f "{{.TMP_INSTALL_DIR}}/processes_to_restart.txt" ]; then + cd {{.TMP_INSTALL_DIR}} + read -r process user_name < "{{.TMP_INSTALL_DIR}}/processes_to_restart.txt" + echo -e "{{.RED}}Please restart $process as privileged user $user_name for instrumentation to be enabled.{{.NOCOLOR}}" + else + echo -e "{{.RED}}You will need to restart your PHP web server in order for web instrumentation to be enabled.{{.NOCOLOR}}" + fi + if [[ "{{.NEW_RELIC_ASSUME_YES}}" != "true" ]]; then + while :; do + echo -n -e "{{.YELLOW}}Press Y if you successfully restarted your web server. Press N if you are unable to restart your web server at this time, and you wish to exit the installation. (default: Y)? {{.NOCOLOR}}" + read answer + echo "" + NEW_RELIC_RESTARTED=$(echo "${answer^^}" | cut -c1-1) + if [[ -z "$NEW_RELIC_RESTARTED" ]]; then + NEW_RELIC_RESTARTED="Y" + fi + if [[ "$NEW_RELIC_RESTARTED" == "N" ]] || [[ "$NEW_RELIC_RESTARTED" == "n" ]]; then + echo -e "{{.WHITE}}Exiting the installation{{.NOCOLOR}}" + #exit 130: Installation was cancelled either using Ctrl+C or by selecting not to continue the installation + exit 130 + fi + if [[ "$NEW_RELIC_RESTARTED" == "Y" ]] || [[ "$NEW_RELIC_RESTARTED" == "y" ]]; then + break + fi + echo -e "{{.WHITE}}Please type Y or N only.{{.NOCOLOR}}" + done + fi + fi + + send_transaction: + cmds: + - | + echo -e "{{.ARROW}}Send queryable transactions{{.GRAY}}" + cd {{.TMP_INSTALL_DIR}} + NR_INSTALL_LOG={{.TMP_INSTALL_DIR}}/nrinstall.log + WEB_PHP_INI_DIR=($(sed -n 's/.*final pi_inidir_dso=\(.*\)\/php.d/\1/p' $NR_INSTALL_LOG)) + WEB_NR_INI_DIR=($(sed -n 's/.*final pi_inidir_dso=\(.*\/\)/\1/p' $NR_INSTALL_LOG)) + if [ -z "${WEB_PHP_INI_DIR}" ]; then + WEB_PHP_INI_DIR=$WEB_NR_INI_DIR + fi + # Fallback to cli ini dir if there is no web specific ini dir + if [ -z "${WEB_PHP_INI_DIR}" ]; then + WEB_PHP_INI_DIR=($(sed -n 's/.*final pi_inidir_cli=\(.*\)\/php.d/\1/p' $NR_INSTALL_LOG)) + WEB_NR_INI_DIR=($(sed -n 's/.*final pi_inidir_cli=\(.*\/\)/\1/p' $NR_INSTALL_LOG)) + if [ -z "${WEB_PHP_INI_DIR}" ]; then + WEB_PHP_INI_DIR=$WEB_NR_INI_DIR + fi + fi + + # + # Get the non-privileged user that was previously stored. + # + if [ -f {{.TMP_INSTALL_DIR}}/nonpriv_users.txt ]; then + nonpriv_user=$(head -1 {{.TMP_INSTALL_DIR}}/nonpriv_users.txt) + else + nonpriv_user=$SUDO_USER + fi + + # + # The first transaction initializes the app. + # The second transaction is for luck. + # The third time’s the charm. + # + if [ -f "{{.TMP_INSTALL_DIR}}/processes_to_restart.txt" ]; then + # + # Web server was detected. Send transaction associated with the webserver php.ini + # and the newrelic.ini file associated with that web server. + # + for i in {1..10}; do + sleep 1 + # + # Loop through all the detected PHP directories where newrelic.ini files + # were placed. + # + for ((i=0; i<${#WEB_PHP_INI_DIR[@]}; i++)); do + # + # Get the PHP binary associated with a specific PHP/newrelic ini file. + # + # + # When there are multiple PHPs detected, the log file creates blocks + # of information with a specific format. + # + web_ini=$(echo "${WEB_NR_INI_DIR[$i]}" | sed 's/\//\\\//g') + WEB_PHP_BIN=$(sed '1,/final pi_inidir_.*='"${web_ini}"'/d;/php -i output follows/,$d' $NR_INSTALL_LOG | sed -n 's/.*pi_bin=\(.*\/\)/\1/p') + if [ -z "${WEB_PHP_BIN}" ]; then + # + # When there is only one PHP detected, the info format is different, + # we could detect from the log file, but since there is only one, + # just use `php`. + # + WEB_PHP_BIN="php" + fi + sudo -u $nonpriv_user $WEB_PHP_BIN -c "${WEB_PHP_INI_DIR[$i]}/php.ini" -c "${WEB_NR_INI_DIR[$i]}/newrelic.ini" -n --ini &>/dev/null + done + done + else + # + # No web server detected, pure PHP CLI. + # + echo -e "{{.WHITE}}Running PHP-CLI with user ${SUDO_USER}{{.GRAY}}" + for i in {1..10}; do + sleep 1 + sudo -u $SUDO_USER php --ini + done + fi + echo -e "{{.WHITE}}Transactions sent{{.GRAY}}" + + cleanup_temp_files: + ignore_error: true + cmds: + - | + echo -e "{{.ARROW}}Cleaning up{{.GRAY}}" + rm -rf /tmp/nrinstall* 2>/dev/null + rm -rf /tmp/newrelic-php-agent.* 2>/dev/null + echo -e "{{.WHITE}}Removed temporary directory used for installation{{.NOCOLOR}}"