install-or-update.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. if ! run setcap "cap_dac_read_search,cap_sys_ptrace=ep" "usr/libexec/netdata/plugins.d/apps.plugin"; then
  170. run chmod 4750 "usr/libexec/netdata/plugins.d/apps.plugin"
  171. fi
  172. if ! run setcap "cap_dac_read_search=ep" "usr/libexec/netdata/plugins.d/slabinfo.plugin"; then
  173. run chmod 4750 "usr/libexec/netdata/plugins.d/slabinfo.plugin"
  174. fi
  175. if ! run setcap "cap_dac_read_search=ep" "usr/libexec/netdata/plugins.d/debugfs.plugin"; then
  176. run chmod 4750 "usr/libexec/netdata/plugins.d/debugfs.plugin"
  177. fi
  178. if ! run setcap "cap_dac_read_search+epi cap_net_admin+epi cap_net_raw=eip" "usr/libexec/netdata/plugins.d/go.d.plugin"; then
  179. run chmod 4750 "usr/libexec/netdata/plugins.d/go.d.plugin"
  180. fi
  181. perf_caps="cap_sys_admin=ep"
  182. if command -v capsh >/dev/null 2>&1 && capsh --supports=cap_perfmon 2>/dev/null; then
  183. perf_caps="cap_perfmon=ep"
  184. fi
  185. if ! run setcap "${perf_caps}" "usr/libexec/netdata/plugins.d/perf.plugin"; then
  186. run chmod 4750 "usr/libexec/netdata/plugins.d/perf.plugin"
  187. fi
  188. else
  189. for x in apps.plugin perf.plugin slabinfo.plugin debugfs.plugin; do
  190. f="usr/libexec/netdata/plugins.d/${x}"
  191. run chmod 4750 "${f}"
  192. done
  193. fi
  194. for x in ndsudo freeipmi.plugin ioping cgroup-network local-listeners network-viewer.plugin ebpf.plugin nfacct.plugin xenstat.plugin; do
  195. f="usr/libexec/netdata/plugins.d/${x}"
  196. if [ -f "${f}" ]; then
  197. run chmod 4750 "${f}"
  198. fi
  199. done
  200. # -----------------------------------------------------------------------------
  201. replace_symlink() {
  202. target="${1}"
  203. name="${2}"
  204. rm -f "${name}"
  205. ln -s "${target}" "${name}"
  206. }
  207. select_system_certs() {
  208. if [ -d /etc/pki/tls ] ; then
  209. echo "${1} /etc/pki/tls for TLS configuration and certificates"
  210. replace_symlink /etc/pki/tls /opt/netdata/etc/ssl
  211. elif [ -d /etc/ssl ] ; then
  212. echo "${1} /etc/ssl for TLS configuration and certificates"
  213. replace_symlink /etc/ssl /opt/netdata/etc/ssl
  214. fi
  215. }
  216. select_internal_certs() {
  217. echo "Using bundled TLS configuration and certificates"
  218. replace_symlink /opt/netdata/share/ssl /opt/netdata/etc/ssl
  219. }
  220. certs_selected() {
  221. [ -L /opt/netdata/etc/ssl ] || return 1
  222. }
  223. test_certs() {
  224. /opt/netdata/bin/curl --fail --max-time 300 --silent --output /dev/null "${NETDATA_CERT_TEST_URL}"
  225. case "$?" in
  226. 35|77) echo "Failed to load certificate files for test." ; return 1 ;;
  227. 60|82|83) echo "Certificates cannot be used to connect to ${NETDATA_CERT_TEST_URL}" ; return 1 ;;
  228. 53|54|66) echo "Unable to use OpenSSL configuration associated with certificates" ; return 1 ;;
  229. 0) echo "Successfully connected to ${NETDATA_CERT_TEST_URL} using certificates" ;;
  230. *) echo "Unable to test certificates due to networking problems, blindly assuming they work" ;;
  231. esac
  232. }
  233. # If the user has manually set up certificates, don’t mess with it.
  234. if [ ! -L /opt/netdata/etc/ssl ] && [ -d /opt/netdata/etc/ssl ] ; then
  235. echo "Preserving existing user configuration for TLS"
  236. else
  237. echo "Configure TLS certificate paths (mode: ${NETDATA_CERT_MODE})"
  238. case "${NETDATA_CERT_MODE}" in
  239. check)
  240. select_system_certs "Testing"
  241. if certs_selected && test_certs; then
  242. select_system_certs "Using"
  243. else
  244. select_internal_certs
  245. fi
  246. ;;
  247. bundled) select_internal_certs ;;
  248. *)
  249. select_system_certs "Using"
  250. if ! certs_selected; then
  251. select_internal_certs
  252. fi
  253. ;;
  254. esac
  255. fi
  256. # -----------------------------------------------------------------------------
  257. echo "Save install options"
  258. grep -qv 'IS_NETDATA_STATIC_BINARY="yes"' "${NETDATA_PREFIX}/etc/netdata/.environment" || echo IS_NETDATA_STATIC_BINARY=\"yes\" >> "${NETDATA_PREFIX}/etc/netdata/.environment"
  259. REINSTALL_OPTIONS="$(echo "${REINSTALL_OPTIONS}" | awk '{gsub("/", "\\/"); print}')"
  260. sed -i "s/REINSTALL_OPTIONS=\".*\"/REINSTALL_OPTIONS=\"${REINSTALL_OPTIONS}\"/" "${NETDATA_PREFIX}/etc/netdata/.environment"
  261. # -----------------------------------------------------------------------------
  262. if [ ${STARTIT} -eq 0 ]; then
  263. create_netdata_conf "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
  264. netdata_banner "is installed now!"
  265. else
  266. progress "starting netdata"
  267. if ! restart_netdata "${NETDATA_PREFIX}/bin/netdata"; then
  268. create_netdata_conf "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
  269. netdata_banner "is installed and running now!"
  270. else
  271. create_netdata_conf "${NETDATA_PREFIX}/etc/netdata/netdata.conf" "http://localhost:19999/netdata.conf"
  272. netdata_banner "is installed now!"
  273. fi
  274. fi
  275. run chmod 0644 "${NETDATA_PREFIX}/etc/netdata/netdata.conf"