#!/bin/bash
+netdata=$(which netdata)
+
+if [ -z "${netdata}" -o ! -f src/netdata ]
+then
+ echo >&2 "Running ./autogen.sh ..."
+ ./autogen.sh || exit 1
+
+ echo >&2 "Running ./configure ..."
+ ./configure --prefix=/usr --sysconfdir=/etc --localstatedir=/var --with-zlib --with-math --with-user=netdata CFLAGS="-march=native -O3 -Wall -Wextra" || exit 1
+
+ echo >&2 "Cleaning a possibly old compilation ..."
+ make clean || exit 1
+
+ echo >&2 "Compiling netdata ..."
+ make || exit 1
+fi
+
+if [ -z "${netdata}" -o src/netdata -nt "${netdata}" ]
+then
+ echo >&2 "Installing netdata ..."
+ make install
+
+ echo >&2 "Adding netdata group ..."
+ getent group netdata > /dev/null || groupadd -r netdata
+
+ echo >&2 "Adding netdata user ..."
+ getent passwd netdata > /dev/null || useradd -r -g netdata -c netdata -s /sbin/nologin -d / netdata
+fi
+
+netdata=$(which netdata)
+
base="`dirname "$0"`"
if [ ! -d "${base}" ]
# -----------------------------------------------------------------------------
# load options from the configuration file
+# create an empty config if it does not exist
+[ ! -f conf.d/netdata.conf ] && touch conf.d/netdata.conf
+
+# function to extract values from the config file
config_option() {
local key="${1}" value="${2}" line=
- if [ -f "conf.d/netdata.conf" ]
+ if [ -s "/etc/netdata/netdata.conf" ]
then
line="$( grep "^[[:space:]]*${key}[[:space:]]*=[[:space:]]*" "conf.d/netdata.conf" | head -n 1 )"
[ ! -z "${line}" ] && value="$( echo "${line}" | cut -d '=' -f 2 | sed -e "s/^[[:space:]]\+//g" -e "s/[[:space:]]\+$//g" )"
}
# user
-defuser="nobody"
+defuser="netdata"
[ ! "${UID}" = "0" ] && defuser="${USER}"
NETDATA_USER="$( config_option "run as user" "${defuser}" )"
NETDATA_PORT="$( config_option "port" ${defport} )"
# directories
-NETDATA_CACHE_DIR="$( config_option "database directory" "cache" )"
-NETDATA_WEB_DIR="$( config_option "web files directory" "web" )"
+NETDATA_CACHE_DIR="$( config_option "database directory" "/var/cache/netdata" )"
+NETDATA_WEB_DIR="$( config_option "web files directory" "/usr/share/netdata/web" )"
NETDATA_LOG_DIR="log"
NETDATA_CONF_DIR="conf.d"
-# -----------------------------------------------------------------------------
-# compile netdata
-
-echo >&2 "Compiling netdata (debug flags = $NETDATA_DEBUG)..."
-if [ $[ NETDATA_DEBUG ] -ne 0 ]
- then
- make install debug=1 || exit 1 # this installs in the current directory
-
- # let netdata core dump if it crashes
- ulimit -c unlimited
-
-else
- make install || exit 1 # this installs in the current directory
-fi
-
-
# -----------------------------------------------------------------------------
# prepare the directories
chmod 0775 "${x}" "${x}" || echo >&2 "WARNING: Cannot change the permissions of the directory ${x} to 0755..."
done
+# fix apps.plugin to be setuid to root
+chown root '/usr/libexec/netdata/plugins.d/apps.plugin' && chmod 4755 '/usr/libexec/netdata/plugins.d/apps.plugin'
+
# -----------------------------------------------------------------------------
# stop a running netdata
# -----------------------------------------------------------------------------
# run netdata
+# avoid extended stat(/etc/localtime)
+# http://stackoverflow.com/questions/4554271/how-to-avoid-excessive-stat-etc-localtime-calls-in-strftime-on-linux
+export TZ=":/etc/localtime"
+
echo >&2 "Starting netdata..."
-`pwd`/netdata "${@}"
+netdata "${@}"
if [ $? -ne 0 ]
then
echo >&2
echo >&2 "SORRY! FAILED TO START NETDATA!"
exit 1
+else
+ echo >&2 "OK. NetData Started!"
fi
# -----------------------------------------------------------------------------
# save a config file, if it is not already there
-if [ ! -f conf.d/netdata.conf ]
+if [ ! -s /etc/netdata/netdata.conf ]
then
+ echo >&2 "Downloading default configuration from netdata..."
sleep 5
- wget 2>/dev/null -O conf.d/netdata.conf.new "http://localhost:${NETDATA_PORT}/netdata.conf"
- if [ $? -eq 0 -a -s conf.d/netdata.conf.new ]
+
+ # remove a possibly obsolete download
+ [ -f /etc/netdata/netdata.conf.new ] && rm /etc/netdata/netdata.conf.new
+
+ # try wget
+ wget 2>/dev/null -O /etc/netdata/netdata.conf.new "http://localhost:${NETDATA_PORT}/netdata.conf"
+ ret=$?
+
+ # try curl
+ if [ $ret -ne 0 -o ! -s /etc/netdata/netdata.conf.net ]
then
- mv conf.d/netdata.conf.new conf.d/netdata.conf
- echo >&2 "New configuration saved for you to edit at conf.d/netdata.conf"
+ curl -s -o /etc/netdata/netdata.conf.new "http://localhost:${NETDATA_PORT}/netdata.conf"
+ ret=$?
+ fi
+
+ if [ $ret -eq 0 -a -s /etc/netdata/netdata.conf.new ]
+ then
+ mv /etc/netdata/netdata.conf.new /etc/netdata/netdata.conf
+ echo >&2 "New configuration saved for you to edit at /etc/netdata/netdata.conf"
+
+ chown "${NETDATA_USER}" /etc/netdata/netdata.conf
+ chmod 0664 /etc/netdata/netdata.conf
else
- rm conf.d/netdata.conf.new
+ echo >&2 "Cannnot download configuration from netdata daemon using url 'http://localhost:${NETDATA_PORT}/netdata.conf'"
+ [ -f /etc/netdata/netdata.conf.new ] && rm /etc/netdata/netdata.conf.new
fi
fi
echo >&2
-echo >&2 "OK. NetData started! Hit http://localhost:${NETDATA_PORT}/ from your browser."
+echo >&2 "Hit http://localhost:${NETDATA_PORT}/ from your browser."
echo >&2
echo >&2 "Enjoy..."
echo >&2