run.sh 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. # Needed to read Proxmox VMs and (LXC) containers configuration files (name resolution + CPU and memory limits)
  16. function add_netdata_to_proxmox_conf_files_group() {
  17. group_guid="$(stat -c %g /host/etc/pve 2>/dev/null || true)"
  18. [ -z "${group_guid}" ] && return
  19. if ! getent group "${group_guid}" >/dev/null; then
  20. echo "Creating proxmox-etc-pve group with GID ${group_guid}"
  21. if ! addgroup -g "${group_guid}" "proxmox-etc-pve"; then
  22. echo >&2 "Failed to add group proxmox-etc-pve with GID ${group_guid}."
  23. return
  24. fi
  25. fi
  26. if ! getent group "${group_guid}" | grep -q netdata; then
  27. echo "Assign netdata user to group ${group_guid}"
  28. if ! usermod -a -G "${group_guid}" "${DOCKER_USR}"; then
  29. echo >&2 "Failed to add netdata user to group with GID ${group_guid}."
  30. return
  31. fi
  32. fi
  33. }
  34. if [ ! "${DISABLE_TELEMETRY:-0}" -eq 0 ] ||
  35. [ -n "$DISABLE_TELEMETRY" ] ||
  36. [ ! "${DO_NOT_TRACK:-0}" -eq 0 ] ||
  37. [ -n "$DO_NOT_TRACK" ]; then
  38. touch /etc/netdata/.opt-out-from-anonymous-statistics
  39. fi
  40. chmod o+rX / 2>/dev/null || echo "Unable to change permissions without errors."
  41. if [ "${EUID}" -eq 0 ]; then
  42. if [ -n "${NETDATA_EXTRA_APK_PACKAGES}" ]; then
  43. 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."
  44. echo >&2 "WARNING: The container will still run, but supplementary packages listed in NETDATA_EXTRA_APK_PACKAGES will not be installed."
  45. echo >&2 "WARNING: To remove these messages, either undefine NETDATA_EXTRA_APK_PACKAGES, or define it to an empty string."
  46. fi
  47. if [ -n "${NETDATA_EXTRA_DEB_PACKAGES}" ]; then
  48. echo "Fetching APT repository metadata."
  49. if ! apt-get update; then
  50. echo "Failed to fetch APT repository metadata."
  51. else
  52. echo "Installing supplementary packages."
  53. export DEBIAN_FRONTEND="noninteractive"
  54. # shellcheck disable=SC2086
  55. if ! apt-get install -y --no-install-recommends ${NETDATA_EXTRA_DEB_PACKAGES}; then
  56. echo "Failed to install supplementary packages."
  57. fi
  58. fi
  59. fi
  60. BALENA_PGID=$(stat -c %g /var/run/balena.sock 2>/dev/null || true)
  61. DOCKER_PGID=$(stat -c %g /var/run/docker.sock 2>/dev/null || true)
  62. re='^[0-9]+$'
  63. if [[ $BALENA_PGID =~ $re ]]; then
  64. echo "Netdata detected balena-engine.sock"
  65. DOCKER_HOST='/var/run/balena-engine.sock'
  66. PGID="$BALENA_PGID"
  67. elif [[ $DOCKER_PGID =~ $re ]]; then
  68. echo "Netdata detected docker.sock"
  69. DOCKER_HOST="/var/run/docker.sock"
  70. PGID="$DOCKER_PGID"
  71. fi
  72. export PGID
  73. export DOCKER_HOST
  74. if [ -n "${PGID}" ]; then
  75. echo "Creating docker group ${PGID}"
  76. addgroup --gid "${PGID}" "docker" || echo >&2 "Could not add group docker with ID ${PGID}, its already there probably"
  77. echo "Assign netdata user to docker group ${PGID}"
  78. usermod --append --groups "docker" "${DOCKER_USR}" || echo >&2 "Could not add netdata user to group docker with ID ${PGID}"
  79. fi
  80. if [ -d "/host/etc/pve" ]; then
  81. add_netdata_to_proxmox_conf_files_group || true
  82. fi
  83. else
  84. echo >&2 "WARNING: Entrypoint started as non-root user. This is not officially supported and some features may not be available."
  85. fi
  86. if mountpoint -q /etc/netdata; then
  87. echo "Copying stock configuration to /etc/netdata"
  88. cp -an /etc/netdata.stock/* /etc/netdata
  89. cp -an /etc/netdata.stock/.[^.]* /etc/netdata
  90. fi
  91. if [ -w "/etc/netdata" ]; then
  92. if mountpoint -q /etc/netdata; then
  93. hostname >/etc/netdata/.container-hostname
  94. else
  95. rm -f /etc/netdata/.container-hostname
  96. fi
  97. fi
  98. exec /usr/sbin/netdata -u "${DOCKER_USR}" -D -s /host -p "${NETDATA_LISTENER_PORT}" "$@"