3 netdata=$(which netdata)
5 if [ -z "${netdata}" -o ! -f src/netdata ]
7 echo >&2 "Running ./autogen.sh ..."
10 echo >&2 "Running ./configure ..."
11 ./configure --prefix=/usr --sysconfdir=/etc --localstatedir=/var --with-zlib --with-math --with-user=netdata CFLAGS="-march=native -O3 -Wall -Wextra" || exit 1
13 echo >&2 "Compiling netdata ..."
17 if [ -z "${netdata}" -o src/netdata -nt "${netdata}" ]
19 echo >&2 "Installing netdata ..."
22 echo >&2 "Adding netdata group ..."
23 getent group netdata > /dev/null || groupadd -r netdata
25 echo >&2 "Adding netdata user ..."
26 getent passwd netdata > /dev/null || useradd -r -g netdata -c netdata -s /sbin/nologin -d / netdata
29 netdata=$(which netdata)
35 echo >&2 "Cannot find my home directory '${base}'."
38 cd "${base}" || exit 1
41 # -----------------------------------------------------------------------------
42 # load options from the configuration file
44 # create an empty config if it does not exist
45 [ ! -f conf.d/netdata.conf ] && touch conf.d/netdata.conf
47 # function to extract values from the config file
49 local key="${1}" value="${2}" line=
51 if [ -s "/etc/netdata/netdata.conf" ]
53 line="$( grep "^[[:space:]]*${key}[[:space:]]*=[[:space:]]*" "conf.d/netdata.conf" | head -n 1 )"
54 [ ! -z "${line}" ] && value="$( echo "${line}" | cut -d '=' -f 2 | sed -e "s/^[[:space:]]\+//g" -e "s/[[:space:]]\+$//g" )"
62 [ ! "${UID}" = "0" ] && defuser="${USER}"
63 NETDATA_USER="$( config_option "run as user" "${defuser}" )"
67 NETDATA_DEBUG="$( config_option "debug flags" ${defdebug} )"
71 NETDATA_PORT="$( config_option "port" ${defport} )"
74 NETDATA_CACHE_DIR="$( config_option "database directory" "/var/cache/netdata" )"
75 NETDATA_WEB_DIR="$( config_option "web files directory" "/usr/share/netdata/web" )"
77 NETDATA_CONF_DIR="conf.d"
80 # -----------------------------------------------------------------------------
81 # prepare the directories
83 echo >&2 "Fixing directory permissions for user ${NETDATA_USER}..."
84 for x in "${NETDATA_WEB_DIR}" "${NETDATA_CONF_DIR}" "${NETDATA_CACHE_DIR}" "${NETDATA_LOG_DIR}"
88 mkdir "${x}" || exit 1
90 chown -R "${NETDATA_USER}" "${x}" || echo >&2 "WARNING: Cannot change the ownership of the files in directory ${x} to ${NETDATA_USER}..."
91 chmod 0775 "${x}" "${x}" || echo >&2 "WARNING: Cannot change the permissions of the directory ${x} to 0755..."
95 # -----------------------------------------------------------------------------
96 # stop a running netdata
98 printf >&2 "Stopping a (possibly) running netdata..."
105 echo >&2 "Cannot stop the running netdata."
110 killall netdata 2>/dev/null
112 test $ret -eq 0 && printf >&2 "." && sleep 2
117 # -----------------------------------------------------------------------------
120 # avoid extended stat(/etc/localtime)
121 # http://stackoverflow.com/questions/4554271/how-to-avoid-excessive-stat-etc-localtime-calls-in-strftime-on-linux
122 export TZ=":/etc/localtime"
124 echo >&2 "Starting netdata..."
130 echo >&2 "SORRY! FAILED TO START NETDATA!"
133 echo >&2 "OK. NetData Started!"
137 # -----------------------------------------------------------------------------
138 # save a config file, if it is not already there
140 if [ ! -s /etc/netdata/netdata.conf ]
142 echo >&2 "Downloading default configuration from netdata..."
145 # remove a possibly obsolete download
146 [ -f /etc/netdata/netdata.conf.new ] && rm /etc/netdata/netdata.conf.new
149 wget 2>/dev/null -O /etc/netdata/netdata.conf.new "http://localhost:${NETDATA_PORT}/netdata.conf"
153 if [ $ret -ne 0 -o ! -s /etc/netdata/netdata.conf.net ]
155 curl -s -o /etc/netdata/netdata.conf.new "http://localhost:${NETDATA_PORT}/netdata.conf"
159 if [ $ret -eq 0 -a -s /etc/netdata/netdata.conf.new ]
161 mv /etc/netdata/netdata.conf.new /etc/netdata/netdata.conf
162 echo >&2 "New configuration saved for you to edit at /etc/netdata/netdata.conf"
164 chown "${NETDATA_USER}" /etc/netdata/netdata.conf
165 chmod 0664 /etc/netdata/netdata.conf
167 echo >&2 "Cannnot download configuration from netdata daemon using url 'http://localhost:${NETDATA_PORT}/netdata.conf'"
168 [ -f /etc/netdata/netdata.conf.new ] && rm /etc/netdata/netdata.conf.new
173 echo >&2 "Hit http://localhost:${NETDATA_PORT}/ from your browser."