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