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