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