install-or-update.sh 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. #!/usr/bin/env bash
  2. # SPDX-License-Identifier: GPL-3.0-or-later
  3. . $(dirname "${0}")/functions.sh
  4. export LC_ALL=C
  5. umask 002
  6. # Be nice on production environments
  7. renice 19 $$ >/dev/null 2>/dev/null
  8. # -----------------------------------------------------------------------------
  9. STARTIT=1
  10. while [ ! -z "${1}" ]
  11. do
  12. if [ "${1}" = "--dont-start-it" ]
  13. then
  14. STARTIT=0
  15. else
  16. echo >&2 "Unknown option '${1}'. Ignoring it."
  17. fi
  18. shift
  19. done
  20. deleted_stock_configs=0
  21. if [ ! -f "etc/netdata/.installer-cleanup-of-stock-configs-done" ]
  22. then
  23. # -----------------------------------------------------------------------------
  24. progress "Deleting stock configuration files from user configuration directory"
  25. declare -A configs_signatures=()
  26. source "system/configs.signatures"
  27. if [ ! -d etc/netdata ]
  28. then
  29. run mkdir -p etc/netdata
  30. fi
  31. md5sum="$(which md5sum 2>/dev/null || command -v md5sum 2>/dev/null || command -v md5 2>/dev/null)"
  32. for x in $(find etc -type f)
  33. do
  34. # find it relative filename
  35. f="${x/etc\/netdata\//}"
  36. # find the stock filename
  37. t="${f/.conf.old/.conf}"
  38. t="${t/.conf.orig/.conf}"
  39. if [ ! -z "${md5sum}" ]
  40. then
  41. # find the checksum of the existing file
  42. md5="$( ${md5sum} <"${x}" | cut -d ' ' -f 1)"
  43. #echo >&2 "md5: ${md5}"
  44. # check if it matches
  45. if [ "${configs_signatures[${md5}]}" = "${t}" ]
  46. then
  47. # it matches the default
  48. run rm -f "${x}"
  49. deleted_stock_configs=$(( deleted_stock_configs + 1 ))
  50. fi
  51. fi
  52. done
  53. touch "etc/netdata/.installer-cleanup-of-stock-configs-done"
  54. fi
  55. # -----------------------------------------------------------------------------
  56. progress "Add user netdata to required user groups"
  57. NETDATA_WANTED_GROUPS="docker nginx varnish haproxy adm nsd proxy squid ceph nobody"
  58. NETDATA_ADDED_TO_GROUPS=""
  59. if [ "${UID}" -eq 0 ]; then
  60. if ! portable_add_group netdata; then
  61. run_failed "Failed to add netdata group"
  62. NETDATA_GROUP="root"
  63. fi
  64. if ! portable_add_user netdata "/opt/netdata"; then
  65. run_failed "Failed to add netdata user"
  66. NETDATA_USER="root"
  67. fi
  68. for g in ${NETDATA_WANTED_GROUPS}; do
  69. # shellcheck disable=SC2086
  70. portable_add_user_to_group ${g} netdata && NETDATA_ADDED_TO_GROUPS="${NETDATA_ADDED_TO_GROUPS} ${g}" || run_failed "Failed to add netdata user to secondary groups"
  71. done
  72. else
  73. run_failed "Failed to add netdata user and group"
  74. run_failed "The installer does not run as root."
  75. fi
  76. # -----------------------------------------------------------------------------
  77. progress "Check SSL certificates paths"
  78. if [ ! -f "/etc/ssl/certs/ca-certificates.crt" ]
  79. then
  80. if [ ! -f /opt/netdata/.curlrc ]
  81. then
  82. cacert=
  83. # CentOS
  84. [ -f "/etc/ssl/certs/ca-bundle.crt" ] && cacert="/etc/ssl/certs/ca-bundle.crt"
  85. if [ ! -z "${cacert}" ]
  86. then
  87. echo "Creating /opt/netdata/.curlrc with cacert=${cacert}"
  88. echo >/opt/netdata/.curlrc "cacert=${cacert}"
  89. else
  90. run_failed "Failed to find /etc/ssl/certs/ca-certificates.crt"
  91. fi
  92. fi
  93. fi
  94. # -----------------------------------------------------------------------------
  95. progress "Install logrotate configuration for netdata"
  96. install_netdata_logrotate || run_failed "Cannot install logrotate file for netdata."
  97. # -----------------------------------------------------------------------------
  98. progress "Install netdata at system init"
  99. install_netdata_service || run_failed "Cannot install netdata init service."
  100. # -----------------------------------------------------------------------------
  101. progress "creating quick links"
  102. dir_should_be_link() {
  103. local p="${1}" t="${2}" d="${3}" old
  104. old="${PWD}"
  105. cd "${p}" || return 0
  106. if [ -e "${d}" ]
  107. then
  108. if [ -h "${d}" ]
  109. 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}"
  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. if [ ${deleted_stock_configs} -gt 0 ]
  130. then
  131. dir_should_be_link etc/netdata ../../usr/lib/netdata/conf.d "000.-.USE.THE.orig.LINK.TO.COPY.AND.EDIT.STOCK.CONFIG.FILES"
  132. fi
  133. # -----------------------------------------------------------------------------
  134. progress "create user config directories"
  135. for x in "python.d" "charts.d" "node.d" "health.d" "statsd.d"
  136. do
  137. if [ ! -d "etc/netdata/${x}" ]
  138. then
  139. run mkdir -p "etc/netdata/${x}" || exit 1
  140. fi
  141. done
  142. # -----------------------------------------------------------------------------
  143. progress "fix permissions"
  144. run chmod g+rx,o+rx /opt
  145. run chown -R ${NETDATA_USER}:${NETDATA_GROUP} /opt/netdata
  146. # -----------------------------------------------------------------------------
  147. progress "fix plugin permissions"
  148. for x in apps.plugin freeipmi.plugin cgroup-network
  149. do
  150. f="usr/libexec/netdata/plugins.d/${x}"
  151. if [ -f "${f}" ]
  152. then
  153. run chown root:${NETDATA_GROUP} "${f}"
  154. run chmod 4750 "${f}"
  155. fi
  156. done
  157. # fix the fping binary
  158. if [ -f bin/fping ]
  159. then
  160. run chown root:${NETDATA_GROUP} bin/fping
  161. run chmod 4750 bin/fping
  162. fi
  163. # -----------------------------------------------------------------------------
  164. if [ ${STARTIT} -eq 0 ]; then
  165. create_netdata_conf "/opt/netdata/etc/netdata/netdata.conf"
  166. netdata_banner "is installed now!"
  167. else
  168. progress "starting netdata"
  169. if ! restart_netdata "/opt/netdata/bin/netdata"; then
  170. create_netdata_conf "/opt/netdata/etc/netdata/netdata.conf"
  171. netdata_banner "is installed and running now!"
  172. else
  173. create_netdata_conf "/opt/netdata/etc/netdata/netdata.conf" "http://localhost:19999/netdata.conf"
  174. netdata_banner "is installed now!"
  175. fi
  176. fi
  177. run chown "${NETDATA_USER}:${NETDATA_GROUP}" "/opt/netdata/etc/netdata/netdata.conf"
  178. run chmod 0664 "/opt/netdata/etc/netdata/netdata.conf"