LC_ALL=C
umask 022
+# Be nice on production environments
+renice 19 $$ >/dev/null 2>/dev/null
+
+processors=$(cat /proc/cpuinfo | grep ^processor | wc -l)
+[ $(( processors )) -lt 1 ] && processors=1
+
# you can set CFLAGS before running installer
CFLAGS="${CFLAGS--O3}"
REINSTALL_PWD="${PWD}"
REINSTALL_COMMAND="$(printf "%q " "$0" "${@}"; printf "\n")"
+banner() {
+ local l1=" ^" \
+ l2=" |.-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-" \
+ l3=" | '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' " \
+ l4=" +----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+--->" \
+ sp=" " \
+ netdata="netdata" start end msg="${*}"
+
+ [ ${#msg} -lt ${#netdata} ] && msg="${msg}${sp:0:$(( ${#netdata} - ${#msg}))}"
+ [ ${#msg} -gt $(( ${#l2} - 20 )) ] && msg="${msg:0:$(( ${#l2} - 23 ))}..."
+
+ start="$(( ${#l2} / 2 - 4 ))"
+ [ $(( start + ${#msg} + 4 )) -gt ${#l2} ] && start=$((${#l2} - ${#msg} - 4))
+ end=$(( ${start} + ${#msg} + 4 ))
+
+ echo >&2
+ echo >&2 "${l1}"
+ echo >&2 "${l2:0:start}${sp:0:2}${netdata}${sp:0:$((end - start - 2 - ${#netdata}))}${l2:end:$((${#l2} - end))}"
+ echo >&2 "${l3:0:start}${sp:0:2}${msg}${sp:0:2}${l3:end:$((${#l2} - end))}"
+ echo >&2 "${l4}"
+ echo >&2
+}
+
service="$(which service 2>/dev/null || command -v service 2>/dev/null)"
systemctl="$(which systemctl 2>/dev/null || command -v systemctl 2>/dev/null)"
service() {
LIBS_ARE_HERE=0
usage() {
+ banner "installer command line options"
cat <<USAGE
${ME} <installer options>
echo >configs.signatures.tmp
- for x in $(find conf.d -name \*.conf)
+ for x in $(find conf.d -name \*.conf | sort)
do
x="${x/conf.d\//}"
echo "${x}"
fi
done
+banner "real-time performance monitoring, done right!"
cat <<BANNER
-Welcome to netdata!
-The real-time performance monitoring system.
-
-You are about to build and install netdata to your system.
+ You are about to build and install netdata to your system.
-It will be installed at these locations:
+ It will be installed at these locations:
- - the daemon at ${NETDATA_PREFIX}/usr/sbin/netdata
- - config files at ${NETDATA_PREFIX}/etc/netdata
- - web files at ${NETDATA_PREFIX}/usr/share/netdata
- - plugins at ${NETDATA_PREFIX}/usr/libexec/netdata
- - cache files at ${NETDATA_PREFIX}/var/cache/netdata
- - db files at ${NETDATA_PREFIX}/var/lib/netdata
- - log files at ${NETDATA_PREFIX}/var/log/netdata
- - pid file at ${NETDATA_PREFIX}/var/run
+ - the daemon at ${NETDATA_PREFIX}/usr/sbin/netdata
+ - config files at ${NETDATA_PREFIX}/etc/netdata
+ - web files at ${NETDATA_PREFIX}/usr/share/netdata
+ - plugins at ${NETDATA_PREFIX}/usr/libexec/netdata
+ - cache files at ${NETDATA_PREFIX}/var/cache/netdata
+ - db files at ${NETDATA_PREFIX}/var/lib/netdata
+ - log files at ${NETDATA_PREFIX}/var/log/netdata
+ - pid file at ${NETDATA_PREFIX}/var/run
-This installer allows you to change the installation path.
-Press Control-C and run the same command with --help for help.
+ This installer allows you to change the installation path.
+ Press Control-C and run the same command with --help for help.
BANNER
then
if [ -z "${NETDATA_PREFIX}" ]
then
+ banner "wrong command line options!"
cat <<NONROOTNOPREFIX
Sorry! This will fail!
then
echo "Will skip autoreconf step"
else
+ banner "autotools v2.60 required"
cat <<"EOF"
-------------------------------------------------------------------------------
fi
build_error() {
+ banner "sorry, it failed to build..."
cat <<EOF
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
-Sorry! NetData failed to build...
+Sorry! netdata failed to build...
You many need to check these:
fi
echo >&2 "Compiling netdata ..."
-run make || exit 1
+run make -j${processors} || exit 1
if [ "${BASH_VERSINFO[0]}" -ge "4" ]
then
if [ $? -ne 0 ]
then
echo >&2 "Adding netdata user account ..."
- run useradd -r -g netdata -c netdata -s $(which nologin || echo '/bin/false') -d / netdata
+ run useradd -r -g netdata -c netdata -s $(which nologin 2>/dev/null || command -v nologin 2>/dev/null || echo '/bin/false') -d / netdata
fi
getent group docker > /dev/null
fi
chmod 0664 "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
fi
- echo >&2 "OK. It is now installed and ready."
+ banner "is installed now!"
+ echo >&2 " enjoy real-time performance and health monitoring..."
exit 0
fi
}
stop_netdata_on_pid() {
- local pid="$1" ret=0 count=0
+ local pid="${1}" ret=0 count=0
- isnetdata $pid || return 0
+ isnetdata ${pid} || return 0
- printf >&2 "Stopping netdata on pid $pid ..."
- while [ ! -z "$pid" -a $ret -eq 0 ]
+ printf >&2 "Stopping netdata on pid ${pid} ..."
+ while [ ! -z "$pid" -a ${ret} -eq 0 ]
do
- if [ $count -gt 45 ]
+ if [ ${count} -gt 45 ]
then
- echo >&2 "Cannot stop the running netdata on pid $pid."
+ echo >&2 "Cannot stop the running netdata on pid ${pid}."
return 1
fi
count=$(( count + 1 ))
- run kill $pid 2>/dev/null
+ run kill ${pid} 2>/dev/null
ret=$?
- test $ret -eq 0 && printf >&2 "." && sleep 2
+ test ${ret} -eq 0 && printf >&2 "." && sleep 2
done
echo >&2
- if [ $ret -eq 0 ]
+ if [ ${ret} -eq 0 ]
then
- echo >&2 "SORRY! CANNOT STOP netdata ON PID $pid !"
+ echo >&2 "SORRY! CANNOT STOP netdata ON PID ${pid} !"
return 1
fi
- echo >&2 "netdata on pid $pid stopped."
+ echo >&2 "netdata on pid ${pid} stopped."
return 0
}
stop_all_netdata() {
- local p
+ local p myns ns
+
+ myns="$(readlink /proc/self/ns/pid 2>/dev/null)"
echo >&2 "Stopping a (possibly) running netdata..."
$(cat /var/run/netdata/netdata.pid 2>/dev/null) \
$(pidof netdata 2>/dev/null)
do
- stop_netdata_on_pid $p
+ ns="$(readlink /proc/${p}/ns/pid 2>/dev/null)"
+
+ if [ -z "${myns}" -o -z "${ns}" -o "${myns}" = "${ns}" ]
+ then
+ stop_netdata_on_pid ${p}
+ fi
done
}
# -----------------------------------------------------------------------------
-# check netdata for systemd
+# check for systemd
issystemd() {
+ local pids p myns ns systemctl
+
# if the directory /etc/systemd/system does not exit, it is not systemd
[ ! -d /etc/systemd/system ] && return 1
+ # if there is no systemctl command, it is not systemd
+ systemctl=$(which systemctl 2>/dev/null || command -v systemctl 2>/dev/null)
+ [ -z "${systemctl}" -o ! -x "${systemctl}" ] && return 1
+
# if pid 1 is systemd, it is systemd
[ "$(basename $(readlink /proc/1/exe) 2>/dev/null)" = "systemd" ] && return 0
- # if systemd is running, it is systemd
- pidof systemd >/dev/null 2>&1 && return 0
+ # if systemd is not running, it is not systemd
+ pids=$(pidof systemd 2>/dev/null)
+ [ -z "${pids}" ] && return 1
+
+ # check if the running systemd processes are not in our namespace
+ myns="$(readlink /proc/self/ns/pid 2>/dev/null)"
+ for p in ${pids}
+ do
+ ns="$(readlink /proc/${p}/ns/pid 2>/dev/null)"
+
+ # if pid of systemd is in our namespace, it is systemd
+ [ ! -z "${myns}" && "${myns}" = "${ns}" ] && return 0
+ done
# else, it is not systemd
return 1
}
+installed_init_d=0
+install_non_systemd_init() {
+ [ "${UID}" != 0 ] && return 1
+
+ local key="unknown"
+ if [ -f /etc/os-release ]
+ then
+ source /etc/os-release || return 1
+ key="${ID}-${VERSION_ID}"
+
+ elif [ -f /etc/centos-release ]
+ then
+ key=$(</etc/centos-release)
+ fi
+
+ if [ -d /etc/init.d -a ! -f /etc/init.d/netdata ]
+ then
+ if [ "${key}" = "gentoo" ]
+ then
+ run cp system/netdata-openrc /etc/init.d/netdata && \
+ run chmod 755 /etc/init.d/netdata && \
+ run rc-update add netdata default && \
+ installed_init_d=1
+
+ elif [ "${key}" = "ubuntu-12.04" -o "${key}" = "ubuntu-14.04" -o "${key}" = "debian-7" ]
+ then
+ run cp system/netdata-lsb /etc/init.d/netdata && \
+ run chmod 755 /etc/init.d/netdata && \
+ run update-rc.d netdata defaults && \
+ run update-rc.d netdata enable && \
+ installed_init_d=1
+
+ elif [ "${key}" = "CentOS release 6.8 (Final)" ]
+ then
+ run cp system/netdata-init-d /etc/init.d/netdata && \
+ run chmod 755 /etc/init.d/netdata && \
+ run chkconfig netdata on && \
+ installed_init_d=1
+ fi
+ fi
+
+ return 0
+}
+
started=0
if [ "${UID}" -eq 0 ]
then
stop_all_netdata
service netdata restart && started=1
+ else
+ install_non_systemd_init
fi
if [ ${started} -eq 0 ]
ret=$?
# try curl
- if [ $ret -ne 0 -o ! -s "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" ]
+ if [ ${ret} -ne 0 -o ! -s "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" ]
then
curl -s -o "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" "http://localhost:${NETDATA_PORT}/netdata.conf"
ret=$?
fi
- if [ $ret -eq 0 -a -s "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" ]
+ if [ ${ret} -eq 0 -a -s "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" ]
then
mv "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
echo >&2 "New configuration saved for you to edit at ${NETDATA_PREFIX}/etc/netdata/netdata.conf"
rm -i /etc/systemd/system/netdata.service
fi
+if [ -f /etc/init.d/netdata ]
+ then
+ echo "Deleting /etc/init.d/netdata ..."
+ rm -i /etc/init.d/netdata
+fi
+
getent passwd netdata > /dev/null
if [ $? -eq 0 ]
then
${NETDATA_PREFIX}/usr/sbin/netdata
-Enjoy!
-
END
echo >&2 "Uninstall script generated: ./netdata-uninstaller.sh"
-cat >netdata-updater.sh.new <<REINSTALL
+if [ -d .git ]
+ then
+ cat >netdata-updater.sh.new <<REINSTALL
#!/usr/bin/env bash
+force=0
+[ "\${1}" = "-f" ] && force=1
+
export PATH="\${PATH}:${PATH}"
export CFLAGS="${CFLAGS}"
-LAST_UID="${UID}"
-if [ "\${LAST_UID}" != "\${UID}" ]
+INSTALL_UID="${UID}"
+if [ "\${INSTALL_UID}" != "\${UID}" ]
then
- echo >&2 "This script should be run as user with uid \${LAST_UID} but it now runs with uid \${UID}"
+ echo >&2 "This script should be run as user with uid \${INSTALL_UID} but it now runs with uid \${UID}"
exit 1
fi
# make sure we cd to the working directory
cd "${REINSTALL_PWD}" || exit 1
+# make sure there is .git here
+[ \${force} -eq 0 -a ! -d .git ] && echo >&2 "No git structures found at: ${REINSTALL_PWD} (use -f for force re-install)" && exit 1
+
# signal netdata to start saving its database
# this is handy if your database is big
pids=\$(pidof netdata)
[ ! -z "\${pids}" ] && kill -USR1 \${pids}
+tmp=
+if [ -t 2 ]
+ then
+ # we are running on a terminal
+ # open fd 3 and send it to stderr
+ exec 3>&2
+else
+ # we are headless
+ # create a temporary file for the log
+ tmp=\$(mktemp /tmp/netdata-updater-log-XXXXXX.log)
+ # open fd 3 and send it to tmp
+ exec 3>\${tmp}
+fi
+
+info() {
+ echo >&3 "\$(date) : INFO: " "\${@}"
+}
+
+emptyline() {
+ echo >&3
+}
+
+error() {
+ echo >&3 "\$(date) : ERROR: " "\${@}"
+}
+
# this is what we will do if it fails (head-less only)
failed() {
- echo >&2 "\$(date) : FAILED TO UPDATE NETDATA : \${1}"
- cat >&2 "\${tmp}"
- rm "\${tmp}"
- exit 1
+ error "FAILED TO UPDATE NETDATA : \${1}"
+
+ if [ ! -z "\${tmp}" ]
+ then
+ cat >&2 "\${tmp}"
+ rm "\${tmp}"
+ fi
+ exit 1
}
get_latest_commit_id() {
- git log | grep ^commit | head -n 1 | cut -d ' ' -f 2
+ git log -1 2>&3 |\\
+ grep ^commit 2>&3 |\\
+ head -n 1 2>&3 |\\
+ cut -d ' ' -f 2 2>&3
}
-last_commit="\$(get_latest_commit_id)"
-[ -z "\${last_commit}" ] && failed "CANNOT GET LAST COMMIT ID"
-
update() {
- if [ -t 1 -a -t 2 ]
- then
- # running on a terminal
- echo >&2 "Running on a terminal - (this script also supports running headless from crontab)"
+ [ -z "\${tmp}" ] && info "Running on a terminal - (this script also supports running headless from crontab)"
- echo >&2
- echo >&2 "Updating source..."
- git pull || exit 1
- new_commit="\$(get_latest_commit_id)"
- [ -z "\${new_commit}" ] && echo >&2 "CANNOT GET NEW LAST COMMIT ID" && exit 1
- [ "\${new_commit}" = "\${last_commit}" ] && echo >&2 "Nothing to be done!" && exit 0
+ emptyline
- echo >&2
- echo >&2 "re-installing..."
- ${REINSTALL_COMMAND// --dont-wait/} --dont-wait || exit 1
+ if [ -d .git ]
+ then
+ info "Updating netdata source from github..."
- else
- # running head-less
- # do not generate any output, unless we fail
+ last_commit="\$(get_latest_commit_id)"
+ [ \${force} -eq 0 -a -z "\${last_commit}" ] && failed "CANNOT GET LAST COMMIT ID (use -f for force re-install)"
- # create a temporary file for the log
- tmp=\$(mktemp /tmp/netdata-updater-log-XXXXXX.log)
+ git pull >&3 2>&3 || failed "CANNOT FETCH LATEST SOURCE (use -f for force re-install)"
- # update source from git
- git pull >>"\${tmp}" 2>&1 || failed "CANNOT FETCH LATEST SOURCE"
new_commit="\$(get_latest_commit_id)"
- [ -z "\${new_commit}" ] && failed "CANNOT GET NEW LAST COMMIT ID"
- [ "\${new_commit}" = "\${last_commit}" ] && exit 0
-
- # install the latest version
- ${REINSTALL_COMMAND// --dont-wait/} --dont-wait >>"\${tmp}" 2>&1 || failed "CANNOT BUILD AND INSTALL NETDATA"
-
- rm "\${tmp}"
+ if [ \${force} -eq 0 ]
+ then
+ [ -z "\${new_commit}" ] && failed "CANNOT GET NEW LAST COMMIT ID (use -f for force re-install)"
+ [ "\${new_commit}" = "\${last_commit}" ] && info "Nothing to be done! (use -f to force re-install)" && exit 0
+ fi
+ elif [ \${force} -eq 0 ]
+ then
+ failed "CANNOT FIND GIT STRUCTURES IN \$(pwd) (use -f for force re-install)"
fi
+ emptyline
+ info "Re-installing netdata..."
+ ${REINSTALL_COMMAND// --dont-wait/} --dont-wait >&3 2>&3 || failed "FAILED TO COMPILE/INSTALL NETDATA"
+
+ [ ! -z "\${tmp}" ] && rm "\${tmp}" && tmp=
return 0
}
###############################################################################
###############################################################################
REINSTALL
-chmod 755 netdata-updater.sh.new
-mv -f netdata-updater.sh.new netdata-updater.sh
-echo >&2 "Update script generated : ./netdata-updater.sh"
+ chmod 755 netdata-updater.sh.new
+ mv -f netdata-updater.sh.new netdata-updater.sh
+ echo >&2 "Update script generated : ./netdata-updater.sh"
+elif [ -f "netdata-updater.sh" ]
+ then
+ rm "netdata-updater.sh"
+fi
+
+banner "is installed and running now!"
+echo >&2 " enjoy real-time performance and health monitoring..."
+echo >&2
+exit 0