install-or-update.sh 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. #!/usr/bin/env bash
  2. # SPDX-License-Identifier: GPL-3.0-or-later
  3. # shellcheck source=./packaging/makeself/functions.sh
  4. . "$(dirname "${0}")"/functions.sh
  5. export LC_ALL=C
  6. umask 002
  7. # Be nice on production environments
  8. renice 19 $$ > /dev/null 2> /dev/null
  9. NETDATA_PREFIX="/opt/netdata"
  10. NETDATA_USER_CONFIG_DIR="${NETDATA_PREFIX}/etc/netdata"
  11. # -----------------------------------------------------------------------------
  12. if [ -d /opt/netdata/etc/netdata.old ]; then
  13. progress "Found old etc/netdata directory, reinstating this"
  14. [ -d /opt/netdata/etc/netdata.new ] && rm -rf /opt/netdata/etc/netdata.new
  15. mv -f /opt/netdata/etc/netdata /opt/netdata/etc/netdata.new
  16. mv -f /opt/netdata/etc/netdata.old /opt/netdata/etc/netdata
  17. progress "Trigger stock config clean up"
  18. rm -f /opt/netdata/etc/netdata/.installer-cleanup-of-stock-configs-done
  19. fi
  20. STARTIT=1
  21. REINSTALL_OPTIONS=""
  22. RELEASE_CHANNEL="nightly"
  23. while [ "${1}" ]; do
  24. case "${1}" in
  25. "--dont-start-it")
  26. STARTIT=0
  27. REINSTALL_OPTIONS="${REINSTALL_OPTIONS} ${1}"
  28. ;;
  29. "--auto-update" | "-u") ;;
  30. "--stable-channel")
  31. RELEASE_CHANNEL="stable"
  32. REINSTALL_OPTIONS="${REINSTALL_OPTIONS} ${1}"
  33. ;;
  34. "--nightly-channel")
  35. RELEASE_CHANNEL="nightly"
  36. REINSTALL_OPTIONS="${REINSTALL_OPTIONS} ${1}"
  37. ;;
  38. "--disable-telemetry")
  39. NETDATA_DISABLE_TELEMETRY=1
  40. REINSTALL_OPTIONS="${REINSTALL_OPTIONS} ${1}"
  41. ;;
  42. *) echo >&2 "Unknown option '${1}'. Ignoring it." ;;
  43. esac
  44. shift 1
  45. done
  46. if [ ! "${DISABLE_TELEMETRY:-0}" -eq 0 ] ||
  47. [ -n "$DISABLE_TELEMETRY" ] ||
  48. [ ! "${DO_NOT_TRACK:-0}" -eq 0 ] ||
  49. [ -n "$DO_NOT_TRACK" ]; then
  50. NETDATA_DISABLE_TELEMETRY=1
  51. REINSTALL_OPTIONS="${REINSTALL_OPTIONS} --disable-telemetry"
  52. fi
  53. # -----------------------------------------------------------------------------
  54. progress "Attempt to create user/group netdata/netadata"
  55. NETDATA_WANTED_GROUPS="docker nginx varnish haproxy adm nsd proxy squid ceph nobody I2C"
  56. NETDATA_ADDED_TO_GROUPS=""
  57. # Default user/group
  58. NETDATA_USER="root"
  59. NETDATA_GROUP="root"
  60. if portable_add_group netdata; then
  61. if portable_add_user netdata "/opt/netdata"; then
  62. progress "Add user netdata to required user groups"
  63. for g in ${NETDATA_WANTED_GROUPS}; do
  64. # shellcheck disable=SC2086
  65. if portable_add_user_to_group ${g} netdata; then
  66. NETDATA_ADDED_TO_GROUPS="${NETDATA_ADDED_TO_GROUPS} ${g}"
  67. else
  68. run_failed "Failed to add netdata user to secondary groups"
  69. fi
  70. done
  71. # Netdata must be able to read /etc/pve/qemu-server/* and /etc/pve/lxc/*
  72. # for reading VMs/containers names, CPU and memory limits on Proxmox.
  73. if [ -d "/etc/pve" ]; then
  74. portable_add_user_to_group "www-data" netdata && NETDATA_ADDED_TO_GROUPS="${NETDATA_ADDED_TO_GROUPS} www-data"
  75. fi
  76. NETDATA_USER="netdata"
  77. NETDATA_GROUP="netdata"
  78. else
  79. run_failed "I could not add user netdata, will be using root"
  80. fi
  81. else
  82. run_failed "I could not add group netdata, so no user netdata will be created as well. Netdata run as root:root"
  83. fi
  84. # -----------------------------------------------------------------------------
  85. progress "Install logrotate configuration for netdata"
  86. install_netdata_logrotate || run_failed "Cannot install logrotate file for netdata."
  87. # -----------------------------------------------------------------------------
  88. progress "Telemetry configuration"
  89. # Opt-out from telemetry program
  90. if [ -n "${NETDATA_DISABLE_TELEMETRY}" ]; then
  91. run touch "${NETDATA_USER_CONFIG_DIR}/.opt-out-from-anonymous-statistics"
  92. else
  93. printf "You can opt out from anonymous statistics via the --disable-telemetry option, or by creating an empty file %s \n\n" "${NETDATA_USER_CONFIG_DIR}/.opt-out-from-anonymous-statistics"
  94. fi
  95. # -----------------------------------------------------------------------------
  96. progress "Install netdata at system init"
  97. install_netdata_service || run_failed "Cannot install netdata init service."
  98. set_netdata_updater_channel || run_failed "Cannot set netdata updater tool release channel to '${RELEASE_CHANNEL}'"
  99. # -----------------------------------------------------------------------------
  100. progress "Install (but not enable) netdata updater tool"
  101. install_netdata_updater || run_failed "Cannot install netdata updater tool."
  102. # -----------------------------------------------------------------------------
  103. progress "creating quick links"
  104. dir_should_be_link() {
  105. local p="${1}" t="${2}" d="${3}" old
  106. old="${PWD}"
  107. cd "${p}" || return 0
  108. if [ -e "${d}" ]; then
  109. if [ -h "${d}" ]; then
  110. run rm "${d}"
  111. else
  112. run mv -f "${d}" "${d}.old.$$"
  113. fi
  114. fi
  115. run ln -s "${t}" "${d}"
  116. cd "${old}" || true
  117. }
  118. dir_should_be_link . bin sbin
  119. dir_should_be_link usr ../bin bin
  120. dir_should_be_link usr ../bin sbin
  121. dir_should_be_link usr . local
  122. dir_should_be_link . etc/netdata netdata-configs
  123. dir_should_be_link . usr/share/netdata/web netdata-web-files
  124. dir_should_be_link . usr/libexec/netdata netdata-plugins
  125. dir_should_be_link . var/lib/netdata netdata-dbs
  126. dir_should_be_link . var/cache/netdata netdata-metrics
  127. dir_should_be_link . var/log/netdata netdata-logs
  128. dir_should_be_link etc/netdata ../../usr/lib/netdata/conf.d orig
  129. # -----------------------------------------------------------------------------
  130. progress "fix permissions"
  131. run chmod g+rx,o+rx /opt
  132. run find /opt/netdata -type d -exec chmod go+rx '{}' \+
  133. run chown -R ${NETDATA_USER}:${NETDATA_GROUP} /opt/netdata/var
  134. # -----------------------------------------------------------------------------
  135. progress "changing plugins ownership and permissions"
  136. if command -v setcap >/dev/null 2>&1; then
  137. run setcap "cap_dac_read_search,cap_sys_ptrace=ep" "usr/libexec/netdata/plugins.d/apps.plugin"
  138. run setcap "cap_dac_read_search=ep" "usr/libexec/netdata/plugins.d/slabinfo.plugin"
  139. run setcap "cap_dac_read_search=ep" "usr/libexec/netdata/plugins.d/debugfs.plugin"
  140. if command -v capsh >/dev/null 2>&1 && capsh --supports=cap_perfmon 2>/dev/null ; then
  141. run setcap "cap_perfmon=ep" "usr/libexec/netdata/plugins.d/perf.plugin"
  142. else
  143. run setcap "cap_sys_admin=ep" "usr/libexec/netdata/plugins.d/perf.plugin"
  144. fi
  145. run setcap "cap_net_admin,cap_net_raw=eip" "usr/libexec/netdata/plugins.d/go.d.plugin"
  146. else
  147. for x in apps.plugin perf.plugin slabinfo.plugin debugfs.plugin; do
  148. f="usr/libexec/netdata/plugins.d/${x}"
  149. run chown root:${NETDATA_GROUP} "${f}"
  150. run chmod 4750 "${f}"
  151. done
  152. fi
  153. for x in freeipmi.plugin ioping cgroup-network ebpf.plugin nfacct.plugin xenstat.plugin; do
  154. f="usr/libexec/netdata/plugins.d/${x}"
  155. if [ -f "${f}" ]; then
  156. run chown root:${NETDATA_GROUP} "${f}"
  157. run chmod 4750 "${f}"
  158. fi
  159. done
  160. # -----------------------------------------------------------------------------
  161. echo "Configure TLS certificate paths"
  162. if [ ! -L /opt/netdata/etc/ssl ] && [ -d /opt/netdata/etc/ssl ] ; then
  163. echo "Preserving existing user configuration for TLS"
  164. else
  165. if [ -d /etc/pki/tls ] ; then
  166. echo "Using /etc/pki/tls for TLS configuration and certificates"
  167. ln -sf /etc/pki/tls /opt/netdata/etc/ssl
  168. elif [ -d /etc/ssl ] ; then
  169. echo "Using /etc/ssl for TLS configuration and certificates"
  170. ln -sf /etc/ssl /opt/netdata/etc/ssl
  171. else
  172. echo "Using bundled TLS configuration and certificates"
  173. ln -sf /opt/netdata/share/ssl /opt/netdata/etc/ssl
  174. fi
  175. fi
  176. # -----------------------------------------------------------------------------
  177. echo "Save install options"
  178. grep -qv 'IS_NETDATA_STATIC_BINARY="yes"' "${NETDATA_PREFIX}/etc/netdata/.environment" || echo IS_NETDATA_STATIC_BINARY=\"yes\" >> "${NETDATA_PREFIX}/etc/netdata/.environment"
  179. sed -i "s/REINSTALL_OPTIONS=\".*\"/REINSTALL_OPTIONS=\"${REINSTALL_OPTIONS}\"/" "${NETDATA_PREFIX}/etc/netdata/.environment"
  180. # -----------------------------------------------------------------------------
  181. if [ ${STARTIT} -eq 0 ]; then
  182. create_netdata_conf "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
  183. netdata_banner "is installed now!"
  184. else
  185. progress "starting netdata"
  186. if ! restart_netdata "${NETDATA_PREFIX}/bin/netdata"; then
  187. create_netdata_conf "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
  188. netdata_banner "is installed and running now!"
  189. else
  190. create_netdata_conf "${NETDATA_PREFIX}/etc/netdata/netdata.conf" "http://localhost:19999/netdata.conf"
  191. netdata_banner "is installed now!"
  192. fi
  193. fi
  194. run chmod 0644 "${NETDATA_PREFIX}/etc/netdata/netdata.conf"