]> arthur.barton.de Git - netdata.git/blob - netdata-installer.sh
Merge pull request #484 from ktsaou/master
[netdata.git] / netdata-installer.sh
1 #!/bin/bash
2
3 # reload the user profile
4 [ -f /etc/profile ] && . /etc/profile
5
6 # fix PKG_CHECK_MODULES error
7 if [ -d /usr/share/aclocal ]
8 then
9         ACLOCAL_PATH=${ACLOCAL_PATH-/usr/share/aclocal}
10         export ACLOCAL_PATH
11 fi
12
13 LC_ALL=C
14 umask 022
15
16 # you can set CFLAGS before running installer
17 CFLAGS="${CFLAGS--O3}"
18
19 # keep a log of this command
20 printf "\n# " >>netdata-installer.log
21 date >>netdata-installer.log
22 printf "CFLAGS=\"%s\" " "${CFLAGS}" >>netdata-installer.log
23 printf "%q " "$0" "${@}" >>netdata-installer.log
24 printf "\n" >>netdata-installer.log
25
26 ME="$0"
27 DONOTSTART=0
28 DONOTWAIT=0
29 NETDATA_PREFIX=
30 LIBS_ARE_HERE=0
31
32 usage() {
33         cat <<-USAGE
34
35         ${ME} <installer options>
36
37         Valid <installer options> are:
38
39            --install /PATH/TO/INSTALL
40
41                         If your give: --install /opt
42                         netdata will be installed in /opt/netdata
43
44            --dont-start-it
45
46                         Do not (re)start netdata.
47                         Just install it.
48
49            --dont-wait
50
51                         Do not wait for the user to press ENTER.
52                         Start immediately building it.
53
54            --zlib-is-really-here
55            --libs-are-really-here
56
57                         If you get errors about missing zlib,
58                         or libuuid but you know it is available,
59                         you have a broken pkg-config.
60                         Use this option to allow it continue
61                         without checking pkg-config.
62
63         Netdata will by default be compiled with gcc optimization -O3
64         If you need to pass different CFLAGS, use something like this:
65
66           CFLAGS="<gcc options>" ${ME} <installer options>
67
68         For the installer to complete successfully, you will need
69         these packages installed:
70
71            gcc make autoconf automake pkg-config zlib1g-dev (or zlib-devel)
72            uuid-dev (or libuuid-devel)
73
74         For the plugins, you will at least need:
75
76            curl nodejs
77
78 USAGE
79 }
80
81 while [ ! -z "${1}" ]
82 do
83         if [ "$1" = "--install" ]
84                 then
85                 NETDATA_PREFIX="${2}/netdata"
86                 shift 2
87         elif [ "$1" = "--zlib-is-really-here" -o "$1" = "--libs-are-really-here" ]
88                 then
89                 LIBS_ARE_HERE=1
90                 shift 1
91         elif [ "$1" = "--dont-start-it" ]
92                 then
93                 DONOTSTART=1
94                 shift 1
95         elif [ "$1" = "--dont-wait" ]
96                 then
97                 DONOTWAIT=1
98                 shift 1
99         elif [ "$1" = "--help" -o "$1" = "-h" ]
100                 then
101                 usage
102                 exit 1
103         else
104                 echo >&2
105                 echo >&2 "ERROR:"
106                 echo >&2 "I cannot understand option '$1'."
107                 usage
108                 exit 1
109         fi
110 done
111
112 cat <<-BANNER
113
114         Welcome to netdata!
115         Nice to see you are giving it a try!
116
117         You are about to build and install netdata to your system.
118
119         It will be installed at these locations:
120
121           - the daemon    at ${NETDATA_PREFIX}/usr/sbin/netdata
122           - config files  at ${NETDATA_PREFIX}/etc/netdata
123           - web files     at ${NETDATA_PREFIX}/usr/share/netdata
124           - plugins       at ${NETDATA_PREFIX}/usr/libexec/netdata
125           - cache files   at ${NETDATA_PREFIX}/var/cache/netdata
126           - db files      at ${NETDATA_PREFIX}/var/lib/netdata
127           - log files     at ${NETDATA_PREFIX}/var/log/netdata
128           - pid file      at ${NETDATA_PREFIX}/var/run
129
130         This installer allows you to change the installation path.
131         Press Control-C and run the same command with --help for help.
132
133 BANNER
134
135 if [ "${UID}" -ne 0 ]
136         then
137         if [ -z "${NETDATA_PREFIX}" ]
138                 then
139                 cat <<-NONROOTNOPREFIX
140
141                 Sorry! This will fail!
142
143                 You are attempting to install netdata as non-root, but you plan to install it
144                 in system paths.
145
146                 Please set an installation prefix, like this:
147
148                         $0 ${@} --install /tmp
149
150                 or, run the installer as root:
151
152                         sudo $0 ${@}
153
154                 We suggest to install it as root, or certain data collectors will not be able
155                 to work. Netdata drops root privileges when running. So, if you plan to keep
156                 it, install it as root to get the full functionality.
157
158 NONROOTNOPREFIX
159                 exit 1
160
161         else
162                 cat <<-NONROOT
163
164                 IMPORTANT:
165                 You are about to install netdata as a non-root user.
166                 Netdata will work, but a few data collection modules that
167                 require root access will fail.
168
169                 If you installing permanently on your system, run the
170                 installer like this:
171
172                         sudo $0 ${@}
173
174 NONROOT
175         fi
176 fi
177
178 have_autotools=
179 if [ "$(type autoreconf 2> /dev/null)" ]
180 then
181         autoconf_maj_min() {
182                 local maj min IFS=.-
183
184                 maj=$1
185                 min=$2
186
187                 set -- $(autoreconf -V | sed -ne '1s/.* \([^ ]*\)$/\1/p')
188                 eval $maj=\$1 $min=\$2
189         }
190         autoconf_maj_min AMAJ AMIN
191
192         if [ "$AMAJ" -gt 2 ]
193         then
194                 have_autotools=Y
195         elif [ "$AMAJ" -eq 2 -a "$AMIN" -ge 60 ]
196         then
197                 have_autotools=Y
198         else
199                 echo "Found autotools $AMAJ.$AMIN"
200         fi
201 else
202         echo "No autotools found"
203 fi
204
205 if [ ! "$have_autotools" ]
206 then
207         if [ -f configure ]
208         then
209                 echo "Will skip autoreconf step"
210         else
211                 cat <<-"EOF"
212
213                 -------------------------------------------------------------------------------
214                 autotools 2.60 or later is required
215
216                 Sorry, you do not seem to have autotools 2.60 or later, which is
217                 required to build from the git sources of netdata.
218
219                 You can either install a suitable version of autotools and automake
220                 or download a netdata package which does not have these dependencies.
221
222                 Source packages where autotools have already been run are available
223                 here:
224                            https://firehol.org/download/netdata/
225
226                 The unsigned/master folder tracks the head of the git tree and released
227                 packages are also available.
228 EOF
229                 exit 1
230         fi
231 fi
232
233 if [ ${DONOTWAIT} -eq 0 ]
234         then
235         if [ ! -z "${NETDATA_PREFIX}" ]
236                 then
237                 read -p "Press ENTER to build and install netdata to '${NETDATA_PREFIX}' > "
238         else
239                 read -p "Press ENTER to build and install netdata to your system > "
240         fi
241 fi
242
243 build_error() {
244         cat <<-EOF
245
246         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
247
248         Sorry! NetData failed to build...
249
250         You many need to check these:
251
252         1. The package uuid-dev (or libuuid-devel) has to be installed.
253
254            If your system cannot find libuuid, although it is installed
255            run me with the option:  --libs-are-really-here
256
257         2. The package zlib1g-dev (or zlib-devel) has to be installed.
258
259            If your system cannot find zlib, although it is installed
260            run me with the option:  --libs-are-really-here
261
262         3. You need basic build tools installed, like:
263
264            gcc make autoconf automake pkg-config
265
266            Autoconf version 2.60 or higher is required.
267
268         If you still cannot get it to build, ask for help at github:
269
270            https://github.com/firehol/netdata/issues
271
272
273 EOF
274         trap - EXIT
275         exit 1
276 }
277
278 run() {
279         printf >>netdata-installer.log "# "
280         printf >>netdata-installer.log "%q " "${@}"
281         printf >>netdata-installer.log " ... "
282
283         printf >&2 "\n"
284         printf >&2 ":-----------------------------------------------------------------------------\n"
285         printf >&2 "Running command:\n"
286         printf >&2 "\n"
287         printf >&2 "%q " "${@}"
288         printf >&2 "\n"
289
290         "${@}"
291
292         local ret=$?
293         if [ ${ret} -ne 0 ]
294                 then
295                 printf >>netdata-installer.log "FAILED!\n"
296         else
297                 printf >>netdata-installer.log "OK\n"
298         fi
299
300         return ${ret}
301 }
302
303 if [ ${LIBS_ARE_HERE} -eq 1 ]
304         then
305         shift
306         echo >&2 "ok, assuming libs are really installed."
307         export ZLIB_CFLAGS=" "
308         export ZLIB_LIBS="-lz"
309         export UUID_CFLAGS=" "
310         export UUID_LIBS="-luuid"
311 fi
312
313 trap build_error EXIT
314
315 if [ "$have_autotools" ]
316 then
317         run ./autogen.sh || exit 1
318 fi
319
320 run ./configure \
321         --prefix="${NETDATA_PREFIX}/usr" \
322         --sysconfdir="${NETDATA_PREFIX}/etc" \
323         --localstatedir="${NETDATA_PREFIX}/var" \
324         --with-zlib --with-math --with-user=netdata \
325         CFLAGS="${CFLAGS}" || exit 1
326
327 # remove the build_error hook
328 trap - EXIT
329
330 if [ -f src/netdata ]
331         then
332         echo >&2 "Cleaning a possibly old compilation ..."
333         run make clean
334 fi
335
336 echo >&2 "Compiling netdata ..."
337 run make || exit 1
338
339 # backup user configurations
340 installer_backup_suffix="${PID}.${RANDOM}"
341 for x in apps_groups.conf charts.d.conf
342 do
343         if [ -f "${NETDATA_PREFIX}/etc/netdata/${x}" ]
344                 then
345                 cp -p "${NETDATA_PREFIX}/etc/netdata/${x}" "${NETDATA_PREFIX}/etc/netdata/${x}.installer_backup.${installer_backup_suffix}"
346
347         elif [ -f "${NETDATA_PREFIX}/etc/netdata/${x}.installer_backup.${installer_backup_suffix}" ]
348                 then
349                 rm -f "${NETDATA_PREFIX}/etc/netdata/${x}.installer_backup.${installer_backup_suffix}"
350         fi
351 done
352
353 echo >&2 "Installing netdata ..."
354 run make install || exit 1
355
356 # restore user configurations
357 for x in apps_groups.conf charts.d.conf
358 do
359         if [ -f "${NETDATA_PREFIX}/etc/netdata/${x}.installer_backup.${installer_backup_suffix}" ]
360                 then
361                 cp -p "${NETDATA_PREFIX}/etc/netdata/${x}.installer_backup.${installer_backup_suffix}" "${NETDATA_PREFIX}/etc/netdata/${x}"
362         fi
363 done
364
365 NETDATA_ADDED_TO_DOCKER=0
366 if [ ${UID} -eq 0 ]
367         then
368         getent group netdata > /dev/null
369         if [ $? -ne 0 ]
370                 then
371                 echo >&2 "Adding netdata user group ..."
372                 run groupadd -r netdata
373         fi
374
375         getent passwd netdata > /dev/null
376         if [ $? -ne 0 ]
377                 then
378                 echo >&2 "Adding netdata user account ..."
379                 run useradd -r -g netdata -c netdata -s /sbin/nologin -d / netdata
380         fi
381
382         getent group docker > /dev/null
383         if [ $? -eq 0 ]
384                 then
385                 # find the users in the docker group
386                 docker=$(getent group docker | cut -d ':' -f 4)
387                 if [[ ",${docker}," =~ ,netdata, ]]
388                         then
389                         # netdata is already there
390                         :
391                 else
392                         # netdata is not in docker group
393                         echo >&2 "Adding netdata user to the docker group (needed to get container names) ..."
394                         run usermod -a -G docker netdata
395                 fi
396                 # let the uninstall script know
397                 NETDATA_ADDED_TO_DOCKER=1
398         fi
399
400         if [ -d /etc/logrotate.d -a ! -f /etc/logrotate.d/netdata ]
401                 then
402                 echo >&2 "Adding netdata logrotate configuration ..."
403                 run cp system/netdata.logrotate /etc/logrotate.d/netdata
404         fi
405 fi
406
407
408 # -----------------------------------------------------------------------------
409 # load options from the configuration file
410
411 # create an empty config if it does not exist
412 [ ! -f "${NETDATA_PREFIX}/etc/netdata/netdata.conf" ] && touch "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
413
414 # function to extract values from the config file
415 config_option() {
416         local key="${1}" value="${2}" line=
417
418         if [ -s "${NETDATA_PREFIX}/etc/netdata/netdata.conf" ]
419                 then
420                 line="$( grep "^[[:space:]]*${key}[[:space:]]*=[[:space:]]*" "${NETDATA_PREFIX}/etc/netdata/netdata.conf" | head -n 1 )"
421                 [ ! -z "${line}" ] && value="$( echo "${line}" | cut -d '=' -f 2 | sed -e "s/^[[:space:]]\+//g" -e "s/[[:space:]]\+$//g" )"
422         fi
423
424         echo "${value}"
425 }
426
427 # user
428 defuser="netdata"
429 [ ! "${UID}" = "0" ] && defuser="${USER}"
430 NETDATA_USER="$( config_option "run as user" "${defuser}" )"
431
432 NETDATA_WEB_USER="$( config_option "web files owner" "${defuser}" )"
433 NETDATA_WEB_GROUP="$( config_option "web files group" "${NETDATA_WEB_USER}" )"
434
435 # debug flags
436 defdebug=0
437 NETDATA_DEBUG="$( config_option "debug flags" ${defdebug} )"
438
439 # port
440 defport=19999
441 NETDATA_PORT="$( config_option "port" ${defport} )"
442
443 # directories
444 NETDATA_LIB_DIR="$( config_option "lib directory" "${NETDATA_PREFIX}/var/lib/netdata" )"
445 NETDATA_CACHE_DIR="$( config_option "cache directory" "${NETDATA_PREFIX}/var/cache/netdata" )"
446 NETDATA_WEB_DIR="$( config_option "web files directory" "${NETDATA_PREFIX}/usr/share/netdata/web" )"
447 NETDATA_LOG_DIR="$( config_option "log directory" "${NETDATA_PREFIX}/var/log/netdata" )"
448 NETDATA_CONF_DIR="$( config_option "config directory" "${NETDATA_PREFIX}/etc/netdata" )"
449 NETDATA_BIND="$( config_option "bind socket to IP" "*" )"
450 NETDATA_RUN_DIR="${NETDATA_PREFIX}/var/run"
451
452
453 # -----------------------------------------------------------------------------
454 # prepare the directories
455
456 # this is needed if NETDATA_PREFIX is not empty
457 if [ ! -d "${NETDATA_RUN_DIR}" ]
458         then
459         mkdir -p "${NETDATA_RUN_DIR}" || exit 1
460 fi
461
462 echo >&2
463 echo >&2 "Fixing directories (user: ${NETDATA_USER})..."
464 for x in "${NETDATA_WEB_DIR}" "${NETDATA_CONF_DIR}" "${NETDATA_CACHE_DIR}" "${NETDATA_LOG_DIR}" "${NETDATA_LIB_DIR}"
465 do
466         if [ ! -d "${x}" ]
467                 then
468                 echo >&2 "Creating directory '${x}'"
469                 run mkdir -p "${x}" || exit 1
470         fi
471
472         if [ ${UID} -eq 0 ]
473                 then
474                 if [ "${x}" = "${NETDATA_WEB_DIR}" ]
475                         then
476                         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}..."
477                 else
478                         run chown -R "${NETDATA_USER}:${NETDATA_USER}" "${x}" || echo >&2 "WARNING: Cannot change the ownership of the files in directory ${x} to ${NETDATA_USER}..."
479                 fi
480         fi
481
482         run chmod 0755 "${x}" || echo >&2 "WARNING: Cannot change the permissions of the directory ${x} to 0755..."
483 done
484
485 if [ ${UID} -eq 0 ]
486         then
487         run chown root "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin"
488         run chmod 0755 "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin"
489         run setcap cap_dac_read_search,cap_sys_ptrace+ep "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin"
490         if [ $? -ne 0 ]
491                 then
492                 # fix apps.plugin to be setuid to root
493                 run chown root "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin"
494                 run chmod 4755 "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin"
495         fi
496 fi
497
498 # -----------------------------------------------------------------------------
499 # check if we can re-start netdata
500
501 if [ ${DONOTSTART} -eq 1 ]
502         then
503         if [ ! -s "${NETDATA_PREFIX}/etc/netdata/netdata.conf" ]
504                 then
505                 echo >&2 "Generating empty config file in: ${NETDATA_PREFIX}/etc/netdata/netdata.conf"
506                 echo "# Get config from http://127.0.0.1:${NETDATA_PORT}/netdata.conf" >"${NETDATA_PREFIX}/etc/netdata/netdata.conf"
507
508                 if [ "${UID}" -eq 0 ]
509                         then
510                         chown "${NETDATA_USER}" "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
511                 fi
512                 chmod 0664 "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
513         fi
514         echo >&2 "OK. It is now installed and ready."
515         exit 0
516 fi
517
518 # -----------------------------------------------------------------------------
519 # stop a running netdata
520
521 isnetdata() {
522         [ -z "$1" -o ! -f "/proc/$1/stat" ] && return 1
523         [ "$(cat "/proc/$1/stat" | cut -d '(' -f 2 | cut -d ')' -f 1)" = "netdata" ] && return 0
524         return 1
525 }
526
527 stop_netdata_on_pid() {
528         local pid="$1" ret=0 count=0
529
530         isnetdata $pid || return 0
531
532         printf >&2 "Stopping netdata on pid $pid ..."
533         while [ ! -z "$pid" -a $ret -eq 0 ]
534         do
535                 if [ $count -gt 45 ]
536                         then
537                         echo >&2 "Cannot stop the running netdata on pid $pid."
538                         return 1
539                 fi
540
541                 count=$(( count + 1 ))
542
543                 run kill $pid 2>/dev/null
544                 ret=$?
545
546                 test $ret -eq 0 && printf >&2 "." && sleep 2
547         done
548
549         echo >&2
550         if [ $ret -eq 0 ]
551         then
552                 echo >&2 "SORRY! CANNOT STOP netdata ON PID $pid !"
553                 return 1
554         fi
555
556         echo >&2 "netdata on pid $pid stopped."
557         return 0
558 }
559
560 stop_all_netdata() {
561         local p
562
563         echo >&2 "Stopping a (possibly) running netdata..."
564
565         for p in $(cat "${NETDATA_RUN_DIR}/netdata.pid" 2>/dev/null) \
566                 $(cat /var/run/netdata.pid 2>/dev/null) \
567                 $(cat /var/run/netdata/netdata.pid 2>/dev/null) \
568                 $(pidof netdata 2>/dev/null)
569         do
570                 stop_netdata_on_pid $p
571         done
572 }
573
574 # -----------------------------------------------------------------------------
575 # check netdata for systemd
576
577 issystemd() {
578         # if the directory /etc/systemd/system does not exit, it is not systemd
579         [ ! -d /etc/systemd/system ] && return 1
580
581         # if pid 1 is systemd, it is systemd
582         [ "$(basename $(readlink /proc/1/exe) 2>/dev/null)" = "systemd" ] && return 0
583
584         # if systemd is running, it is systemd
585         pidof systemd >/dev/null 2>&1 && return 0
586
587         # else, it is not systemd
588         return 1
589 }
590
591 started=0
592 if [ "${UID}" -eq 0 ]
593         then
594
595         if issystemd
596         then
597                 # systemd is running on this system
598
599                 if [ ! -f /etc/systemd/system/netdata.service ]
600                 then
601                         echo >&2 "Installing systemd service..."
602                         run cp system/netdata.service /etc/systemd/system/netdata.service && \
603                                 run systemctl daemon-reload && \
604                                 run systemctl enable netdata
605                 else
606                         run service netdata stop
607                 fi
608
609                 stop_all_netdata
610                 run service netdata restart && started=1
611         fi
612
613         if [ ${started} -eq 0 ]
614         then
615                 # check if we can use the system service
616                 run service netdata stop
617                 stop_all_netdata
618                 run service netdata restart && started=1
619                 if [ ${started} -eq 0 ]
620                 then
621                         run service netdata start && started=1
622                 fi
623         fi
624 fi
625
626 if [ ${started} -eq 0 ]
627 then
628         # still not started...
629
630         stop_all_netdata
631
632         echo >&2 "Starting netdata..."
633         run ${NETDATA_PREFIX}/usr/sbin/netdata -P ${NETDATA_RUN_DIR}/netdata.pid "${@}"
634         if [ $? -ne 0 ]
635                 then
636                 echo >&2
637                 echo >&2 "SORRY! FAILED TO START NETDATA!"
638                 exit 1
639         else
640                 echo >&2 "OK. NetData Started!"
641         fi
642
643         echo >&2
644 fi
645
646 # -----------------------------------------------------------------------------
647 # save a config file, if it is not already there
648
649 if [ ! -s "${NETDATA_PREFIX}/etc/netdata/netdata.conf" ]
650         then
651         echo >&2
652         echo >&2 "-------------------------------------------------------------------------------"
653         echo >&2
654         echo >&2 "Downloading default configuration from netdata..."
655         sleep 5
656
657         # remove a possibly obsolete download
658         [ -f "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" ] && rm "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new"
659
660         # disable a proxy to get data from the local netdata
661         export http_proxy=
662         export https_proxy=
663
664         # try wget
665         wget 2>/dev/null -O "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" "http://localhost:${NETDATA_PORT}/netdata.conf"
666         ret=$?
667
668         # try curl
669         if [ $ret -ne 0 -o ! -s "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" ]
670                 then
671                 curl -s -o "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" "http://localhost:${NETDATA_PORT}/netdata.conf"
672                 ret=$?
673         fi
674
675         if [ $ret -eq 0 -a -s "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" ]
676                 then
677                 mv "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
678                 echo >&2 "New configuration saved for you to edit at ${NETDATA_PREFIX}/etc/netdata/netdata.conf"
679
680                 if [ "${UID}" -eq 0 ]
681                         then
682                         chown "${NETDATA_USER}" "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
683                 fi
684                 chmod 0664 "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
685         else
686                 echo >&2 "Cannnot download configuration from netdata daemon using url 'http://localhost:${NETDATA_PORT}/netdata.conf'"
687                 [ -f "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" ] && rm "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new"
688         fi
689 fi
690
691 # -----------------------------------------------------------------------------
692 # Check for KSM
693
694 ksm_is_available_but_disabled() {
695         cat <<-KSM1
696
697         -------------------------------------------------------------------------------
698         Memory de-duplication instructions
699
700         You have kernel memory de-duper (called Kernel Same-page Merging,
701         or KSM) available, but it is not currently enabled.
702
703         To enable it run:
704
705         echo 1 >/sys/kernel/mm/ksm/run
706         echo 1000 >/sys/kernel/mm/ksm/sleep_millisecs
707
708         If you enable it, you will save 40-60% of netdata memory.
709
710 KSM1
711 }
712
713 ksm_is_not_available() {
714         cat <<-KSM2
715
716         -------------------------------------------------------------------------------
717         Memory de-duplication not present in your kernel
718
719         It seems you do not have kernel memory de-duper (called Kernel Same-page
720         Merging, or KSM) available.
721
722         To enable it, you need a kernel built with CONFIG_KSM=y
723
724         If you can have it, you will save 40-60% of netdata memory.
725
726 KSM2
727 }
728
729 if [ -f "/sys/kernel/mm/ksm/run" ]
730         then
731         if [ $(cat "/sys/kernel/mm/ksm/run") != "1" ]
732                 then
733                 ksm_is_available_but_disabled
734         fi
735 else
736         ksm_is_not_available
737 fi
738
739 # -----------------------------------------------------------------------------
740 # Check for version.txt
741
742 if [ ! -s web/version.txt ]
743         then
744         cat <<-VERMSG
745
746         -------------------------------------------------------------------------------
747         Version update check warning
748
749         The way you downloaded netdata, we cannot find its version. This means the
750         Update check on the dashboard, will not work.
751
752         If you want to have version update check, please re-install it
753         following the procedure in:
754
755         https://github.com/firehol/netdata/wiki/Installation
756
757 VERMSG
758 fi
759
760 # -----------------------------------------------------------------------------
761 # apps.plugin warning
762
763 if [ "${UID}" -ne 0 ]
764         then
765         cat <<-SETUID_WARNING
766
767         -------------------------------------------------------------------------------
768         apps.plugin needs privileges
769
770         Since you have installed netdata as a normal user, to have apps.plugin collect
771         all the needed data, you have to give it the access rights it needs, by running
772         either of the following sets of commands:
773
774         To run apps.plugin with escalated capabilities:
775
776                 sudo chown root:${NETDATA_USER} "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin"
777                 sudo chmod 0750 "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin"
778                 sudo setcap cap_dac_read_search,cap_sys_ptrace+ep "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin"
779
780         or, to run apps.plugin as root:
781
782                 sudo chown root "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin"
783                 sudo chmod 4755 "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin"
784
785         apps.plugin is performing a hard-coded function of data collection for all
786         running processes. It cannot be instructed from the netdata daemon to perform
787         any task, so it is pretty safe to do this.
788
789 SETUID_WARNING
790 fi
791
792 # -----------------------------------------------------------------------------
793 # Keep un-install info
794
795 cat >netdata-uninstaller.sh <<-UNINSTALL
796         #!/bin/bash
797
798         # this script will uninstall netdata
799
800         if [ "\$1" != "--force" ]
801                 then
802                 echo >&2 "This script will REMOVE netdata from your system."
803                 echo >&2 "Run it again with --force to do it."
804                 exit 1
805         fi
806
807         echo >&2 "Stopping a possibly running netdata..."
808         for p in \$(pidof netdata); do kill \$x; done
809         sleep 2
810
811         deletedir() {
812                 if [ ! -z "\$1" -a -d "\$1" ]
813                         then
814                         echo
815                         echo "Deleting directory '\$1' ..."
816                         rm -I -R "\$1"
817                 fi
818         }
819
820         if [ ! -z "${NETDATA_PREFIX}" -a -d "${NETDATA_PREFIX}" ]
821                 then
822                 # installation prefix was given
823
824                 deletedir "${NETDATA_PREFIX}"
825
826         else
827                 # installation prefix was NOT given
828
829                 if [ -f "${NETDATA_PREFIX}/usr/sbin/netdata" ]
830                         then
831                         echo "Deleting ${NETDATA_PREFIX}/usr/sbin/netdata ..."
832                         rm -i "${NETDATA_PREFIX}/usr/sbin/netdata"
833                 fi
834
835                 deletedir "${NETDATA_PREFIX}/etc/netdata"
836                 deletedir "${NETDATA_PREFIX}/usr/share/netdata"
837                 deletedir "${NETDATA_PREFIX}/usr/libexec/netdata"
838                 deletedir "${NETDATA_PREFIX}/var/lib/netdata"
839                 deletedir "${NETDATA_PREFIX}/var/cache/netdata"
840                 deletedir "${NETDATA_PREFIX}/var/log/netdata"
841         fi
842
843         if [ -f /etc/logrotate.d/netdata ]
844                 then
845                 echo "Deleting /etc/logrotate.d/netdata ..."
846                 rm -i /etc/logrotate.d/netdata
847         fi
848
849         if [ -f /etc/systemd/system/netdata.service ]
850                 then
851                 echo "Deleting /etc/systemd/system/netdata.service ..."
852                 rm -i /etc/systemd/system/netdata.service
853         fi
854
855         getent passwd netdata > /dev/null
856         if [ $? -eq 0 ]
857                 then
858                 echo
859                 echo "You may also want to remove the user netdata"
860                 echo "by running:"
861                 echo "   userdel netdata"
862         fi
863
864         getent group netdata > /dev/null
865         if [ $? -eq 0 ]
866                 then
867                 echo
868                 echo "You may also want to remove the group netdata"
869                 echo "by running:"
870                 echo "   groupdel netdata"
871         fi
872
873         getent group docker > /dev/null
874         if [ $? -eq 0 -a "${NETDATA_ADDED_TO_DOCKER}" = "1" ]
875                 then
876                 echo
877                 echo "You may also want to remove the netdata user from the docker group"
878                 echo "by running:"
879                 echo "   gpasswd -d netdata docker"
880         fi
881
882 UNINSTALL
883 chmod 750 netdata-uninstaller.sh
884
885 # -----------------------------------------------------------------------------
886
887 if [ "${NETDATA_BIND}" = "*" ]
888         then
889         access="localhost"
890 else
891         access="${NETDATA_BIND}"
892 fi
893
894 cat <<-END
895
896
897         -------------------------------------------------------------------------------
898
899         OK. NetData is installed and it is running (listening to ${NETDATA_BIND}:${NETDATA_PORT}).
900
901         -------------------------------------------------------------------------------
902
903         INFO: Command line options changed. -pidfile, -nd and -ch are deprecated.
904         If you use custom startup scripts, please run netdata -h to see the 
905         corresponding options and update your scripts.
906
907         Hit http://${access}:${NETDATA_PORT}/ from your browser.
908
909         To stop netdata, just kill it, with:
910
911           killall netdata
912
913         To start it, just run it:
914
915           ${NETDATA_PREFIX}/usr/sbin/netdata
916
917
918         Enjoy!
919
920 END
921 echo >&2 "Uninstall script generated: ./netdata-uninstaller.sh"