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