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