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