-#!/bin/bash
+#!/usr/bin/env bash
# reload the user profile
[ -f /etc/profile ] && . /etc/profile
USAGE
}
+md5sum="$(which md5sum 2>/dev/null || command -v md5sum 2>/dev/null)"
+get_git_config_signatures() {
+ local x s file md5
+
+ [ ! -d "conf.d" ] && echo >&2 "Wrong directory." && return 1
+ [ -z "${md5sum}" -o ! -x "${md5sum}" ] && echo >&2 "No md5sum command." && return 1
+
+ echo >configs.signatures.tmp
+
+ for x in $(find conf.d -name \*.conf)
+ do
+ x="${x/conf.d\//}"
+ echo "${x}"
+ for c in $(git log --follow "conf.d/${x}" | grep ^commit | cut -d ' ' -f 2)
+ do
+ git checkout ${c} "conf.d/${x}" || continue
+ s="$(cat "conf.d/${x}" | md5sum | cut -d ' ' -f 1)"
+ echo >>configs.signatures.tmp "${x}:${s}"
+ echo " ${s}"
+ done
+ git checkout HEAD "conf.d/${x}" || break
+ done
+
+ cat configs.signatures.tmp |\
+ grep -v "^$" |\
+ sort -u |\
+ {
+ echo "declare -A configs_signatures=("
+ IFS=":"
+ while read file md5
+ do
+ echo " ['${md5}']='${file}'"
+ done
+ echo ")"
+ } >configs.signatures
+
+ rm configs.signatures.tmp
+
+ return 0
+}
+
+
while [ ! -z "${1}" ]
do
if [ "$1" = "--install" ]
then
usage
exit 1
+ elif [ "$1" = "get_git_config_signatures" ]
+ then
+ get_git_config_signatures && exit 0
+ exit 1
else
echo >&2
echo >&2 "ERROR:"
echo >&2 "Compiling netdata ..."
run make || exit 1
+declare -A configs_signatures=()
+if [ -f "configs.signatures" ]
+ then
+ source "configs.signatures" || echo >&2 "ERROR: Failed to load configs.signatures !"
+fi
+
+# migrate existing configuration files
+# for node.d and charts.d
+if [ -d "${NETDATA_PREFIX}/etc/netdata" ]
+ then
+ # the configuration directory exists
+
+ if [ ! -d "${NETDATA_PREFIX}/etc/netdata/charts.d" ]
+ then
+ run mkdir "${NETDATA_PREFIX}/etc/netdata/charts.d"
+ fi
+
+ # move the charts.d config files
+ for x in apache ap cpu_apps cpufreq example exim hddtemp load_average mem_apps mysql nginx nut opensips phpfpm postfix sensors squid tomcat
+ do
+ for y in "" ".old" ".orig"
+ do
+ if [ -f "${NETDATA_PREFIX}/etc/netdata/${x}.conf${y}" -a ! -f "${NETDATA_PREFIX}/etc/netdata/charts.d/${x}.conf${y}" ]
+ then
+ run mv -f "${NETDATA_PREFIX}/etc/netdata/${x}.conf${y}" "${NETDATA_PREFIX}/etc/netdata/charts.d/${x}.conf${y}"
+ fi
+ done
+ done
+
+ if [ ! -d "${NETDATA_PREFIX}/etc/netdata/node.d" ]
+ then
+ run mkdir "${NETDATA_PREFIX}/etc/netdata/node.d"
+ fi
+
+ # move the node.d config files
+ for x in named sma_webbox snmp
+ do
+ for y in "" ".old" ".orig"
+ do
+ if [ -f "${NETDATA_PREFIX}/etc/netdata/${x}.conf${y}" ]
+ then
+ run mv -f "${NETDATA_PREFIX}/etc/netdata/${x}.conf${y}" "${NETDATA_PREFIX}/etc/netdata/node.d/${x}.conf${y}"
+ fi
+ done
+ done
+fi
+
# backup user configurations
installer_backup_suffix="${PID}.${RANDOM}"
-for x in apps_groups.conf charts.d.conf
+for x in $(find "${NETDATA_PREFIX}/etc/netdata/" -name '*.conf' -type f)
do
- if [ -f "${NETDATA_PREFIX}/etc/netdata/${x}" ]
+ if [ -f "${x}" ]
then
- cp -p "${NETDATA_PREFIX}/etc/netdata/${x}" "${NETDATA_PREFIX}/etc/netdata/${x}.installer_backup.${installer_backup_suffix}"
+ # make a backup of the configuration file
+ cp -p "${x}" "${x}.old"
- elif [ -f "${NETDATA_PREFIX}/etc/netdata/${x}.installer_backup.${installer_backup_suffix}" ]
+ if [ -z "${md5sum}" -o ! -x "${md5sum}" ]
+ then
+ # we don't have md5sum - keep it
+ cp -p "${x}" "${x}.installer_backup.${installer_backup_suffix}"
+ else
+ # find it relative filename
+ f="${x/*\/etc\/netdata\//}"
+
+ # find its checksum
+ md5="$(cat "${x}" | ${md5sum} | cut -d ' ' -f 1)"
+
+ # copy the original
+ if [ -f "conf.d/${f}" ]
+ then
+ cp "conf.d/${f}" "${x}.orig"
+ fi
+
+ if [ "${configs_signatures[${md5}]}" = "${f}" ]
+ then
+ # it is a stock version - don't keep it
+ echo >&2 "File '${x}' is stock version."
+ else
+ # edited by user - keep it
+ echo >&2 "File '${x}' has been edited by user."
+ cp -p "${x}" "${x}.installer_backup.${installer_backup_suffix}"
+ fi
+ fi
+
+ elif [ -f "${x}.installer_backup.${installer_backup_suffix}" ]
then
- rm -f "${NETDATA_PREFIX}/etc/netdata/${x}.installer_backup.${installer_backup_suffix}"
+ rm -f "${x}.installer_backup.${installer_backup_suffix}"
fi
done
run make install || exit 1
# restore user configurations
-for x in apps_groups.conf charts.d.conf
+for x in $(find "${NETDATA_PREFIX}/etc/netdata/" -name '*.conf' -type f)
do
- if [ -f "${NETDATA_PREFIX}/etc/netdata/${x}.installer_backup.${installer_backup_suffix}" ]
+ if [ -f "${x}.installer_backup.${installer_backup_suffix}" ]
then
- cp -p "${NETDATA_PREFIX}/etc/netdata/${x}.installer_backup.${installer_backup_suffix}" "${NETDATA_PREFIX}/etc/netdata/${x}"
+ cp -p "${x}.installer_backup.${installer_backup_suffix}" "${x}"
+ rm -f "${x}.installer_backup.${installer_backup_suffix}"
fi
done
if [ $? -ne 0 ]
then
echo >&2 "Adding netdata user account ..."
- run useradd -r -g netdata -c netdata -s /sbin/nologin -d / netdata
+ run useradd -r -g netdata -c netdata -s $(which nologin || echo '/bin/false') -d / netdata
fi
getent group docker > /dev/null
echo >&2
echo >&2 "Fixing directories (user: ${NETDATA_USER})..."
-for x in "${NETDATA_WEB_DIR}" "${NETDATA_CONF_DIR}" "${NETDATA_CACHE_DIR}" "${NETDATA_LOG_DIR}" "${NETDATA_LIB_DIR}"
+for x in "${NETDATA_WEB_DIR}" "${NETDATA_CONF_DIR}" "${NETDATA_CACHE_DIR}" "${NETDATA_LOG_DIR}" "${NETDATA_LIB_DIR}" "${NETDATA_CONF_DIR}/python.d" "${NETDATA_CONF_DIR}/charts.d" "${NETDATA_CONF_DIR}/node.d"
do
if [ ! -d "${x}" ]
then
# -----------------------------------------------------------------------------
# check netdata for systemd
-running=0
-pidof systemd >/dev/null 2>&1
-if [ $? -eq 0 -a "${UID}" -eq 0 ]
-then
- # systemd is running on this system
+issystemd() {
+ # if the directory /etc/systemd/system does not exit, it is not systemd
+ [ ! -d /etc/systemd/system ] && return 1
+
+ # if pid 1 is systemd, it is systemd
+ [ "$(basename $(readlink /proc/1/exe) 2>/dev/null)" = "systemd" ] && return 0
- if [ ! -f /etc/systemd/system/netdata.service ]
+ # if systemd is running, it is systemd
+ pidof systemd >/dev/null 2>&1 && return 0
+
+ # else, it is not systemd
+ return 1
+}
+
+started=0
+if [ "${UID}" -eq 0 ]
then
- echo >&2 "Installing systemd service..."
- run cp system/netdata.service /etc/systemd/system/netdata.service && \
- run systemctl daemon-reload && \
- run systemctl enable netdata
- else
- run service netdata stop
- fi
- stop_all_netdata
- run service netdata start && running=1
-fi
+ if issystemd
+ then
+ # systemd is running on this system
-if [ ${running} -eq 0 -a "${UID}" -eq 0 ]
-then
- service netdata status >/dev/null 2>&1
- if [ $? -eq 0 ]
+ if [ ! -f /etc/systemd/system/netdata.service ]
+ then
+ echo >&2 "Installing systemd service..."
+ run cp system/netdata.service /etc/systemd/system/netdata.service && \
+ run systemctl daemon-reload && \
+ run systemctl enable netdata
+ else
+ run service netdata stop
+ fi
+
+ stop_all_netdata
+ run service netdata restart && started=1
+ fi
+
+ if [ ${started} -eq 0 ]
then
- # nice guy, he installed netdata to his system
+ # check if we can use the system service
run service netdata stop
stop_all_netdata
- run service netdata start && running=1
+ run service netdata restart && started=1
+ if [ ${started} -eq 0 ]
+ then
+ run service netdata start && started=1
+ fi
fi
fi
-if [ ${running} -eq 0 ]
+if [ ${started} -eq 0 ]
then
- # still not running...
+ # still not started...
stop_all_netdata
fi
echo >&2 "Stopping a possibly running netdata..."
- for p in \$(pidof netdata); do kill \$x; done
+ for p in \$(pidof netdata); do kill \$p; done
sleep 2
deletedir() {
-------------------------------------------------------------------------------
INFO: Command line options changed. -pidfile, -nd and -ch are deprecated.
- If you use custom stratup scripts please run netdata -h to see the
+ If you use custom startup scripts, please run netdata -h to see the
corresponding options and update your scripts.
Hit http://${access}:${NETDATA_PORT}/ from your browser.