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}'."
13 # reload the user profile
14 [ -f /etc/profile ] && . /etc/profile
16 # make sure /etc/profile does not change our current directory
17 cd "${netdata_source_dir}" || exit 1
19 # fix PKG_CHECK_MODULES error
20 if [ -d /usr/share/aclocal ]
22 ACLOCAL_PATH=${ACLOCAL_PATH-/usr/share/aclocal}
29 # Be nice on production environments
30 renice 19 $$ >/dev/null 2>/dev/null
32 processors=$(grep ^processor </proc/cpuinfo | wc -l)
33 [ $(( processors )) -lt 1 ] && processors=1
35 # you can set CFLAGS before running installer
36 CFLAGS="${CFLAGS--O2}"
37 [ "z${CFLAGS}" = "z-O3" ] && CFLAGS="-O2"
39 # keep a log of this command
40 printf "\n# " >>netdata-installer.log
41 date >>netdata-installer.log
42 printf "CFLAGS=\"%s\" " "${CFLAGS}" >>netdata-installer.log
43 printf "%q " "$0" "${@}" >>netdata-installer.log
44 printf "\n" >>netdata-installer.log
46 REINSTALL_PWD="${PWD}"
47 REINSTALL_COMMAND="$(printf "%q " "$0" "${@}"; printf "\n")"
51 l2=" |.-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-" \
52 l3=" | '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' " \
53 l4=" +----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+--->" \
55 netdata="netdata" start end msg="${*}"
57 [ ${#msg} -lt ${#netdata} ] && msg="${msg}${sp:0:$(( ${#netdata} - ${#msg}))}"
58 [ ${#msg} -gt $(( ${#l2} - 20 )) ] && msg="${msg:0:$(( ${#l2} - 23 ))}..."
60 start="$(( ${#l2} / 2 - 4 ))"
61 [ $(( start + ${#msg} + 4 )) -gt ${#l2} ] && start=$((${#l2} - ${#msg} - 4))
62 end=$(( ${start} + ${#msg} + 4 ))
66 echo >&2 "${l2:0:start}${sp:0:2}${netdata}${sp:0:$((end - start - 2 - ${#netdata}))}${l2:end:$((${#l2} - end))}"
67 echo >&2 "${l3:0:start}${sp:0:2}${msg}${sp:0:2}${l3:end:$((${#l2} - end))}"
72 service="$(which service 2>/dev/null || command -v service 2>/dev/null)"
73 systemctl="$(which systemctl 2>/dev/null || command -v systemctl 2>/dev/null)"
75 local cmd="${1}" action="${2}"
77 if [ ! -z "${service}" ]
79 run "${service}" "${cmd}" "${action}"
81 elif [ ! -z "${systemctl}" ]
83 run "${systemctl}" "${action}" "${cmd}"
96 banner "installer command line options"
99 ${ME} <installer options>
101 Valid <installer options> are:
103 --install /PATH/TO/INSTALL
105 If your give: --install /opt
106 netdata will be installed in /opt/netdata
110 Do not (re)start netdata.
115 Do not wait for the user to press ENTER.
116 Start immediately building it.
118 --zlib-is-really-here
119 --libs-are-really-here
121 If you get errors about missing zlib,
122 or libuuid but you know it is available,
123 you have a broken pkg-config.
124 Use this option to allow it continue
125 without checking pkg-config.
127 Netdata will by default be compiled with gcc optimization -O2
128 If you need to pass different CFLAGS, use something like this:
130 CFLAGS="<gcc options>" ${ME} <installer options>
132 For the installer to complete successfully, you will need
133 these packages installed:
135 gcc make autoconf automake pkg-config zlib1g-dev (or zlib-devel)
136 uuid-dev (or libuuid-devel)
138 For the plugins, you will at least need:
145 md5sum="$(which md5sum 2>/dev/null || command -v md5sum 2>/dev/null)"
146 get_git_config_signatures() {
149 [ ! -d "conf.d" ] && echo >&2 "Wrong directory." && return 1
150 [ -z "${md5sum}" -o ! -x "${md5sum}" ] && echo >&2 "No md5sum command." && return 1
152 echo >configs.signatures.tmp
154 for x in $(find conf.d -name \*.conf)
158 for c in $(git log --follow "conf.d/${x}" | grep ^commit | cut -d ' ' -f 2)
160 git checkout ${c} "conf.d/${x}" || continue
161 s="$(cat "conf.d/${x}" | md5sum | cut -d ' ' -f 1)"
162 echo >>configs.signatures.tmp "${s}:${x}"
165 git checkout HEAD "conf.d/${x}" || break
168 cat configs.signatures.tmp |\
172 echo "declare -A configs_signatures=("
176 echo " ['${md5}']='${file}'"
179 } >configs.signatures
181 rm configs.signatures.tmp
187 while [ ! -z "${1}" ]
189 if [ "$1" = "--install" ]
191 NETDATA_PREFIX="${2}/netdata"
193 elif [ "$1" = "--zlib-is-really-here" -o "$1" = "--libs-are-really-here" ]
197 elif [ "$1" = "--dont-start-it" ]
201 elif [ "$1" = "--dont-wait" ]
205 elif [ "$1" = "--help" -o "$1" = "-h" ]
209 elif [ "$1" = "get_git_config_signatures" ]
211 get_git_config_signatures && exit 0
216 echo >&2 "I cannot understand option '$1'."
222 banner "real-time performance monitoring, done right!"
225 You are about to build and install netdata to your system.
227 It will be installed at these locations:
229 - the daemon at ${NETDATA_PREFIX}/usr/sbin/netdata
230 - config files at ${NETDATA_PREFIX}/etc/netdata
231 - web files at ${NETDATA_PREFIX}/usr/share/netdata
232 - plugins at ${NETDATA_PREFIX}/usr/libexec/netdata
233 - cache files at ${NETDATA_PREFIX}/var/cache/netdata
234 - db files at ${NETDATA_PREFIX}/var/lib/netdata
235 - log files at ${NETDATA_PREFIX}/var/log/netdata
236 - pid file at ${NETDATA_PREFIX}/var/run
238 This installer allows you to change the installation path.
239 Press Control-C and run the same command with --help for help.
243 if [ "${UID}" -ne 0 ]
245 if [ -z "${NETDATA_PREFIX}" ]
247 banner "wrong command line options!"
248 cat <<NONROOTNOPREFIX
250 Sorry! This will fail!
252 You are attempting to install netdata as non-root, but you plan to install it
255 Please set an installation prefix, like this:
257 $0 ${@} --install /tmp
259 or, run the installer as root:
263 We suggest to install it as root, or certain data collectors will not be able
264 to work. Netdata drops root privileges when running. So, if you plan to keep
265 it, install it as root to get the full functionality.
274 You are about to install netdata as a non-root user.
275 Netdata will work, but a few data collection modules that
276 require root access will fail.
278 If you installing permanently on your system, run the
288 if [ "$(type autoreconf 2> /dev/null)" ]
296 set -- $(autoreconf -V | sed -ne '1s/.* \([^ ]*\)$/\1/p')
297 eval $maj=\$1 $min=\$2
299 autoconf_maj_min AMAJ AMIN
304 elif [ "$AMAJ" -eq 2 -a "$AMIN" -ge 60 ]
308 echo "Found autotools $AMAJ.$AMIN"
311 echo "No autotools found"
314 if [ ! "$have_autotools" ]
318 echo "Will skip autoreconf step"
320 banner "autotools v2.60 required"
323 -------------------------------------------------------------------------------
324 autotools 2.60 or later is required
326 Sorry, you do not seem to have autotools 2.60 or later, which is
327 required to build from the git sources of netdata.
329 You can either install a suitable version of autotools and automake
330 or download a netdata package which does not have these dependencies.
332 Source packages where autotools have already been run are available
334 https://firehol.org/download/netdata/
336 The unsigned/master folder tracks the head of the git tree and released
337 packages are also available.
343 if [ ${DONOTWAIT} -eq 0 ]
345 if [ ! -z "${NETDATA_PREFIX}" ]
347 read -p "Press ENTER to build and install netdata to '${NETDATA_PREFIX}' > "
349 read -p "Press ENTER to build and install netdata to your system > "
354 banner "sorry, it failed to build..."
357 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
359 Sorry! netdata failed to build...
361 You many need to check these:
363 1. The package uuid-dev (or libuuid-devel) has to be installed.
365 If your system cannot find libuuid, although it is installed
366 run me with the option: --libs-are-really-here
368 2. The package zlib1g-dev (or zlib-devel) has to be installed.
370 If your system cannot find zlib, although it is installed
371 run me with the option: --libs-are-really-here
373 3. You need basic build tools installed, like:
375 gcc make autoconf automake pkg-config
377 Autoconf version 2.60 or higher is required.
379 If you still cannot get it to build, ask for help at github:
381 https://github.com/firehol/netdata/issues
390 printf >>netdata-installer.log "# "
391 printf >>netdata-installer.log "%q " "${@}"
392 printf >>netdata-installer.log " ... "
395 printf >&2 ":-----------------------------------------------------------------------------\n"
396 printf >&2 "Running command (in $(pwd)):\n"
398 printf >&2 "%q " "${@}"
406 printf >>netdata-installer.log "FAILED!\n"
408 printf >>netdata-installer.log "OK\n"
414 if [ ${LIBS_ARE_HERE} -eq 1 ]
417 echo >&2 "ok, assuming libs are really installed."
418 export ZLIB_CFLAGS=" "
419 export ZLIB_LIBS="-lz"
420 export UUID_CFLAGS=" "
421 export UUID_LIBS="-luuid"
424 trap build_error EXIT
426 if [ "$have_autotools" ]
428 run ./autogen.sh || exit 1
432 --prefix="${NETDATA_PREFIX}/usr" \
433 --sysconfdir="${NETDATA_PREFIX}/etc" \
434 --localstatedir="${NETDATA_PREFIX}/var" \
435 --with-zlib --with-math --with-user=netdata \
436 CFLAGS="${CFLAGS}" || exit 1
438 # remove the build_error hook
441 if [ -f src/netdata ]
443 echo >&2 "Cleaning a possibly old compilation ..."
447 echo >&2 "Compiling netdata ..."
448 run make -j${processors} || exit 1
450 if [ "${BASH_VERSINFO[0]}" -ge "4" ]
452 declare -A configs_signatures=()
453 if [ -f "configs.signatures" ]
455 source "configs.signatures" || echo >&2 "ERROR: Failed to load configs.signatures !"
459 # migrate existing configuration files
460 # for node.d and charts.d
461 if [ -d "${NETDATA_PREFIX}/etc/netdata" ]
463 # the configuration directory exists
465 if [ ! -d "${NETDATA_PREFIX}/etc/netdata/charts.d" ]
467 run mkdir "${NETDATA_PREFIX}/etc/netdata/charts.d"
470 # move the charts.d config files
471 for x in apache ap cpu_apps cpufreq example exim hddtemp load_average mem_apps mysql nginx nut opensips phpfpm postfix sensors squid tomcat
473 for y in "" ".old" ".orig"
475 if [ -f "${NETDATA_PREFIX}/etc/netdata/${x}.conf${y}" -a ! -f "${NETDATA_PREFIX}/etc/netdata/charts.d/${x}.conf${y}" ]
477 run mv -f "${NETDATA_PREFIX}/etc/netdata/${x}.conf${y}" "${NETDATA_PREFIX}/etc/netdata/charts.d/${x}.conf${y}"
482 if [ ! -d "${NETDATA_PREFIX}/etc/netdata/node.d" ]
484 run mkdir "${NETDATA_PREFIX}/etc/netdata/node.d"
487 # move the node.d config files
488 for x in named sma_webbox snmp
490 for y in "" ".old" ".orig"
492 if [ -f "${NETDATA_PREFIX}/etc/netdata/${x}.conf${y}" -a ! -f "${NETDATA_PREFIX}/etc/netdata/node.d/${x}.conf${y}" ]
494 run mv -f "${NETDATA_PREFIX}/etc/netdata/${x}.conf${y}" "${NETDATA_PREFIX}/etc/netdata/node.d/${x}.conf${y}"
500 # backup user configurations
501 installer_backup_suffix="${PID}.${RANDOM}"
502 for x in $(find "${NETDATA_PREFIX}/etc/netdata/" -name '*.conf' -type f)
506 # make a backup of the configuration file
507 cp -p "${x}" "${x}.old"
509 if [ -z "${md5sum}" -o ! -x "${md5sum}" ]
511 # we don't have md5sum - keep it
512 cp -p "${x}" "${x}.installer_backup.${installer_backup_suffix}"
514 # find it relative filename
515 f="${x/*\/etc\/netdata\//}"
518 md5="$(cat "${x}" | ${md5sum} | cut -d ' ' -f 1)"
521 if [ -f "conf.d/${f}" ]
523 cp "conf.d/${f}" "${x}.orig"
526 if [ "${BASH_VERSINFO[0]}" -ge "4" ]
528 if [ "${configs_signatures[${md5}]}" = "${f}" ]
530 # it is a stock version - don't keep it
531 echo >&2 "File '${x}' is stock version."
533 # edited by user - keep it
534 echo >&2 "File '${x}' has been edited by user."
535 cp -p "${x}" "${x}.installer_backup.${installer_backup_suffix}"
538 echo >&2 "File '${x}' cannot be check for custom edits."
539 cp -p "${x}" "${x}.installer_backup.${installer_backup_suffix}"
543 elif [ -f "${x}.installer_backup.${installer_backup_suffix}" ]
545 rm -f "${x}.installer_backup.${installer_backup_suffix}"
549 echo >&2 "Installing netdata ..."
550 run make install || exit 1
552 # restore user configurations
553 for x in $(find "${NETDATA_PREFIX}/etc/netdata/" -name '*.conf' -type f)
555 if [ -f "${x}.installer_backup.${installer_backup_suffix}" ]
557 cp -p "${x}.installer_backup.${installer_backup_suffix}" "${x}"
558 rm -f "${x}.installer_backup.${installer_backup_suffix}"
562 echo >&2 "Fixing permissions ..."
565 which "${1}" >/dev/null 2>&1 && return 0
566 command -v "${1}" >/dev/null 2>&1 && return 0
570 portable_add_user() {
571 local username="${1}"
573 getent passwd "${username}" > /dev/null 2>&1
574 [ $? -eq 0 ] && return 0
576 echo >&2 "Adding ${username} user account ..."
578 local nologin="$(which nologin 2>/dev/null || command -v nologin 2>/dev/null || echo '/bin/false')"
583 run useradd -r -g "${username}" -c "${username}" -s "${nologin}" -d / "${username}" && return 0
589 run pw useradd "${username}" -d / -g "${username}" -s "${nologin}" && return 0
595 run adduser -D -G "${username}" "${username}" && return 0
598 echo >&2 "Failed to add ${username} user account !"
603 portable_add_group() {
604 local groupname="${1}"
606 getent group "${groupname}" > /dev/null 2>&1
607 [ $? -eq 0 ] && return 0
609 echo >&2 "Adding ${groupname} user group ..."
612 if check_cmd groupadd
614 run groupadd -r "${groupname}" && return 0
620 run pw groupadd "${groupname}" && return 0
624 if check_cmd addgroup
626 run addgroup "${groupname}" && return 0
629 echo >&2 "Failed to add ${groupname} user group !"
633 portable_add_user_to_group() {
634 local groupname="${1}" username="${2}"
636 getent group "${groupname}" > /dev/null 2>&1
637 [ $? -ne 0 ] && return 1
639 # find the user is already in the group
640 local users=$(getent group "${groupname}" | cut -d ':' -f 4)
641 if [[ ",${users}," =~ ,${username}, ]]
643 # username is already there
646 # username is not in group
647 echo >&2 "Adding ${username} user to the ${groupname} group ..."
652 run usermod -a -G "${groupname}" "${username}" && return 0
658 run pw groupmod "${groupname}" -m "${username}" && return 0
662 if check_cmd addgroup
664 run addgroup "${username}" "${groupname}" && return 0
667 echo >&2 "Failed to add user ${username} to group ${groupname} !"
673 # man systemd-detect-virt
674 local cmd=$(which systemd-detect-virt 2>/dev/null || command -v systemd-detect-virt 2>/dev/null)
675 if [ ! -z "${cmd}" -a -x "${cmd}" ]
677 "${cmd}" --container >/dev/null 2>&1 && return 0
680 # /proc/1/sched exposes the host's pid of our init !
681 # http://stackoverflow.com/a/37016302
682 local pid=$( cat /proc/1/sched | head -n 1 | { IFS='(),#:' read name pid th threads; echo $pid; } )
683 local p=$(( pid + 0 ))
684 [ ${pid} -ne 1 ] && return 0
686 # lxc sets environment variable 'container'
687 [ ! -z "${container}" ] && return 0
689 # docker creates /.dockerenv
690 # http://stackoverflow.com/a/25518345
691 [ -f "/.dockerenv" ] && return 0
693 # ubuntu and debian supply /bin/running-in-container
694 # https://www.apt-browse.org/browse/ubuntu/trusty/main/i386/upstart/1.12.1-0ubuntu4/file/bin/running-in-container
695 if [ -x "/bin/running-in-container" ]
697 "/bin/running-in-container" >/dev/null 2>&1 && return 0
703 run find ./system/ -type f -a \! -name \*.in -a \! -name Makefile\* -a \! -name \*.conf -a \! -name \*.service -a \! -name \*.logrotate -exec chmod 755 {} \;
705 NETDATA_ADDED_TO_DOCKER=0
706 NETDATA_ADDED_TO_NGINX=0
707 NETDATA_ADDED_TO_VARNISH=0
708 NETDATA_ADDED_TO_HAPROXY=0
709 NETDATA_ADDED_TO_ADM=0
712 portable_add_group netdata
713 portable_add_user netdata
714 portable_add_user_to_group docker netdata && NETDATA_ADDED_TO_DOCKER=1
715 portable_add_user_to_group nginx netdata && NETDATA_ADDED_TO_NGINX=1
716 portable_add_user_to_group varnish netdata && NETDATA_ADDED_TO_VARNISH=1
717 portable_add_user_to_group haproxy netdata && NETDATA_ADDED_TO_HAPROXY=1
718 portable_add_user_to_group adm netdata && NETDATA_ADDED_TO_ADM=1
720 if [ -d /etc/logrotate.d -a ! -f /etc/logrotate.d/netdata ]
722 echo >&2 "Adding netdata logrotate configuration ..."
723 run cp system/netdata.logrotate /etc/logrotate.d/netdata
726 if [ -f /etc/logrotate.d/netdata ]
728 echo >&2 "Fixing netdata logrotate permissions ..."
729 run chmod 644 /etc/logrotate.d/netdata
734 # -----------------------------------------------------------------------------
735 # load options from the configuration file
737 # create an empty config if it does not exist
738 [ ! -f "${NETDATA_PREFIX}/etc/netdata/netdata.conf" ] && touch "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
740 # function to extract values from the config file
742 local key="${1}" value="${2}" line=
744 if [ -s "${NETDATA_PREFIX}/etc/netdata/netdata.conf" ]
746 line="$( grep "^[[:space:]]*${key}[[:space:]]*=[[:space:]]*" "${NETDATA_PREFIX}/etc/netdata/netdata.conf" | head -n 1 )"
747 [ ! -z "${line}" ] && value="$( echo "${line}" | cut -d '=' -f 2 | sed -e "s/^[[:space:]]\+//g" -e "s/[[:space:]]\+$//g" )"
753 # the user netdata will run as
754 if [ "${UID}" = "0" ]
756 NETDATA_USER="$( config_option "run as user" "netdata" )"
758 NETDATA_USER="${USER}"
761 # the owners of the web files
762 NETDATA_WEB_USER="$( config_option "web files owner" "${NETDATA_USER}" )"
763 NETDATA_WEB_GROUP="$( config_option "web files group" "${NETDATA_WEB_USER}" )"
766 NETDATA_DEBUG="$( config_option "debug flags" 0 )"
770 NETDATA_PORT="$( config_option "default port" ${defport} )"
771 NETDATA_PORT2="$( config_option "port" ${defport} )"
773 if [ "${NETDATA_PORT}" != "${NETDATA_PORT2}" ]
775 if [ "${NETDATA_PORT2}" != "${defport}" ]
777 NETDATA_PORT="${NETDATA_PORT2}"
782 NETDATA_LIB_DIR="$( config_option "lib directory" "${NETDATA_PREFIX}/var/lib/netdata" )"
783 NETDATA_CACHE_DIR="$( config_option "cache directory" "${NETDATA_PREFIX}/var/cache/netdata" )"
784 NETDATA_WEB_DIR="$( config_option "web files directory" "${NETDATA_PREFIX}/usr/share/netdata/web" )"
785 NETDATA_LOG_DIR="$( config_option "log directory" "${NETDATA_PREFIX}/var/log/netdata" )"
786 NETDATA_CONF_DIR="$( config_option "config directory" "${NETDATA_PREFIX}/etc/netdata" )"
787 NETDATA_RUN_DIR="${NETDATA_PREFIX}/var/run"
790 # -----------------------------------------------------------------------------
791 # prepare the directories
793 # this is needed if NETDATA_PREFIX is not empty
794 if [ ! -d "${NETDATA_RUN_DIR}" ]
796 mkdir -p "${NETDATA_RUN_DIR}" || exit 1
800 echo >&2 "Fixing directories (user: ${NETDATA_USER})..."
804 for x in "python.d" "charts.d" "node.d"
806 if [ ! -d "${NETDATA_CONF_DIR}/${x}" ]
808 echo >&2 "Creating directory '${NETDATA_CONF_DIR}/${x}'"
809 run mkdir -p "${NETDATA_CONF_DIR}/${x}" || exit 1
812 run chown -R "${NETDATA_USER}:${NETDATA_USER}" "${NETDATA_CONF_DIR}"
813 run find "${NETDATA_CONF_DIR}" -type f -exec chmod 0660 {} \;
814 run find "${NETDATA_CONF_DIR}" -type d -exec chmod 0775 {} \;
818 if [ ! -d "${NETDATA_WEB_DIR}" ]
820 echo >&2 "Creating directory '${NETDATA_WEB_DIR}'"
821 run mkdir -p "${NETDATA_WEB_DIR}" || exit 1
823 run chown -R "${NETDATA_WEB_USER}:${NETDATA_WEB_GROUP}" "${NETDATA_WEB_DIR}"
824 run find "${NETDATA_WEB_DIR}" -type f -exec chmod 0664 {} \;
825 run find "${NETDATA_WEB_DIR}" -type d -exec chmod 0775 {} \;
829 for x in "${NETDATA_LIB_DIR}" "${NETDATA_CACHE_DIR}" "${NETDATA_LOG_DIR}"
833 echo >&2 "Creating directory '${x}'"
834 run mkdir -p "${x}" || exit 1
837 run chown -R "${NETDATA_USER}:${NETDATA_USER}" "${x}"
838 #run find "${x}" -type f -exec chmod 0660 {} \;
839 #run find "${x}" -type d -exec chmod 0770 {} \;
842 run chmod 755 "${NETDATA_LOG_DIR}"
848 run chown "${NETDATA_USER}:root" "${NETDATA_LOG_DIR}"
849 run chown -R root "${NETDATA_PREFIX}/usr/libexec/netdata"
850 run find "${NETDATA_PREFIX}/usr/libexec/netdata" -type d -exec chmod 0755 {} \;
851 run find "${NETDATA_PREFIX}/usr/libexec/netdata" -type f -exec chmod 0644 {} \;
852 run find "${NETDATA_PREFIX}/usr/libexec/netdata" -type f -a -name \*.plugin -exec chmod 0755 {} \;
853 run find "${NETDATA_PREFIX}/usr/libexec/netdata" -type f -a -name \*.sh -exec chmod 0755 {} \;
858 run setcap cap_dac_read_search,cap_sys_ptrace+ep "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin"
861 if [ ${setcap_ret} -eq 0 ]
863 # if we managed to setcap
864 # but we fail to execute apps.plugin
865 # trigger setuid to root
866 "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin" -v >/dev/null 2>&1
871 if [ ${setcap_ret} -ne 0 ]
873 # fix apps.plugin to be setuid to root
874 run chown root "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin"
875 run chmod 4755 "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin"
878 run chown "${NETDATA_USER}:${NETDATA_USER}" "${NETDATA_LOG_DIR}"
879 run chown -R "${NETDATA_USER}:${NETDATA_USER}" "${NETDATA_PREFIX}/usr/libexec/netdata"
880 run find "${NETDATA_PREFIX}/usr/libexec/netdata" -type f -exec chmod 0755 {} \;
881 run find "${NETDATA_PREFIX}/usr/libexec/netdata" -type d -exec chmod 0755 {} \;
884 # --- fix #1292 bug ---
886 [ -d "${NETDATA_PREFIX}/usr/libexec" ] && run chmod a+rX "${NETDATA_PREFIX}/usr/libexec"
887 [ -d "${NETDATA_PREFIX}/usr/share/netdata" ] && run chmod a+rX "${NETDATA_PREFIX}/usr/share/netdata"
890 # -----------------------------------------------------------------------------
891 # check if we can re-start netdata
893 if [ ${DONOTSTART} -eq 1 ]
895 if [ ! -s "${NETDATA_PREFIX}/etc/netdata/netdata.conf" ]
897 echo >&2 "Generating empty config file in: ${NETDATA_PREFIX}/etc/netdata/netdata.conf"
898 echo "# Get config from http://127.0.0.1:${NETDATA_PORT}/netdata.conf" >"${NETDATA_PREFIX}/etc/netdata/netdata.conf"
900 if [ "${UID}" -eq 0 ]
902 chown "${NETDATA_USER}" "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
904 chmod 0644 "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
906 banner "is installed now!"
907 echo >&2 " enjoy real-time performance and health monitoring..."
911 # -----------------------------------------------------------------------------
912 # stop a running netdata
917 [ -z "$1" -o ! -f "/proc/$1/stat" ] && return 1
918 [ "$(cat "/proc/$1/stat" | cut -d '(' -f 2 | cut -d ')' -f 1)" = "netdata" ] && return 0
924 stop_netdata_on_pid() {
925 local pid="${1}" ret=0 count=0
927 isnetdata ${pid} || return 0
929 printf >&2 "Stopping netdata on pid ${pid} ..."
930 while [ ! -z "$pid" -a ${ret} -eq 0 ]
932 if [ ${count} -gt 45 ]
934 echo >&2 "Cannot stop the running netdata on pid ${pid}."
938 count=$(( count + 1 ))
940 run kill ${pid} 2>/dev/null
943 test ${ret} -eq 0 && printf >&2 "." && sleep 2
949 echo >&2 "SORRY! CANNOT STOP netdata ON PID ${pid} !"
953 echo >&2 "netdata on pid ${pid} stopped."
960 myns="$(readlink /proc/self/ns/pid 2>/dev/null)"
962 echo >&2 "Stopping a (possibly) running netdata..."
964 for p in $(cat "${NETDATA_RUN_DIR}/netdata.pid" 2>/dev/null) \
965 $(cat /var/run/netdata.pid 2>/dev/null) \
966 $(cat /var/run/netdata/netdata.pid 2>/dev/null) \
967 $(pidof netdata 2>/dev/null)
969 ns="$(readlink /proc/${p}/ns/pid 2>/dev/null)"
971 if [ -z "${myns}" -o -z "${ns}" -o "${myns}" = "${ns}" ]
973 stop_netdata_on_pid ${p}
978 # -----------------------------------------------------------------------------
982 local pids p myns ns systemctl
984 # if the directory /etc/systemd/system does not exit, it is not systemd
985 [ ! -d /etc/systemd/system ] && return 1
987 # if there is no systemctl command, it is not systemd
988 systemctl=$(which systemctl 2>/dev/null || command -v systemctl 2>/dev/null)
989 [ -z "${systemctl}" -o ! -x "${systemctl}" ] && return 1
991 # if pid 1 is systemd, it is systemd
992 [ "$(basename $(readlink /proc/1/exe) 2>/dev/null)" = "systemd" ] && return 0
994 # if systemd is not running, it is not systemd
995 pids=$(pidof systemd 2>/dev/null)
996 [ -z "${pids}" ] && return 1
998 # check if the running systemd processes are not in our namespace
999 myns="$(readlink /proc/self/ns/pid 2>/dev/null)"
1002 ns="$(readlink /proc/${p}/ns/pid 2>/dev/null)"
1004 # if pid of systemd is in our namespace, it is systemd
1005 [ ! -z "${myns}" && "${myns}" = "${ns}" ] && return 0
1008 # else, it is not systemd
1013 install_non_systemd_init() {
1014 [ "${UID}" != 0 ] && return 1
1017 if [ -f /etc/os-release ]
1019 source /etc/os-release || return 1
1020 key="${ID}-${VERSION_ID}"
1022 elif [ -f /etc/centos-release ]
1024 key=$(</etc/centos-release)
1027 if [ -d /etc/init.d -a ! -f /etc/init.d/netdata ]
1029 if [ "${key}" = "gentoo" ]
1031 run cp system/netdata-openrc /etc/init.d/netdata && \
1032 run chmod 755 /etc/init.d/netdata && \
1033 run rc-update add netdata default && \
1036 elif [ "${key}" = "ubuntu-12.04" -o "${key}" = "ubuntu-14.04" -o "${key}" = "debian-7" ]
1038 run cp system/netdata-lsb /etc/init.d/netdata && \
1039 run chmod 755 /etc/init.d/netdata && \
1040 run update-rc.d netdata defaults && \
1041 run update-rc.d netdata enable && \
1044 elif [ "${key}" = "CentOS release 6.8 (Final)" -o "${key}" = "amzn-2016.09" ]
1046 run cp system/netdata-init-d /etc/init.d/netdata && \
1047 run chmod 755 /etc/init.d/netdata && \
1048 run chkconfig netdata on && \
1057 if [ "${UID}" -eq 0 ]
1062 # systemd is running on this system
1064 if [ ! -f /etc/systemd/system/netdata.service ]
1066 echo >&2 "Installing systemd service..."
1067 run cp system/netdata.service /etc/systemd/system/netdata.service && \
1068 run systemctl daemon-reload && \
1069 run systemctl enable netdata
1071 service netdata stop
1075 service netdata restart && started=1
1077 install_non_systemd_init
1080 if [ ${started} -eq 0 ]
1082 # check if we can use the system service
1083 service netdata stop
1085 service netdata restart && started=1
1086 if [ ${started} -eq 0 ]
1088 service netdata start && started=1
1093 if [ ${started} -eq 0 ]
1095 # still not started...
1099 echo >&2 "Starting netdata..."
1100 run ${NETDATA_PREFIX}/usr/sbin/netdata -P ${NETDATA_RUN_DIR}/netdata.pid "${@}"
1104 echo >&2 "SORRY! FAILED TO START NETDATA!"
1107 echo >&2 "OK. NetData Started!"
1113 # -----------------------------------------------------------------------------
1114 # save a config file, if it is not already there
1116 if [ ! -s "${NETDATA_PREFIX}/etc/netdata/netdata.conf" ]
1119 echo >&2 "-------------------------------------------------------------------------------"
1121 echo >&2 "Downloading default configuration from netdata..."
1124 # remove a possibly obsolete download
1125 [ -f "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" ] && rm "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new"
1127 # disable a proxy to get data from the local netdata
1132 wget 2>/dev/null -O "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" "http://localhost:${NETDATA_PORT}/netdata.conf"
1136 if [ ${ret} -ne 0 -o ! -s "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" ]
1138 curl -s -o "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" "http://localhost:${NETDATA_PORT}/netdata.conf"
1142 if [ ${ret} -eq 0 -a -s "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" ]
1144 mv "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
1145 echo >&2 "New configuration saved for you to edit at ${NETDATA_PREFIX}/etc/netdata/netdata.conf"
1147 if [ "${UID}" -eq 0 ]
1149 chown "${NETDATA_USER}" "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
1151 chmod 0664 "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
1153 echo >&2 "Cannnot download configuration from netdata daemon using url 'http://localhost:${NETDATA_PORT}/netdata.conf'"
1154 [ -f "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" ] && rm "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new"
1158 # -----------------------------------------------------------------------------
1161 ksm_is_available_but_disabled() {
1164 -------------------------------------------------------------------------------
1165 Memory de-duplication instructions
1167 You have kernel memory de-duper (called Kernel Same-page Merging,
1168 or KSM) available, but it is not currently enabled.
1172 echo 1 >/sys/kernel/mm/ksm/run
1173 echo 1000 >/sys/kernel/mm/ksm/sleep_millisecs
1175 If you enable it, you will save 40-60% of netdata memory.
1180 ksm_is_not_available() {
1183 -------------------------------------------------------------------------------
1184 Memory de-duplication not present in your kernel
1186 It seems you do not have kernel memory de-duper (called Kernel Same-page
1187 Merging, or KSM) available.
1189 To enable it, you need a kernel built with CONFIG_KSM=y
1191 If you can have it, you will save 40-60% of netdata memory.
1196 if [ -f "/sys/kernel/mm/ksm/run" ]
1198 if [ $(cat "/sys/kernel/mm/ksm/run") != "1" ]
1200 ksm_is_available_but_disabled
1203 ksm_is_not_available
1206 # -----------------------------------------------------------------------------
1207 # Check for version.txt
1209 if [ ! -s web/version.txt ]
1213 -------------------------------------------------------------------------------
1214 Version update check warning
1216 The way you downloaded netdata, we cannot find its version. This means the
1217 Update check on the dashboard, will not work.
1219 If you want to have version update check, please re-install it
1220 following the procedure in:
1222 https://github.com/firehol/netdata/wiki/Installation
1227 # -----------------------------------------------------------------------------
1228 # apps.plugin warning
1230 if [ "${UID}" -ne 0 ]
1232 cat <<SETUID_WARNING
1234 -------------------------------------------------------------------------------
1235 apps.plugin needs privileges
1237 Since you have installed netdata as a normal user, to have apps.plugin collect
1238 all the needed data, you have to give it the access rights it needs, by running
1239 either of the following sets of commands:
1241 To run apps.plugin with escalated capabilities:
1243 sudo chown root:${NETDATA_USER} "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin"
1244 sudo chmod 0750 "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin"
1245 sudo setcap cap_dac_read_search,cap_sys_ptrace+ep "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin"
1247 or, to run apps.plugin as root:
1249 sudo chown root "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin"
1250 sudo chmod 4755 "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin"
1252 apps.plugin is performing a hard-coded function of data collection for all
1253 running processes. It cannot be instructed from the netdata daemon to perform
1254 any task, so it is pretty safe to do this.
1259 # -----------------------------------------------------------------------------
1260 # Keep un-install info
1262 cat >netdata-uninstaller.sh <<UNINSTALL
1265 # this script will uninstall netdata
1267 if [ "\$1" != "--force" ]
1269 echo >&2 "This script will REMOVE netdata from your system."
1270 echo >&2 "Run it again with --force to do it."
1274 echo >&2 "Stopping a possibly running netdata..."
1275 for p in \$(pidof netdata); do kill \$p; done
1279 if [ ! -z "\$1" -a -d "\$1" ]
1282 echo "Deleting directory '\$1' ..."
1287 if [ ! -z "${NETDATA_PREFIX}" -a -d "${NETDATA_PREFIX}" ]
1289 # installation prefix was given
1291 deletedir "${NETDATA_PREFIX}"
1294 # installation prefix was NOT given
1296 if [ -f "${NETDATA_PREFIX}/usr/sbin/netdata" ]
1298 echo "Deleting ${NETDATA_PREFIX}/usr/sbin/netdata ..."
1299 rm -i "${NETDATA_PREFIX}/usr/sbin/netdata"
1302 deletedir "${NETDATA_PREFIX}/etc/netdata"
1303 deletedir "${NETDATA_PREFIX}/usr/share/netdata"
1304 deletedir "${NETDATA_PREFIX}/usr/libexec/netdata"
1305 deletedir "${NETDATA_PREFIX}/var/lib/netdata"
1306 deletedir "${NETDATA_PREFIX}/var/cache/netdata"
1307 deletedir "${NETDATA_PREFIX}/var/log/netdata"
1310 if [ -f /etc/logrotate.d/netdata ]
1312 echo "Deleting /etc/logrotate.d/netdata ..."
1313 rm -i /etc/logrotate.d/netdata
1316 if [ -f /etc/systemd/system/netdata.service ]
1318 echo "Deleting /etc/systemd/system/netdata.service ..."
1319 rm -i /etc/systemd/system/netdata.service
1322 if [ -f /etc/init.d/netdata ]
1324 echo "Deleting /etc/init.d/netdata ..."
1325 rm -i /etc/init.d/netdata
1328 getent passwd netdata > /dev/null
1332 echo "You may also want to remove the user netdata"
1334 echo " userdel netdata"
1337 getent group netdata > /dev/null
1341 echo "You may also want to remove the group netdata"
1343 echo " groupdel netdata"
1346 getent group docker > /dev/null
1347 if [ $? -eq 0 -a "${NETDATA_ADDED_TO_DOCKER}" = "1" ]
1350 echo "You may also want to remove the netdata user from the docker group"
1352 echo " gpasswd -d netdata docker"
1355 getent group nginx > /dev/null
1356 if [ $? -eq 0 -a "${NETDATA_ADDED_TO_NGINX}" = "1" ]
1359 echo "You may also want to remove the netdata user from the nginx group"
1361 echo " gpasswd -d netdata nginx"
1364 getent group varnish > /dev/null
1365 if [ $? -eq 0 -a "${NETDATA_ADDED_TO_VARNISH}" = "1" ]
1368 echo "You may also want to remove the netdata user from the varnish group"
1370 echo " gpasswd -d netdata varnish"
1373 getent group haproxy > /dev/null
1374 if [ $? -eq 0 -a "${NETDATA_ADDED_TO_HAPROXY}" = "1" ]
1377 echo "You may also want to remove the netdata user from the haproxy group"
1379 echo " gpasswd -d netdata haproxy"
1382 getent group adm > /dev/null
1383 if [ $? -eq 0 -a "${NETDATA_ADDED_TO_ADM}" = "1" ]
1386 echo "You may also want to remove the netdata user from the adm group"
1388 echo " gpasswd -d netdata adm"
1393 chmod 750 netdata-uninstaller.sh
1395 # -----------------------------------------------------------------------------
1400 -------------------------------------------------------------------------------
1402 OK. NetData is installed and it is running.
1404 -------------------------------------------------------------------------------
1406 By default netdata listens on all IPs on port ${NETDATA_PORT},
1407 so you can access it with:
1409 http://this.machine.ip:${NETDATA_PORT}/
1411 To stop netdata, just kill it, with:
1415 To start it, just run it:
1417 ${NETDATA_PREFIX}/usr/sbin/netdata
1421 echo >&2 "Uninstall script generated: ./netdata-uninstaller.sh"
1425 cat >netdata-updater.sh.new <<REINSTALL
1429 [ "\${1}" = "-f" ] && force=1
1431 export PATH="\${PATH}:${PATH}"
1432 export CFLAGS="${CFLAGS}"
1434 INSTALL_UID="${UID}"
1435 if [ "\${INSTALL_UID}" != "\${UID}" ]
1437 echo >&2 "This script should be run as user with uid \${INSTALL_UID} but it now runs with uid \${UID}"
1441 # make sure we cd to the working directory
1442 cd "${REINSTALL_PWD}" || exit 1
1444 # make sure there is .git here
1445 [ \${force} -eq 0 -a ! -d .git ] && echo >&2 "No git structures found at: ${REINSTALL_PWD} (use -f for force re-install)" && exit 1
1447 # signal netdata to start saving its database
1448 # this is handy if your database is big
1449 pids=\$(pidof netdata)
1450 [ ! -z "\${pids}" ] && kill -USR1 \${pids}
1455 # we are running on a terminal
1456 # open fd 3 and send it to stderr
1460 # create a temporary file for the log
1461 tmp=\$(mktemp /tmp/netdata-updater-log-XXXXXX.log)
1462 # open fd 3 and send it to tmp
1467 echo >&3 "\$(date) : INFO: " "\${@}"
1475 echo >&3 "\$(date) : ERROR: " "\${@}"
1478 # this is what we will do if it fails (head-less only)
1480 error "FAILED TO UPDATE NETDATA : \${1}"
1482 if [ ! -z "\${tmp}" ]
1490 get_latest_commit_id() {
1492 grep ^commit 2>&3 |\\
1494 cut -d ' ' -f 2 2>&3
1498 [ -z "\${tmp}" ] && info "Running on a terminal - (this script also supports running headless from crontab)"
1504 info "Updating netdata source from github..."
1506 last_commit="\$(get_latest_commit_id)"
1507 [ \${force} -eq 0 -a -z "\${last_commit}" ] && failed "CANNOT GET LAST COMMIT ID (use -f for force re-install)"
1509 git pull >&3 2>&3 || failed "CANNOT FETCH LATEST SOURCE (use -f for force re-install)"
1511 new_commit="\$(get_latest_commit_id)"
1512 if [ \${force} -eq 0 ]
1514 [ -z "\${new_commit}" ] && failed "CANNOT GET NEW LAST COMMIT ID (use -f for force re-install)"
1515 [ "\${new_commit}" = "\${last_commit}" ] && info "Nothing to be done! (use -f to force re-install)" && exit 0
1517 elif [ \${force} -eq 0 ]
1519 failed "CANNOT FIND GIT STRUCTURES IN \$(pwd) (use -f for force re-install)"
1523 info "Re-installing netdata..."
1524 ${REINSTALL_COMMAND// --dont-wait/} --dont-wait >&3 2>&3 || failed "FAILED TO COMPILE/INSTALL NETDATA"
1526 [ ! -z "\${tmp}" ] && rm "\${tmp}" && tmp=
1530 # the installer updates this script - so we run and exit in a single line
1532 ###############################################################################
1533 ###############################################################################
1535 chmod 755 netdata-updater.sh.new
1536 mv -f netdata-updater.sh.new netdata-updater.sh
1537 echo >&2 "Update script generated : ./netdata-updater.sh"
1538 elif [ -f "netdata-updater.sh" ]
1540 rm "netdata-updater.sh"
1543 banner "is installed and running now!"
1544 echo >&2 " enjoy real-time performance and health monitoring..."