]> arthur.barton.de Git - netdata.git/blobdiff - netdata-installer.sh
Merge pull request #574 from paulfantom/master
[netdata.git] / netdata-installer.sh
index 11597c346c7a138862b4bf166616a26378760d1f..da0ec82159338c46f0a027a5163b9eede22addfe 100755 (executable)
@@ -303,7 +303,7 @@ run() {
 if [ ${LIBS_ARE_HERE} -eq 1 ]
        then
        shift
-       echo >&2 "ok, assuming zlib is really installed."
+       echo >&2 "ok, assuming libs are really installed."
        export ZLIB_CFLAGS=" "
        export ZLIB_LIBS="-lz"
        export UUID_CFLAGS=" "
@@ -337,11 +337,16 @@ echo >&2 "Compiling netdata ..."
 run make || exit 1
 
 # backup user configurations
-for x in apps_groups.conf charts.d.conf
+installer_backup_suffix="${PID}.${RANDOM}"
+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"
+               cp -p "${x}" "${x}.installer_backup.${installer_backup_suffix}"
+
+       elif [ -f "${x}.installer_backup.${installer_backup_suffix}" ]
+               then
+               rm -f "${x}.installer_backup.${installer_backup_suffix}"
        fi
 done
 
@@ -349,11 +354,12 @@ echo >&2 "Installing netdata ..."
 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" ]
+       if [ -f "${x}.installer_backup.${installer_backup_suffix}" ]
                then
-               cp -p "${NETDATA_PREFIX}/etc/netdata/${x}.installer_backup" "${NETDATA_PREFIX}/etc/netdata/${x}"
+               cp -p "${x}.installer_backup.${installer_backup_suffix}" "${x}"
+               rm -f "${x}.installer_backup.${installer_backup_suffix}"
        fi
 done
 
@@ -371,7 +377,7 @@ if [ ${UID} -eq 0 ]
        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
@@ -391,6 +397,12 @@ if [ ${UID} -eq 0 ]
                # let the uninstall script know
                NETDATA_ADDED_TO_DOCKER=1
        fi
+
+       if [ -d /etc/logrotate.d -a ! -f /etc/logrotate.d/netdata ]
+               then
+               echo >&2 "Adding netdata logrotate configuration ..."
+               run cp system/netdata.logrotate /etc/logrotate.d/netdata
+       fi
 fi
 
 
@@ -448,6 +460,7 @@ if [ ! -d "${NETDATA_RUN_DIR}" ]
        mkdir -p "${NETDATA_RUN_DIR}" || exit 1
 fi
 
+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}"
 do
@@ -512,60 +525,124 @@ isnetdata() {
        return 1
 }
 
+stop_netdata_on_pid() {
+       local pid="$1" ret=0 count=0
 
-echo >&2
-echo >&2 "-------------------------------------------------------------------------------"
-echo >&2
-printf >&2 "Stopping a (possibly) running netdata..."
-ret=0
-count=0
-while [ $ret -eq 0 ]
-do
-       if [ $count -gt 30 ]
-               then
-               echo >&2 "Cannot stop the running netdata."
-               exit 1
-       fi
+       isnetdata $pid || return 0
 
-       count=$((count + 1))
+       printf >&2 "Stopping netdata on pid $pid ..."
+       while [ ! -z "$pid" -a $ret -eq 0 ]
+       do
+               if [ $count -gt 45 ]
+                       then
+                       echo >&2 "Cannot stop the running netdata on pid $pid."
+                       return 1
+               fi
+
+               count=$(( count + 1 ))
 
-       pid=$(cat "${NETDATA_RUN_DIR}/netdata.pid" 2>/dev/null)
-       # backwards compatibility
-       [ -z "${pid}" ] && pid=$(cat /var/run/netdata.pid 2>/dev/null)
-       [ -z "${pid}" ] && pid=$(cat /var/run/netdata/netdata.pid 2>/dev/null)
-       
-       isnetdata $pid || pid=
-       if [ ! -z "${pid}" ]
-               then
                run kill $pid 2>/dev/null
                ret=$?
-       else
-               run killall netdata 2>/dev/null
-               ret=$?
+
+               test $ret -eq 0 && printf >&2 "." && sleep 2
+       done
+
+       echo >&2
+       if [ $ret -eq 0 ]
+       then
+               echo >&2 "SORRY! CANNOT STOP netdata ON PID $pid !"
+               return 1
        fi
 
-       test $ret -eq 0 && printf >&2 "." && sleep 2
-done
-echo >&2
-echo >&2
+       echo >&2 "netdata on pid $pid stopped."
+       return 0
+}
+
+stop_all_netdata() {
+       local p
 
+       echo >&2 "Stopping a (possibly) running netdata..."
+
+       for p in $(cat "${NETDATA_RUN_DIR}/netdata.pid" 2>/dev/null) \
+               $(cat /var/run/netdata.pid 2>/dev/null) \
+               $(cat /var/run/netdata/netdata.pid 2>/dev/null) \
+               $(pidof netdata 2>/dev/null)
+       do
+               stop_netdata_on_pid $p
+       done
+}
 
 # -----------------------------------------------------------------------------
-# run netdata
+# check netdata for systemd
+
+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
 
-echo >&2 "Starting netdata..."
-run ${NETDATA_PREFIX}/usr/sbin/netdata -pidfile ${NETDATA_RUN_DIR}/netdata.pid "${@}"
+       # if systemd is running, it is systemd
+       pidof systemd >/dev/null 2>&1 && return 0
 
-if [ $? -ne 0 ]
+       # else, it is not systemd
+       return 1
+}
+
+started=0
+if [ "${UID}" -eq 0 ]
        then
-       echo >&2
-       echo >&2 "SORRY! FAILED TO START NETDATA!"
-       exit 1
-else
-       echo >&2 "OK. NetData Started!"
+
+       if issystemd
+       then
+               # systemd is running on this system
+
+               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
+               # check if we can use the system service
+               run service netdata stop
+               stop_all_netdata
+               run service netdata restart && started=1
+               if [ ${started} -eq 0 ]
+               then
+                       run service netdata start && started=1
+               fi
+       fi
 fi
-echo >&2
 
+if [ ${started} -eq 0 ]
+then
+       # still not started...
+
+       stop_all_netdata
+
+       echo >&2 "Starting netdata..."
+       run ${NETDATA_PREFIX}/usr/sbin/netdata -P ${NETDATA_RUN_DIR}/netdata.pid "${@}"
+       if [ $? -ne 0 ]
+               then
+               echo >&2
+               echo >&2 "SORRY! FAILED TO START NETDATA!"
+               exit 1
+       else
+               echo >&2 "OK. NetData Started!"
+       fi
+
+       echo >&2
+fi
 
 # -----------------------------------------------------------------------------
 # save a config file, if it is not already there
@@ -621,7 +698,7 @@ ksm_is_available_but_disabled() {
        -------------------------------------------------------------------------------
        Memory de-duplication instructions
 
-       I see you have kernel memory de-duper (called Kernel Same-page Merging,
+       You have kernel memory de-duper (called Kernel Same-page Merging,
        or KSM) available, but it is not currently enabled.
 
        To enable it run:
@@ -706,8 +783,6 @@ if [ "${UID}" -ne 0 ]
                sudo chown root "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin"
                sudo chmod 4755 "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin"
 
-       These commands allow apps.plugin to run as root.
-
        apps.plugin is performing a hard-coded function of data collection for all
        running processes. It cannot be instructed from the netdata daemon to perform
        any task, so it is pretty safe to do this.
@@ -731,7 +806,7 @@ cat >netdata-uninstaller.sh <<-UNINSTALL
        fi
 
        echo >&2 "Stopping a possibly running netdata..."
-       killall netdata
+       for p in \$(pidof netdata); do kill \$x; done
        sleep 2
 
        deletedir() {
@@ -766,6 +841,18 @@ cat >netdata-uninstaller.sh <<-UNINSTALL
                deletedir "${NETDATA_PREFIX}/var/log/netdata"
        fi
 
+       if [ -f /etc/logrotate.d/netdata ]
+               then
+               echo "Deleting /etc/logrotate.d/netdata ..."
+               rm -i /etc/logrotate.d/netdata
+       fi
+
+       if [ -f /etc/systemd/system/netdata.service ]
+               then
+               echo "Deleting /etc/systemd/system/netdata.service ..."
+               rm -i /etc/systemd/system/netdata.service
+       fi
+
        getent passwd netdata > /dev/null
        if [ $? -eq 0 ]
                then
@@ -814,6 +901,9 @@ cat <<-END
 
        -------------------------------------------------------------------------------
 
+       INFO: Command line options changed. -pidfile, -nd and -ch are deprecated.
+       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.