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 ..."
18 echo >&2 "Compiling netdata ..."
21 if [ -z "${netdata}" -o src/netdata -nt "${netdata}" ]
23 echo >&2 "Installing netdata ..."
26 echo >&2 "Adding netdata group ..."
27 getent group netdata > /dev/null || groupadd -r netdata
29 echo >&2 "Adding netdata user ..."
30 getent passwd netdata > /dev/null || useradd -r -g netdata -c netdata -s /sbin/nologin -d / netdata
33 netdata=$(which netdata)
36 # -----------------------------------------------------------------------------
37 # load options from the configuration file
39 # create an empty config if it does not exist
40 [ ! -f conf.d/netdata.conf ] && touch conf.d/netdata.conf
42 # function to extract values from the config file
44 local key="${1}" value="${2}" line=
46 if [ -s "/etc/netdata/netdata.conf" ]
48 line="$( grep "^[[:space:]]*${key}[[:space:]]*=[[:space:]]*" "/etc/netdata/netdata.conf" | head -n 1 )"
49 [ ! -z "${line}" ] && value="$( echo "${line}" | cut -d '=' -f 2 | sed -e "s/^[[:space:]]\+//g" -e "s/[[:space:]]\+$//g" )"
57 [ ! "${UID}" = "0" ] && defuser="${USER}"
58 NETDATA_USER="$( config_option "run as user" "${defuser}" )"
62 NETDATA_DEBUG="$( config_option "debug flags" ${defdebug} )"
66 NETDATA_PORT="$( config_option "port" ${defport} )"
69 NETDATA_CACHE_DIR="$( config_option "cache directory" "/var/cache/netdata" )"
70 NETDATA_WEB_DIR="$( config_option "web files directory" "/usr/share/netdata/web" )"
71 NETDATA_LOG_DIR="$( config_option "log directory" "/var/log/netdata" )"
72 NETDATA_CONF_DIR="$( config_option "config directory" "/etc/netdata" )"
75 # -----------------------------------------------------------------------------
76 # prepare the directories
78 echo >&2 "Fixing directory permissions for user ${NETDATA_USER}..."
79 for x in "${NETDATA_WEB_DIR}" "${NETDATA_CONF_DIR}" "${NETDATA_CACHE_DIR}" "${NETDATA_LOG_DIR}"
83 echo >&2 "Creating directory '${x}'"
84 mkdir -p "${x}" || exit 1
86 chown -R "${NETDATA_USER}" "${x}" || echo >&2 "WARNING: Cannot change the ownership of the files in directory ${x} to ${NETDATA_USER}..."
87 chmod 0775 "${x}" "${x}" || echo >&2 "WARNING: Cannot change the permissions of the directory ${x} to 0755..."
90 # fix apps.plugin to be setuid to root
91 chown root '/usr/libexec/netdata/plugins.d/apps.plugin' && chmod 4755 '/usr/libexec/netdata/plugins.d/apps.plugin'
94 # -----------------------------------------------------------------------------
95 # stop a running netdata
97 printf >&2 "Stopping a (possibly) running netdata..."
104 echo >&2 "Cannot stop the running netdata."
109 killall netdata 2>/dev/null
111 test $ret -eq 0 && printf >&2 "." && sleep 2
116 # -----------------------------------------------------------------------------
119 echo >&2 "Starting netdata..."
125 echo >&2 "SORRY! FAILED TO START NETDATA!"
128 echo >&2 "OK. NetData Started!"
132 # -----------------------------------------------------------------------------
133 # save a config file, if it is not already there
135 if [ ! -s /etc/netdata/netdata.conf ]
137 echo >&2 "Downloading default configuration from netdata..."
140 # remove a possibly obsolete download
141 [ -f /etc/netdata/netdata.conf.new ] && rm /etc/netdata/netdata.conf.new
144 wget 2>/dev/null -O /etc/netdata/netdata.conf.new "http://localhost:${NETDATA_PORT}/netdata.conf"
148 if [ $ret -ne 0 -o ! -s /etc/netdata/netdata.conf.net ]
150 curl -s -o /etc/netdata/netdata.conf.new "http://localhost:${NETDATA_PORT}/netdata.conf"
154 if [ $ret -eq 0 -a -s /etc/netdata/netdata.conf.new ]
156 mv /etc/netdata/netdata.conf.new /etc/netdata/netdata.conf
157 echo >&2 "New configuration saved for you to edit at /etc/netdata/netdata.conf"
159 chown "${NETDATA_USER}" /etc/netdata/netdata.conf
160 chmod 0664 /etc/netdata/netdata.conf
162 echo >&2 "Cannnot download configuration from netdata daemon using url 'http://localhost:${NETDATA_PORT}/netdata.conf'"
163 [ -f /etc/netdata/netdata.conf.new ] && rm /etc/netdata/netdata.conf.new
168 echo >&2 "Hit http://localhost:${NETDATA_PORT}/ from your browser."