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