3 export PATH="${PATH}:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin"
5 netdata_source_dir="$(pwd)"
6 installer_dir="$(dirname "${0}")"
8 if [ "${netdata_source_dir}" != "${installer_dir}" -a "${installer_dir}" != "." ]
10 echo >&2 "Warninng: you are currently in '${netdata_source_dir}' but the installer is in '${installer_dir}'."
14 # -----------------------------------------------------------------------------
15 # reload the user profile
17 [ -f /etc/profile ] && . /etc/profile
19 # make sure /etc/profile does not change our current directory
20 cd "${netdata_source_dir}" || exit 1
23 # -----------------------------------------------------------------------------
24 # load the required functions
26 if [ -f "${installer_dir}/installer/functions.sh" ]
28 source "${installer_dir}/installer/functions.sh" || exit 1
30 source "${netdata_source_dir}/installer/functions.sh" || exit 1
33 # make sure we save all commands we run
34 run_logfile="netdata-installer.log"
37 # -----------------------------------------------------------------------------
38 # fix PKG_CHECK_MODULES error
40 if [ -d /usr/share/aclocal ]
42 ACLOCAL_PATH=${ACLOCAL_PATH-/usr/share/aclocal}
49 # Be nice on production environments
50 renice 19 $$ >/dev/null 2>/dev/null
52 processors=$(grep ^processor </proc/cpuinfo 2>/dev/null | wc -l)
53 [ $(( processors )) -lt 1 ] && processors=1
55 # you can set CFLAGS before running installer
56 CFLAGS="${CFLAGS--O2}"
57 [ "z${CFLAGS}" = "z-O3" ] && CFLAGS="-O2"
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
66 REINSTALL_PWD="${PWD}"
67 REINSTALL_COMMAND="$(printf "%q " "$0" "${@}"; printf "\n")"
69 setcap="$(which setcap 2>/dev/null || command -v setcap 2>/dev/null)"
76 NETDATA_CONFIGURE_OPTIONS="${NETDATA_CONFIGURE_OPTIONS-}"
79 netdata_banner "installer command line options"
82 ${ME} <installer options>
84 Valid <installer options> are:
86 --install /PATH/TO/INSTALL
88 If you give: --install /opt
89 netdata will be installed in /opt/netdata
93 Do not (re)start netdata.
98 Do not wait for the user to press ENTER.
99 Start immediately building it.
101 --enable-plugin-freeipmi
103 Enable the FreeIPMI plugin.
105 --zlib-is-really-here
106 --libs-are-really-here
108 If you get errors about missing zlib,
109 or libuuid but you know it is available,
110 you have a broken pkg-config.
111 Use this option to allow it continue
112 without checking pkg-config.
114 Netdata will by default be compiled with gcc optimization -O2
115 If you need to pass different CFLAGS, use something like this:
117 CFLAGS="<gcc options>" ${ME} <installer options>
119 For the installer to complete successfully, you will need
120 these packages installed:
122 gcc make autoconf automake pkg-config zlib1g-dev (or zlib-devel)
123 uuid-dev (or libuuid-devel)
125 For the plugins, you will at least need:
132 md5sum="$(which md5sum 2>/dev/null || command -v md5sum 2>/dev/null)"
133 get_git_config_signatures() {
136 [ ! -d "conf.d" ] && echo >&2 "Wrong directory." && return 1
137 [ -z "${md5sum}" -o ! -x "${md5sum}" ] && echo >&2 "No md5sum command." && return 1
139 echo >configs.signatures.tmp
141 for x in $(find conf.d -name \*.conf)
145 for c in $(git log --follow "conf.d/${x}" | grep ^commit | cut -d ' ' -f 2)
147 git checkout ${c} "conf.d/${x}" || continue
148 s="$(cat "conf.d/${x}" | md5sum | cut -d ' ' -f 1)"
149 echo >>configs.signatures.tmp "${s}:${x}"
152 git checkout HEAD "conf.d/${x}" || break
155 cat configs.signatures.tmp |\
159 echo "declare -A configs_signatures=("
163 echo " ['${md5}']='${file}'"
166 } >configs.signatures
168 rm configs.signatures.tmp
174 while [ ! -z "${1}" ]
176 if [ "$1" = "--install" ]
178 NETDATA_PREFIX="${2}/netdata"
180 elif [ "$1" = "--zlib-is-really-here" -o "$1" = "--libs-are-really-here" ]
184 elif [ "$1" = "--dont-start-it" ]
188 elif [ "$1" = "--dont-wait" ]
192 elif [ "$1" = "--enable-plugin-freeipmi" ]
194 NETDATA_CONFIGURE_OPTIONS="${NETDATA_CONFIGURE_OPTIONS} --enable-plugin-freeipmi"
196 elif [ "$1" = "--help" -o "$1" = "-h" ]
200 elif [ "$1" = "get_git_config_signatures" ]
202 get_git_config_signatures && exit 0
207 echo >&2 "I cannot understand option '$1'."
213 netdata_banner "real-time performance monitoring, done right!"
216 You are about to build and install netdata to your system.
218 It will be installed at these locations:
220 - the daemon at ${TPUT_CYAN}${NETDATA_PREFIX}/usr/sbin/netdata${TPUT_RESET}
221 - config files in ${TPUT_CYAN}${NETDATA_PREFIX}/etc/netdata${TPUT_RESET}
222 - web files in ${TPUT_CYAN}${NETDATA_PREFIX}/usr/share/netdata${TPUT_RESET}
223 - plugins in ${TPUT_CYAN}${NETDATA_PREFIX}/usr/libexec/netdata${TPUT_RESET}
224 - cache files in ${TPUT_CYAN}${NETDATA_PREFIX}/var/cache/netdata${TPUT_RESET}
225 - db files in ${TPUT_CYAN}${NETDATA_PREFIX}/var/lib/netdata${TPUT_RESET}
226 - log files in ${TPUT_CYAN}${NETDATA_PREFIX}/var/log/netdata${TPUT_RESET}
229 [ "${UID}" -eq 0 ] && cat <<BANNER2
230 - pid file at ${TPUT_CYAN}${NETDATA_PREFIX}/var/run/netdata.pid${TPUT_RESET}
231 - logrotate file at ${TPUT_CYAN}/etc/logrotate.d/netdata${TPUT_RESET}
236 This installer allows you to change the installation path.
237 Press Control-C and run the same command with --help for help.
241 if [ "${UID}" -ne 0 ]
243 if [ -z "${NETDATA_PREFIX}" ]
245 netdata_banner "wrong command line options!"
246 cat <<NONROOTNOPREFIX
248 ${TPUT_RED}${TPUT_BOLD}Sorry! This will fail!${TPUT_RESET}
250 You are attempting to install netdata as non-root, but you plan
251 to install it in system paths.
253 Please set an installation prefix, like this:
255 $0 ${@} --install /tmp
257 or, run the installer as root:
261 We suggest to install it as root, or certain data collectors will
262 not be able to work. Netdata drops root privileges when running.
263 So, if you plan to keep it, install it as root to get the full
272 ${TPUT_RED}${TPUT_BOLD}IMPORTANT${TPUT_RESET}:
273 You are about to install netdata as a non-root user.
274 Netdata will work, but a few data collection modules that
275 require root access will fail.
277 If you installing netdata permanently on your system, run
278 the installer like this:
280 ${TPUT_YELLOW}${TPUT_BOLD}sudo $0 ${@}${TPUT_RESET}
287 if [ "$(type autoreconf 2> /dev/null)" ]
295 set -- $(autoreconf -V | sed -ne '1s/.* \([^ ]*\)$/\1/p')
296 eval $maj=\$1 $min=\$2
298 autoconf_maj_min AMAJ AMIN
303 elif [ "$AMAJ" -eq 2 -a "$AMIN" -ge 60 ]
307 echo "Found autotools $AMAJ.$AMIN"
310 echo "No autotools found"
313 if [ ! "$have_autotools" ]
317 echo "Will skip autoreconf step"
319 netdata_banner "autotools v2.60 required"
322 -------------------------------------------------------------------------------
323 autotools 2.60 or later is required
325 Sorry, you do not seem to have autotools 2.60 or later, which is
326 required to build from the git sources of netdata.
328 You can either install a suitable version of autotools and automake
329 or download a netdata package which does not have these dependencies.
331 Source packages where autotools have already been run are available
333 https://firehol.org/download/netdata/
335 The unsigned/master folder tracks the head of the git tree and released
336 packages are also available.
342 if [ ${DONOTWAIT} -eq 0 ]
344 if [ ! -z "${NETDATA_PREFIX}" ]
346 eval "read >&2 -ep \$'\001${TPUT_BOLD}${TPUT_GREEN}\002Press ENTER to build and install netdata to \'\001${TPUT_CYAN}\002${NETDATA_PREFIX}\001${TPUT_YELLOW}\002\'\001${TPUT_RESET}\002 > ' -e -r REPLY"
348 eval "read >&2 -ep \$'\001${TPUT_BOLD}${TPUT_GREEN}\002Press ENTER to build and install netdata to your system\001${TPUT_RESET}\002 > ' -e -r REPLY"
353 netdata_banner "sorry, it failed to build..."
356 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
358 Sorry! netdata failed to build...
360 You many need to check these:
362 1. The package uuid-dev (or libuuid-devel) has to be installed.
364 If your system cannot find libuuid, although it is installed
365 run me with the option: --libs-are-really-here
367 2. The package zlib1g-dev (or zlib-devel) has to be installed.
369 If your system cannot find zlib, although it is installed
370 run me with the option: --libs-are-really-here
372 3. You need basic build tools installed, like:
374 gcc make autoconf automake pkg-config
376 Autoconf version 2.60 or higher is required.
378 If you still cannot get it to build, ask for help at github:
380 https://github.com/firehol/netdata/issues
388 if [ ${LIBS_ARE_HERE} -eq 1 ]
391 echo >&2 "ok, assuming libs are really installed."
392 export ZLIB_CFLAGS=" "
393 export ZLIB_LIBS="-lz"
394 export UUID_CFLAGS=" "
395 export UUID_LIBS="-luuid"
398 trap build_error EXIT
401 # -----------------------------------------------------------------------------
403 progress "Run autotools to configure the build environment"
405 if [ "$have_autotools" ]
407 run ./autogen.sh || exit 1
411 --prefix="${NETDATA_PREFIX}/usr" \
412 --sysconfdir="${NETDATA_PREFIX}/etc" \
413 --localstatedir="${NETDATA_PREFIX}/var" \
416 --with-user=netdata \
417 ${NETDATA_CONFIGURE_OPTIONS} \
418 CFLAGS="${CFLAGS}" || exit 1
420 # remove the build_error hook
423 # -----------------------------------------------------------------------------
424 progress "Cleanup compilation directory"
426 [ -f src/netdata ] && run make clean
428 # -----------------------------------------------------------------------------
429 progress "Compile netdata"
431 run make -j${processors} || exit 1
434 # -----------------------------------------------------------------------------
435 progress "Migrate configuration files for node.d.plugin and charts.d.plugin"
437 # migrate existing configuration files
438 # for node.d and charts.d
439 if [ -d "${NETDATA_PREFIX}/etc/netdata" ]
441 # the configuration directory exists
443 if [ ! -d "${NETDATA_PREFIX}/etc/netdata/charts.d" ]
445 run mkdir "${NETDATA_PREFIX}/etc/netdata/charts.d"
448 # move the charts.d config files
449 for x in apache ap cpu_apps cpufreq example exim hddtemp load_average mem_apps mysql nginx nut opensips phpfpm postfix sensors squid tomcat
451 for y in "" ".old" ".orig"
453 if [ -f "${NETDATA_PREFIX}/etc/netdata/${x}.conf${y}" -a ! -f "${NETDATA_PREFIX}/etc/netdata/charts.d/${x}.conf${y}" ]
455 run mv -f "${NETDATA_PREFIX}/etc/netdata/${x}.conf${y}" "${NETDATA_PREFIX}/etc/netdata/charts.d/${x}.conf${y}"
460 if [ ! -d "${NETDATA_PREFIX}/etc/netdata/node.d" ]
462 run mkdir "${NETDATA_PREFIX}/etc/netdata/node.d"
465 # move the node.d config files
466 for x in named sma_webbox snmp
468 for y in "" ".old" ".orig"
470 if [ -f "${NETDATA_PREFIX}/etc/netdata/${x}.conf${y}" -a ! -f "${NETDATA_PREFIX}/etc/netdata/node.d/${x}.conf${y}" ]
472 run mv -f "${NETDATA_PREFIX}/etc/netdata/${x}.conf${y}" "${NETDATA_PREFIX}/etc/netdata/node.d/${x}.conf${y}"
478 # -----------------------------------------------------------------------------
479 progress "Backup existing netdata configuration before installing it"
481 if [ "${BASH_VERSINFO[0]}" -ge "4" ]
483 declare -A configs_signatures=()
484 if [ -f "configs.signatures" ]
486 source "configs.signatures" || echo >&2 "ERROR: Failed to load configs.signatures !"
490 # backup user configurations
491 installer_backup_suffix="${PID}.${RANDOM}"
492 for x in $(find "${NETDATA_PREFIX}/etc/netdata/" -name '*.conf' -type f)
496 # make a backup of the configuration file
497 cp -p "${x}" "${x}.old"
499 if [ -z "${md5sum}" -o ! -x "${md5sum}" ]
501 # we don't have md5sum - keep it
502 echo >&2 "File '${TPUT_CYAN}${x}${TPUT_RESET}' ${TPUT_RET}is not known to distribution${TPUT_RESET}. Keeping it."
503 run cp -p "${x}" "${x}.installer_backup.${installer_backup_suffix}"
505 # find it relative filename
506 f="${x/*\/etc\/netdata\//}"
509 md5="$(cat "${x}" | ${md5sum} | cut -d ' ' -f 1)"
512 if [ -f "conf.d/${f}" ]
514 cp "conf.d/${f}" "${x}.orig"
517 if [ "${BASH_VERSINFO[0]}" -ge "4" ]
519 if [ "${configs_signatures[${md5}]}" = "${f}" ]
521 # it is a stock version - don't keep it
522 echo >&2 "File '${TPUT_CYAN}${x}${TPUT_RESET}' is stock version."
524 # edited by user - keep it
525 echo >&2 "File '${TPUT_CYAN}${x}${TPUT_RESET}' ${TPUT_RED} has been edited by user${TPUT_RESET}. Keeping it."
526 run cp -p "${x}" "${x}.installer_backup.${installer_backup_suffix}"
529 echo >&2 "File '${TPUT_CYAN}${x}${TPUT_RESET}' ${TPUT_RET}cannot be checked for custom edits${TPUT_RESET}. Keeping it."
530 run cp -p "${x}" "${x}.installer_backup.${installer_backup_suffix}"
534 elif [ -f "${x}.installer_backup.${installer_backup_suffix}" ]
536 rm -f "${x}.installer_backup.${installer_backup_suffix}"
541 # -----------------------------------------------------------------------------
542 progress "Install netdata"
544 run make install || exit 1
547 # -----------------------------------------------------------------------------
548 progress "Restore user edited netdata configuration files"
550 for x in $(find "${NETDATA_PREFIX}/etc/netdata/" -name '*.conf' -type f)
552 if [ -f "${x}.installer_backup.${installer_backup_suffix}" ]
554 run cp -p "${x}.installer_backup.${installer_backup_suffix}" "${x}" && \
555 run rm -f "${x}.installer_backup.${installer_backup_suffix}"
560 # -----------------------------------------------------------------------------
561 progress "Fix generated files permissions"
563 run find ./system/ -type f -a \! -name \*.in -a \! -name Makefile\* -a \! -name \*.conf -a \! -name \*.service -a \! -name \*.logrotate -exec chmod 755 {} \;
566 # -----------------------------------------------------------------------------
567 progress "Add user netdata to required user groups"
569 NETDATA_ADDED_TO_DOCKER=0
570 NETDATA_ADDED_TO_NGINX=0
571 NETDATA_ADDED_TO_VARNISH=0
572 NETDATA_ADDED_TO_HAPROXY=0
573 NETDATA_ADDED_TO_ADM=0
576 portable_add_group netdata
577 portable_add_user netdata
578 portable_add_user_to_group docker netdata && NETDATA_ADDED_TO_DOCKER=1
579 portable_add_user_to_group nginx netdata && NETDATA_ADDED_TO_NGINX=1
580 portable_add_user_to_group varnish netdata && NETDATA_ADDED_TO_VARNISH=1
581 portable_add_user_to_group haproxy netdata && NETDATA_ADDED_TO_HAPROXY=1
582 portable_add_user_to_group adm netdata && NETDATA_ADDED_TO_ADM=1
585 run_failed "The installer does not run as root."
588 # -----------------------------------------------------------------------------
589 progress "Install logrotate configuration for netdata"
593 if [ -d /etc/logrotate.d -a ! -f /etc/logrotate.d/netdata ]
595 run cp system/netdata.logrotate /etc/logrotate.d/netdata
598 if [ -f /etc/logrotate.d/netdata ]
600 run chmod 644 /etc/logrotate.d/netdata
605 # -----------------------------------------------------------------------------
606 progress "Read installation options from netdata.conf"
608 # create an empty config if it does not exist
609 [ ! -f "${NETDATA_PREFIX}/etc/netdata/netdata.conf" ] && \
610 touch "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
612 # function to extract values from the config file
614 local key="${1}" value="${2}" line=
616 if [ -s "${NETDATA_PREFIX}/etc/netdata/netdata.conf" ]
618 line="$( grep "^[[:space:]]*${key}[[:space:]]*=[[:space:]]*" "${NETDATA_PREFIX}/etc/netdata/netdata.conf" | head -n 1 )"
619 [ ! -z "${line}" ] && value="$( echo "${line}" | cut -d '=' -f 2 | sed -e "s/^[[:space:]]\+//g" -e "s/[[:space:]]\+$//g" )"
625 # the user netdata will run as
626 if [ "${UID}" = "0" ]
628 NETDATA_USER="$( config_option "run as user" "netdata" )"
630 NETDATA_USER="${USER}"
633 # the owners of the web files
634 NETDATA_WEB_USER="$( config_option "web files owner" "${NETDATA_USER}" )"
635 NETDATA_WEB_GROUP="$( config_option "web files group" "${NETDATA_WEB_USER}" )"
638 NETDATA_DEBUG="$( config_option "debug flags" 0 )"
642 NETDATA_PORT="$( config_option "default port" ${defport} )"
643 NETDATA_PORT2="$( config_option "port" ${defport} )"
645 if [ "${NETDATA_PORT}" != "${NETDATA_PORT2}" ]
647 if [ "${NETDATA_PORT2}" != "${defport}" ]
649 NETDATA_PORT="${NETDATA_PORT2}"
654 NETDATA_LIB_DIR="$( config_option "lib directory" "${NETDATA_PREFIX}/var/lib/netdata" )"
655 NETDATA_CACHE_DIR="$( config_option "cache directory" "${NETDATA_PREFIX}/var/cache/netdata" )"
656 NETDATA_WEB_DIR="$( config_option "web files directory" "${NETDATA_PREFIX}/usr/share/netdata/web" )"
657 NETDATA_LOG_DIR="$( config_option "log directory" "${NETDATA_PREFIX}/var/log/netdata" )"
658 NETDATA_CONF_DIR="$( config_option "config directory" "${NETDATA_PREFIX}/etc/netdata" )"
659 NETDATA_RUN_DIR="${NETDATA_PREFIX}/var/run"
662 # -----------------------------------------------------------------------------
663 progress "Fix permissions of netdata directories (using user '${NETDATA_USER}')"
665 if [ ! -d "${NETDATA_RUN_DIR}" ]
667 # this is needed if NETDATA_PREFIX is not empty
668 run mkdir -p "${NETDATA_RUN_DIR}" || exit 1
673 for x in "python.d" "charts.d" "node.d"
675 if [ ! -d "${NETDATA_CONF_DIR}/${x}" ]
677 echo >&2 "Creating directory '${NETDATA_CONF_DIR}/${x}'"
678 run mkdir -p "${NETDATA_CONF_DIR}/${x}" || exit 1
681 run chown -R "${NETDATA_USER}:${NETDATA_USER}" "${NETDATA_CONF_DIR}"
682 run find "${NETDATA_CONF_DIR}" -type f -exec chmod 0660 {} \;
683 run find "${NETDATA_CONF_DIR}" -type d -exec chmod 0775 {} \;
687 if [ ! -d "${NETDATA_WEB_DIR}" ]
689 echo >&2 "Creating directory '${NETDATA_WEB_DIR}'"
690 run mkdir -p "${NETDATA_WEB_DIR}" || exit 1
692 run chown -R "${NETDATA_WEB_USER}:${NETDATA_WEB_GROUP}" "${NETDATA_WEB_DIR}"
693 run find "${NETDATA_WEB_DIR}" -type f -exec chmod 0664 {} \;
694 run find "${NETDATA_WEB_DIR}" -type d -exec chmod 0775 {} \;
698 for x in "${NETDATA_LIB_DIR}" "${NETDATA_CACHE_DIR}" "${NETDATA_LOG_DIR}"
702 echo >&2 "Creating directory '${x}'"
703 run mkdir -p "${x}" || exit 1
706 run chown -R "${NETDATA_USER}:${NETDATA_USER}" "${x}"
707 #run find "${x}" -type f -exec chmod 0660 {} \;
708 #run find "${x}" -type d -exec chmod 0770 {} \;
711 run chmod 755 "${NETDATA_LOG_DIR}"
717 run chown "${NETDATA_USER}:root" "${NETDATA_LOG_DIR}"
718 run chown -R root "${NETDATA_PREFIX}/usr/libexec/netdata"
719 run find "${NETDATA_PREFIX}/usr/libexec/netdata" -type d -exec chmod 0755 {} \;
720 run find "${NETDATA_PREFIX}/usr/libexec/netdata" -type f -exec chmod 0644 {} \;
721 run find "${NETDATA_PREFIX}/usr/libexec/netdata" -type f -a -name \*.plugin -exec chmod 0755 {} \;
722 run find "${NETDATA_PREFIX}/usr/libexec/netdata" -type f -a -name \*.sh -exec chmod 0755 {} \;
727 if [ ! -z "${setcap}" ]
729 run setcap cap_dac_read_search,cap_sys_ptrace+ep "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin"
733 if [ ${setcap_ret} -eq 0 ]
735 # if we managed to setcap
736 # but we fail to execute apps.plugin
737 # trigger setuid to root
738 "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin" -t >/dev/null 2>&1
743 if [ ${setcap_ret} -ne 0 ]
745 # fix apps.plugin to be setuid to root
746 run chown root "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin"
747 run chmod 4755 "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin"
750 if [ -f "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/freeipmi.plugin" ]
752 run chown root "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/freeipmi.plugin"
753 run chmod 4755 "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/freeipmi.plugin"
757 run chown "${NETDATA_USER}:${NETDATA_USER}" "${NETDATA_LOG_DIR}"
758 run chown -R "${NETDATA_USER}:${NETDATA_USER}" "${NETDATA_PREFIX}/usr/libexec/netdata"
759 run find "${NETDATA_PREFIX}/usr/libexec/netdata" -type f -exec chmod 0755 {} \;
760 run find "${NETDATA_PREFIX}/usr/libexec/netdata" -type d -exec chmod 0755 {} \;
763 # --- fix #1292 bug ---
765 [ -d "${NETDATA_PREFIX}/usr/libexec" ] && run chmod a+rX "${NETDATA_PREFIX}/usr/libexec"
766 [ -d "${NETDATA_PREFIX}/usr/share/netdata" ] && run chmod a+rX "${NETDATA_PREFIX}/usr/share/netdata"
770 # -----------------------------------------------------------------------------
771 progress "Install netdata at system init"
774 install_non_systemd_init() {
775 [ "${UID}" != 0 ] && return 1
778 if [ -f /etc/os-release ]
780 source /etc/os-release || return 1
781 key="${ID}-${VERSION_ID}"
783 elif [ -f /etc/centos-release ]
785 key=$(</etc/centos-release)
788 if [ -d /etc/init.d -a ! -f /etc/init.d/netdata ]
790 if [ "${key}" = "gentoo" ]
792 run cp system/netdata-openrc /etc/init.d/netdata && \
793 run chmod 755 /etc/init.d/netdata && \
794 run rc-update add netdata default && \
797 elif [ "${key}" = "ubuntu-12.04" -o "${key}" = "ubuntu-14.04" -o "${key}" = "debian-7" ]
799 run cp system/netdata-lsb /etc/init.d/netdata && \
800 run chmod 755 /etc/init.d/netdata && \
801 run update-rc.d netdata defaults && \
802 run update-rc.d netdata enable && \
805 elif [ "${key}" = "CentOS release 6.8 (Final)" -o "${key}" = "amzn-2016.09" ]
807 run cp system/netdata-init-d /etc/init.d/netdata && \
808 run chmod 755 /etc/init.d/netdata && \
809 run chkconfig netdata on && \
817 if [ "${UID}" -eq 0 ]
822 # systemd is running on this system
824 if [ ! -f /etc/systemd/system/netdata.service ]
826 echo >&2 "Installing systemd service..."
827 run cp system/netdata.service /etc/systemd/system/netdata.service && \
828 run systemctl daemon-reload && \
829 run systemctl enable netdata
832 install_non_systemd_init
837 # -----------------------------------------------------------------------------
838 # check if we can re-start netdata
845 [ -z "$1" -o ! -f "/proc/$1/stat" ] && return 1
846 [ "$(cat "/proc/$1/stat" | cut -d '(' -f 2 | cut -d ')' -f 1)" = "netdata" ] && return 0
852 stop_netdata_on_pid() {
853 local pid="${1}" ret=0 count=0
855 isnetdata ${pid} || return 0
857 printf >&2 "Stopping netdata on pid ${pid} ..."
858 while [ ! -z "$pid" -a ${ret} -eq 0 ]
860 if [ ${count} -gt 45 ]
862 echo >&2 "Cannot stop the running netdata on pid ${pid}."
866 count=$(( count + 1 ))
868 run kill ${pid} 2>/dev/null
871 test ${ret} -eq 0 && printf >&2 "." && sleep 2
877 echo >&2 "SORRY! CANNOT STOP netdata ON PID ${pid} !"
881 echo >&2 "netdata on pid ${pid} stopped."
888 myns="$(readlink /proc/self/ns/pid 2>/dev/null)"
890 # echo >&2 "Stopping a (possibly) running netdata (namespace '${myns}')..."
892 for p in $(cat "${NETDATA_RUN_DIR}/netdata.pid" 2>/dev/null) \
893 $(cat /var/run/netdata.pid 2>/dev/null) \
894 $(cat /var/run/netdata/netdata.pid 2>/dev/null) \
895 $(pidof netdata 2>/dev/null)
897 ns="$(readlink /proc/${p}/ns/pid 2>/dev/null)"
899 if [ -z "${myns}" -o -z "${ns}" -o "${myns}" = "${ns}" ]
901 stop_netdata_on_pid ${p}
906 if [ ${DONOTSTART} -eq 1 ]
908 if [ ! -s "${NETDATA_PREFIX}/etc/netdata/netdata.conf" ]
910 echo "# Get config from http://127.0.0.1:${NETDATA_PORT}/netdata.conf" >"${NETDATA_PREFIX}/etc/netdata/netdata.conf"
912 if [ "${UID}" -eq 0 ]
914 chown "${NETDATA_USER}" "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
916 chmod 0644 "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
921 progress "Start netdata"
923 if [ "${UID}" -eq 0 ]
927 service netdata restart && started=1
928 if [ ${started} -eq 0 ]
930 service netdata start && started=1
934 if [ ${started} -eq 0 ]
936 # still not started...
940 echo >&2 "Starting netdata..."
941 run ${NETDATA_PREFIX}/usr/sbin/netdata -P ${NETDATA_RUN_DIR}/netdata.pid "${@}"
945 echo >&2 "SORRY! FAILED TO START NETDATA!"
948 echo >&2 "OK. NetData Started!"
954 # -----------------------------------------------------------------------------
955 # save a config file, if it is not already there
957 if [ ! -s "${NETDATA_PREFIX}/etc/netdata/netdata.conf" ]
960 echo >&2 "-------------------------------------------------------------------------------"
962 echo >&2 "Downloading default configuration from netdata..."
965 # remove a possibly obsolete download
966 [ -f "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" ] && rm "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new"
968 # disable a proxy to get data from the local netdata
973 wget 2>/dev/null -O "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" "http://localhost:${NETDATA_PORT}/netdata.conf"
977 if [ ${ret} -ne 0 -o ! -s "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" ]
979 curl -s -o "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" "http://localhost:${NETDATA_PORT}/netdata.conf"
983 if [ ${ret} -eq 0 -a -s "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" ]
985 mv "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
986 echo >&2 "New configuration saved for you to edit at ${NETDATA_PREFIX}/etc/netdata/netdata.conf"
988 if [ "${UID}" -eq 0 ]
990 chown "${NETDATA_USER}" "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
992 chmod 0664 "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
994 echo >&2 "Cannnot download configuration from netdata daemon using url 'http://localhost:${NETDATA_PORT}/netdata.conf'"
995 [ -f "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" ] && rm "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new"
1000 # -----------------------------------------------------------------------------
1001 progress "Check KSM (kernel memory deduper)"
1003 ksm_is_available_but_disabled() {
1006 ${TPUT_BOLD}Memory de-duplication instructions${TPUT_RESET}
1008 You have kernel memory de-duper (called Kernel Same-page Merging,
1009 or KSM) available, but it is not currently enabled.
1013 ${TPUT_YELLOW}${TPUT_BOLD}echo 1 >/sys/kernel/mm/ksm/run${TPUT_RESET}
1014 ${TPUT_YELLOW}${TPUT_BOLD}echo 1000 >/sys/kernel/mm/ksm/sleep_millisecs${TPUT_RESET}
1016 If you enable it, you will save 40-60% of netdata memory.
1021 ksm_is_not_available() {
1024 ${TPUT_BOLD}Memory de-duplication not present in your kernel${TPUT_RESET}
1026 It seems you do not have kernel memory de-duper (called Kernel Same-page
1027 Merging, or KSM) available.
1029 To enable it, you need a kernel built with CONFIG_KSM=y
1031 If you can have it, you will save 40-60% of netdata memory.
1036 if [ -f "/sys/kernel/mm/ksm/run" ]
1038 if [ $(cat "/sys/kernel/mm/ksm/run") != "1" ]
1040 ksm_is_available_but_disabled
1043 ksm_is_not_available
1046 # -----------------------------------------------------------------------------
1047 progress "Check version.txt"
1049 if [ ! -s web/version.txt ]
1053 ${TPUT_BOLD}Version update check warning${TPUT_RESET}
1055 The way you downloaded netdata, we cannot find its version. This means the
1056 Update check on the dashboard, will not work.
1058 If you want to have version update check, please re-install it
1059 following the procedure in:
1061 https://github.com/firehol/netdata/wiki/Installation
1066 # -----------------------------------------------------------------------------
1067 progress "Check apps.plugin"
1069 if [ "${UID}" -ne 0 ]
1071 cat <<SETUID_WARNING
1073 ${TPUT_BOLD}apps.plugin needs privileges${TPUT_RESET}
1075 Since you have installed netdata as a normal user, to have apps.plugin collect
1076 all the needed data, you have to give it the access rights it needs, by running
1077 either of the following sets of commands:
1079 To run apps.plugin with escalated capabilities:
1081 ${TPUT_YELLOW}${TPUT_BOLD}sudo chown root:${NETDATA_USER} \"${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin\"${TPUT_RESET}
1082 ${TPUT_YELLOW}${TPUT_BOLD}sudo chmod 0750 \"${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin\"${TPUT_RESET}
1083 ${TPUT_YELLOW}${TPUT_BOLD}sudo setcap cap_dac_read_search,cap_sys_ptrace+ep \"${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin\"${TPUT_RESET}
1085 or, to run apps.plugin as root:
1087 ${TPUT_YELLOW}${TPUT_BOLD}sudo chown root \"${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin\"${TPUT_RESET}
1088 ${TPUT_YELLOW}${TPUT_BOLD}sudo chmod 4755 \"${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin\"${TPUT_RESET}
1090 apps.plugin is performing a hard-coded function of data collection for all
1091 running processes. It cannot be instructed from the netdata daemon to perform
1092 any task, so it is pretty safe to do this.
1097 # -----------------------------------------------------------------------------
1098 progress "Generate netdata-uninstaller.sh"
1100 cat >netdata-uninstaller.sh <<UNINSTALL
1103 # this script will uninstall netdata
1105 if [ "\$1" != "--force" ]
1107 echo >&2 "This script will REMOVE netdata from your system."
1108 echo >&2 "Run it again with --force to do it."
1112 echo >&2 "Stopping a possibly running netdata..."
1113 for p in \$(pidof netdata); do kill \$p; done
1117 if [ ! -z "\$1" -a -d "\$1" ]
1120 echo "Deleting directory '\$1' ..."
1125 if [ ! -z "${NETDATA_PREFIX}" -a -d "${NETDATA_PREFIX}" ]
1127 # installation prefix was given
1129 deletedir "${NETDATA_PREFIX}"
1132 # installation prefix was NOT given
1134 if [ -f "${NETDATA_PREFIX}/usr/sbin/netdata" ]
1136 echo "Deleting ${NETDATA_PREFIX}/usr/sbin/netdata ..."
1137 rm -i "${NETDATA_PREFIX}/usr/sbin/netdata"
1140 deletedir "${NETDATA_PREFIX}/etc/netdata"
1141 deletedir "${NETDATA_PREFIX}/usr/share/netdata"
1142 deletedir "${NETDATA_PREFIX}/usr/libexec/netdata"
1143 deletedir "${NETDATA_PREFIX}/var/lib/netdata"
1144 deletedir "${NETDATA_PREFIX}/var/cache/netdata"
1145 deletedir "${NETDATA_PREFIX}/var/log/netdata"
1148 if [ -f /etc/logrotate.d/netdata ]
1150 echo "Deleting /etc/logrotate.d/netdata ..."
1151 rm -i /etc/logrotate.d/netdata
1154 if [ -f /etc/systemd/system/netdata.service ]
1156 echo "Deleting /etc/systemd/system/netdata.service ..."
1157 rm -i /etc/systemd/system/netdata.service
1160 if [ -f /etc/init.d/netdata ]
1162 echo "Deleting /etc/init.d/netdata ..."
1163 rm -i /etc/init.d/netdata
1166 getent passwd netdata > /dev/null
1170 echo "You may also want to remove the user netdata"
1172 echo " userdel netdata"
1175 getent group netdata > /dev/null
1179 echo "You may also want to remove the group netdata"
1181 echo " groupdel netdata"
1184 getent group docker > /dev/null
1185 if [ $? -eq 0 -a "${NETDATA_ADDED_TO_DOCKER}" = "1" ]
1188 echo "You may also want to remove the netdata user from the docker group"
1190 echo " gpasswd -d netdata docker"
1193 getent group nginx > /dev/null
1194 if [ $? -eq 0 -a "${NETDATA_ADDED_TO_NGINX}" = "1" ]
1197 echo "You may also want to remove the netdata user from the nginx group"
1199 echo " gpasswd -d netdata nginx"
1202 getent group varnish > /dev/null
1203 if [ $? -eq 0 -a "${NETDATA_ADDED_TO_VARNISH}" = "1" ]
1206 echo "You may also want to remove the netdata user from the varnish group"
1208 echo " gpasswd -d netdata varnish"
1211 getent group haproxy > /dev/null
1212 if [ $? -eq 0 -a "${NETDATA_ADDED_TO_HAPROXY}" = "1" ]
1215 echo "You may also want to remove the netdata user from the haproxy group"
1217 echo " gpasswd -d netdata haproxy"
1220 getent group adm > /dev/null
1221 if [ $? -eq 0 -a "${NETDATA_ADDED_TO_ADM}" = "1" ]
1224 echo "You may also want to remove the netdata user from the adm group"
1226 echo " gpasswd -d netdata adm"
1231 chmod 750 netdata-uninstaller.sh
1233 # -----------------------------------------------------------------------------
1234 progress "Basic netdata instructions"
1238 netdata by default listens on all IPs on port ${NETDATA_PORT},
1239 so you can access it with:
1241 ${TPUT_CYAN}${TPUT_BOLD}http://this.machine.ip:${NETDATA_PORT}/${TPUT_RESET}
1243 To stop netdata, just kill it, with:
1245 ${TPUT_YELLOW}${TPUT_BOLD}killall netdata${TPUT_RESET}
1247 To start it, just run it:
1249 ${TPUT_YELLOW}${TPUT_BOLD}${NETDATA_PREFIX}/usr/sbin/netdata${TPUT_RESET}
1253 echo >&2 "Uninstall script generated: ${TPUT_RED}${TPUT_BOLD}./netdata-uninstaller.sh${TPUT_RESET}"
1257 cat >netdata-updater.sh.new <<REINSTALL
1261 [ "\${1}" = "-f" ] && force=1
1263 export PATH="\${PATH}:${PATH}"
1264 export CFLAGS="${CFLAGS}"
1265 export NETDATA_CONFIGURE_OPTIONS="${NETDATA_CONFIGURE_OPTIONS}"
1267 INSTALL_UID="${UID}"
1268 if [ "\${INSTALL_UID}" != "\${UID}" ]
1270 echo >&2 "This script should be run as user with uid \${INSTALL_UID} but it now runs with uid \${UID}"
1274 # make sure we cd to the working directory
1275 cd "${REINSTALL_PWD}" || exit 1
1277 # make sure there is .git here
1278 [ \${force} -eq 0 -a ! -d .git ] && echo >&2 "No git structures found at: ${REINSTALL_PWD} (use -f for force re-install)" && exit 1
1280 # signal netdata to start saving its database
1281 # this is handy if your database is big
1282 pids=\$(pidof netdata)
1283 [ ! -z "\${pids}" ] && kill -USR1 \${pids}
1288 # we are running on a terminal
1289 # open fd 3 and send it to stderr
1293 # create a temporary file for the log
1294 tmp=\$(mktemp /tmp/netdata-updater-log-XXXXXX.log)
1295 # open fd 3 and send it to tmp
1300 echo >&3 "\$(date) : INFO: " "\${@}"
1308 echo >&3 "\$(date) : ERROR: " "\${@}"
1311 # this is what we will do if it fails (head-less only)
1313 error "FAILED TO UPDATE NETDATA : \${1}"
1315 if [ ! -z "\${tmp}" ]
1323 get_latest_commit_id() {
1325 grep ^commit 2>&3 |\\
1327 cut -d ' ' -f 2 2>&3
1331 [ -z "\${tmp}" ] && info "Running on a terminal - (this script also supports running headless from crontab)"
1337 info "Updating netdata source from github..."
1339 last_commit="\$(get_latest_commit_id)"
1340 [ \${force} -eq 0 -a -z "\${last_commit}" ] && failed "CANNOT GET LAST COMMIT ID (use -f for force re-install)"
1342 git pull >&3 2>&3 || failed "CANNOT FETCH LATEST SOURCE (use -f for force re-install)"
1344 new_commit="\$(get_latest_commit_id)"
1345 if [ \${force} -eq 0 ]
1347 [ -z "\${new_commit}" ] && failed "CANNOT GET NEW LAST COMMIT ID (use -f for force re-install)"
1348 [ "\${new_commit}" = "\${last_commit}" ] && info "Nothing to be done! (use -f to force re-install)" && exit 0
1350 elif [ \${force} -eq 0 ]
1352 failed "CANNOT FIND GIT STRUCTURES IN \$(pwd) (use -f for force re-install)"
1356 info "Re-installing netdata..."
1357 ${REINSTALL_COMMAND// --dont-wait/} --dont-wait >&3 2>&3 || failed "FAILED TO COMPILE/INSTALL NETDATA"
1359 [ ! -z "\${tmp}" ] && rm "\${tmp}" && tmp=
1363 # the installer updates this script - so we run and exit in a single line
1365 ###############################################################################
1366 ###############################################################################
1368 chmod 755 netdata-updater.sh.new
1369 mv -f netdata-updater.sh.new netdata-updater.sh
1370 echo >&2 "Update script generated : ${TPUT_GREEN}${TPUT_BOLD}./netdata-updater.sh${TPUT_RESET}"
1372 echo >&2 "${TPUT_DIM}${TPUT_BOLD}netdata-updater.sh${TPUT_RESET}${TPUT_DIM} can work from cron. It will trigger an email from cron"
1373 echo >&2 "only if it fails (it does not print anything if it can update netdata).${TPUT_RESET}"
1374 if [ "${UID}" -eq 0 -a -d "/etc/cron.daily" -a ! -f "/etc/cron.daily/netdata-updater.sh" ]
1376 echo >&2 "${TPUT_DIM}Run this to automatically check and install netdata updates once per day:${TPUT_RESET}"
1378 echo >&2 "${TPUT_YELLOW}${TPUT_BOLD}ln -s ${PWD}/netdata-updater.sh /etc/cron.daily/netdata-updater.sh${TPUT_RESET}"
1380 elif [ -f "netdata-updater.sh" ]
1382 rm "netdata-updater.sh"
1385 # -----------------------------------------------------------------------------
1387 progress "We are done!"
1389 if [ ${started} -eq 1 ]
1391 netdata_banner "is installed and running now!"
1393 netdata_banner "is installed now!"
1396 echo >&2 " enjoy real-time performance and health monitoring..."