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