]> arthur.barton.de Git - netdata.git/blobdiff - netdata-installer.sh
preserve configs across installaitons
[netdata.git] / netdata-installer.sh
index 80cdb653f5147dbe43dcf9dd45649902aebe9f7e..da0ec82159338c46f0a027a5163b9eede22addfe 100755 (executable)
@@ -338,15 +338,15 @@ run make || exit 1
 
 # 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}"
+               cp -p "${x}" "${x}.installer_backup.${installer_backup_suffix}"
 
-       elif [ -f "${NETDATA_PREFIX}/etc/netdata/${x}.installer_backup.${installer_backup_suffix}" ]
+       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
 
@@ -354,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.${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
 
@@ -376,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
@@ -524,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
 
-echo >&2 "Starting netdata..."
-run ${NETDATA_PREFIX}/usr/sbin/netdata -P ${NETDATA_RUN_DIR}/netdata.pid "${@}"
+issystemd() {
+       # if the directory /etc/systemd/system does not exit, it is not systemd
+       [ ! -d /etc/systemd/system ] && return 1
 
-if [ $? -ne 0 ]
+       # if pid 1 is systemd, it is systemd
+       [ "$(basename $(readlink /proc/1/exe) 2>/dev/null)" = "systemd" ] && return 0
+
+       # 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
-       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
@@ -633,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:
@@ -741,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() {
@@ -782,6 +847,12 @@ cat >netdata-uninstaller.sh <<-UNINSTALL
                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
@@ -831,7 +902,7 @@ cat <<-END
        -------------------------------------------------------------------------------
 
        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.