]> arthur.barton.de Git - netdata.git/blobdiff - netdata.start
build: remove uselss code
[netdata.git] / netdata.start
index 0b28375cc1fc15baeaffde1088b1fcf74914e71b..94c2f35f2a4150f866f3c1ef36a7c1881ffeed71 100755 (executable)
 #!/bin/bash
 
-base="`dirname "$0"`"
+netdata=$(which netdata)
 
-if [ ! -d "$base" -o ! -f "$base/netdata.c" -o ! -d "$base/web" ]
+if [ -z "${netdata}" -o ! -f src/netdata ]
 then
-       echo >&2 "Cannot find my home directory '${base}'."
-       exit 1
-fi
-cd "$base" || exit 1
+        echo >&2 "Running ./autogen.sh ..."
+        ./autogen.sh || exit 1
 
-# the detail of the data that will be kept in netdata
-NETDATA_CONFIG_INTERNAL_UPDATE_EVERY=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
 
-# how many entries to keep in memory
-NETDATA_CONFIG_INTERNAL_HISTORY_LINES=2400
+        echo >&2 "Cleaning a possibly old compilation ..."
+        make clean || exit 1
 
-# the user to run netdata under
-NETDATA_CONFIG_USER=nobody
+        echo >&2 "Compiling netdata ..."
+        make || exit 1
+fi
 
-# set to 1, to enable debugging
-NETDATA_CONFIG_DEBUG=0
+if [ -z "${netdata}" -o src/netdata -nt "${netdata}" ]
+then
+        echo >&2 "Installing netdata ..."
+        make install
 
-# our port
-NETDATA_CONFIG_PORT=19999
+        echo >&2 "Adding netdata group ..."
+        getent group netdata > /dev/null || groupadd -r netdata
 
-# get user configuration
-if [ -f netdata.conf ]
-then
-       . netdata.conf
+        echo >&2 "Adding netdata user ..."
+        getent passwd netdata > /dev/null || useradd -r -g netdata -c netdata -s /sbin/nologin -d / netdata
 fi
 
-echo "Stopping a (possibly) running netdata..."
-killall netdata 2>/dev/null
-killall tc-all.sh 2>/dev/null
-sleep 2
+netdata=$(which netdata)
+
+base="`dirname "$0"`"
 
-echo "Compiling netdata"
-if [ $NETDATA_CONFIG_DEBUG -eq 1 ]
+if [ ! -d "${base}" ]
 then
-       ulimit -c unlimited
-       gcc -Wall -ggdb -o netdata netdata.c -lpthread -lz || exit 1
-       debug_opts="-df 0xfffffedf -dl netdata.log"
+       echo >&2 "Cannot find my home directory '${base}'."
+       exit 1
+fi
+cd "${base}" || exit 1
+
+
+# -----------------------------------------------------------------------------
+# 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 [ -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" )"
+       fi
+
+       echo "${value}"
+}
+
+# user
+defuser="netdata"
+[ ! "${UID}" = "0" ] && defuser="${USER}"
+NETDATA_USER="$( config_option "run as user" "${defuser}" )"
+
+# debug flags
+defdebug=0
+NETDATA_DEBUG="$( config_option "debug flags" ${defdebug} )"
+
+# port
+defport=19999
+NETDATA_PORT="$( config_option "port" ${defport} )"
+
+# directories
+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"
+
+
+# -----------------------------------------------------------------------------
+# prepare the directories
+
+echo >&2 "Fixing directory permissions for user ${NETDATA_USER}..."
+for x in "${NETDATA_WEB_DIR}" "${NETDATA_CONF_DIR}" "${NETDATA_CACHE_DIR}" "${NETDATA_LOG_DIR}"
+do
+       if [ ! -d "${x}" ]
+               then
+               mkdir "${x}" || exit 1
+       fi
+       chown -R "${NETDATA_USER}" "${x}" || echo >&2 "WARNING: Cannot change the ownership of the files in directory ${x} to ${NETDATA_USER}..."
+       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
+
+printf >&2 "Stopping a (possibly) running netdata..."
+ret=0
+count=0
+while [ $ret -eq 0 ]
+do
+       if [ $count -gt 15 ]
+               then
+               echo >&2 "Cannot stop the running netdata."
+               exit 1
+       fi
+
+       count=$((count + 1))
+       killall netdata 2>/dev/null
+       ret=$?
+       test $ret -eq 0 && printf >&2 "." && sleep 2
+done
+echo >&2
+
+
+# -----------------------------------------------------------------------------
+# 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..."
+netdata "${@}"
+
+if [ $? -ne 0 ]
+       then
+       echo >&2
+       echo >&2 "SORRY! FAILED TO START NETDATA!"
+       exit 1
 else
-       gcc -Wall -O3 -o netdata netdata.c -lpthread -lz || exit 1
-       debug_opts=
+       echo >&2 "OK. NetData Started!"
 fi
 
-NETDATA_TITLE_DEFAULT=
-NETDATA_PRIORITY_DEFAULT=
 
-# exporting NETDATA variables to netdata
-export `set 2>&1 | grep ^NETDATA_TITLE_ `
-export `set 2>&1 | grep ^NETDATA_PRIORITY_ `
+# -----------------------------------------------------------------------------
+# save a config file, if it is not already there
 
-echo "Starting netdata"
-if [ "$USER" = "root" ]
-then
-       chown -R "$NETDATA_CONFIG_USER" web || exit 1
-       chmod    0775 web   || exit 1
-       chmod -R 0664 web/* || exit 1
-       `pwd`/netdata $debug_opts -d -u $NETDATA_CONFIG_USER -t $NETDATA_CONFIG_INTERNAL_UPDATE_EVERY -l $NETDATA_CONFIG_INTERNAL_HISTORY_LINES -p $NETDATA_CONFIG_PORT || exit 1
-else
-       echo >&2 "WARNING: NOT RUNNING AS ROOT - CANNOT SWITCH TO USER $NETDATA_CONFIG_USER"
-       echo >&2 "WARNING: MAKE SURE FILES IN web/ ARE OWNED BY $USER - or it will not work"
-       `pwd`/netdata $debug_opts -d -t $NETDATA_CONFIG_INTERNAL_UPDATE_EVERY -l $NETDATA_CONFIG_INTERNAL_HISTORY_LINES -p $NETDATA_CONFIG_PORT || exit 1
-fi
-sleep 2
+if [ ! -s /etc/netdata/netdata.conf ]
+       then
+       echo >&2 "Downloading default configuration from netdata..."
+       sleep 5
 
-if [ "$USER" = "root" ]
-then
-       chown -R "$NETDATA_CONFIG_USER" web || exit 1
-       chmod    0775 web   || exit 1
-       chmod -R 0664 web/* || exit 1
-fi
+       # 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=$?
 
-# merge the server variables into the config
-eval "`wget -O -  http://localhost:$NETDATA_CONFIG_PORT/envlist 2>/dev/null | tr " " "_" `"
+       # try curl
+       if [ $ret -ne 0 -o ! -s /etc/netdata/netdata.conf.net ]
+               then
+               curl -s -o /etc/netdata/netdata.conf.new "http://localhost:${NETDATA_PORT}/netdata.conf"
+               ret=$?
+       fi
 
-# save config back
-set 2>/dev/null |grep ^NETDATA_CONFIG_         >netdata.conf
-set 2>/dev/null |grep ^NETDATA_PRIORITY_       >>netdata.conf
-set 2>/dev/null |grep ^NETDATA_TITLE_          >>netdata.conf
+       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
+               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 "All Done."
-echo "Just hit http://127.0.0.1:$NETDATA_CONFIG_PORT/ from your browser."
-echo
-echo "You can edit config options in file netdata.conf"
-echo
+echo >&2
+echo >&2 "Hit http://localhost:${NETDATA_PORT}/ from your browser."
+echo >&2
+echo >&2 "Enjoy..."
+echo >&2