#!/bin/bash base="`dirname "$0"`" if [ ! -d "${base}" ] then echo >&2 "Cannot find my home directory '${base}'." exit 1 fi cd "${base}" || exit 1 # ----------------------------------------------------------------------------- # load options from the configuration file config_option() { local key="${1}" value="${2}" line= if [ -f "conf.d/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="nobody" [ ! "${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" "cache" )" NETDATA_WEB_DIR="$( config_option "web files directory" "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 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 # ----------------------------------------------------------------------------- # 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 echo >&2 "Starting netdata..." `pwd`/netdata "${@}" if [ $? -ne 0 ] then echo >&2 echo >&2 "SORRY! FAILED TO START NETDATA!" exit 1 fi # ----------------------------------------------------------------------------- # save a config file, if it is not already there if [ ! -f conf.d/netdata.conf ] then 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 ] 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" else rm conf.d/netdata.conf.new fi fi echo >&2 echo >&2 "OK. NetData started! Hit http://localhost:${NETDATA_PORT}/ from your browser." echo >&2 echo >&2 "Enjoy..." echo >&2