#!/bin/bash
-base="`dirname "$0"`"
+netdata=$(which netdata)
-if [ ! -d "$base" -o ! -f "$base/netdata.c" ]
+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
-# every how many seconds to show the graphs
-NETDATA_CONFIG_UPDATE_EVERY=2
+ 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
-# the detail of the data that will be kept in netdata
-NETDATA_CONFIG_INTERNAL_UPDATE_EVERY=1
+ echo >&2 "Cleaning a possibly old compilation ..."
+ make clean || exit 1
-# how many points should the graphs have
-NETDATA_CONFIG_HISTORY_POINTS=120
+ echo >&2 "Compiling netdata ..."
+ make || exit 1
+fi
-# how many points should be kept in memory
-NETDATA_CONFIG_HISTORY_DATA=3600
+if [ -z "${netdata}" -o src/netdata -nt "${netdata}" ]
+then
+ echo >&2 "Installing netdata ..."
+ make install
-# every how many graph refreshes, the page should be reloaded
-# this fixes a bug in the google graphs API which leaks memory
-# when refreshes graphs
-NETDATA_CONFIG_RELOAD_EVERY=500
+ echo >&2 "Adding netdata group ..."
+ getent group netdata > /dev/null || groupadd -r netdata
-if [ -f netdata.conf ]
-then
- source netdata.conf
+ echo >&2 "Adding netdata user ..."
+ getent passwd netdata > /dev/null || useradd -r -g netdata -c netdata -s /sbin/nologin -d / netdata
fi
-# how many history lines to keep in netdata
-NETDATA_HISTORY_LINES=$[NETDATA_CONFIG_UPDATE_EVERY * NETDATA_CONFIG_HISTORY_POINTS / NETDATA_CONFIG_INTERNAL_UPDATE_EVERY]
+netdata=$(which netdata)
-echo "Creating a directory for netdata..."
-data=
-for x in /run/netdata /var/run/netdata /tmp/netdata
-do
- echo " Trying '${x}'..."
- if [ ! -d "${x}" ]
- then
- mkdir "${x}" 2>/dev/null
- if [ $? -eq 0 ]
- then
- echo " OK. '${x}' works."
- data="${x}"
- break
- fi
- else
- echo " OK. '${x}' works."
- data="${x}"
- break
- fi
-done
+base="`dirname "$0"`"
-if [ -z "${data}" ]
+if [ ! -d "${base}" ]
then
- echo >&2 "Cannot find where to put netdata files."
+ echo >&2 "Cannot find my home directory '${base}'."
exit 1
fi
+cd "${base}" || exit 1
-if [ -h data ]
-then
- echo "Removing existing $base/data link"
- rm data || exit 1
-fi
-if [ ! -d data ]
-then
- echo "Linking '${data}' to $base/data"
- ln -s "${data}" data || exit 1
-else
- echo >&2 "Directory $base/data already exists. Not touching it, however it should be a link '${data}'."
-fi
+# -----------------------------------------------------------------------------
+# load options from the configuration file
-cp "${base}/all.xsl" "${data}/"
-cp "${base}/netdata.js" "${data}/"
-cp "${base}/tc-all.sh" "${data}/"
-chmod 700 "${data}/tc-all.sh"
+# create an empty config if it does not exist
+[ ! -f conf.d/netdata.conf ] && touch conf.d/netdata.conf
-echo "Finding proper parameters for dashboard..."
+# function to extract values from the config file
+config_option() {
+ local key="${1}" value="${2}" line=
-echo "Stopping a (possibly) running netdata..."
-killall netdata 2>/dev/null
-killall tc-all.sh 2>/dev/null
+ 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
-sleep 2
+ echo "${value}"
+}
-echo "Compiling netdata"
-# gcc -Wall -O3 -o netdata netdata.c -lpthread || exit 1
-gcc -Wall -ggdb -o netdata netdata.c -lpthread || exit 1
+# user
+defuser="netdata"
+[ ! "${UID}" = "0" ] && defuser="${USER}"
+NETDATA_USER="$( config_option "run as user" "${defuser}" )"
-echo "Starting netdata"
-p=`pwd`
-cd data
-ulimit -c unlimited
-$p/netdata -d -u $NETDATA_CONFIG_INTERNAL_UPDATE_EVERY -l $NETDATA_HISTORY_LINES || exit 1
-cd "$p"
+# debug flags
+defdebug=0
+NETDATA_DEBUG="$( config_option "debug flags" ${defdebug} )"
-sleep 2
+# port
+defport=19999
+NETDATA_PORT="$( config_option "port" ${defport} )"
-# count all graphs
-all=`wget http://127.0.0.1:19999/list -O - 2>/dev/null`
-count=0
-for x in $all
-do
- y=`echo "$x" | tr ".-" "__"`
- eval "t=\${NETDATA_PRIORITY_${y}}"
- if [ ! "$t" = "IGNORE" ]
- then
- count=$[count + 1]
- fi
-done
+# 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 "We have $count graphs..."
-
-echo "Generating ${data}/index.html"
-host="`hostname`"
-cat >${data}/index.html <<EOF
-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-<html xmlns="http://www.w3.org/1999/xhtml">
-<style>
- * {font-family:Arial}
- div {float: left; margin: 0 0 0 0; }
-</style>
-<title>${host} netdata</title>
-<head>
- <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
-
- <!--Load the AJAX API-->
- <script type="text/javascript" src="https://www.google.com/jsapi"></script>
- <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
- <script type="text/javascript" src="/file/netdata.js"></script>
- <script type="text/javascript">
-
- // Set a callback to run when the Google Visualization API is loaded.
- google.setOnLoadCallback(drawCharts);
-
- function drawCharts() {
- // EDIT: add one line per interface you have
- // EDIT:
- // EDIT: name div id json data graph vertical axis title
- // EDIT: --------------------------------------------------------------------------------------------------
-EOF
-
-for x in $all
+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
- y=`echo "$x" | tr ".-" "__"`
-
- title=
- vtitle=
- case "$x" in
- net.*)
- title="Network Usage for "
- vtitle="bandwidth in kilobits/s"
- ;;
-
- disk.*)
- title="Disk I/O for "
- vtitle="I/O in kilobytes/s"
- ;;
-
- tc.*)
- title="Class utilization for "
- vtitle="bandwidth in kilobits/s"
- ;;
-
- esac
-
- eval "NETDATA_TITLE_${y}=\${NETDATA_TITLE_${y}:-${x}}"
- eval "t=\${NETDATA_TITLE_${y}}"
-
- eval "p=\${NETDATA_PRIORITY_${y}}"
- if [ ! "$p" = "IGNORE" ]
- then
- cat >>${data}/index.html <<EOF2
- addChart('${x}', '${y}_div', 0, 0, "data/${x}/${NETDATA_CONFIG_HISTORY_POINTS}/${NETDATA_CONFIG_UPDATE_EVERY}/average/", "${title}${t} (${x})", "${vtitle}");
-EOF2
+ 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
-cat >>${data}/index.html <<EOF3
- refreshCharts(999999);
- }
-
- var refreshCount = 0;
- function myChartsRefresh() {
- refreshCount += 2;
- if(refreshCount > $NETDATA_CONFIG_RELOAD_EVERY) location.reload();
-
- // refresh up to 2 charts per second
- refreshCharts(2);
- }
-
- setInterval(myChartsRefresh, 1000);
-
- //window.onresize = function(event) {
- // refreshCharts(999999);
- //};
- </script>
-
- </head>
-
-<body>
- <!--
- EDIT: add one div per interface you have
- EDIT: use the same id above and bellow!
- -->
-EOF3
-
-tmp="/tmp/$RANDOM.netdata.$$.$RANDOM"
-for x in $all
+# 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
- y=`echo "$x" | tr ".-" "__"`
- eval "NETDATA_PRIORITY_${y}=\${NETDATA_PRIORITY_${y}:-${x}}"
- eval "t=\${NETDATA_PRIORITY_${y}}"
- if [ ! "$t" = "IGNORE" ]
- then
- echo "${t}|${y}"
+ if [ $count -gt 15 ]
+ then
+ echo >&2 "Cannot stop the running netdata."
+ exit 1
fi
-done >"${tmp}"
-for x in `cat "${tmp}" | sort`
-do
- n="`echo "$x" | cut -d '|' -f 2-`"
- cat >>${data}/index.html <<EOF4
- <div id="${n}_div"></div>
-EOF4
+ count=$((count + 1))
+ killall netdata 2>/dev/null
+ ret=$?
+ test $ret -eq 0 && printf >&2 "." && sleep 2
done
-rm -f "${tmp}"
+echo >&2
-cat >>${data}/index.html <<EOF5
- </body>
-</html>
-EOF5
-set|grep ^NETDATA_ >netdata.conf
+# -----------------------------------------------------------------------------
+# run netdata
-if [ ! -h "${data}/data" ]
-then
- cd "${data}"
- ln -s . data
+# 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
+ echo >&2 "OK. NetData Started!"
fi
-echo "All Done."
-echo "Just hit http://127.0.0.1:19999/ from your browser."
+# -----------------------------------------------------------------------------
+# save a config file, if it is not already there
+
+if [ ! -s /etc/netdata/netdata.conf ]
+ then
+ echo >&2 "Downloading default configuration from netdata..."
+ sleep 5
+
+ # 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=$?
+
+ # 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
+
+ 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 >&2
+echo >&2 "Hit http://localhost:${NETDATA_PORT}/ from your browser."
+echo >&2
+echo >&2 "Enjoy..."
+echo >&2