allowed to install netdata as any user (without root) #149; now the installer preserves user configuration in apps_groups.conf and charts.d.conf #209; web files owner and group is preserved
Costa Tsaousis (ktsaou) committed
Apr 17, 2016 at 17:32 UTC
e35c0f5b275b55e04bcc0bdb7793332005912911
1 file changed
+170
-56
netdata-installer.sh
+170
-56
@@ -1,5 +1,8 @@
1
#!/bin/bash
2
3
+# reload the user profile
4
+[ -f /etc/profile ] && . /etc/profile
5
+
6
LC_ALL=C
7
8
# you can set CFLAGS before running installer
@@ -118,14 +121,47 @@ Press Control-C and run the same command with --help for help.
121
122
BANNER
123
121
-if [ ! "${UID}" = 0 -o "$1" = "-h" -o "$1" = "--help" ]
124
+if [ "${UID}" -ne 0 ]
125
then
123
- echo >&2
124
- echo >&2 "You have to run netdata as root."
125
- echo >&2 "The netdata daemon will drop priviliges"
126
- echo >&2 "but you have to start it as root."
127
- echo >&2
128
- exit 1
126
+ if [ -z "${NETDATA_PREFIX}" ]
127
+ then
128
+ cat <<NONROOTNOPREFIX
129
+
130
+Sorry! This will wrong!
131
+
132
+You are attempting to install netdata as non-root, but you plan to install it
133
+in system paths.
134
+
135
+Please set an installation prefix, like this:
136
+
137
+ $0 ${@} --install /tmp
138
+
139
+or, run the installer as root:
140
+
141
+ sudo $0 ${@}
142
+
143
+We suggest to install it as root, or certain data collectors will not be able
144
+to work. Netdata drops root privileges when running. So, if you plan to keep
145
+it, install it as root to get the full functionality.
146
+
147
+NONROOTNOPREFIX
148
+ exit 1
149
+
150
+ else
151
+ cat <<NONROOT
152
+
153
+IMPORTANT:
154
+You are about to install netdata as a non-root user.
155
+Netdata will work, but a few data collection modules that
156
+require root access will fail.
157
+
158
+If you installing permanently on your system, run the
159
+installer like this:
160
+
161
+ sudo $0 ${@}
162
+
163
+NONROOT
164
+ fi
165
fi
166
167
have_autotools=
@@ -163,6 +199,9 @@ then
199
else
200
cat <<-"EOF"
201
202
+ -------------------------------------------------------------------------------
203
+ autotools 2.60 or later is required
204
+
205
Sorry, you do not seem to have autotools 2.60 or later, which is
206
required to build from the git sources of netdata.
207
@@ -190,9 +229,6 @@ if [ ${DONOTWAIT} -eq 0 ]
229
fi
230
fi
231
193
-# reload the profile
194
-[ -f /etc/profile ] && . /etc/profile
195
-
232
build_error() {
233
cat <<EOF
234
@@ -213,6 +249,7 @@ You many need to check these:
249
3. If your system cannot find ZLIB, although it is installed
250
run me with the option: --zlib-is-really-here
251
252
+
253
If you still cannot get it to build, ask for help at github:
254
255
https://github.com/firehol/netdata/issues
@@ -224,6 +261,7 @@ EOF
261
}
262
263
run() {
264
+ printf >&2 "\n"
265
printf >&2 ":-----------------------------------------------------------------------------\n"
266
printf >&2 "Running command:\n"
267
printf >&2 "\n"
@@ -246,11 +284,9 @@ trap build_error EXIT
284
285
if [ "$have_autotools" ]
286
then
249
- echo >&2 "Running ./autogen.sh ..."
287
run ./autogen.sh || exit 1
288
fi
289
253
-echo >&2 "Running ./configure ..."
290
run ./configure \
291
--prefix="${NETDATA_PREFIX}/usr" \
292
--sysconfdir="${NETDATA_PREFIX}/etc" \
@@ -270,15 +306,35 @@ fi
306
echo >&2 "Compiling netdata ..."
307
run make || exit 1
308
309
+# backup user configurations
310
+for x in apps_groups.conf charts.d.conf
311
+do
312
+ if [ -f "${NETDATA_PREFIX}/etc/netdata/${x}" ]
313
+ then
314
+ cp -p "${NETDATA_PREFIX}/etc/netdata/${x}" "${NETDATA_PREFIX}/etc/netdata/${x}.installer_backup"
315
+ fi
316
+done
317
+
318
echo >&2 "Installing netdata ..."
319
run make install || exit 1
320
276
-echo >&2 "Adding netdata user group ..."
277
-getent group netdata > /dev/null || run groupadd -r netdata
321
+# restore user configurations
322
+for x in apps_groups.conf charts.d.conf
323
+do
324
+ if [ -f "${NETDATA_PREFIX}/etc/netdata/${x}.installer_backup" ]
325
+ then
326
+ cp -p "${NETDATA_PREFIX}/etc/netdata/${x}.installer_backup" "${NETDATA_PREFIX}/etc/netdata/${x}"
327
+ fi
328
+done
329
279
-echo >&2 "Adding netdata user account ..."
280
-getent passwd netdata > /dev/null || run useradd -r -g netdata -c netdata -s /sbin/nologin -d / netdata
330
+if [ ${UID} -eq 0 ]
331
+ then
332
+ echo >&2 "Adding netdata user group ..."
333
+ getent group netdata > /dev/null || run groupadd -r netdata
334
335
+ echo >&2 "Adding netdata user account ..."
336
+ getent passwd netdata > /dev/null || run useradd -r -g netdata -c netdata -s /sbin/nologin -d / netdata
337
+fi
338
339
340
# -----------------------------------------------------------------------------
@@ -305,6 +361,9 @@ defuser="netdata"
361
[ ! "${UID}" = "0" ] && defuser="${USER}"
362
NETDATA_USER="$( config_option "run as user" "${defuser}" )"
363
364
+NETDATA_WEB_USER="$( config_option "web files owner" "${defuser}" )"
365
+NETDATA_WEB_GROUP="$( config_option "web files group" "${NETDATA_WEB_USER}" )"
366
+
367
# debug flags
368
defdebug=0
369
NETDATA_DEBUG="$( config_option "debug flags" ${defdebug} )"
@@ -330,7 +389,7 @@ if [ ! -d "${NETDATA_RUN_DIR}" ]
389
mkdir -p "${NETDATA_RUN_DIR}" || exit 1
390
fi
391
333
-echo >&2 "Fixing directory permissions for user ${NETDATA_USER}..."
392
+echo >&2 "Fixing directories (user: ${NETDATA_USER})..."
393
for x in "${NETDATA_WEB_DIR}" "${NETDATA_CONF_DIR}" "${NETDATA_CACHE_DIR}" "${NETDATA_LOG_DIR}"
394
do
395
if [ ! -d "${x}" ]
@@ -338,14 +397,26 @@ do
397
echo >&2 "Creating directory '${x}'"
398
run mkdir -p "${x}" || exit 1
399
fi
341
- run chown -R "${NETDATA_USER}:${NETDATA_USER}" "${x}" || echo >&2 "WARNING: Cannot change the ownership of the files in directory ${x} to ${NETDATA_USER}..."
400
+
401
+ if [ ${UID} -eq 0 ]
402
+ then
403
+ if [ "${x}" = "${NETDATA_WEB_DIR}" ]
404
+ then
405
+ run chown -R "${NETDATA_WEB_USER}:${NETDATA_WEB_GROUP}" "${x}" || echo >&2 "WARNING: Cannot change the ownership of the files in directory ${x} to ${NETDATA_WEB_USER}:${NETDATA_WEB_GROUP}..."
406
+ else
407
+ run chown -R "${NETDATA_USER}:${NETDATA_USER}" "${x}" || echo >&2 "WARNING: Cannot change the ownership of the files in directory ${x} to ${NETDATA_USER}..."
408
+ fi
409
+ fi
410
+
411
run chmod 0775 "${x}" || echo >&2 "WARNING: Cannot change the permissions of the directory ${x} to 0755..."
412
done
413
345
-# fix apps.plugin to be setuid to root
346
-run chown root "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin"
347
-run chmod 4755 "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin"
348
-
414
+if [ ${UID} -eq 0 ]
415
+ then
416
+ # fix apps.plugin to be setuid to root
417
+ run chown root "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin"
418
+ run chmod 4755 "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin"
419
+fi
420
421
# -----------------------------------------------------------------------------
422
# check if we can re-start netdata
@@ -356,7 +427,11 @@ if [ ${DONOTSTART} -eq 1 ]
427
then
428
echo >&2 "Generating empty config file in: ${NETDATA_PREFIX}/etc/netdata/netdata.conf"
429
echo "# Get config from http://127.0.0.1:${NETDATA_PORT}/netdata.conf" >"${NETDATA_PREFIX}/etc/netdata/netdata.conf"
359
- chown "${NETDATA_USER}" "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
430
+
431
+ if [ "${UID}" -eq 0 ]
432
+ then
433
+ chown "${NETDATA_USER}" "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
434
+ fi
435
chmod 0664 "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
436
fi
437
echo >&2 "OK. It is now installed and ready."
@@ -454,7 +529,10 @@ if [ ! -s "${NETDATA_PREFIX}/etc/netdata/netdata.conf" ]
529
mv "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
530
echo >&2 "New configuration saved for you to edit at ${NETDATA_PREFIX}/etc/netdata/netdata.conf"
531
457
- chown "${NETDATA_USER}" "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
532
+ if [ "${UID}" -eq 0 ]
533
+ then
534
+ chown "${NETDATA_USER}" "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
535
+ fi
536
chmod 0664 "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
537
else
538
echo >&2 "Cannnot download configuration from netdata daemon using url 'http://localhost:${NETDATA_PORT}/netdata.conf'"
@@ -462,36 +540,14 @@ if [ ! -s "${NETDATA_PREFIX}/etc/netdata/netdata.conf" ]
540
fi
541
fi
542
465
-# -----------------------------------------------------------------------------
466
-
467
-cat <<END
468
-
469
-
470
--------------------------------------------------------------------------------
471
-
472
-ok. NetData is installed and is running.
473
-
474
-Hit http://localhost:${NETDATA_PORT}/ from your browser.
475
-
476
-To stop netdata, just kill it, with:
477
-
478
- killall netdata
479
-
480
-To start it, just run it:
481
-
482
- ${NETDATA_PREFIX}/usr/sbin/netdata
483
-
484
-Enjoy!
485
-
486
-END
487
-
543
# -----------------------------------------------------------------------------
544
# Check for KSM
545
546
ksm_is_available_but_disabled() {
547
cat <<KSM1
548
494
-INFORMATION:
549
+-------------------------------------------------------------------------------
550
+Memory de-duplication instructions
551
552
I see you have kernel memory de-duper (called Kernel Same-page Merging,
553
or KSM) available, but it is not currently enabled.
@@ -501,7 +557,7 @@ To enable it run:
557
echo 1 >/sys/kernel/mm/ksm/run
558
echo 1000 >/sys/kernel/mm/ksm/sleep_millisecs
559
504
-If you enable it, you will save 20-60% of netdata memory.
560
+If you enable it, you will save 40-60% of netdata memory.
561
562
KSM1
563
}
@@ -509,14 +565,15 @@ KSM1
565
ksm_is_not_available() {
566
cat <<KSM2
567
512
-INFORMATION:
568
+-------------------------------------------------------------------------------
569
+Memory de-duplication not present in your kernel
570
514
-I see you do not have kernel memory de-duper (called Kernel Same-page
571
+It seems you do not have kernel memory de-duper (called Kernel Same-page
572
Merging, or KSM) available.
573
574
To enable it, you need a kernel built with CONFIG_KSM=y
575
519
-If you can have it, you will save 20-60% of netdata memory.
576
+If you can have it, you will save 40-60% of netdata memory.
577
578
KSM2
579
}
@@ -538,20 +595,46 @@ if [ ! -s web/version.txt ]
595
then
596
cat <<VERMSG
597
541
-VERSION UPDATE CHECK:
598
+-------------------------------------------------------------------------------
599
+Version update check warning
600
543
-The way you downloaded netdata, we cannot find its version.
544
-This means the Update check on the dashboard, will not work.
601
+The way you downloaded netdata, we cannot find its version. This means the
602
+Update check on the dashboard, will not work.
603
604
If you want to have version update check, please re-install it
605
following the procedure in:
606
607
https://github.com/firehol/netdata/wiki/Installation
608
551
-
609
VERMSG
610
fi
611
612
+# -----------------------------------------------------------------------------
613
+# apps.plugin warning
614
+
615
+if [ "${UID}" -ne 0 ]
616
+ then
617
+cat <<SETUID_WARNING
618
+
619
+-------------------------------------------------------------------------------
620
+apps.plugin needs privileges
621
+
622
+Since you have installed netdata as a normal user, to have apps.plugin collect
623
+all the needed data, you have to give it the access rights it needs, by running
624
+these commands:
625
+
626
+ sudo chown root "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin"
627
+ sudo chmod 4755 "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin"
628
+
629
+The commands allow apps.plugin to run as root.
630
+
631
+apps.plugin is performing a hard-coded function of data collection for all
632
+running processes. It cannot be instructed from the netdata daemon to perform
633
+any task, so it is pretty safe to do this.
634
+
635
+SETUID_WARNING
636
+fi
637
+
638
# -----------------------------------------------------------------------------
639
# Keep un-install info
640
@@ -623,4 +706,35 @@ fi
706
UNINSTALL
707
chmod 750 netdata-uninstaller.sh
708
709
+# -----------------------------------------------------------------------------
710
+
711
+cat <<END
712
+
713
+
714
+-------------------------------------------------------------------------------
715
+
716
+OK. NetData is installed and it is running.
717
+
718
+-------------------------------------------------------------------------------
719
+
720
+
721
+Hit http://localhost:${NETDATA_PORT}/ from your browser.
722
+
723
+To stop netdata, just kill it, with:
724
+
725
+ killall netdata
726
+
727
+To start it, just run it:
728
+
729
+ ${NETDATA_PREFIX}/usr/sbin/netdata
730
+
731
+
732
+Enjoy!
733
+
734
+ Give netdata a Github Star, at:
735
+
736
+ https://github.com/firehol/netdata/wiki
737
+
738
+
739
+END
740
echo >&2 "Uninstall script generated: ./netdata-uninstaller.sh"