run.sh 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #!/usr/bin/env bash
  2. #
  3. # Entry point script for netdata
  4. #
  5. # Copyright: 2018 and later Netdata Inc.
  6. # SPDX-License-Identifier: GPL-3.0-or-later
  7. #
  8. # Author : Pavlos Emm. Katsoulakis <paul@netdata.cloud>
  9. # Author : Austin S. Hemmelgarn <austin@netdata.cloud>
  10. set -e
  11. if [ ! -w / ] && [ "${EUID}" -eq 0 ]; then
  12. echo >&2 "WARNING: This Docker host appears to not properly support newer stat system calls. This is known to cause issues with Netdata (most notably, nodes running on such hosts **cannot be claimed**)."
  13. echo >&2 "WARNING: For more information, see https://learn.netdata.cloud/docs/agent/claim#known-issues-on-older-hosts-with-seccomp-enabled"
  14. fi
  15. if [ ! "${DISABLE_TELEMETRY:-0}" -eq 0 ] ||
  16. [ -n "$DISABLE_TELEMETRY" ] ||
  17. [ ! "${DO_NOT_TRACK:-0}" -eq 0 ] ||
  18. [ -n "$DO_NOT_TRACK" ]; then
  19. touch /etc/netdata/.opt-out-from-anonymous-statistics
  20. fi
  21. chmod o+rX / 2>/dev/null || echo "Unable to change permissions without errors."
  22. BALENA_PGID=$(stat -c %g /var/run/balena.sock 2>/dev/null || true)
  23. DOCKER_PGID=$(stat -c %g /var/run/docker.sock 2>/dev/null || true)
  24. re='^[0-9]+$'
  25. if [[ $BALENA_PGID =~ $re ]]; then
  26. echo "Netdata detected balena-engine.sock"
  27. DOCKER_HOST='/var/run/balena-engine.sock'
  28. PGID="$BALENA_PGID"
  29. elif [[ $DOCKER_PGID =~ $re ]]; then
  30. echo "Netdata detected docker.sock"
  31. DOCKER_HOST="/var/run/docker.sock"
  32. PGID="$DOCKER_PGID"
  33. fi
  34. export PGID
  35. export DOCKER_HOST
  36. if [ -n "${PGID}" ]; then
  37. echo "Creating docker group ${PGID}"
  38. addgroup --gid "${PGID}" "docker" || echo >&2 "Could not add group docker with ID ${PGID}, its already there probably"
  39. echo "Assign netdata user to docker group ${PGID}"
  40. usermod --append --groups "docker" "${DOCKER_USR}" || echo >&2 "Could not add netdata user to group docker with ID ${PGID}"
  41. fi
  42. # Needed to read Proxmox VMs and (LXC) containers configuration files (name resolution + CPU and memory limits)
  43. function add_netdata_to_proxmox_conf_files_group() {
  44. group_guid="$(stat -c %g /host/etc/pve 2>/dev/null || true)"
  45. [ -z "${group_guid}" ] && return
  46. if ! getent group "${group_guid}" >/dev/null; then
  47. echo "Creating proxmox-etc-pve group with GID ${group_guid}"
  48. if ! addgroup -g "${group_guid}" "proxmox-etc-pve"; then
  49. echo >&2 "Failed to add group proxmox-etc-pve with GID ${group_guid}."
  50. return
  51. fi
  52. fi
  53. if ! getent group "${group_guid}" | grep -q netdata; then
  54. echo "Assign netdata user to group ${group_guid}"
  55. if ! usermod -a -G "${group_guid}" "${DOCKER_USR}"; then
  56. echo >&2 "Failed to add netdata user to group with GID ${group_guid}."
  57. return
  58. fi
  59. fi
  60. }
  61. if [ -d "/host/etc/pve" ]; then
  62. add_netdata_to_proxmox_conf_files_group || true
  63. fi
  64. if mountpoint -q /etc/netdata; then
  65. echo "Copying stock configuration to /etc/netdata"
  66. cp -an /etc/netdata.stock/* /etc/netdata
  67. cp -an /etc/netdata.stock/.[^.]* /etc/netdata
  68. fi
  69. if [ -w "/etc/netdata" ]; then
  70. if mountpoint -q /etc/netdata; then
  71. hostname >/etc/netdata/.container-hostname
  72. else
  73. rm -f /etc/netdata/.container-hostname
  74. fi
  75. fi
  76. if [ -n "${NETDATA_CLAIM_URL}" ] && [ -n "${NETDATA_CLAIM_TOKEN}" ] && [ ! -f /var/lib/netdata/cloud.d/claimed_id ]; then
  77. # shellcheck disable=SC2086
  78. /usr/sbin/netdata-claim.sh -token="${NETDATA_CLAIM_TOKEN}" \
  79. -url="${NETDATA_CLAIM_URL}" \
  80. ${NETDATA_CLAIM_ROOMS:+-rooms="${NETDATA_CLAIM_ROOMS}"} \
  81. ${NETDATA_CLAIM_PROXY:+-proxy="${NETDATA_CLAIM_PROXY}"} \
  82. ${NETDATA_EXTRA_CLAIM_OPTS} \
  83. -daemon-not-running
  84. fi
  85. if [ -n "${NETDATA_EXTRA_APK_PACKAGES}" ]; then
  86. echo >&2 "WARNING: Netdata’s Docker images have switched from Alpine to Debian as a base platform. Supplementary package support is now handled through the NETDATA_EXTRA_DEB_PACKAGES variable instead of NETDATA_EXTRA_APK_PACKAGES."
  87. echo >&2 "WARNING: The container will still run, but supplementary packages listed in NETDATA_EXTRA_APK_PACKAGES will not be installed."
  88. echo >&2 "WARNING: To remove these messages, either undefine NETDATA_EXTRA_APK_PACKAGES, or define it to an empty string."
  89. fi
  90. if [ -n "${NETDATA_EXTRA_DEB_PACKAGES}" ]; then
  91. echo "Fetching APT repository metadata."
  92. if ! apt-get update; then
  93. echo "Failed to fetch APT repository metadata."
  94. else
  95. echo "Installing supplementary packages."
  96. export DEBIAN_FRONTEND="noninteractive"
  97. # shellcheck disable=SC2086
  98. if ! apt-get install -y --no-install-recommends ${NETDATA_EXTRA_DEB_PACKAGES}; then
  99. echo "Failed to install supplementary packages."
  100. fi
  101. fi
  102. fi
  103. exec /usr/sbin/netdata -u "${DOCKER_USR}" -D -s /host -p "${NETDATA_LISTENER_PORT}" "$@"