]> arthur.barton.de Git - netdata.git/blob - netdata-installer.sh
Merge pull request #1607 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 002
17
18 # Be nice on production environments
19 renice 19 $$ >/dev/null 2>/dev/null
20
21 processors=$(cat /proc/cpuinfo  | grep ^processor | wc -l)
22 [ $(( processors )) -lt 1 ] && processors=1
23
24 # you can set CFLAGS before running installer
25 CFLAGS="${CFLAGS--O2}"
26 [ "z${CFLAGS}" = "z-O3" ] && CFLAGS="-O2"
27
28 # keep a log of this command
29 printf "\n# " >>netdata-installer.log
30 date >>netdata-installer.log
31 printf "CFLAGS=\"%s\" " "${CFLAGS}" >>netdata-installer.log
32 printf "%q " "$0" "${@}" >>netdata-installer.log
33 printf "\n" >>netdata-installer.log
34
35 REINSTALL_PWD="${PWD}"
36 REINSTALL_COMMAND="$(printf "%q " "$0" "${@}"; printf "\n")"
37
38 banner() {
39     local   l1="  ^"                                                                      \
40             l2="  |.-.   .-.   .-.   .-.   .-.   .-.   .-.   .-.   .-.   .-.   .-.   .-.   .-"  \
41             l3="  |   '-'   '-'   '-'   '-'   '-'   '-'   '-'   '-'   '-'   '-'   '-'   '-'  "  \
42             l4="  +----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+--->" \
43             sp="                                                                        " \
44             netdata="netdata" start end msg="${*}"
45
46     [ ${#msg} -lt ${#netdata} ] && msg="${msg}${sp:0:$(( ${#netdata} - ${#msg}))}"
47     [ ${#msg} -gt $(( ${#l2} - 20 )) ] && msg="${msg:0:$(( ${#l2} - 23 ))}..."
48
49     start="$(( ${#l2} / 2 - 4 ))"
50     [ $(( start + ${#msg} + 4 )) -gt ${#l2} ] && start=$((${#l2} - ${#msg} - 4))
51     end=$(( ${start} + ${#msg} + 4 ))
52
53     echo >&2
54     echo >&2 "${l1}"
55     echo >&2 "${l2:0:start}${sp:0:2}${netdata}${sp:0:$((end - start - 2 - ${#netdata}))}${l2:end:$((${#l2} - end))}"
56     echo >&2 "${l3:0:start}${sp:0:2}${msg}${sp:0:2}${l3:end:$((${#l2} - end))}"
57     echo >&2 "${l4}"
58     echo >&2
59 }
60
61 service="$(which service 2>/dev/null || command -v service 2>/dev/null)"
62 systemctl="$(which systemctl 2>/dev/null || command -v systemctl 2>/dev/null)"
63 service() {
64     local cmd="${1}" action="${2}"
65
66     if [ ! -z "${service}" ]
67     then
68         run "${service}" "${cmd}" "${action}"
69         return $?
70     elif [ ! -z "${systemctl}" ]
71     then
72         run "${systemctl}" "${action}" "${cmd}"
73         return $?
74     fi
75     return 1
76 }
77
78 ME="$0"
79 DONOTSTART=0
80 DONOTWAIT=0
81 NETDATA_PREFIX=
82 LIBS_ARE_HERE=0
83
84 usage() {
85     banner "installer command line options"
86     cat <<USAGE
87
88 ${ME} <installer options>
89
90 Valid <installer options> are:
91
92    --install /PATH/TO/INSTALL
93
94         If your give: --install /opt
95         netdata will be installed in /opt/netdata
96
97    --dont-start-it
98
99         Do not (re)start netdata.
100         Just install it.
101
102    --dont-wait
103
104         Do not wait for the user to press ENTER.
105         Start immediately building it.
106
107    --zlib-is-really-here
108    --libs-are-really-here
109
110         If you get errors about missing zlib,
111         or libuuid but you know it is available,
112         you have a broken pkg-config.
113         Use this option to allow it continue
114         without checking pkg-config.
115
116 Netdata will by default be compiled with gcc optimization -O2
117 If you need to pass different CFLAGS, use something like this:
118
119   CFLAGS="<gcc options>" ${ME} <installer options>
120
121 For the installer to complete successfully, you will need
122 these packages installed:
123
124    gcc make autoconf automake pkg-config zlib1g-dev (or zlib-devel)
125    uuid-dev (or libuuid-devel)
126
127 For the plugins, you will at least need:
128
129    curl nodejs
130
131 USAGE
132 }
133
134 md5sum="$(which md5sum 2>/dev/null || command -v md5sum 2>/dev/null)"
135 get_git_config_signatures() {
136     local x s file md5
137
138     [ ! -d "conf.d" ] && echo >&2 "Wrong directory." && return 1
139     [ -z "${md5sum}" -o ! -x "${md5sum}" ] && echo >&2 "No md5sum command." && return 1
140
141     echo >configs.signatures.tmp
142
143     for x in $(find conf.d -name \*.conf)
144     do
145             x="${x/conf.d\//}"
146             echo "${x}"
147             for c in $(git log --follow "conf.d/${x}" | grep ^commit | cut -d ' ' -f 2)
148             do
149                     git checkout ${c} "conf.d/${x}" || continue
150                     s="$(cat "conf.d/${x}" | md5sum | cut -d ' ' -f 1)"
151                     echo >>configs.signatures.tmp "${s}:${x}"
152                     echo "    ${s}"
153             done
154             git checkout HEAD "conf.d/${x}" || break
155     done
156
157     cat configs.signatures.tmp |\
158         grep -v "^$" |\
159         sort -u |\
160         {
161             echo "declare -A configs_signatures=("
162             IFS=":"
163             while read md5 file
164             do
165                 echo "  ['${md5}']='${file}'"
166             done
167             echo ")"
168         } >configs.signatures
169
170     rm configs.signatures.tmp
171
172     return 0
173 }
174
175
176 while [ ! -z "${1}" ]
177 do
178     if [ "$1" = "--install" ]
179         then
180         NETDATA_PREFIX="${2}/netdata"
181         shift 2
182     elif [ "$1" = "--zlib-is-really-here" -o "$1" = "--libs-are-really-here" ]
183         then
184         LIBS_ARE_HERE=1
185         shift 1
186     elif [ "$1" = "--dont-start-it" ]
187         then
188         DONOTSTART=1
189         shift 1
190     elif [ "$1" = "--dont-wait" ]
191         then
192         DONOTWAIT=1
193         shift 1
194     elif [ "$1" = "--help" -o "$1" = "-h" ]
195         then
196         usage
197         exit 1
198     elif [ "$1" = "get_git_config_signatures" ]
199         then
200         get_git_config_signatures && exit 0
201         exit 1
202     else
203         echo >&2
204         echo >&2 "ERROR:"
205         echo >&2 "I cannot understand option '$1'."
206         usage
207         exit 1
208     fi
209 done
210
211 banner "real-time performance monitoring, done right!"
212 cat <<BANNER
213
214   You are about to build and install netdata to your system.
215
216   It will be installed at these locations:
217
218    - the daemon    at ${NETDATA_PREFIX}/usr/sbin/netdata
219    - config files  at ${NETDATA_PREFIX}/etc/netdata
220    - web files     at ${NETDATA_PREFIX}/usr/share/netdata
221    - plugins       at ${NETDATA_PREFIX}/usr/libexec/netdata
222    - cache files   at ${NETDATA_PREFIX}/var/cache/netdata
223    - db files      at ${NETDATA_PREFIX}/var/lib/netdata
224    - log files     at ${NETDATA_PREFIX}/var/log/netdata
225    - pid file      at ${NETDATA_PREFIX}/var/run
226
227   This installer allows you to change the installation path.
228   Press Control-C and run the same command with --help for help.
229
230 BANNER
231
232 if [ "${UID}" -ne 0 ]
233     then
234     if [ -z "${NETDATA_PREFIX}" ]
235         then
236         banner "wrong command line options!"
237         cat <<NONROOTNOPREFIX
238
239 Sorry! This will fail!
240
241 You are attempting to install netdata as non-root, but you plan to install it
242 in system paths.
243
244 Please set an installation prefix, like this:
245
246     $0 ${@} --install /tmp
247
248 or, run the installer as root:
249
250     sudo $0 ${@}
251
252 We suggest to install it as root, or certain data collectors will not be able
253 to work. Netdata drops root privileges when running. So, if you plan to keep
254 it, install it as root to get the full functionality.
255
256 NONROOTNOPREFIX
257         exit 1
258
259     else
260         cat <<NONROOT
261
262 IMPORTANT:
263 You are about to install netdata as a non-root user.
264 Netdata will work, but a few data collection modules that
265 require root access will fail.
266
267 If you installing permanently on your system, run the
268 installer like this:
269
270     sudo $0 ${@}
271
272 NONROOT
273     fi
274 fi
275
276 have_autotools=
277 if [ "$(type autoreconf 2> /dev/null)" ]
278 then
279     autoconf_maj_min() {
280         local maj min IFS=.-
281
282         maj=$1
283         min=$2
284
285         set -- $(autoreconf -V | sed -ne '1s/.* \([^ ]*\)$/\1/p')
286         eval $maj=\$1 $min=\$2
287     }
288     autoconf_maj_min AMAJ AMIN
289
290     if [ "$AMAJ" -gt 2 ]
291     then
292         have_autotools=Y
293     elif [ "$AMAJ" -eq 2 -a "$AMIN" -ge 60 ]
294     then
295         have_autotools=Y
296     else
297         echo "Found autotools $AMAJ.$AMIN"
298     fi
299 else
300     echo "No autotools found"
301 fi
302
303 if [ ! "$have_autotools" ]
304 then
305     if [ -f configure ]
306     then
307         echo "Will skip autoreconf step"
308     else
309         banner "autotools v2.60 required"
310         cat <<"EOF"
311
312 -------------------------------------------------------------------------------
313 autotools 2.60 or later is required
314
315 Sorry, you do not seem to have autotools 2.60 or later, which is
316 required to build from the git sources of netdata.
317
318 You can either install a suitable version of autotools and automake
319 or download a netdata package which does not have these dependencies.
320
321 Source packages where autotools have already been run are available
322 here:
323        https://firehol.org/download/netdata/
324
325 The unsigned/master folder tracks the head of the git tree and released
326 packages are also available.
327 EOF
328         exit 1
329     fi
330 fi
331
332 if [ ${DONOTWAIT} -eq 0 ]
333     then
334     if [ ! -z "${NETDATA_PREFIX}" ]
335         then
336         read -p "Press ENTER to build and install netdata to '${NETDATA_PREFIX}' > "
337     else
338         read -p "Press ENTER to build and install netdata to your system > "
339     fi
340 fi
341
342 build_error() {
343     banner "sorry, it failed to build..."
344     cat <<EOF
345
346 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
347
348 Sorry! netdata failed to build...
349
350 You many need to check these:
351
352 1. The package uuid-dev (or libuuid-devel) has to be installed.
353
354    If your system cannot find libuuid, although it is installed
355    run me with the option:  --libs-are-really-here
356
357 2. The package zlib1g-dev (or zlib-devel) has to be installed.
358
359    If your system cannot find zlib, although it is installed
360    run me with the option:  --libs-are-really-here
361
362 3. You need basic build tools installed, like:
363
364    gcc make autoconf automake pkg-config
365
366    Autoconf version 2.60 or higher is required.
367
368 If you still cannot get it to build, ask for help at github:
369
370    https://github.com/firehol/netdata/issues
371
372
373 EOF
374     trap - EXIT
375     exit 1
376 }
377
378 run() {
379     printf >>netdata-installer.log "# "
380     printf >>netdata-installer.log "%q " "${@}"
381     printf >>netdata-installer.log " ... "
382
383     printf >&2 "\n"
384     printf >&2 ":-----------------------------------------------------------------------------\n"
385     printf >&2 "Running command:\n"
386     printf >&2 "\n"
387     printf >&2 "%q " "${@}"
388     printf >&2 "\n"
389
390     "${@}"
391
392     local ret=$?
393     if [ ${ret} -ne 0 ]
394         then
395         printf >>netdata-installer.log "FAILED!\n"
396     else
397         printf >>netdata-installer.log "OK\n"
398     fi
399
400     return ${ret}
401 }
402
403 if [ ${LIBS_ARE_HERE} -eq 1 ]
404     then
405     shift
406     echo >&2 "ok, assuming libs are really installed."
407     export ZLIB_CFLAGS=" "
408     export ZLIB_LIBS="-lz"
409     export UUID_CFLAGS=" "
410     export UUID_LIBS="-luuid"
411 fi
412
413 trap build_error EXIT
414
415 if [ "$have_autotools" ]
416 then
417     run ./autogen.sh || exit 1
418 fi
419
420 run ./configure \
421     --prefix="${NETDATA_PREFIX}/usr" \
422     --sysconfdir="${NETDATA_PREFIX}/etc" \
423     --localstatedir="${NETDATA_PREFIX}/var" \
424     --with-zlib --with-math --with-user=netdata \
425     CFLAGS="${CFLAGS}" || exit 1
426
427 # remove the build_error hook
428 trap - EXIT
429
430 if [ -f src/netdata ]
431     then
432     echo >&2 "Cleaning a possibly old compilation ..."
433     run make clean
434 fi
435
436 echo >&2 "Compiling netdata ..."
437 run make -j${processors} || exit 1
438
439 if [ "${BASH_VERSINFO[0]}" -ge "4" ]
440 then
441     declare -A configs_signatures=()
442     if [ -f "configs.signatures" ]
443         then
444         source "configs.signatures" || echo >&2 "ERROR: Failed to load configs.signatures !"
445     fi
446 fi
447
448 # migrate existing configuration files
449 # for node.d and charts.d
450 if [ -d "${NETDATA_PREFIX}/etc/netdata" ]
451     then
452     # the configuration directory exists
453
454     if [ ! -d "${NETDATA_PREFIX}/etc/netdata/charts.d" ]
455         then
456         run mkdir "${NETDATA_PREFIX}/etc/netdata/charts.d"
457     fi
458
459     # move the charts.d config files
460     for x in apache ap cpu_apps cpufreq example exim hddtemp load_average mem_apps mysql nginx nut opensips phpfpm postfix sensors squid tomcat
461     do
462         for y in "" ".old" ".orig"
463         do
464             if [ -f "${NETDATA_PREFIX}/etc/netdata/${x}.conf${y}" -a ! -f "${NETDATA_PREFIX}/etc/netdata/charts.d/${x}.conf${y}" ]
465                 then
466                 run mv -f "${NETDATA_PREFIX}/etc/netdata/${x}.conf${y}" "${NETDATA_PREFIX}/etc/netdata/charts.d/${x}.conf${y}"
467             fi
468         done
469     done
470
471     if [ ! -d "${NETDATA_PREFIX}/etc/netdata/node.d" ]
472         then
473         run mkdir "${NETDATA_PREFIX}/etc/netdata/node.d"
474     fi
475
476     # move the node.d config files
477     for x in named sma_webbox snmp
478     do
479         for y in "" ".old" ".orig"
480         do
481             if [ -f "${NETDATA_PREFIX}/etc/netdata/${x}.conf${y}" -a ! -f "${NETDATA_PREFIX}/etc/netdata/node.d/${x}.conf${y}" ]
482                 then
483                 run mv -f "${NETDATA_PREFIX}/etc/netdata/${x}.conf${y}" "${NETDATA_PREFIX}/etc/netdata/node.d/${x}.conf${y}"
484             fi
485         done
486     done
487 fi
488
489 # backup user configurations
490 installer_backup_suffix="${PID}.${RANDOM}"
491 for x in $(find "${NETDATA_PREFIX}/etc/netdata/" -name '*.conf' -type f)
492 do
493     if [ -f "${x}" ]
494         then
495         # make a backup of the configuration file
496         cp -p "${x}" "${x}.old"
497
498         if [ -z "${md5sum}" -o ! -x "${md5sum}" ]
499             then
500             # we don't have md5sum - keep it
501             cp -p "${x}" "${x}.installer_backup.${installer_backup_suffix}"
502         else
503             # find it relative filename
504             f="${x/*\/etc\/netdata\//}"
505
506             # find its checksum
507             md5="$(cat "${x}" | ${md5sum} | cut -d ' ' -f 1)"
508
509             # copy the original
510             if [ -f "conf.d/${f}" ]
511                 then
512                 cp "conf.d/${f}" "${x}.orig"
513             fi
514
515             if [ "${BASH_VERSINFO[0]}" -ge "4" ]
516             then
517                 if [ "${configs_signatures[${md5}]}" = "${f}" ]
518                 then
519                     # it is a stock version - don't keep it
520                     echo >&2 "File '${x}' is stock version."
521                 else
522                     # edited by user - keep it
523                     echo >&2 "File '${x}' has been edited by user."
524                     cp -p "${x}" "${x}.installer_backup.${installer_backup_suffix}"
525                 fi
526             else
527                 echo >&2 "File '${x}' cannot be check for custom edits."
528                 cp -p "${x}" "${x}.installer_backup.${installer_backup_suffix}"
529             fi
530         fi
531
532     elif [ -f "${x}.installer_backup.${installer_backup_suffix}" ]
533         then
534         rm -f "${x}.installer_backup.${installer_backup_suffix}"
535     fi
536 done
537
538 echo >&2 "Installing netdata ..."
539 run make install || exit 1
540
541 # restore user configurations
542 for x in $(find "${NETDATA_PREFIX}/etc/netdata/" -name '*.conf' -type f)
543 do
544     if [ -f "${x}.installer_backup.${installer_backup_suffix}" ]
545         then
546         cp -p "${x}.installer_backup.${installer_backup_suffix}" "${x}"
547         rm -f "${x}.installer_backup.${installer_backup_suffix}"
548     fi
549 done
550
551 echo >&2 "Fixing permissions ..."
552
553 check_cmd() {
554     which "${1}" >/dev/null 2>&1 && return 0
555     command -v "${1}" >/dev/null 2>&1 && return 0
556     return 1
557 }
558
559 portable_add_user() {
560     local username="${1}"
561
562     getent passwd "${username}" > /dev/null 2>&1
563     [ $? -eq 0 ] && return 0
564
565     echo >&2 "Adding ${username} user account ..."
566
567     local nologin="$(which nologin 2>/dev/null || command -v nologin 2>/dev/null || echo '/bin/false')"
568
569     # Linux
570     if check_cmd useradd
571     then
572         run useradd -r -g "${username}" -c "${username}" -s "${nologin}" -d / "${username}" && return 0
573     fi
574
575     # FreeBSD
576     if check_cmd pw
577     then
578         run pw useradd "${username}" -d / -g "${username}" -s "${nologin}" && return 0
579     fi
580
581     # BusyBox
582     if check_cmd adduser
583     then
584         run adduser -D -G "${username}" "${username}" && return 0
585     fi
586
587     echo >&2 "Failed to add ${username} user account !"
588
589     return 1
590 }
591
592 portable_add_group() {
593     local groupname="${1}"
594
595     getent group "${groupname}" > /dev/null 2>&1
596     [ $? -eq 0 ] && return 0
597
598     echo >&2 "Adding ${groupname} user group ..."
599
600     # Linux
601     if check_cmd groupadd
602     then
603         run groupadd -r "${groupname}" && return 0
604     fi
605
606     # FreeBSD
607     if check_cmd pw
608     then
609         run pw groupadd "${groupname}" && return 0
610     fi
611
612     # BusyBox
613     if check_cmd addgroup
614     then
615         run addgroup "${groupname}" && return 0
616     fi
617
618     echo >&2 "Failed to add ${groupname} user group !"
619     return 1
620 }
621
622 portable_add_user_to_group() {
623     local groupname="${1}" username="${2}"
624
625     getent group "${groupname}" > /dev/null 2>&1
626     [ $? -ne 0 ] && return 1
627
628     # find the user is already in the group
629     local users=$(getent group "${groupname}" | cut -d ':' -f 4)
630     if [[ ",${users}," =~ ,${username}, ]]
631         then
632         # username is already there
633         return 0
634     else
635         # username is not in group
636         echo >&2 "Adding ${username} user to the ${groupname} group ..."
637
638         # Linux
639         if check_cmd usermod
640         then
641             run usermod -a -G "${groupname}" "${username}" && return 0
642         fi
643
644         # FreeBSD
645         if check_cmd pw
646         then
647             run pw groupmod "${groupname}" -m "${username}" && return 0
648         fi
649
650         # BusyBox
651         if check_cmd addgroup
652         then
653             run addgroup "${username}" "${groupname}" && return 0
654         fi
655
656         echo >&2 "Failed to add user ${username} to group ${groupname} !"
657         return 1
658     fi
659 }
660
661 iscontainer() {
662     # man systemd-detect-virt
663     local cmd=$(which systemd-detect-virt 2>/dev/null || command -v systemd-detect-virt 2>/dev/null)
664     if [ ! -z "${cmd}" -a -x "${cmd}" ]
665         then
666         "${cmd}" --container >/dev/null 2>&1 && return 0
667     fi
668
669     # /proc/1/sched exposes the host's pid of our init !
670     # http://stackoverflow.com/a/37016302
671     local pid=$( cat /proc/1/sched | head -n 1 | { IFS='(),#:' read name pid th threads; echo $pid; } )
672     local p=$(( pid + 0 ))
673     [ ${pid} -ne 1 ] && return 0
674
675     # lxc sets environment variable 'container'
676     [ ! -z "${container}" ] && return 0
677
678     # docker creates /.dockerenv
679     # http://stackoverflow.com/a/25518345
680     [ -f "/.dockerenv" ] && return 0
681
682     # ubuntu and debian supply /bin/running-in-container
683     # https://www.apt-browse.org/browse/ubuntu/trusty/main/i386/upstart/1.12.1-0ubuntu4/file/bin/running-in-container
684     if [ -x "/bin/running-in-container" ]
685         then
686         "/bin/running-in-container" >/dev/null 2>&1 && return 0
687     fi
688
689     return 1
690 }
691
692 run find ./system/ -type f -a \! -name \*.in -a \! -name Makefile\* -a \! -name \*.conf -a \! -name \*.service -a \! -name \*.logrotate -exec chmod 755 {} \;
693
694 NETDATA_ADDED_TO_DOCKER=0
695 NETDATA_ADDED_TO_NGINX=0
696 NETDATA_ADDED_TO_VARNISH=0
697 NETDATA_ADDED_TO_HAPROXY=0
698 if [ ${UID} -eq 0 ]
699     then
700     portable_add_group netdata
701     portable_add_user netdata
702     portable_add_user_to_group docker netdata && NETDATA_ADDED_TO_DOCKER=1
703     portable_add_user_to_group nginx  netdata && NETDATA_ADDED_TO_NGINX=1
704     portable_add_user_to_group varnish  netdata && NETDATA_ADDED_TO_VARNISH=1
705     portable_add_user_to_group haproxy  netdata && NETDATA_ADDED_TO_HAPROXY=1
706
707     if [ -d /etc/logrotate.d -a ! -f /etc/logrotate.d/netdata ]
708         then
709         echo >&2 "Adding netdata logrotate configuration ..."
710         run cp system/netdata.logrotate /etc/logrotate.d/netdata
711     fi
712     
713     if [ -f /etc/logrotate.d/netdata ]
714         then
715         echo >&2 "Fixing netdata logrotate permissions ..."
716         run chmod 644 /etc/logrotate.d/netdata
717     fi
718 fi
719
720
721 # -----------------------------------------------------------------------------
722 # load options from the configuration file
723
724 # create an empty config if it does not exist
725 [ ! -f "${NETDATA_PREFIX}/etc/netdata/netdata.conf" ] && touch "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
726
727 # function to extract values from the config file
728 config_option() {
729     local key="${1}" value="${2}" line=
730
731     if [ -s "${NETDATA_PREFIX}/etc/netdata/netdata.conf" ]
732         then
733         line="$( grep "^[[:space:]]*${key}[[:space:]]*=[[:space:]]*" "${NETDATA_PREFIX}/etc/netdata/netdata.conf" | head -n 1 )"
734         [ ! -z "${line}" ] && value="$( echo "${line}" | cut -d '=' -f 2 | sed -e "s/^[[:space:]]\+//g" -e "s/[[:space:]]\+$//g" )"
735     fi
736
737     echo "${value}"
738 }
739
740 # the user netdata will run as
741 if [ "${UID}" = "0" ]
742     then
743     NETDATA_USER="$( config_option "run as user" "netdata" )"
744 else
745     NETDATA_USER="${USER}"
746 fi
747
748 # the owners of the web files
749 NETDATA_WEB_USER="$(  config_option "web files owner" "${NETDATA_USER}" )"
750 NETDATA_WEB_GROUP="$( config_option "web files group" "${NETDATA_WEB_USER}" )"
751
752 # debug flags
753 NETDATA_DEBUG="$( config_option "debug flags" 0 )"
754
755 # port
756 defport=19999
757 NETDATA_PORT="$( config_option "default port" ${defport} )"
758 NETDATA_PORT2="$( config_option "port" ${defport} )"
759
760 if [ "${NETDATA_PORT}" != "${NETDATA_PORT2}" ]
761 then
762     if [ "${NETDATA_PORT2}" != "${defport}" ]
763     then
764         NETDATA_PORT="${NETDATA_PORT2}"
765     fi
766 fi
767
768 # directories
769 NETDATA_LIB_DIR="$( config_option "lib directory" "${NETDATA_PREFIX}/var/lib/netdata" )"
770 NETDATA_CACHE_DIR="$( config_option "cache directory" "${NETDATA_PREFIX}/var/cache/netdata" )"
771 NETDATA_WEB_DIR="$( config_option "web files directory" "${NETDATA_PREFIX}/usr/share/netdata/web" )"
772 NETDATA_LOG_DIR="$( config_option "log directory" "${NETDATA_PREFIX}/var/log/netdata" )"
773 NETDATA_CONF_DIR="$( config_option "config directory" "${NETDATA_PREFIX}/etc/netdata" )"
774 NETDATA_RUN_DIR="${NETDATA_PREFIX}/var/run"
775
776
777 # -----------------------------------------------------------------------------
778 # prepare the directories
779
780 # this is needed if NETDATA_PREFIX is not empty
781 if [ ! -d "${NETDATA_RUN_DIR}" ]
782     then
783     mkdir -p "${NETDATA_RUN_DIR}" || exit 1
784 fi
785
786 echo >&2
787 echo >&2 "Fixing directories (user: ${NETDATA_USER})..."
788
789 # --- conf dir ----
790
791 for x in "python.d" "charts.d" "node.d"
792 do
793     if [ ! -d "${NETDATA_CONF_DIR}/${x}" ]
794         then
795         echo >&2 "Creating directory '${NETDATA_CONF_DIR}/${x}'"
796         run mkdir -p "${NETDATA_CONF_DIR}/${x}" || exit 1
797     fi
798 done
799 run chown -R "${NETDATA_USER}:${NETDATA_USER}" "${NETDATA_CONF_DIR}"
800 run find "${NETDATA_CONF_DIR}" -type f -exec chmod 0660 {} \;
801 run find "${NETDATA_CONF_DIR}" -type d -exec chmod 0775 {} \;
802
803 # --- web dir ----
804
805 if [ ! -d "${NETDATA_WEB_DIR}" ]
806     then
807     echo >&2 "Creating directory '${NETDATA_WEB_DIR}'"
808     run mkdir -p "${NETDATA_WEB_DIR}" || exit 1
809 fi
810 run chown -R "${NETDATA_WEB_USER}:${NETDATA_WEB_GROUP}" "${NETDATA_WEB_DIR}"
811 run find "${NETDATA_WEB_DIR}" -type f -exec chmod 0664 {} \;
812 run find "${NETDATA_WEB_DIR}" -type d -exec chmod 0775 {} \;
813
814 # --- data dirs ----
815
816 for x in "${NETDATA_LIB_DIR}" "${NETDATA_CACHE_DIR}" "${NETDATA_LOG_DIR}"
817 do
818     if [ ! -d "${x}" ]
819         then
820         echo >&2 "Creating directory '${x}'"
821         run mkdir -p "${x}" || exit 1
822     fi
823
824     run chown -R "${NETDATA_USER}:${NETDATA_USER}" "${x}"
825     #run find "${x}" -type f -exec chmod 0660 {} \;
826     #run find "${x}" -type d -exec chmod 0770 {} \;
827 done
828
829 run chmod 755 "${NETDATA_LOG_DIR}"
830
831 # --- plugins ----
832
833 if [ ${UID} -eq 0 ]
834     then
835     run chown "${NETDATA_USER}:root" "${NETDATA_LOG_DIR}"
836     run chown -R root "${NETDATA_PREFIX}/usr/libexec/netdata"
837     run find "${NETDATA_PREFIX}/usr/libexec/netdata" -type d -exec chmod 0755 {} \;
838     run find "${NETDATA_PREFIX}/usr/libexec/netdata" -type f -exec chmod 0644 {} \;
839     run find "${NETDATA_PREFIX}/usr/libexec/netdata" -type f -a -name \*.plugin -exec chmod 0755 {} \;
840     run find "${NETDATA_PREFIX}/usr/libexec/netdata" -type f -a -name \*.sh -exec chmod 0755 {} \;
841
842     setcap_ret=1
843     if ! iscontainer
844         then
845         run setcap cap_dac_read_search,cap_sys_ptrace+ep "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin"
846         setcap_ret=$?
847
848         if [ ${setcap_ret} -eq 0 ]
849             then
850             # if we managed to setcap
851             # but we fail to execute apps.plugin
852             # trigger setuid to root
853             "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin" -v >/dev/null 2>&1
854             setcap_ret=$?
855         fi
856     fi
857
858     if [ ${setcap_ret} -ne 0 ]
859         then
860         # fix apps.plugin to be setuid to root
861         run chown root "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin"
862         run chmod 4755 "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin"
863     fi
864 else
865     run chown "${NETDATA_USER}:${NETDATA_USER}" "${NETDATA_LOG_DIR}"
866     run chown -R "${NETDATA_USER}:${NETDATA_USER}" "${NETDATA_PREFIX}/usr/libexec/netdata"
867     run find "${NETDATA_PREFIX}/usr/libexec/netdata" -type f -exec chmod 0755 {} \;
868     run find "${NETDATA_PREFIX}/usr/libexec/netdata" -type d -exec chmod 0755 {} \;
869 fi
870
871 # --- fix #1292 bug ---
872
873 [ -d "${NETDATA_PREFIX}/usr/libexec" ] && run chmod a+rX "${NETDATA_PREFIX}/usr/libexec"
874 [ -d "${NETDATA_PREFIX}/usr/share/netdata" ] && run chmod a+rX "${NETDATA_PREFIX}/usr/share/netdata"
875
876
877 # -----------------------------------------------------------------------------
878 # check if we can re-start netdata
879
880 if [ ${DONOTSTART} -eq 1 ]
881     then
882     if [ ! -s "${NETDATA_PREFIX}/etc/netdata/netdata.conf" ]
883         then
884         echo >&2 "Generating empty config file in: ${NETDATA_PREFIX}/etc/netdata/netdata.conf"
885         echo "# Get config from http://127.0.0.1:${NETDATA_PORT}/netdata.conf" >"${NETDATA_PREFIX}/etc/netdata/netdata.conf"
886
887         if [ "${UID}" -eq 0 ]
888             then
889             chown "${NETDATA_USER}" "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
890         fi
891         chmod 0644 "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
892     fi
893     banner "is installed now!"
894     echo >&2 "  enjoy real-time performance and health monitoring..."
895     exit 0
896 fi
897
898 # -----------------------------------------------------------------------------
899 # stop a running netdata
900
901 isnetdata() {
902     if [ -d /proc/self ]
903     then
904         [ -z "$1" -o ! -f "/proc/$1/stat" ] && return 1
905         [ "$(cat "/proc/$1/stat" | cut -d '(' -f 2 | cut -d ')' -f 1)" = "netdata" ] && return 0
906         return 1
907     fi
908     return 0
909 }
910
911 stop_netdata_on_pid() {
912     local pid="${1}" ret=0 count=0
913
914     isnetdata ${pid} || return 0
915
916     printf >&2 "Stopping netdata on pid ${pid} ..."
917     while [ ! -z "$pid" -a ${ret} -eq 0 ]
918     do
919         if [ ${count} -gt 45 ]
920             then
921             echo >&2 "Cannot stop the running netdata on pid ${pid}."
922             return 1
923         fi
924
925         count=$(( count + 1 ))
926
927         run kill ${pid} 2>/dev/null
928         ret=$?
929
930         test ${ret} -eq 0 && printf >&2 "." && sleep 2
931     done
932
933     echo >&2
934     if [ ${ret} -eq 0 ]
935     then
936         echo >&2 "SORRY! CANNOT STOP netdata ON PID ${pid} !"
937         return 1
938     fi
939
940     echo >&2 "netdata on pid ${pid} stopped."
941     return 0
942 }
943
944 stop_all_netdata() {
945     local p myns ns
946
947     myns="$(readlink /proc/self/ns/pid 2>/dev/null)"
948
949     echo >&2 "Stopping a (possibly) running netdata..."
950
951     for p in $(cat "${NETDATA_RUN_DIR}/netdata.pid" 2>/dev/null) \
952         $(cat /var/run/netdata.pid 2>/dev/null) \
953         $(cat /var/run/netdata/netdata.pid 2>/dev/null) \
954         $(pidof netdata 2>/dev/null)
955     do
956         ns="$(readlink /proc/${p}/ns/pid 2>/dev/null)"
957
958         if [ -z "${myns}" -o -z "${ns}" -o "${myns}" = "${ns}" ]
959             then
960             stop_netdata_on_pid ${p}
961         fi
962     done
963 }
964
965 # -----------------------------------------------------------------------------
966 # check for systemd
967
968 issystemd() {
969     local pids p myns ns systemctl
970
971     # if the directory /etc/systemd/system does not exit, it is not systemd
972     [ ! -d /etc/systemd/system ] && return 1
973
974     # if there is no systemctl command, it is not systemd
975     systemctl=$(which systemctl 2>/dev/null || command -v systemctl 2>/dev/null)
976     [ -z "${systemctl}" -o ! -x "${systemctl}" ] && return 1
977
978     # if pid 1 is systemd, it is systemd
979     [ "$(basename $(readlink /proc/1/exe) 2>/dev/null)" = "systemd" ] && return 0
980
981     # if systemd is not running, it is not systemd
982     pids=$(pidof systemd 2>/dev/null)
983     [ -z "${pids}" ] && return 1
984
985     # check if the running systemd processes are not in our namespace
986     myns="$(readlink /proc/self/ns/pid 2>/dev/null)"
987     for p in ${pids}
988     do
989         ns="$(readlink /proc/${p}/ns/pid 2>/dev/null)"
990
991         # if pid of systemd is in our namespace, it is systemd
992         [ ! -z "${myns}" && "${myns}" = "${ns}" ] && return 0
993     done
994
995     # else, it is not systemd
996     return 1
997 }
998
999 installed_init_d=0
1000 install_non_systemd_init() {
1001     [ "${UID}" != 0 ] && return 1
1002
1003     local key="unknown"
1004     if [ -f /etc/os-release ]
1005         then
1006         source /etc/os-release || return 1
1007         key="${ID}-${VERSION_ID}"
1008
1009     elif [ -f /etc/centos-release ]
1010         then
1011         key=$(</etc/centos-release)
1012     fi
1013
1014     if [ -d /etc/init.d -a ! -f /etc/init.d/netdata ]
1015         then
1016         if [ "${key}" = "gentoo" ]
1017             then
1018             run cp system/netdata-openrc /etc/init.d/netdata && \
1019             run chmod 755 /etc/init.d/netdata && \
1020             run rc-update add netdata default && \
1021             installed_init_d=1
1022         
1023         elif [ "${key}" = "ubuntu-12.04" -o "${key}" = "ubuntu-14.04" -o "${key}" = "debian-7" ]
1024             then
1025             run cp system/netdata-lsb /etc/init.d/netdata && \
1026             run chmod 755 /etc/init.d/netdata && \
1027             run update-rc.d netdata defaults && \
1028             run update-rc.d netdata enable && \
1029             installed_init_d=1
1030
1031         elif [ "${key}" = "CentOS release 6.8 (Final)" -o "${key}" = "amzn-2016.09" ]
1032             then
1033             run cp system/netdata-init-d /etc/init.d/netdata && \
1034             run chmod 755 /etc/init.d/netdata && \
1035             run chkconfig netdata on && \
1036             installed_init_d=1
1037         fi
1038     fi
1039
1040     return 0
1041 }
1042
1043 started=0
1044 if [ "${UID}" -eq 0 ]
1045     then
1046
1047     if issystemd
1048     then
1049         # systemd is running on this system
1050
1051         if [ ! -f /etc/systemd/system/netdata.service ]
1052         then
1053             echo >&2 "Installing systemd service..."
1054             run cp system/netdata.service /etc/systemd/system/netdata.service && \
1055                 run systemctl daemon-reload && \
1056                 run systemctl enable netdata
1057         else
1058             service netdata stop
1059         fi
1060
1061         stop_all_netdata
1062         service netdata restart && started=1
1063     else
1064         install_non_systemd_init
1065     fi
1066
1067     if [ ${started} -eq 0 ]
1068     then
1069         # check if we can use the system service
1070         service netdata stop
1071         stop_all_netdata
1072         service netdata restart && started=1
1073         if [ ${started} -eq 0 ]
1074         then
1075             service netdata start && started=1
1076         fi
1077     fi
1078 fi
1079
1080 if [ ${started} -eq 0 ]
1081 then
1082     # still not started...
1083
1084     stop_all_netdata
1085
1086     echo >&2 "Starting netdata..."
1087     run ${NETDATA_PREFIX}/usr/sbin/netdata -P ${NETDATA_RUN_DIR}/netdata.pid "${@}"
1088     if [ $? -ne 0 ]
1089         then
1090         echo >&2
1091         echo >&2 "SORRY! FAILED TO START NETDATA!"
1092         exit 1
1093     else
1094         echo >&2 "OK. NetData Started!"
1095     fi
1096
1097     echo >&2
1098 fi
1099
1100 # -----------------------------------------------------------------------------
1101 # save a config file, if it is not already there
1102
1103 if [ ! -s "${NETDATA_PREFIX}/etc/netdata/netdata.conf" ]
1104     then
1105     echo >&2
1106     echo >&2 "-------------------------------------------------------------------------------"
1107     echo >&2
1108     echo >&2 "Downloading default configuration from netdata..."
1109     sleep 5
1110
1111     # remove a possibly obsolete download
1112     [ -f "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" ] && rm "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new"
1113
1114     # disable a proxy to get data from the local netdata
1115     export http_proxy=
1116     export https_proxy=
1117
1118     # try wget
1119     wget 2>/dev/null -O "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" "http://localhost:${NETDATA_PORT}/netdata.conf"
1120     ret=$?
1121
1122     # try curl
1123     if [ ${ret} -ne 0 -o ! -s "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" ]
1124         then
1125         curl -s -o "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" "http://localhost:${NETDATA_PORT}/netdata.conf"
1126         ret=$?
1127     fi
1128
1129     if [ ${ret} -eq 0 -a -s "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" ]
1130         then
1131         mv "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
1132         echo >&2 "New configuration saved for you to edit at ${NETDATA_PREFIX}/etc/netdata/netdata.conf"
1133
1134         if [ "${UID}" -eq 0 ]
1135             then
1136             chown "${NETDATA_USER}" "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
1137         fi
1138         chmod 0664 "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
1139     else
1140         echo >&2 "Cannnot download configuration from netdata daemon using url 'http://localhost:${NETDATA_PORT}/netdata.conf'"
1141         [ -f "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" ] && rm "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new"
1142     fi
1143 fi
1144
1145 # -----------------------------------------------------------------------------
1146 # Check for KSM
1147
1148 ksm_is_available_but_disabled() {
1149     cat <<KSM1
1150
1151 -------------------------------------------------------------------------------
1152 Memory de-duplication instructions
1153
1154 You have kernel memory de-duper (called Kernel Same-page Merging,
1155 or KSM) available, but it is not currently enabled.
1156
1157 To enable it run:
1158
1159 echo 1 >/sys/kernel/mm/ksm/run
1160 echo 1000 >/sys/kernel/mm/ksm/sleep_millisecs
1161
1162 If you enable it, you will save 40-60% of netdata memory.
1163
1164 KSM1
1165 }
1166
1167 ksm_is_not_available() {
1168     cat <<KSM2
1169
1170 -------------------------------------------------------------------------------
1171 Memory de-duplication not present in your kernel
1172
1173 It seems you do not have kernel memory de-duper (called Kernel Same-page
1174 Merging, or KSM) available.
1175
1176 To enable it, you need a kernel built with CONFIG_KSM=y
1177
1178 If you can have it, you will save 40-60% of netdata memory.
1179
1180 KSM2
1181 }
1182
1183 if [ -f "/sys/kernel/mm/ksm/run" ]
1184     then
1185     if [ $(cat "/sys/kernel/mm/ksm/run") != "1" ]
1186         then
1187         ksm_is_available_but_disabled
1188     fi
1189 else
1190     ksm_is_not_available
1191 fi
1192
1193 # -----------------------------------------------------------------------------
1194 # Check for version.txt
1195
1196 if [ ! -s web/version.txt ]
1197     then
1198     cat <<VERMSG
1199
1200 -------------------------------------------------------------------------------
1201 Version update check warning
1202
1203 The way you downloaded netdata, we cannot find its version. This means the
1204 Update check on the dashboard, will not work.
1205
1206 If you want to have version update check, please re-install it
1207 following the procedure in:
1208
1209 https://github.com/firehol/netdata/wiki/Installation
1210
1211 VERMSG
1212 fi
1213
1214 # -----------------------------------------------------------------------------
1215 # apps.plugin warning
1216
1217 if [ "${UID}" -ne 0 ]
1218     then
1219     cat <<SETUID_WARNING
1220
1221 -------------------------------------------------------------------------------
1222 apps.plugin needs privileges
1223
1224 Since you have installed netdata as a normal user, to have apps.plugin collect
1225 all the needed data, you have to give it the access rights it needs, by running
1226 either of the following sets of commands:
1227
1228 To run apps.plugin with escalated capabilities:
1229
1230     sudo chown root:${NETDATA_USER} "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin"
1231     sudo chmod 0750 "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin"
1232     sudo setcap cap_dac_read_search,cap_sys_ptrace+ep "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin"
1233
1234 or, to run apps.plugin as root:
1235
1236     sudo chown root "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin"
1237     sudo chmod 4755 "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin"
1238
1239 apps.plugin is performing a hard-coded function of data collection for all
1240 running processes. It cannot be instructed from the netdata daemon to perform
1241 any task, so it is pretty safe to do this.
1242
1243 SETUID_WARNING
1244 fi
1245
1246 # -----------------------------------------------------------------------------
1247 # Keep un-install info
1248
1249 cat >netdata-uninstaller.sh <<UNINSTALL
1250 #!/usr/bin/env bash
1251
1252 # this script will uninstall netdata
1253
1254 if [ "\$1" != "--force" ]
1255     then
1256     echo >&2 "This script will REMOVE netdata from your system."
1257     echo >&2 "Run it again with --force to do it."
1258     exit 1
1259 fi
1260
1261 echo >&2 "Stopping a possibly running netdata..."
1262 for p in \$(pidof netdata); do kill \$p; done
1263 sleep 2
1264
1265 deletedir() {
1266     if [ ! -z "\$1" -a -d "\$1" ]
1267         then
1268         echo
1269         echo "Deleting directory '\$1' ..."
1270         rm -I -R "\$1"
1271     fi
1272 }
1273
1274 if [ ! -z "${NETDATA_PREFIX}" -a -d "${NETDATA_PREFIX}" ]
1275     then
1276     # installation prefix was given
1277
1278     deletedir "${NETDATA_PREFIX}"
1279
1280 else
1281     # installation prefix was NOT given
1282
1283     if [ -f "${NETDATA_PREFIX}/usr/sbin/netdata" ]
1284         then
1285         echo "Deleting ${NETDATA_PREFIX}/usr/sbin/netdata ..."
1286         rm -i "${NETDATA_PREFIX}/usr/sbin/netdata"
1287     fi
1288
1289     deletedir "${NETDATA_PREFIX}/etc/netdata"
1290     deletedir "${NETDATA_PREFIX}/usr/share/netdata"
1291     deletedir "${NETDATA_PREFIX}/usr/libexec/netdata"
1292     deletedir "${NETDATA_PREFIX}/var/lib/netdata"
1293     deletedir "${NETDATA_PREFIX}/var/cache/netdata"
1294     deletedir "${NETDATA_PREFIX}/var/log/netdata"
1295 fi
1296
1297 if [ -f /etc/logrotate.d/netdata ]
1298     then
1299     echo "Deleting /etc/logrotate.d/netdata ..."
1300     rm -i /etc/logrotate.d/netdata
1301 fi
1302
1303 if [ -f /etc/systemd/system/netdata.service ]
1304     then
1305     echo "Deleting /etc/systemd/system/netdata.service ..."
1306     rm -i /etc/systemd/system/netdata.service
1307 fi
1308
1309 if [ -f /etc/init.d/netdata ]
1310     then
1311     echo "Deleting /etc/init.d/netdata ..."
1312     rm -i /etc/init.d/netdata
1313 fi
1314
1315 getent passwd netdata > /dev/null
1316 if [ $? -eq 0 ]
1317     then
1318     echo
1319     echo "You may also want to remove the user netdata"
1320     echo "by running:"
1321     echo "   userdel netdata"
1322 fi
1323
1324 getent group netdata > /dev/null
1325 if [ $? -eq 0 ]
1326     then
1327     echo
1328     echo "You may also want to remove the group netdata"
1329     echo "by running:"
1330     echo "   groupdel netdata"
1331 fi
1332
1333 getent group docker > /dev/null
1334 if [ $? -eq 0 -a "${NETDATA_ADDED_TO_DOCKER}" = "1" ]
1335     then
1336     echo
1337     echo "You may also want to remove the netdata user from the docker group"
1338     echo "by running:"
1339     echo "   gpasswd -d netdata docker"
1340 fi
1341
1342 getent group nginx > /dev/null
1343 if [ $? -eq 0 -a "${NETDATA_ADDED_TO_NGINX}" = "1" ]
1344     then
1345     echo
1346     echo "You may also want to remove the netdata user from the nginx group"
1347     echo "by running:"
1348     echo "   gpasswd -d netdata nginx"
1349 fi
1350
1351 getent group varnish > /dev/null
1352 if [ $? -eq 0 -a "${NETDATA_ADDED_TO_VARNISH}" = "1" ]
1353     then
1354     echo
1355     echo "You may also want to remove the netdata user from the varnish group"
1356     echo "by running:"
1357     echo "   gpasswd -d netdata varnish"
1358 fi
1359
1360 getent group haproxy > /dev/null
1361 if [ $? -eq 0 -a "${NETDATA_ADDED_TO_HAPROXY}" = "1" ]
1362     then
1363     echo
1364     echo "You may also want to remove the netdata user from the haproxy group"
1365     echo "by running:"
1366     echo "   gpasswd -d netdata haproxy"
1367 fi
1368
1369
1370 UNINSTALL
1371 chmod 750 netdata-uninstaller.sh
1372
1373 # -----------------------------------------------------------------------------
1374
1375 cat <<END
1376
1377
1378 -------------------------------------------------------------------------------
1379
1380 OK. NetData is installed and it is running.
1381
1382 -------------------------------------------------------------------------------
1383
1384 By default netdata listens on all IPs on port ${NETDATA_PORT},
1385 so you can access it with:
1386
1387 http://this.machine.ip:${NETDATA_PORT}/
1388
1389 To stop netdata, just kill it, with:
1390
1391   killall netdata
1392
1393 To start it, just run it:
1394
1395   ${NETDATA_PREFIX}/usr/sbin/netdata
1396
1397
1398 END
1399 echo >&2 "Uninstall script generated: ./netdata-uninstaller.sh"
1400
1401 if [ -d .git ]
1402     then
1403     cat >netdata-updater.sh.new <<REINSTALL
1404 #!/usr/bin/env bash
1405
1406 force=0
1407 [ "\${1}" = "-f" ] && force=1
1408
1409 export PATH="\${PATH}:${PATH}"
1410 export CFLAGS="${CFLAGS}"
1411
1412 INSTALL_UID="${UID}"
1413 if [ "\${INSTALL_UID}" != "\${UID}" ]
1414     then
1415     echo >&2 "This script should be run as user with uid \${INSTALL_UID} but it now runs with uid \${UID}"
1416     exit 1
1417 fi
1418
1419 # make sure we cd to the working directory
1420 cd "${REINSTALL_PWD}" || exit 1
1421
1422 # make sure there is .git here
1423 [ \${force} -eq 0 -a ! -d .git ] && echo >&2 "No git structures found at: ${REINSTALL_PWD} (use -f for force re-install)" && exit 1
1424
1425 # signal netdata to start saving its database
1426 # this is handy if your database is big
1427 pids=\$(pidof netdata)
1428 [ ! -z "\${pids}" ] && kill -USR1 \${pids}
1429
1430 tmp=
1431 if [ -t 2 ]
1432     then
1433     # we are running on a terminal
1434     # open fd 3 and send it to stderr
1435     exec 3>&2
1436 else
1437     # we are headless
1438     # create a temporary file for the log
1439     tmp=\$(mktemp /tmp/netdata-updater-log-XXXXXX.log)
1440     # open fd 3 and send it to tmp
1441     exec 3>\${tmp}
1442 fi
1443
1444 info() {
1445     echo >&3 "\$(date) : INFO: " "\${@}"
1446 }
1447
1448 emptyline() {
1449     echo >&3
1450 }
1451
1452 error() {
1453     echo >&3 "\$(date) : ERROR: " "\${@}"
1454 }
1455
1456 # this is what we will do if it fails (head-less only)
1457 failed() {
1458     error "FAILED TO UPDATE NETDATA : \${1}"
1459
1460     if [ ! -z "\${tmp}" ]
1461     then
1462         cat >&2 "\${tmp}"
1463         rm "\${tmp}"
1464     fi
1465     exit 1
1466 }
1467
1468 get_latest_commit_id() {
1469     git log -1           2>&3 |\\
1470         grep ^commit     2>&3 |\\
1471         head -n 1        2>&3 |\\
1472         cut -d ' ' -f 2  2>&3
1473 }
1474
1475 update() {
1476     [ -z "\${tmp}" ] && info "Running on a terminal - (this script also supports running headless from crontab)"
1477
1478     emptyline
1479
1480     if [ -d .git ]
1481         then
1482         info "Updating netdata source from github..."
1483
1484         last_commit="\$(get_latest_commit_id)"
1485         [ \${force} -eq 0 -a -z "\${last_commit}" ] && failed "CANNOT GET LAST COMMIT ID (use -f for force re-install)"
1486
1487         git pull >&3 2>&3 || failed "CANNOT FETCH LATEST SOURCE (use -f for force re-install)"
1488
1489         new_commit="\$(get_latest_commit_id)"
1490         if [ \${force} -eq 0 ]
1491             then
1492             [ -z "\${new_commit}" ] && failed "CANNOT GET NEW LAST COMMIT ID (use -f for force re-install)"
1493             [ "\${new_commit}" = "\${last_commit}" ] && info "Nothing to be done! (use -f to force re-install)" && exit 0
1494         fi
1495     elif [ \${force} -eq 0 ]
1496         then
1497         failed "CANNOT FIND GIT STRUCTURES IN \$(pwd) (use -f for force re-install)"
1498     fi
1499
1500     emptyline
1501     info "Re-installing netdata..."
1502     ${REINSTALL_COMMAND// --dont-wait/} --dont-wait >&3 2>&3 || failed "FAILED TO COMPILE/INSTALL NETDATA"
1503
1504     [ ! -z "\${tmp}" ] && rm "\${tmp}" && tmp=
1505     return 0
1506 }
1507
1508 # the installer updates this script - so we run and exit in a single line
1509 update && exit 0
1510 ###############################################################################
1511 ###############################################################################
1512 REINSTALL
1513     chmod 755 netdata-updater.sh.new
1514     mv -f netdata-updater.sh.new netdata-updater.sh
1515     echo >&2 "Update script generated   : ./netdata-updater.sh"
1516 elif [ -f "netdata-updater.sh" ]
1517     then
1518     rm "netdata-updater.sh"
1519 fi
1520
1521 banner "is installed and running now!"
1522 echo >&2 "  enjoy real-time performance and health monitoring..."
1523 echo >&2 
1524 exit 0