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