install-or-update.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. NETDATA_CERT_MODE="${NETDATA_CERT_MODE:-auto}"
  23. NETDATA_CERT_TEST_URL="${NETDATA_CERT_TEST_URL:-https://app.netdata.cloud}"
  24. RELEASE_CHANNEL="nightly"
  25. while [ "${1}" ]; do
  26. case "${1}" in
  27. "--dont-start-it")
  28. STARTIT=0
  29. REINSTALL_OPTIONS="${REINSTALL_OPTIONS} ${1}"
  30. ;;
  31. "--auto-update" | "-u") ;;
  32. "--stable-channel")
  33. RELEASE_CHANNEL="stable"
  34. REINSTALL_OPTIONS="${REINSTALL_OPTIONS} ${1}"
  35. ;;
  36. "--nightly-channel")
  37. RELEASE_CHANNEL="nightly"
  38. REINSTALL_OPTIONS="${REINSTALL_OPTIONS} ${1}"
  39. ;;
  40. "--disable-telemetry")
  41. NETDATA_DISABLE_TELEMETRY=1
  42. REINSTALL_OPTIONS="${REINSTALL_OPTIONS} ${1}"
  43. ;;
  44. "--certificates")
  45. case "${2}" in
  46. auto|system) NETDATA_CERT_MODE="auto" ;;
  47. check) NETDATA_CERT_MODE="check" ;;
  48. bundled) NETDATA_CERT_MODE="bundled" ;;
  49. *) run_failed "Unknown certificate handling mode '${2}'. Supported modes are auto, check, system, and bundled."; exit 1 ;;
  50. esac
  51. shift 1
  52. ;;
  53. "--certificate-test-url")
  54. NETDATA_CERT_TEST_URL="${2}"
  55. shift 1
  56. ;;
  57. *) echo >&2 "Unknown option '${1}'. Ignoring it." ;;
  58. esac
  59. shift 1
  60. done
  61. if [ ! "${DISABLE_TELEMETRY:-0}" -eq 0 ] ||
  62. [ -n "$DISABLE_TELEMETRY" ] ||
  63. [ ! "${DO_NOT_TRACK:-0}" -eq 0 ] ||
  64. [ -n "$DO_NOT_TRACK" ]; then
  65. NETDATA_DISABLE_TELEMETRY=1
  66. REINSTALL_OPTIONS="${REINSTALL_OPTIONS} --disable-telemetry"
  67. fi
  68. if [ -n "${NETDATA_CERT_MODE}" ]; then
  69. REINSTALL_OPTIONS="${REINSTALL_OPTIONS} --certificates ${NETDATA_CERT_MODE}"
  70. fi
  71. if [ -n "${NETDATA_CERT_TEST_URL}" ]; then
  72. REINSTALL_OPTIONS="${REINSTALL_OPTIONS} --certificate-test-url ${NETDATA_CERT_TEST_URL}"
  73. fi
  74. # -----------------------------------------------------------------------------
  75. progress "Attempt to create user/group netdata/netadata"
  76. NETDATA_WANTED_GROUPS="docker nginx varnish haproxy adm nsd proxy squid ceph nobody I2C"
  77. NETDATA_ADDED_TO_GROUPS=""
  78. # Default user/group
  79. NETDATA_USER="root"
  80. NETDATA_GROUP="root"
  81. if portable_add_group netdata; then
  82. if portable_add_user netdata "/opt/netdata"; then
  83. progress "Add user netdata to required user groups"
  84. for g in ${NETDATA_WANTED_GROUPS}; do
  85. # shellcheck disable=SC2086
  86. if portable_add_user_to_group ${g} netdata; then
  87. NETDATA_ADDED_TO_GROUPS="${NETDATA_ADDED_TO_GROUPS} ${g}"
  88. else
  89. run_failed "Failed to add netdata user to secondary groups"
  90. fi
  91. done
  92. # Netdata must be able to read /etc/pve/qemu-server/* and /etc/pve/lxc/*
  93. # for reading VMs/containers names, CPU and memory limits on Proxmox.
  94. if [ -d "/etc/pve" ]; then
  95. portable_add_user_to_group "www-data" netdata && NETDATA_ADDED_TO_GROUPS="${NETDATA_ADDED_TO_GROUPS} www-data"
  96. fi
  97. NETDATA_USER="netdata"
  98. NETDATA_GROUP="netdata"
  99. else
  100. run_failed "I could not add user netdata, will be using root"
  101. fi
  102. else
  103. run_failed "I could not add group netdata, so no user netdata will be created as well. Netdata run as root:root"
  104. fi
  105. # -----------------------------------------------------------------------------
  106. progress "Install logrotate configuration for netdata"
  107. install_netdata_logrotate || run_failed "Cannot install logrotate file for netdata."
  108. progress "Install journald configuration for netdata"
  109. install_netdata_journald_conf || run_failed "Cannot install journald file for netdata."
  110. # -----------------------------------------------------------------------------
  111. progress "Telemetry configuration"
  112. # Opt-out from telemetry program
  113. if [ -n "${NETDATA_DISABLE_TELEMETRY}" ]; then
  114. run touch "${NETDATA_USER_CONFIG_DIR}/.opt-out-from-anonymous-statistics"
  115. else
  116. 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"
  117. fi
  118. # -----------------------------------------------------------------------------
  119. progress "Install netdata at system init"
  120. install_netdata_service || run_failed "Cannot install netdata init service."
  121. set_netdata_updater_channel || run_failed "Cannot set netdata updater tool release channel to '${RELEASE_CHANNEL}'"
  122. # -----------------------------------------------------------------------------
  123. progress "Install (but not enable) netdata updater tool"
  124. install_netdata_updater || run_failed "Cannot install netdata updater tool."
  125. # -----------------------------------------------------------------------------
  126. progress "creating quick links"
  127. dir_should_be_link() {
  128. local p="${1}" t="${2}" d="${3}" old
  129. old="${PWD}"
  130. cd "${p}" || return 0
  131. if [ -e "${d}" ]; then
  132. if [ -h "${d}" ]; then
  133. run rm "${d}"
  134. else
  135. run mv -f "${d}" "${d}.old.$$"
  136. fi
  137. fi
  138. run ln -s "${t}" "${d}"
  139. cd "${old}" || true
  140. }
  141. dir_should_be_link . bin sbin
  142. dir_should_be_link usr ../bin bin
  143. dir_should_be_link usr ../bin sbin
  144. dir_should_be_link usr . local
  145. dir_should_be_link . etc/netdata netdata-configs
  146. dir_should_be_link . usr/share/netdata/web netdata-web-files
  147. dir_should_be_link . usr/libexec/netdata netdata-plugins
  148. dir_should_be_link . var/lib/netdata netdata-dbs
  149. dir_should_be_link . var/cache/netdata netdata-metrics
  150. dir_should_be_link . var/log/netdata netdata-logs
  151. dir_should_be_link etc/netdata ../../usr/lib/netdata/conf.d orig
  152. # -----------------------------------------------------------------------------
  153. progress "fix permissions"
  154. run chmod g+rx,o+rx /opt
  155. run find /opt/netdata -type d -exec chmod go+rx '{}' \+
  156. run chown -R ${NETDATA_USER}:${NETDATA_GROUP} /opt/netdata/var
  157. if [ -d /opt/netdata/usr/libexec/netdata/plugins.d/ebpf.d ]; then
  158. run chown -R root:${NETDATA_GROUP} /opt/netdata/usr/libexec/netdata/plugins.d/ebpf.d
  159. fi
  160. # -----------------------------------------------------------------------------
  161. progress "changing plugins ownership and permissions"
  162. for x in ndsudo apps.plugin perf.plugin slabinfo.plugin debugfs.plugin freeipmi.plugin ioping cgroup-network local-listeners network-viewer.plugin ebpf.plugin nfacct.plugin xenstat.plugin python.d.plugin charts.d.plugin go.d.plugin ioping.plugin cgroup-network-helper.sh; do
  163. f="usr/libexec/netdata/plugins.d/${x}"
  164. if [ -f "${f}" ]; then
  165. run chown root:${NETDATA_GROUP} "${f}"
  166. fi
  167. done
  168. if command -v setcap >/dev/null 2>&1; then
  169. run setcap "cap_dac_read_search,cap_sys_ptrace=ep" "usr/libexec/netdata/plugins.d/apps.plugin"
  170. run setcap "cap_dac_read_search=ep" "usr/libexec/netdata/plugins.d/slabinfo.plugin"
  171. run setcap "cap_dac_read_search=ep" "usr/libexec/netdata/plugins.d/debugfs.plugin"
  172. if command -v capsh >/dev/null 2>&1 && capsh --supports=cap_perfmon 2>/dev/null ; then
  173. run setcap "cap_perfmon=ep" "usr/libexec/netdata/plugins.d/perf.plugin"
  174. else
  175. run setcap "cap_sys_admin=ep" "usr/libexec/netdata/plugins.d/perf.plugin"
  176. fi
  177. run setcap "cap_dac_read_search+epi cap_net_admin+epi cap_net_raw=eip" "usr/libexec/netdata/plugins.d/go.d.plugin"
  178. else
  179. for x in apps.plugin perf.plugin slabinfo.plugin debugfs.plugin; do
  180. f="usr/libexec/netdata/plugins.d/${x}"
  181. run chmod 4750 "${f}"
  182. done
  183. fi
  184. for x in ndsudo freeipmi.plugin ioping cgroup-network local-listeners network-viewer.plugin ebpf.plugin nfacct.plugin xenstat.plugin; do
  185. f="usr/libexec/netdata/plugins.d/${x}"
  186. if [ -f "${f}" ]; then
  187. run chmod 4750 "${f}"
  188. fi
  189. done
  190. # -----------------------------------------------------------------------------
  191. replace_symlink() {
  192. target="${1}"
  193. name="${2}"
  194. rm -f "${name}"
  195. ln -s "${target}" "${name}"
  196. }
  197. select_system_certs() {
  198. if [ -d /etc/pki/tls ] ; then
  199. echo "${1} /etc/pki/tls for TLS configuration and certificates"
  200. replace_symlink /etc/pki/tls /opt/netdata/etc/ssl
  201. elif [ -d /etc/ssl ] ; then
  202. echo "${1} /etc/ssl for TLS configuration and certificates"
  203. replace_symlink /etc/ssl /opt/netdata/etc/ssl
  204. fi
  205. }
  206. select_internal_certs() {
  207. echo "Using bundled TLS configuration and certificates"
  208. replace_symlink /opt/netdata/share/ssl /opt/netdata/etc/ssl
  209. }
  210. certs_selected() {
  211. [ -L /opt/netdata/etc/ssl ] || return 1
  212. }
  213. test_certs() {
  214. /opt/netdata/bin/curl --fail --max-time 300 --silent --output /dev/null "${NETDATA_CERT_TEST_URL}"
  215. case "$?" in
  216. 35|77) echo "Failed to load certificate files for test." ; return 1 ;;
  217. 60|82|83) echo "Certificates cannot be used to connect to ${NETDATA_CERT_TEST_URL}" ; return 1 ;;
  218. 53|54|66) echo "Unable to use OpenSSL configuration associated with certificates" ; return 1 ;;
  219. 0) echo "Successfully connected to ${NETDATA_CERT_TEST_URL} using certificates" ;;
  220. *) echo "Unable to test certificates due to networking problems, blindly assuming they work" ;;
  221. esac
  222. }
  223. # If the user has manually set up certificates, don’t mess with it.
  224. if [ ! -L /opt/netdata/etc/ssl ] && [ -d /opt/netdata/etc/ssl ] ; then
  225. echo "Preserving existing user configuration for TLS"
  226. else
  227. echo "Configure TLS certificate paths (mode: ${NETDATA_CERT_MODE})"
  228. case "${NETDATA_CERT_MODE}" in
  229. check)
  230. select_system_certs "Testing"
  231. if certs_selected && test_certs; then
  232. select_system_certs "Using"
  233. else
  234. select_internal_certs
  235. fi
  236. ;;
  237. bundled) select_internal_certs ;;
  238. *)
  239. select_system_certs "Using"
  240. if ! certs_selected; then
  241. select_internal_certs
  242. fi
  243. ;;
  244. esac
  245. fi
  246. # -----------------------------------------------------------------------------
  247. echo "Save install options"
  248. grep -qv 'IS_NETDATA_STATIC_BINARY="yes"' "${NETDATA_PREFIX}/etc/netdata/.environment" || echo IS_NETDATA_STATIC_BINARY=\"yes\" >> "${NETDATA_PREFIX}/etc/netdata/.environment"
  249. REINSTALL_OPTIONS="$(echo "${REINSTALL_OPTIONS}" | awk '{gsub("/", "\\/"); print}')"
  250. sed -i "s/REINSTALL_OPTIONS=\".*\"/REINSTALL_OPTIONS=\"${REINSTALL_OPTIONS}\"/" "${NETDATA_PREFIX}/etc/netdata/.environment"
  251. # -----------------------------------------------------------------------------
  252. if [ ${STARTIT} -eq 0 ]; then
  253. create_netdata_conf "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
  254. netdata_banner "is installed now!"
  255. else
  256. progress "starting netdata"
  257. if ! restart_netdata "${NETDATA_PREFIX}/bin/netdata"; then
  258. create_netdata_conf "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
  259. netdata_banner "is installed and running now!"
  260. else
  261. create_netdata_conf "${NETDATA_PREFIX}/etc/netdata/netdata.conf" "http://localhost:19999/netdata.conf"
  262. netdata_banner "is installed now!"
  263. fi
  264. fi
  265. run chmod 0644 "${NETDATA_PREFIX}/etc/netdata/netdata.conf"