-
Notifications
You must be signed in to change notification settings - Fork 54
/
debian.yml
670 lines (607 loc) · 29.3 KB
/
debian.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
# 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 Debian systems with php-fpm, nginx, or apache.
repository: https://github.com/newrelic/newrelic-php-agent
installTargets:
- type: application
os: linux
platform: "debian"
kernelArch: x86_64
- type: application
os: linux
platform: "ubuntu"
kernelArch: x86_64
keywords:
- Apm
- php
- fpm
processMatch:
# - 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 apt-get: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
# need directory for extra repositories to exist
if [ ! -d "/etc/apt/sources.list.d/" ]; then
echo "This installation recipe for the New Relic PHP Agent on Linux requires the directory /etc/apt/sources.list.d to exist." >&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;240m'
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: update_apt
- task: add_gnupg2_if_required
- task: add_nr_source
- task: add_gpg_key
- task: update_apt_nr_source
- task: install_php
- 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{{.GRAY}}"
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 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
update_apt:
cmds:
- |
# Get latest definitions and skip any failure because of deprecation
apt-get -o Acquire::Check-Valid-Until=false update -yq
silent: true
# apt will return an error if fails to update any of its sources. Ignore these errors and let the "install_infra" task fail.
ignore_error: true
add_gnupg2_if_required:
cmds:
- |
# Check if gnupg is already installed, and if not, install it.
if [ $(({{.DEBIAN_VERSION}})) -ge 10 ]; then
if [ {{.HAS_GPG}} -eq 0 ] ; then
sudo apt-get install gnupg2 -y
fi
fi
vars:
HAS_GPG:
sh: command -v gpg | wc -l
DEBIAN_VERSION:
sh: awk -F= '/VERSION_ID/ {print $2}' /etc/os-release
add_nr_source:
cmds:
- |
echo 'deb http://apt.newrelic.com/debian/ newrelic non-free' | sudo tee /etc/apt/sources.list.d/newrelic.list
silent: true
add_gpg_key:
cmds:
- |
curl -s https://download.newrelic.com/548C16BF.gpg | sudo apt-key add -
silent: true
update_apt_nr_source:
cmds:
- |
# Get latest definitions and skip any failure because of deprecation
# apt will return an error if fails to update any of its sources. Ignore these errors and let the "install_infra" task fail.
apt-get -o Acquire::Check-Valid-Until=false update -yq
ignore_error: true
install_php:
cmds:
- |
echo -e "{{.ARROW}}Installing New Relic PHP Agent package{{.GRAY}}"
sudo DEBIAN_FRONTEND=noninteractive apt-get install newrelic-php5 -y -qq
silent: true
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 "{{.GRAY}}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}}
rm -f {{.TMP_INSTALL_DIR}}/web_info.txt 2>/dev/null
rm -f {{.TMP_INSTALL_DIR}}/cli_info.txt 2>/dev/null
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}}"
INI_DIRS=($WEB_INI_DIR $CLI_INI_DIR)
for dirs in $INI_DIRS; do
for ini in $dirs; do
#
# Get the PHP INI Directory associated with this specific NR INI file
#
php_ini_dir=$(echo $ini | sed -n 's/\(.*\)\/conf.d/\1/p')
if [ -z "${php_ini_dir}" ]; then
php_ini_dir=$ini
fi
#
# Get the PHP Binary directory associated with this particular NR INI file.
sed_slash_ini=$(echo "${ini}" | sed 's/\//\\\//g')
php_bin=$(sed '1,/final pi_inidir_.*='"${sed_slash_ini}"'/d;/php -i output follows/,$d' $NR_INSTALL_LOG | sed -n 's/.*pi_bin=\(.*\/\)/\1/p')
if [ -z "${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`.
#
php_bin="php"
fi
#
# Modify the fresh copy of a brand new New Relic INI file.
#
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
sed -i 's/;newrelic.loglevel = "info"/newrelic.loglevel = "verbosedebug"/' $ini_full_name
sed -i 's/;newrelic.daemon.loglevel = "info"/newrelic.daemon.loglevel = "debug"/' $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
#
# Determine the target directory that the symlink is pointing to.
#
target_ini_dir="/etc/php5/conf.d/"
if [ -x /usr/sbin/phpquery ]; then
target_ini_dir="${ini}/../../mods-available/"
elif [ -x /usr/sbin/php5endmod ]; then
target_ini_dir="/etc/php5/mods-available/"
fi
if [ ! -d $target_ini_dir ]; then
echo -e "{{.RED}}Warning: Unable to move ${ini_full_name} to ${target_ini_dir}{{.GRAY}}"
continue
fi
#
# Move newrelic.ini created by tarball to the name that the installer will use.
# This ensures we always get the most up to date newrelic.ini file on the
# system.
#
mv ${ini_full_name} ${target_ini_dir}
#
# We rely on the package installer to enable the newrelic module.
#
#
# For each php directory the installation has detected, scrape three pieces
# of information from the logfile: The php binary location, the location
# of the php.ini file associated with that binary, and the location of the
# newrelic.ini file associated with that binary. If the logfile indicated
# it was a specific web directory, the information is saved in web_info.txt;
# otherwise, the information is saved in cli_info.txt.
#
if [ -z "${CLI_DIRS}" ]; then
echo "$php_bin $php_ini_dir $target_ini_dir" >> {{.TMP_INSTALL_DIR}}/web_info.txt
else
echo "$php_bin $php_ini_dir $target_ini_dir" >> {{.TMP_INSTALL_DIR}}/cli_info.txt
fi
done;
CLI_DIRS=true
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.{{.NOCOLORi}}"
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}}
#
# 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.
#
if [ -f "{{.TMP_INSTALL_DIR}}/web_info.txt" ]; then
info_text="{{.TMP_INSTALL_DIR}}/web_info.txt"
else
#
# Fallback to CLI if no web info was detected.
#
info_text="{{.TMP_INSTALL_DIR}}/cli_info.txt"
fi
while read -r bin_loc php_ini_dir nr_ini_dir; do
sudo -u $nonpriv_user $bin_loc -c "${php_ini_dir}/php.ini" -c "${nr_ini_dir}/newrelic.ini" -n --ini &>/dev/null
done < "${info_text}"
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}}"