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