]> arthur.barton.de Git - netdata.git/blob - netdata-installer.sh
c49ef32ceb2d14dbc2f4b486351e4db00b7c871c
[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 # backup user configurations
511 installer_backup_suffix="${PID}.${RANDOM}"
512 for x in $(find "${NETDATA_PREFIX}/etc/netdata/" -name '*.conf' -type f)
513 do
514     if [ -f "${x}" ]
515         then
516         # make a backup of the configuration file
517         cp -p "${x}" "${x}.old"
518
519         if [ -z "${md5sum}" -o ! -x "${md5sum}" ]
520             then
521             # we don't have md5sum - keep it
522             echo >&2 "File '${TPUT_CYAN}${x}${TPUT_RESET}' ${TPUT_RET}is not known to distribution${TPUT_RESET}. Keeping it."
523             run cp -p "${x}" "${x}.installer_backup.${installer_backup_suffix}"
524         else
525             # find it relative filename
526             f="${x/*\/etc\/netdata\//}"
527
528             # find its checksum
529             md5="$(cat "${x}" | ${md5sum} | cut -d ' ' -f 1)"
530
531             # copy the original
532             if [ -f "conf.d/${f}" ]
533                 then
534                 cp "conf.d/${f}" "${x}.orig"
535             fi
536
537             if [ "${BASH_VERSINFO[0]}" -ge "4" ]
538             then
539                 if [ "${configs_signatures[${md5}]}" = "${f}" ]
540                 then
541                     # it is a stock version - don't keep it
542                     echo >&2 "File '${TPUT_CYAN}${x}${TPUT_RESET}' is stock version."
543                 else
544                     # edited by user - keep it
545                     echo >&2 "File '${TPUT_CYAN}${x}${TPUT_RESET}' ${TPUT_RED} has been edited by user${TPUT_RESET}. Keeping it."
546                     run cp -p "${x}" "${x}.installer_backup.${installer_backup_suffix}"
547                 fi
548             else
549                 echo >&2 "File '${TPUT_CYAN}${x}${TPUT_RESET}' ${TPUT_RET}cannot be checked for custom edits${TPUT_RESET}. Keeping it."
550                 run cp -p "${x}" "${x}.installer_backup.${installer_backup_suffix}"
551             fi
552         fi
553
554     elif [ -f "${x}.installer_backup.${installer_backup_suffix}" ]
555         then
556         rm -f "${x}.installer_backup.${installer_backup_suffix}"
557     fi
558 done
559
560
561 # -----------------------------------------------------------------------------
562 progress "Install netdata"
563
564 run make install || exit 1
565
566
567 # -----------------------------------------------------------------------------
568 progress "Restore user edited netdata configuration files"
569
570 for x in $(find "${NETDATA_PREFIX}/etc/netdata/" -name '*.conf' -type f)
571 do
572     if [ -f "${x}.installer_backup.${installer_backup_suffix}" ]
573         then
574         run cp -p "${x}.installer_backup.${installer_backup_suffix}" "${x}" && \
575             run rm -f "${x}.installer_backup.${installer_backup_suffix}"
576     fi
577 done
578
579
580 # -----------------------------------------------------------------------------
581 progress "Fix generated files permissions"
582
583 run find ./system/ -type f -a \! -name \*.in -a \! -name Makefile\* -a \! -name \*.conf -a \! -name \*.service -a \! -name \*.logrotate -exec chmod 755 {} \;
584
585
586 # -----------------------------------------------------------------------------
587 progress "Add user netdata to required user groups"
588
589 NETDATA_ADDED_TO_DOCKER=0
590 NETDATA_ADDED_TO_NGINX=0
591 NETDATA_ADDED_TO_VARNISH=0
592 NETDATA_ADDED_TO_HAPROXY=0
593 NETDATA_ADDED_TO_ADM=0
594 NETDATA_ADDED_TO_NSD=0
595 if [ ${UID} -eq 0 ]
596     then
597     portable_add_group netdata
598     portable_add_user netdata
599     portable_add_user_to_group docker   netdata && NETDATA_ADDED_TO_DOCKER=1
600     portable_add_user_to_group nginx    netdata && NETDATA_ADDED_TO_NGINX=1
601     portable_add_user_to_group varnish  netdata && NETDATA_ADDED_TO_VARNISH=1
602     portable_add_user_to_group haproxy  netdata && NETDATA_ADDED_TO_HAPROXY=1
603     portable_add_user_to_group adm      netdata && NETDATA_ADDED_TO_ADM=1
604     portable_add_user_to_group nsd      netdata && NETDATA_ADDED_TO_NSD=1
605     run_ok
606 else
607     run_failed "The installer does not run as root."
608 fi
609
610 # -----------------------------------------------------------------------------
611 progress "Install logrotate configuration for netdata"
612
613 if [ ${UID} -eq 0 ]
614     then
615     if [ -d /etc/logrotate.d -a ! -f /etc/logrotate.d/netdata ]
616         then
617         run cp system/netdata.logrotate /etc/logrotate.d/netdata
618     fi
619     
620     if [ -f /etc/logrotate.d/netdata ]
621         then
622         run chmod 644 /etc/logrotate.d/netdata
623     fi
624 fi
625
626
627 # -----------------------------------------------------------------------------
628 progress "Read installation options from netdata.conf"
629
630 # create an empty config if it does not exist
631 [ ! -f "${NETDATA_PREFIX}/etc/netdata/netdata.conf" ] && \
632     touch "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
633
634 # function to extract values from the config file
635 config_option() {
636     local key="${1}" value="${2}" line=
637
638     if [ -s "${NETDATA_PREFIX}/etc/netdata/netdata.conf" ]
639         then
640         line="$( grep "^[[:space:]]*${key}[[:space:]]*=[[:space:]]*" "${NETDATA_PREFIX}/etc/netdata/netdata.conf" | head -n 1 )"
641         [ ! -z "${line}" ] && value="$( echo "${line}" | cut -d '=' -f 2 | sed -e "s/^[[:space:]]\+//g" -e "s/[[:space:]]\+$//g" )"
642     fi
643
644     echo "${value}"
645 }
646
647 # the user netdata will run as
648 if [ "${UID}" = "0" ]
649     then
650     NETDATA_USER="$( config_option "run as user" "netdata" )"
651 else
652     NETDATA_USER="${USER}"
653 fi
654
655 # the owners of the web files
656 NETDATA_WEB_USER="$(  config_option "web files owner" "${NETDATA_USER}" )"
657 NETDATA_WEB_GROUP="$( config_option "web files group" "${NETDATA_WEB_USER}" )"
658
659 # debug flags
660 NETDATA_DEBUG="$( config_option "debug flags" 0 )"
661
662 # port
663 defport=19999
664 NETDATA_PORT="$( config_option "default port" ${defport} )"
665 NETDATA_PORT2="$( config_option "port" ${defport} )"
666
667 if [ "${NETDATA_PORT}" != "${NETDATA_PORT2}" ]
668 then
669     if [ "${NETDATA_PORT2}" != "${defport}" ]
670     then
671         NETDATA_PORT="${NETDATA_PORT2}"
672     fi
673 fi
674
675 # directories
676 NETDATA_LIB_DIR="$( config_option "lib directory" "${NETDATA_PREFIX}/var/lib/netdata" )"
677 NETDATA_CACHE_DIR="$( config_option "cache directory" "${NETDATA_PREFIX}/var/cache/netdata" )"
678 NETDATA_WEB_DIR="$( config_option "web files directory" "${NETDATA_PREFIX}/usr/share/netdata/web" )"
679 NETDATA_LOG_DIR="$( config_option "log directory" "${NETDATA_PREFIX}/var/log/netdata" )"
680 NETDATA_CONF_DIR="$( config_option "config directory" "${NETDATA_PREFIX}/etc/netdata" )"
681 NETDATA_RUN_DIR="${NETDATA_PREFIX}/var/run"
682
683
684 # -----------------------------------------------------------------------------
685 progress "Fix permissions of netdata directories (using user '${NETDATA_USER}')"
686
687 if [ ! -d "${NETDATA_RUN_DIR}" ]
688     then
689     # this is needed if NETDATA_PREFIX is not empty
690     run mkdir -p "${NETDATA_RUN_DIR}" || exit 1
691 fi
692
693 # --- conf dir ----
694
695 for x in "python.d" "charts.d" "node.d"
696 do
697     if [ ! -d "${NETDATA_CONF_DIR}/${x}" ]
698         then
699         echo >&2 "Creating directory '${NETDATA_CONF_DIR}/${x}'"
700         run mkdir -p "${NETDATA_CONF_DIR}/${x}" || exit 1
701     fi
702 done
703 run chown -R "${NETDATA_USER}:${NETDATA_USER}" "${NETDATA_CONF_DIR}"
704 run find "${NETDATA_CONF_DIR}" -type f -exec chmod 0660 {} \;
705 run find "${NETDATA_CONF_DIR}" -type d -exec chmod 0775 {} \;
706
707 # --- web dir ----
708
709 if [ ! -d "${NETDATA_WEB_DIR}" ]
710     then
711     echo >&2 "Creating directory '${NETDATA_WEB_DIR}'"
712     run mkdir -p "${NETDATA_WEB_DIR}" || exit 1
713 fi
714 run chown -R "${NETDATA_WEB_USER}:${NETDATA_WEB_GROUP}" "${NETDATA_WEB_DIR}"
715 run find "${NETDATA_WEB_DIR}" -type f -exec chmod 0664 {} \;
716 run find "${NETDATA_WEB_DIR}" -type d -exec chmod 0775 {} \;
717
718 # --- data dirs ----
719
720 for x in "${NETDATA_LIB_DIR}" "${NETDATA_CACHE_DIR}" "${NETDATA_LOG_DIR}"
721 do
722     if [ ! -d "${x}" ]
723         then
724         echo >&2 "Creating directory '${x}'"
725         run mkdir -p "${x}" || exit 1
726     fi
727
728     run chown -R "${NETDATA_USER}:${NETDATA_USER}" "${x}"
729     #run find "${x}" -type f -exec chmod 0660 {} \;
730     #run find "${x}" -type d -exec chmod 0770 {} \;
731 done
732
733 run chmod 755 "${NETDATA_LOG_DIR}"
734
735 # --- plugins ----
736
737 if [ ${UID} -eq 0 ]
738     then
739     # find the admin group
740     admin_group=
741     test -z "${admin_group}" && getent group root >/dev/null 2>&1 && admin_group="root"
742     test -z "${admin_group}" && getent group daemon >/dev/null 2>&1 && admin_group="daemon"
743     test -z "${admin_group}" && admin_group="${NETDATA_USER}"
744
745     run chown "${NETDATA_USER}:${admin_group}" "${NETDATA_LOG_DIR}"
746     run chown -R root "${NETDATA_PREFIX}/usr/libexec/netdata"
747     run find "${NETDATA_PREFIX}/usr/libexec/netdata" -type d -exec chmod 0755 {} \;
748     run find "${NETDATA_PREFIX}/usr/libexec/netdata" -type f -exec chmod 0644 {} \;
749     run find "${NETDATA_PREFIX}/usr/libexec/netdata" -type f -a -name \*.plugin -exec chmod 0755 {} \;
750     run find "${NETDATA_PREFIX}/usr/libexec/netdata" -type f -a -name \*.sh -exec chmod 0755 {} \;
751
752     setcap_ret=1
753     if ! iscontainer
754         then
755         if [ ! -z "${setcap}" ]
756             then
757             run setcap cap_dac_read_search,cap_sys_ptrace+ep "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin"
758             setcap_ret=$?
759         fi
760
761         if [ ${setcap_ret} -eq 0 ]
762             then
763             # if we managed to setcap
764             # but we fail to execute apps.plugin
765             # trigger setuid to root
766             "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin" -t >/dev/null 2>&1
767             setcap_ret=$?
768         fi
769     fi
770
771     if [ ${setcap_ret} -ne 0 ]
772         then
773         # fix apps.plugin to be setuid to root
774         run chown root "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin"
775         run chmod 4755 "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin"
776     fi
777
778     if [ -f "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/freeipmi.plugin" ]
779         then
780         run chown root "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/freeipmi.plugin"
781         run chmod 4755 "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/freeipmi.plugin"
782     fi
783
784 else
785     run chown "${NETDATA_USER}:${NETDATA_USER}" "${NETDATA_LOG_DIR}"
786     run chown -R "${NETDATA_USER}:${NETDATA_USER}" "${NETDATA_PREFIX}/usr/libexec/netdata"
787     run find "${NETDATA_PREFIX}/usr/libexec/netdata" -type f -exec chmod 0755 {} \;
788     run find "${NETDATA_PREFIX}/usr/libexec/netdata" -type d -exec chmod 0755 {} \;
789 fi
790
791 # --- fix #1292 bug ---
792
793 [ -d "${NETDATA_PREFIX}/usr/libexec" ]       && run chmod a+rX "${NETDATA_PREFIX}/usr/libexec"
794 [ -d "${NETDATA_PREFIX}/usr/share/netdata" ] && run chmod a+rX "${NETDATA_PREFIX}/usr/share/netdata"
795
796
797
798 # -----------------------------------------------------------------------------
799 progress "Install netdata at system init"
800
801 installed_init_d=0
802 install_non_systemd_init() {
803     [ "${UID}" != 0 ] && return 1
804
805     local key="unknown"
806     if [ -f /etc/os-release ]
807         then
808         source /etc/os-release || return 1
809         key="${ID}-${VERSION_ID}"
810
811     elif [ -f /etc/centos-release ]
812         then
813         key=$(</etc/centos-release)
814     fi
815
816     if [ -d /etc/init.d -a ! -f /etc/init.d/netdata ]
817         then
818         if [ "${key}" = "gentoo" ]
819             then
820             run cp system/netdata-openrc /etc/init.d/netdata && \
821             run chmod 755 /etc/init.d/netdata && \
822             run rc-update add netdata default && \
823             installed_init_d=1
824         
825         elif [ "${key}" = "ubuntu-12.04" -o "${key}" = "ubuntu-14.04" -o "${key}" = "debian-7" ]
826             then
827             run cp system/netdata-lsb /etc/init.d/netdata && \
828             run chmod 755 /etc/init.d/netdata && \
829             run update-rc.d netdata defaults && \
830             run update-rc.d netdata enable && \
831             installed_init_d=1
832
833         elif [ "${key}" = "CentOS release 6.8 (Final)" -o "${key}" = "amzn-2016.09" ]
834             then
835             run cp system/netdata-init-d /etc/init.d/netdata && \
836             run chmod 755 /etc/init.d/netdata && \
837             run chkconfig netdata on && \
838             installed_init_d=1
839         fi
840     fi
841
842     return 0
843 }
844
845 if [ "${UID}" -eq 0 ]
846     then
847
848     if issystemd
849     then
850         # systemd is running on this system
851
852         if [ ! -f /etc/systemd/system/netdata.service ]
853         then
854             echo >&2 "Installing systemd service..."
855             run cp system/netdata.service /etc/systemd/system/netdata.service && \
856                 run systemctl daemon-reload && \
857                 run systemctl enable netdata
858         fi
859     else
860         install_non_systemd_init
861     fi
862 fi
863
864
865 # -----------------------------------------------------------------------------
866 # check if we can re-start netdata
867
868 started=0
869
870 isnetdata() {
871     if [ -d /proc/self ]
872     then
873         [ -z "$1" -o ! -f "/proc/$1/stat" ] && return 1
874         [ "$(cat "/proc/$1/stat" | cut -d '(' -f 2 | cut -d ')' -f 1)" = "netdata" ] && return 0
875         return 1
876     fi
877     return 0
878 }
879
880 stop_netdata_on_pid() {
881     local pid="${1}" ret=0 count=0
882
883     isnetdata ${pid} || return 0
884
885     printf >&2 "Stopping netdata on pid ${pid} ..."
886     while [ ! -z "$pid" -a ${ret} -eq 0 ]
887     do
888         if [ ${count} -gt 45 ]
889             then
890             echo >&2 "Cannot stop the running netdata on pid ${pid}."
891             return 1
892         fi
893
894         count=$(( count + 1 ))
895
896         run kill ${pid} 2>/dev/null
897         ret=$?
898
899         test ${ret} -eq 0 && printf >&2 "." && sleep 2
900     done
901
902     echo >&2
903     if [ ${ret} -eq 0 ]
904     then
905         echo >&2 "SORRY! CANNOT STOP netdata ON PID ${pid} !"
906         return 1
907     fi
908
909     echo >&2 "netdata on pid ${pid} stopped."
910     return 0
911 }
912
913 stop_all_netdata() {
914     local p myns ns
915
916     myns="$(readlink /proc/self/ns/pid 2>/dev/null)"
917
918     # echo >&2 "Stopping a (possibly) running netdata (namespace '${myns}')..."
919
920     for p in $(cat "${NETDATA_RUN_DIR}/netdata.pid" 2>/dev/null) \
921         $(cat /var/run/netdata.pid 2>/dev/null) \
922         $(cat /var/run/netdata/netdata.pid 2>/dev/null) \
923         $(pidof netdata 2>/dev/null)
924     do
925         ns="$(readlink /proc/${p}/ns/pid 2>/dev/null)"
926
927         if [ -z "${myns}" -o -z "${ns}" -o "${myns}" = "${ns}" ]
928             then
929             stop_netdata_on_pid ${p}
930         fi
931     done
932 }
933
934 if [ ${DONOTSTART} -eq 1 ]
935     then
936     if [ ! -s "${NETDATA_PREFIX}/etc/netdata/netdata.conf" ]
937         then
938         echo "# Get config from http://127.0.0.1:${NETDATA_PORT}/netdata.conf" >"${NETDATA_PREFIX}/etc/netdata/netdata.conf"
939
940         if [ "${UID}" -eq 0 ]
941             then
942             chown "${NETDATA_USER}" "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
943         fi
944         chmod 0644 "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
945     fi
946
947 else
948
949     progress "Start netdata"
950
951     if [ "${UID}" -eq 0 ]
952         then
953         service netdata stop
954         stop_all_netdata
955         service netdata restart && started=1
956         if [ ${started} -eq 0 ]
957         then
958             service netdata start && started=1
959         fi
960     fi
961
962     if [ ${started} -eq 0 ]
963     then
964         # still not started...
965
966         stop_all_netdata
967
968         echo >&2 "Starting netdata..."
969         run ${NETDATA_PREFIX}/usr/sbin/netdata -P ${NETDATA_RUN_DIR}/netdata.pid "${@}"
970         if [ $? -ne 0 ]
971             then
972             echo >&2
973             echo >&2 "SORRY! FAILED TO START NETDATA!"
974             exit 1
975         else
976             echo >&2 "OK. NetData Started!"
977         fi
978
979         echo >&2
980     fi
981
982     # -----------------------------------------------------------------------------
983     # save a config file, if it is not already there
984
985     if [ ! -s "${NETDATA_PREFIX}/etc/netdata/netdata.conf" ]
986         then
987         echo >&2
988         echo >&2 "-------------------------------------------------------------------------------"
989         echo >&2
990         echo >&2 "Downloading default configuration from netdata..."
991         sleep 5
992
993         # remove a possibly obsolete download
994         [ -f "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" ] && rm "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new"
995
996         # disable a proxy to get data from the local netdata
997         export http_proxy=
998         export https_proxy=
999
1000         # try wget
1001         wget 2>/dev/null -O "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" "http://localhost:${NETDATA_PORT}/netdata.conf"
1002         ret=$?
1003
1004         # try curl
1005         if [ ${ret} -ne 0 -o ! -s "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" ]
1006             then
1007             curl -s -o "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" "http://localhost:${NETDATA_PORT}/netdata.conf"
1008             ret=$?
1009         fi
1010
1011         if [ ${ret} -eq 0 -a -s "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" ]
1012             then
1013             mv "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
1014             echo >&2 "New configuration saved for you to edit at ${NETDATA_PREFIX}/etc/netdata/netdata.conf"
1015
1016             if [ "${UID}" -eq 0 ]
1017                 then
1018                 chown "${NETDATA_USER}" "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
1019             fi
1020             chmod 0664 "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
1021         else
1022             echo >&2 "Cannnot download configuration from netdata daemon using url 'http://localhost:${NETDATA_PORT}/netdata.conf'"
1023             [ -f "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" ] && rm "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new"
1024         fi
1025     fi
1026 fi
1027
1028 # -----------------------------------------------------------------------------
1029 progress "Check KSM (kernel memory deduper)"
1030
1031 ksm_is_available_but_disabled() {
1032     cat <<KSM1
1033
1034 ${TPUT_BOLD}Memory de-duplication instructions${TPUT_RESET}
1035
1036 You have kernel memory de-duper (called Kernel Same-page Merging,
1037 or KSM) available, but it is not currently enabled.
1038
1039 To enable it run:
1040
1041     ${TPUT_YELLOW}${TPUT_BOLD}echo 1 >/sys/kernel/mm/ksm/run${TPUT_RESET}
1042     ${TPUT_YELLOW}${TPUT_BOLD}echo 1000 >/sys/kernel/mm/ksm/sleep_millisecs${TPUT_RESET}
1043
1044 If you enable it, you will save 40-60% of netdata memory.
1045
1046 KSM1
1047 }
1048
1049 ksm_is_not_available() {
1050     cat <<KSM2
1051
1052 ${TPUT_BOLD}Memory de-duplication not present in your kernel${TPUT_RESET}
1053
1054 It seems you do not have kernel memory de-duper (called Kernel Same-page
1055 Merging, or KSM) available.
1056
1057 To enable it, you need a kernel built with CONFIG_KSM=y
1058
1059 If you can have it, you will save 40-60% of netdata memory.
1060
1061 KSM2
1062 }
1063
1064 if [ -f "/sys/kernel/mm/ksm/run" ]
1065     then
1066     if [ $(cat "/sys/kernel/mm/ksm/run") != "1" ]
1067         then
1068         ksm_is_available_but_disabled
1069     fi
1070 else
1071     ksm_is_not_available
1072 fi
1073
1074 # -----------------------------------------------------------------------------
1075 progress "Check version.txt"
1076
1077 if [ ! -s web/version.txt ]
1078     then
1079     cat <<VERMSG
1080
1081 ${TPUT_BOLD}Version update check warning${TPUT_RESET}
1082
1083 The way you downloaded netdata, we cannot find its version. This means the
1084 Update check on the dashboard, will not work.
1085
1086 If you want to have version update check, please re-install it
1087 following the procedure in:
1088
1089 https://github.com/firehol/netdata/wiki/Installation
1090
1091 VERMSG
1092 fi
1093
1094 # -----------------------------------------------------------------------------
1095 progress "Check apps.plugin"
1096
1097 if [ "${UID}" -ne 0 ]
1098     then
1099     cat <<SETUID_WARNING
1100
1101 ${TPUT_BOLD}apps.plugin needs privileges${TPUT_RESET}
1102
1103 Since you have installed netdata as a normal user, to have apps.plugin collect
1104 all the needed data, you have to give it the access rights it needs, by running
1105 either of the following sets of commands:
1106
1107 To run apps.plugin with escalated capabilities:
1108
1109     ${TPUT_YELLOW}${TPUT_BOLD}sudo chown root:${NETDATA_USER} \"${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin\"${TPUT_RESET}
1110     ${TPUT_YELLOW}${TPUT_BOLD}sudo chmod 0750 \"${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin\"${TPUT_RESET}
1111     ${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}
1112
1113 or, to run apps.plugin as root:
1114
1115     ${TPUT_YELLOW}${TPUT_BOLD}sudo chown root \"${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin\"${TPUT_RESET}
1116     ${TPUT_YELLOW}${TPUT_BOLD}sudo chmod 4755 \"${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin\"${TPUT_RESET}
1117
1118 apps.plugin is performing a hard-coded function of data collection for all
1119 running processes. It cannot be instructed from the netdata daemon to perform
1120 any task, so it is pretty safe to do this.
1121
1122 SETUID_WARNING
1123 fi
1124
1125 # -----------------------------------------------------------------------------
1126 progress "Generate netdata-uninstaller.sh"
1127
1128 cat >netdata-uninstaller.sh <<UNINSTALL
1129 #!/usr/bin/env bash
1130
1131 # this script will uninstall netdata
1132
1133 if [ "\$1" != "--force" ]
1134     then
1135     echo >&2 "This script will REMOVE netdata from your system."
1136     echo >&2 "Run it again with --force to do it."
1137     exit 1
1138 fi
1139
1140 echo >&2 "Stopping a possibly running netdata..."
1141 for p in \$(pidof netdata); do kill \$p; done
1142 sleep 2
1143
1144 deletedir() {
1145     if [ ! -z "\$1" -a -d "\$1" ]
1146         then
1147         echo
1148         echo "Deleting directory '\$1' ..."
1149         rm -I -R "\$1"
1150     fi
1151 }
1152
1153 if [ ! -z "${NETDATA_PREFIX}" -a -d "${NETDATA_PREFIX}" ]
1154     then
1155     # installation prefix was given
1156
1157     deletedir "${NETDATA_PREFIX}"
1158
1159 else
1160     # installation prefix was NOT given
1161
1162     if [ -f "${NETDATA_PREFIX}/usr/sbin/netdata" ]
1163         then
1164         echo "Deleting ${NETDATA_PREFIX}/usr/sbin/netdata ..."
1165         rm -i "${NETDATA_PREFIX}/usr/sbin/netdata"
1166     fi
1167
1168     deletedir "${NETDATA_PREFIX}/etc/netdata"
1169     deletedir "${NETDATA_PREFIX}/usr/share/netdata"
1170     deletedir "${NETDATA_PREFIX}/usr/libexec/netdata"
1171     deletedir "${NETDATA_PREFIX}/var/lib/netdata"
1172     deletedir "${NETDATA_PREFIX}/var/cache/netdata"
1173     deletedir "${NETDATA_PREFIX}/var/log/netdata"
1174 fi
1175
1176 if [ -f /etc/logrotate.d/netdata ]
1177     then
1178     echo "Deleting /etc/logrotate.d/netdata ..."
1179     rm -i /etc/logrotate.d/netdata
1180 fi
1181
1182 if [ -f /etc/systemd/system/netdata.service ]
1183     then
1184     echo "Deleting /etc/systemd/system/netdata.service ..."
1185     rm -i /etc/systemd/system/netdata.service
1186 fi
1187
1188 if [ -f /etc/init.d/netdata ]
1189     then
1190     echo "Deleting /etc/init.d/netdata ..."
1191     rm -i /etc/init.d/netdata
1192 fi
1193
1194 getent passwd netdata > /dev/null
1195 if [ $? -eq 0 ]
1196     then
1197     echo
1198     echo "You may also want to remove the user netdata"
1199     echo "by running:"
1200     echo "   userdel netdata"
1201 fi
1202
1203 getent group netdata > /dev/null
1204 if [ $? -eq 0 ]
1205     then
1206     echo
1207     echo "You may also want to remove the group netdata"
1208     echo "by running:"
1209     echo "   groupdel netdata"
1210 fi
1211
1212 getent group docker > /dev/null
1213 if [ $? -eq 0 -a "${NETDATA_ADDED_TO_DOCKER}" = "1" ]
1214     then
1215     echo
1216     echo "You may also want to remove the netdata user from the docker group"
1217     echo "by running:"
1218     echo "   gpasswd -d netdata docker"
1219 fi
1220
1221 getent group nginx > /dev/null
1222 if [ $? -eq 0 -a "${NETDATA_ADDED_TO_NGINX}" = "1" ]
1223     then
1224     echo
1225     echo "You may also want to remove the netdata user from the nginx group"
1226     echo "by running:"
1227     echo "   gpasswd -d netdata nginx"
1228 fi
1229
1230 getent group varnish > /dev/null
1231 if [ $? -eq 0 -a "${NETDATA_ADDED_TO_VARNISH}" = "1" ]
1232     then
1233     echo
1234     echo "You may also want to remove the netdata user from the varnish group"
1235     echo "by running:"
1236     echo "   gpasswd -d netdata varnish"
1237 fi
1238
1239 getent group haproxy > /dev/null
1240 if [ $? -eq 0 -a "${NETDATA_ADDED_TO_HAPROXY}" = "1" ]
1241     then
1242     echo
1243     echo "You may also want to remove the netdata user from the haproxy group"
1244     echo "by running:"
1245     echo "   gpasswd -d netdata haproxy"
1246 fi
1247
1248 getent group adm > /dev/null
1249 if [ $? -eq 0 -a "${NETDATA_ADDED_TO_ADM}" = "1" ]
1250     then
1251     echo
1252     echo "You may also want to remove the netdata user from the adm group"
1253     echo "by running:"
1254     echo "   gpasswd -d netdata adm"
1255 fi
1256
1257 getent group nsd > /dev/null
1258 if [ $? -eq 0 -a "${NETDATA_ADDED_TO_NSD}" = "1" ]
1259     then
1260     echo
1261     echo "You may also want to remove the netdata user from the nsd group"
1262     echo "by running:"
1263     echo "   gpasswd -d netdata nsd"
1264 fi
1265
1266
1267 UNINSTALL
1268 chmod 750 netdata-uninstaller.sh
1269
1270 # -----------------------------------------------------------------------------
1271 progress "Basic netdata instructions"
1272
1273 cat <<END
1274
1275 netdata by default listens on all IPs on port ${NETDATA_PORT},
1276 so you can access it with:
1277
1278   ${TPUT_CYAN}${TPUT_BOLD}http://this.machine.ip:${NETDATA_PORT}/${TPUT_RESET}
1279
1280 To stop netdata, just kill it, with:
1281
1282   ${TPUT_YELLOW}${TPUT_BOLD}killall netdata${TPUT_RESET}
1283
1284 To start it, just run it:
1285
1286   ${TPUT_YELLOW}${TPUT_BOLD}${NETDATA_PREFIX}/usr/sbin/netdata${TPUT_RESET}
1287
1288
1289 END
1290 echo >&2 "Uninstall script generated: ${TPUT_RED}${TPUT_BOLD}./netdata-uninstaller.sh${TPUT_RESET}"
1291
1292 if [ -d .git ]
1293     then
1294     cat >netdata-updater.sh.new <<REINSTALL
1295 #!/usr/bin/env bash
1296
1297 force=0
1298 [ "\${1}" = "-f" ] && force=1
1299
1300 export PATH="\${PATH}:${PATH}"
1301 export CFLAGS="${CFLAGS}"
1302 export NETDATA_CONFIGURE_OPTIONS="${NETDATA_CONFIGURE_OPTIONS}"
1303
1304 INSTALL_UID="${UID}"
1305 if [ "\${INSTALL_UID}" != "\${UID}" ]
1306     then
1307     echo >&2 "This script should be run as user with uid \${INSTALL_UID} but it now runs with uid \${UID}"
1308     exit 1
1309 fi
1310
1311 # make sure we cd to the working directory
1312 cd "${REINSTALL_PWD}" || exit 1
1313
1314 # make sure there is .git here
1315 [ \${force} -eq 0 -a ! -d .git ] && echo >&2 "No git structures found at: ${REINSTALL_PWD} (use -f for force re-install)" && exit 1
1316
1317 # signal netdata to start saving its database
1318 # this is handy if your database is big
1319 pids=\$(pidof netdata)
1320 [ ! -z "\${pids}" ] && kill -USR1 \${pids}
1321
1322 tmp=
1323 if [ -t 2 ]
1324     then
1325     # we are running on a terminal
1326     # open fd 3 and send it to stderr
1327     exec 3>&2
1328 else
1329     # we are headless
1330     # create a temporary file for the log
1331     tmp=\$(mktemp /tmp/netdata-updater-log-XXXXXX.log)
1332     # open fd 3 and send it to tmp
1333     exec 3>\${tmp}
1334 fi
1335
1336 info() {
1337     echo >&3 "\$(date) : INFO: " "\${@}"
1338 }
1339
1340 emptyline() {
1341     echo >&3
1342 }
1343
1344 error() {
1345     echo >&3 "\$(date) : ERROR: " "\${@}"
1346 }
1347
1348 # this is what we will do if it fails (head-less only)
1349 failed() {
1350     error "FAILED TO UPDATE NETDATA : \${1}"
1351
1352     if [ ! -z "\${tmp}" ]
1353     then
1354         cat >&2 "\${tmp}"
1355         rm "\${tmp}"
1356     fi
1357     exit 1
1358 }
1359
1360 get_latest_commit_id() {
1361     git log -1           2>&3 |\\
1362         grep ^commit     2>&3 |\\
1363         head -n 1        2>&3 |\\
1364         cut -d ' ' -f 2  2>&3
1365 }
1366
1367 update() {
1368     [ -z "\${tmp}" ] && info "Running on a terminal - (this script also supports running headless from crontab)"
1369
1370     emptyline
1371
1372     if [ -d .git ]
1373         then
1374         info "Updating netdata source from github..."
1375
1376         last_commit="\$(get_latest_commit_id)"
1377         [ \${force} -eq 0 -a -z "\${last_commit}" ] && failed "CANNOT GET LAST COMMIT ID (use -f for force re-install)"
1378
1379         git pull >&3 2>&3 || failed "CANNOT FETCH LATEST SOURCE (use -f for force re-install)"
1380
1381         new_commit="\$(get_latest_commit_id)"
1382         if [ \${force} -eq 0 ]
1383             then
1384             [ -z "\${new_commit}" ] && failed "CANNOT GET NEW LAST COMMIT ID (use -f for force re-install)"
1385             [ "\${new_commit}" = "\${last_commit}" ] && info "Nothing to be done! (use -f to force re-install)" && exit 0
1386         fi
1387     elif [ \${force} -eq 0 ]
1388         then
1389         failed "CANNOT FIND GIT STRUCTURES IN \$(pwd) (use -f for force re-install)"
1390     fi
1391
1392     emptyline
1393     info "Re-installing netdata..."
1394     ${REINSTALL_COMMAND// --dont-wait/} --dont-wait >&3 2>&3 || failed "FAILED TO COMPILE/INSTALL NETDATA"
1395
1396     [ ! -z "\${tmp}" ] && rm "\${tmp}" && tmp=
1397     return 0
1398 }
1399
1400 # the installer updates this script - so we run and exit in a single line
1401 update && exit 0
1402 ###############################################################################
1403 ###############################################################################
1404 REINSTALL
1405     chmod 755 netdata-updater.sh.new
1406     mv -f netdata-updater.sh.new netdata-updater.sh
1407     echo >&2 "Update script generated   : ${TPUT_GREEN}${TPUT_BOLD}./netdata-updater.sh${TPUT_RESET}"
1408     echo >&2
1409     echo >&2 "${TPUT_DIM}${TPUT_BOLD}netdata-updater.sh${TPUT_RESET}${TPUT_DIM} can work from cron. It will trigger an email from cron"
1410     echo >&2 "only if it fails (it does not print anything if it can update netdata).${TPUT_RESET}"
1411     if [ "${UID}" -eq 0 -a -d "/etc/cron.daily" -a ! -f "/etc/cron.daily/netdata-updater.sh" ]
1412         then
1413         echo >&2 "${TPUT_DIM}Run this to automatically check and install netdata updates once per day:${TPUT_RESET}"
1414         echo >&2
1415         echo >&2 "${TPUT_YELLOW}${TPUT_BOLD}ln -s ${PWD}/netdata-updater.sh /etc/cron.daily/netdata-updater.sh${TPUT_RESET}"
1416     fi
1417 elif [ -f "netdata-updater.sh" ]
1418     then
1419     rm "netdata-updater.sh"
1420 fi
1421
1422 # -----------------------------------------------------------------------------
1423 echo >&2
1424 progress "We are done!"
1425
1426 if [ ${started} -eq 1 ]
1427     then
1428     netdata_banner "is installed and running now!"
1429 else
1430     netdata_banner "is installed now!"
1431 fi
1432
1433 echo >&2 "  enjoy real-time performance and health monitoring..."
1434 echo >&2 
1435 exit 0