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 "Cleaning a possibly old compilation ..."
16 echo >&2 "Compiling netdata ..."
20 if [ -z "${netdata}" -o src/netdata -nt "${netdata}" ]
22 echo >&2 "Installing netdata ..."
25 echo >&2 "Adding netdata group ..."
26 getent group netdata > /dev/null || groupadd -r netdata
28 echo >&2 "Adding netdata user ..."
29 getent passwd netdata > /dev/null || useradd -r -g netdata -c netdata -s /sbin/nologin -d / netdata
32 netdata=$(which netdata)
38 echo >&2 "Cannot find my home directory '${base}'."
41 cd "${base}" || exit 1
44 # -----------------------------------------------------------------------------
45 # load options from the configuration file
47 # create an empty config if it does not exist
48 [ ! -f conf.d/netdata.conf ] && touch conf.d/netdata.conf
50 # function to extract values from the config file
52 local key="${1}" value="${2}" line=
54 if [ -s "/etc/netdata/netdata.conf" ]
56 line="$( grep "^[[:space:]]*${key}[[:space:]]*=[[:space:]]*" "conf.d/netdata.conf" | head -n 1 )"
57 [ ! -z "${line}" ] && value="$( echo "${line}" | cut -d '=' -f 2 | sed -e "s/^[[:space:]]\+//g" -e "s/[[:space:]]\+$//g" )"
65 [ ! "${UID}" = "0" ] && defuser="${USER}"
66 NETDATA_USER="$( config_option "run as user" "${defuser}" )"
70 NETDATA_DEBUG="$( config_option "debug flags" ${defdebug} )"
74 NETDATA_PORT="$( config_option "port" ${defport} )"
77 NETDATA_CACHE_DIR="$( config_option "database directory" "/var/cache/netdata" )"
78 NETDATA_WEB_DIR="$( config_option "web files directory" "/usr/share/netdata/web" )"
80 NETDATA_CONF_DIR="conf.d"
83 # -----------------------------------------------------------------------------
84 # prepare the directories
86 echo >&2 "Fixing directory permissions for user ${NETDATA_USER}..."
87 for x in "${NETDATA_WEB_DIR}" "${NETDATA_CONF_DIR}" "${NETDATA_CACHE_DIR}" "${NETDATA_LOG_DIR}"
91 mkdir "${x}" || exit 1
93 chown -R "${NETDATA_USER}" "${x}" || echo >&2 "WARNING: Cannot change the ownership of the files in directory ${x} to ${NETDATA_USER}..."
94 chmod 0775 "${x}" "${x}" || echo >&2 "WARNING: Cannot change the permissions of the directory ${x} to 0755..."
97 # fix apps.plugin to be setuid to root
98 chown root '/usr/libexec/netdata/plugins.d/apps.plugin' && chmod 4755 '/usr/libexec/netdata/plugins.d/apps.plugin'
101 # -----------------------------------------------------------------------------
102 # stop a running netdata
104 printf >&2 "Stopping a (possibly) running netdata..."
111 echo >&2 "Cannot stop the running netdata."
116 killall netdata 2>/dev/null
118 test $ret -eq 0 && printf >&2 "." && sleep 2
123 # -----------------------------------------------------------------------------
126 # avoid extended stat(/etc/localtime)
127 # http://stackoverflow.com/questions/4554271/how-to-avoid-excessive-stat-etc-localtime-calls-in-strftime-on-linux
128 export TZ=":/etc/localtime"
130 echo >&2 "Starting netdata..."
136 echo >&2 "SORRY! FAILED TO START NETDATA!"
139 echo >&2 "OK. NetData Started!"
143 # -----------------------------------------------------------------------------
144 # save a config file, if it is not already there
146 if [ ! -s /etc/netdata/netdata.conf ]
148 echo >&2 "Downloading default configuration from netdata..."
151 # remove a possibly obsolete download
152 [ -f /etc/netdata/netdata.conf.new ] && rm /etc/netdata/netdata.conf.new
155 wget 2>/dev/null -O /etc/netdata/netdata.conf.new "http://localhost:${NETDATA_PORT}/netdata.conf"
159 if [ $ret -ne 0 -o ! -s /etc/netdata/netdata.conf.net ]
161 curl -s -o /etc/netdata/netdata.conf.new "http://localhost:${NETDATA_PORT}/netdata.conf"
165 if [ $ret -eq 0 -a -s /etc/netdata/netdata.conf.new ]
167 mv /etc/netdata/netdata.conf.new /etc/netdata/netdata.conf
168 echo >&2 "New configuration saved for you to edit at /etc/netdata/netdata.conf"
170 chown "${NETDATA_USER}" /etc/netdata/netdata.conf
171 chmod 0664 /etc/netdata/netdata.conf
173 echo >&2 "Cannnot download configuration from netdata daemon using url 'http://localhost:${NETDATA_PORT}/netdata.conf'"
174 [ -f /etc/netdata/netdata.conf.new ] && rm /etc/netdata/netdata.conf.new
179 echo >&2 "Hit http://localhost:${NETDATA_PORT}/ from your browser."