kickstart.sh 83 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272
  1. #!/bin/sh
  2. #
  3. # SPDX-License-Identifier: GPL-3.0-or-later
  4. #
  5. # Next unused error code: F0515
  6. # ======================================================================
  7. # Constants
  8. DEFAULT_RELEASE_CHANNEL="nightly"
  9. KICKSTART_OPTIONS="${*}"
  10. KICKSTART_SOURCE="$(
  11. self=${0}
  12. while [ -L "${self}" ]
  13. do
  14. cd "${self%/*}" || exit 1
  15. self=$(readlink "${self}")
  16. done
  17. cd "${self%/*}" || exit 1
  18. echo "$(pwd -P)/${self##*/}"
  19. )"
  20. DEFAULT_PLUGIN_PACKAGES=""
  21. PATH="${PATH}:/usr/local/bin:/usr/local/sbin"
  22. REPOCONFIG_DEB_VERSION="2-1"
  23. REPOCONFIG_RPM_VERSION="2-1"
  24. START_TIME="$(date +%s)"
  25. STATIC_INSTALL_ARCHES="x86_64 armv7l aarch64 ppc64le"
  26. # ======================================================================
  27. # URLs used throughout the script
  28. AGENT_BUG_REPORT_URL="https://github.com/netdata/netdata/issues/new/choose"
  29. CLOUD_BUG_REPORT_URL="https://github.com/netdata/netdata-cloud/issues/new/choose"
  30. DISCORD_INVITE="https://discord.gg/5ygS846fR6"
  31. DISCUSSIONS_URL="https://github.com/netdata/netdata/discussions"
  32. DOCS_URL="https://learn.netdata.cloud/docs/"
  33. FORUM_URL="https://community.netdata.cloud/"
  34. INSTALL_DOC_URL="https://learn.netdata.cloud/docs/install-the-netdata-agent/one-line-installer-for-all-linux-systems"
  35. PACKAGES_SCRIPT="https://raw.githubusercontent.com/netdata/netdata/master/packaging/installer/install-required-packages.sh"
  36. PUBLIC_CLOUD_URL="https://app.netdata.cloud"
  37. REPOCONFIG_DEB_URL_PREFIX="https://repo.netdata.cloud/repos/repoconfig"
  38. REPOCONFIG_RPM_URL_PREFIX="https://repo.netdata.cloud/repos/repoconfig"
  39. TELEMETRY_URL="https://us-east1-netdata-analytics-bi.cloudfunctions.net/ingest_agent_events"
  40. # ======================================================================
  41. # Defaults for environment variables
  42. DRY_RUN=0
  43. SELECTED_INSTALL_METHOD="none"
  44. INSTALL_TYPE="unknown"
  45. INSTALL_PREFIX=""
  46. NETDATA_AUTO_UPDATES="default"
  47. NETDATA_CLAIM_URL="https://app.netdata.cloud"
  48. NETDATA_COMMAND="default"
  49. NETDATA_DISABLE_CLOUD=0
  50. NETDATA_INSTALLER_OPTIONS=""
  51. NETDATA_FORCE_METHOD=""
  52. NETDATA_OFFLINE_INSTALL_SOURCE=""
  53. NETDATA_REQUIRE_CLOUD=1
  54. NETDATA_WARNINGS=""
  55. RELEASE_CHANNEL="default"
  56. if [ -n "$DISABLE_TELEMETRY" ]; then
  57. NETDATA_DISABLE_TELEMETRY="${DISABLE_TELEMETRY}"
  58. elif [ -n "$DO_NOT_TRACK" ]; then
  59. NETDATA_DISABLE_TELEMETRY="${DO_NOT_TRACK}"
  60. else
  61. NETDATA_DISABLE_TELEMETRY=0
  62. fi
  63. NETDATA_TARBALL_BASEURL="${NETDATA_TARBALL_BASEURL:-https://github.com/netdata/netdata-nightlies/releases}"
  64. if echo "${0}" | grep -q 'kickstart-static64'; then
  65. NETDATA_FORCE_METHOD='static'
  66. fi
  67. if [ ! -t 1 ]; then
  68. INTERACTIVE=0
  69. else
  70. INTERACTIVE=1
  71. fi
  72. CURL="$(PATH="${PATH}:/opt/netdata/bin" command -v curl 2>/dev/null && true)"
  73. # ======================================================================
  74. # Shared messages used in multiple places throughout the script.
  75. BADCACHE_MSG="Usually this is a result of an older copy of the file being cached somewhere upstream and can be resolved by retrying in an hour"
  76. BADNET_MSG="This is usually a result of a networking issue"
  77. ERROR_F0003="Could not find a usable HTTP client. Either curl or wget is required to proceed with installation."
  78. BADOPT_MSG="If you are following a third-party guide online, please see ${INSTALL_DOC_URL} for current instructions for using this script. If you are using a local copy of this script instead of fetching it from our servers, consider updating it. If you intended to pass this option to the installer code, please use either --local-build-options or --static-install-options to specify it instead."
  79. # ======================================================================
  80. # Core program logic
  81. main() {
  82. case "${ACTION}" in
  83. uninstall)
  84. uninstall
  85. printf >&2 "Finished uninstalling the Netdata Agent."
  86. deferred_warnings
  87. cleanup
  88. trap - EXIT
  89. exit 0
  90. ;;
  91. reinstall-clean)
  92. NEW_INSTALL_PREFIX="${INSTALL_PREFIX}"
  93. uninstall
  94. cleanup
  95. ACTION=''
  96. INSTALL_PREFIX="${NEW_INSTALL_PREFIX}"
  97. # shellcheck disable=SC2086
  98. main
  99. trap - EXIT
  100. exit 0
  101. ;;
  102. prepare-offline)
  103. prepare_offline_install_source "${OFFLINE_TARGET}"
  104. deferred_warnings
  105. trap - EXIT
  106. exit 0
  107. ;;
  108. esac
  109. set_tmpdir
  110. if [ -n "${INSTALL_VERSION}" ]; then
  111. if echo "${INSTALL_VERSION}" | grep -E -o "^[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+$" > /dev/null 2>&1; then
  112. NEW_SELECTED_RELEASE_CHANNEL="stable"
  113. else
  114. NEW_SELECTED_RELEASE_CHANNEL="nightly"
  115. fi
  116. if ! [ "${NEW_SELECTED_RELEASE_CHANNEL}" = "${SELECTED_RELEASE_CHANNEL}" ]; then
  117. warning "Selected release channel does not match this version and it will be changed automatically."
  118. SELECTED_RELEASE_CHANNEL="${NEW_SELECTED_RELEASE_CHANNEL}"
  119. fi
  120. fi
  121. case "${SYSTYPE}" in
  122. Linux) install_on_linux ;;
  123. Darwin) install_on_macos ;;
  124. FreeBSD) install_on_freebsd ;;
  125. esac
  126. if [ -n "${NETDATA_CLAIM_TOKEN}" ]; then
  127. claim
  128. elif [ "${NETDATA_DISABLE_CLOUD}" -eq 1 ]; then
  129. soft_disable_cloud
  130. fi
  131. set_auto_updates
  132. printf >&2 "%s\n\n" "Successfully installed the Netdata Agent."
  133. deferred_warnings
  134. success_banner
  135. telemetry_event INSTALL_SUCCESS "" ""
  136. cleanup
  137. trap - EXIT
  138. }
  139. # ======================================================================
  140. # Usage info
  141. usage() {
  142. cat << HEREDOC
  143. USAGE: kickstart.sh [options]
  144. where options include:
  145. --non-interactive Do not prompt for user input. (default: prompt if there is a controlling terminal)
  146. --interactive Prompt for user input even if there is no controlling terminal.
  147. --dont-start-it Do not start the agent by default (only for static installs or local builds)
  148. --dry-run Report what we would do with the given options on this system, but don’t actually do anything.
  149. --release-channel Specify the release channel to use for the install (default: ${DEFAULT_RELEASE_CHANNEL})
  150. --stable-channel Equivalent to "--release-channel stable"
  151. --nightly-channel Equivalent to "--release-channel nightly"
  152. --no-updates Do not enable automatic updates (default: enable automatic updates using the best supported scheduling method)
  153. --auto-update Enable automatic updates.
  154. --auto-update-type Specify a particular scheduling type for auto-updates (valid types: systemd, interval, crontab)
  155. --disable-telemetry Opt-out of anonymous statistics.
  156. --native-only Only install if native binary packages are available.
  157. --static-only Only install if a static build is available.
  158. --build-only Only install using a local build.
  159. --disable-cloud Disable support for Netdata Cloud (default: detect)
  160. --require-cloud Only install if Netdata Cloud can be enabled. Overrides --disable-cloud.
  161. --install-prefix <path> Specify an installation prefix for local builds (default: autodetect based on system type).
  162. --old-install-prefix <path> Specify an old local builds installation prefix for uninstall/reinstall (if it's not default).
  163. --install-version <version> Specify the version of Netdata to install.
  164. --claim-token Use a specified token for claiming to Netdata Cloud.
  165. --claim-rooms When claiming, add the node to the specified rooms.
  166. --claim-* Specify other options for the claiming script.
  167. --no-cleanup Don't do any cleanup steps. This is intended to help with debugging the installer.
  168. --local-build-options Specify additional options to pass to the installer code when building locally. Only valid if --build-only is also specified.
  169. --static-install-options Specify additional options to pass to the static installer code. Only valid if --static-only is also specified.
  170. The following options are mutually exclusive and specifiy special operations other than trying to install Netdata normally or update an existing install:
  171. --reinstall If there is an existing install, reinstall it instead of trying to update it. If there is no existing install, install netdata normally.
  172. --reinstall-even-if-unsafe If there is an existing install, reinstall it instead of trying to update it, even if doing so is known to potentially break things. If there is no existing install, install Netdata normally.
  173. --reinstall-clean If there is an existing install, uninstall it before trying to install Netdata. Fails if there is no existing install.
  174. --uninstall Uninstall an existing installation of Netdata. Fails if there is no existing install.
  175. --claim-only If there is an existing install, only try to claim it without attempting to update it. If there is no existing install, install and claim Netdata normally.
  176. --repositories-only Only install repository configuration packages instead of doing a full install of Netdata. Automatically sets --native-only.
  177. --prepare-offline-install-source Instead of installing the agent, prepare a directory that can be used to install on another system without needing to download anything.
  178. Additionally, this script may use the following environment variables:
  179. TMPDIR: Used to specify where to put temporary files. On most systems, the default we select
  180. automatically should be fine. The user running the script needs to both be able to
  181. write files to the temporary directory, and run files from that location.
  182. ROOTCMD: Used to specify a command to use to run another command with root privileges if needed. By
  183. default we try to use sudo, doas, or pkexec (in that order of preference), but if
  184. you need special options for one of those to work, or have a different tool to do
  185. the same thing on your system, you can specify it here.
  186. DISABLE_TELEMETRY If set to a value other than 0, behave as if \`--disable-telemetry\` was specified.
  187. HEREDOC
  188. }
  189. # ======================================================================
  190. # Telemetry functions
  191. telemetry_event() {
  192. if [ "${NETDATA_DISABLE_TELEMETRY}" -eq 1 ] || [ "${DRY_RUN}" -eq 1 ]; then
  193. return 0
  194. fi
  195. now="$(date +%s)"
  196. total_duration="$((now - START_TIME))"
  197. if [ -e "/etc/os-release" ]; then
  198. eval "$(grep -E "^(NAME|ID|ID_LIKE|VERSION|VERSION_ID)=" < /etc/os-release | sed 's/^/HOST_/')"
  199. fi
  200. if [ -z "${HOST_NAME}" ] || [ -z "${HOST_VERSION}" ] || [ -z "${HOST_ID}" ]; then
  201. if [ -f "/etc/lsb-release" ]; then
  202. DISTRIB_ID="unknown"
  203. DISTRIB_RELEASE="unknown"
  204. DISTRIB_CODENAME="unknown"
  205. eval "$(grep -E "^(DISTRIB_ID|DISTRIB_RELEASE|DISTRIB_CODENAME)=" < /etc/lsb-release)"
  206. if [ -z "${HOST_NAME}" ]; then HOST_NAME="${DISTRIB_ID}"; fi
  207. if [ -z "${HOST_VERSION}" ]; then HOST_VERSION="${DISTRIB_RELEASE}"; fi
  208. if [ -z "${HOST_ID}" ]; then HOST_ID="${DISTRIB_CODENAME}"; fi
  209. fi
  210. fi
  211. KERNEL_NAME="$(uname -s)"
  212. if [ "${KERNEL_NAME}" = FreeBSD ]; then
  213. TOTAL_RAM="$(sysctl -n hw.physmem)"
  214. elif [ "${KERNEL_NAME}" = Darwin ]; then
  215. TOTAL_RAM="$(sysctl -n hw.memsize)"
  216. elif [ -r /proc/meminfo ]; then
  217. TOTAL_RAM="$(grep -F MemTotal /proc/meminfo | cut -f 2 -d ':' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' | cut -f 1 -d ' ')"
  218. TOTAL_RAM="$((TOTAL_RAM * 1024))"
  219. fi
  220. MD5_PATH="$(exec <&- 2>&-; which md5sum || command -v md5sum || type md5sum)"
  221. if [ "${KERNEL_NAME}" = Darwin ] && command -v ioreg >/dev/null 2>&1; then
  222. DISTINCT_ID="macos-$(ioreg -rd1 -c IOPlatformExpertDevice | awk '/IOPlatformUUID/ { split($0, line, "\""); printf("%s\n", line[4]); }')"
  223. elif [ -f /etc/machine-id ] && [ -n "$MD5_PATH" ]; then
  224. DISTINCT_ID="machine-$($MD5_PATH < /etc/machine-id | cut -f1 -d" ")"
  225. elif [ -f /var/db/dbus/machine-id ] && [ -n "$MD5_PATH" ]; then
  226. DISTINCT_ID="dbus-$($MD5_PATH < /var/db/dbus/machine-id | cut -f1 -d" ")"
  227. elif [ -f /var/lib/dbus/machine-id ] && [ -n "$MD5_PATH" ]; then
  228. DISTINCT_ID="dbus-$($MD5_PATH < /var/lib/dbus/machine-id | cut -f1 -d" ")"
  229. elif command -v uuidgen > /dev/null 2>&1; then
  230. DISTINCT_ID="uuid-$(uuidgen | tr '[:upper:]' '[:lower:]')"
  231. else
  232. DISTINCT_ID="null"
  233. fi
  234. REQ_BODY="$(cat << EOF
  235. {
  236. "event": "${1}",
  237. "properties": {
  238. "distinct_id": "${DISTINCT_ID}",
  239. "event_source": "agent installer",
  240. "\$current_url": "agent installer",
  241. "\$pathname": "netdata-installer",
  242. "\$host": "installer.netdata.io",
  243. "\$ip": "127.0.0.1",
  244. "script_variant": "kickstart-ng",
  245. "error_code": "${3}",
  246. "error_message": "${2}",
  247. "install_options": "${KICKSTART_OPTIONS}",
  248. "install_interactivity": "${INTERACTIVE}",
  249. "install_auto_updates": "${NETDATA_AUTO_UPDATES}",
  250. "install_command": "${NETDATA_COMMAND}",
  251. "total_runtime": "${total_duration}",
  252. "selected_install_method": "${SELECTED_INSTALL_METHOD}",
  253. "netdata_release_channel": "${RELEASE_CHANNEL:-null}",
  254. "netdata_install_type": "${INSTALL_TYPE}",
  255. "host_os_name": "${HOST_NAME:-unknown}",
  256. "host_os_id": "${HOST_ID:-unknown}",
  257. "host_os_id_like": "${HOST_ID_LIKE:-unknown}",
  258. "host_os_version": "${HOST_VERSION:-unknown}",
  259. "host_os_version_id": "${HOST_VERSION_ID:-unknown}",
  260. "system_kernel_name": "${KERNEL_NAME}",
  261. "system_kernel_version": "$(uname -r)",
  262. "system_architecture": "$(uname -m)",
  263. "system_total_ram": "${TOTAL_RAM:-unknown}",
  264. "system_distinct_id": "${DISTINCT_ID}"
  265. }
  266. }
  267. EOF
  268. )"
  269. if [ -n "${CURL}" ]; then
  270. "${CURL}" --silent -o /dev/null -X POST --max-time 2 --header "Content-Type: application/json" -d "${REQ_BODY}" "${TELEMETRY_URL}" > /dev/null
  271. elif command -v wget > /dev/null 2>&1; then
  272. if wget --help 2>&1 | grep BusyBox > /dev/null 2>&1; then
  273. # BusyBox-compatible version of wget, there is no --no-check-certificate option
  274. wget -q -O - \
  275. -T 1 \
  276. --header 'Content-Type: application/json' \
  277. --post-data "${REQ_BODY}" \
  278. "${TELEMETRY_URL}" > /dev/null
  279. else
  280. wget -q -O - --no-check-certificate \
  281. --method POST \
  282. --timeout=1 \
  283. --header 'Content-Type: application/json' \
  284. --body-data "${REQ_BODY}" \
  285. "${TELEMETRY_URL}" > /dev/null
  286. fi
  287. fi
  288. }
  289. trap_handler() {
  290. code="${1}"
  291. lineno="${2}"
  292. deferred_warnings
  293. printf >&2 "%s\n\n" "${TPUT_BGRED}${TPUT_WHITE}${TPUT_BOLD} ERROR ${TPUT_RESET} Installer exited unexpectedly (${code}-${lineno})"
  294. case "${code}" in
  295. 0) printf >&2 "%s\n" "This is almost certainly the result of a bug. If you have time, please report it at ${AGENT_BUG_REPORT_URL}." ;;
  296. *)
  297. printf >&2 "%s\n" "This is probably a result of a transient issue on your system. Things should work correctly if you try again."
  298. printf >&2 "%s\n" "If you continue to experience this issue, you can reacn out to us for support on:"
  299. support_list
  300. ;;
  301. esac
  302. telemetry_event INSTALL_CRASH "Installer exited unexpectedly (${code}-${lineno})" "E${code}-${lineno}"
  303. trap - EXIT
  304. cleanup
  305. exit 1
  306. }
  307. trap 'trap_handler 0 ${LINENO}' EXIT
  308. trap 'trap_handler 1 0' HUP
  309. trap 'trap_handler 2 0' INT
  310. trap 'trap_handler 3 0' QUIT
  311. trap 'trap_handler 13 0' PIPE
  312. trap 'trap_handler 15 0' TERM
  313. # ======================================================================
  314. # Utility functions
  315. setup_terminal() {
  316. TPUT_RESET=""
  317. TPUT_WHITE=""
  318. TPUT_BGRED=""
  319. TPUT_BGGREEN=""
  320. TPUT_BOLD=""
  321. TPUT_DIM=""
  322. # Is stderr on the terminal? If not, then fail
  323. test -t 2 || return 1
  324. if command -v tput > /dev/null 2>&1; then
  325. if num_colors=$(tput colors 2> /dev/null) && [ "${num_colors:-0}" -ge 8 ]; then
  326. # Enable colors
  327. TPUT_RESET="$(tput sgr 0)"
  328. TPUT_WHITE="$(tput setaf 7)"
  329. TPUT_BGRED="$(tput setab 1)"
  330. TPUT_BGGREEN="$(tput setab 2)"
  331. TPUT_BOLD="$(tput bold)"
  332. TPUT_DIM="$(tput dim)"
  333. fi
  334. fi
  335. echo "${TPUT_RESET}"
  336. return 0
  337. }
  338. support_list() {
  339. printf >&2 "%s\n" " - GitHub: ${DISCUSSIONS_URL}"
  340. printf >&2 "%s\n" " - Discord: ${DISCORD_INVITE}"
  341. printf >&2 "%s\n" " - Our community forums: ${FORUM_URL}"
  342. }
  343. success_banner() {
  344. printf >&2 "%s\n\n" "Official documentation can be found online at ${DOCS_URL}."
  345. if [ -z "${CLAIM_TOKEN}" ]; then
  346. printf >&2 "%s\n\n" "Looking to monitor all of your infrastructure with Netdata? Check out Netdata Cloud at ${PUBLIC_CLOUD_URL}."
  347. fi
  348. printf >&2 "%s\n" "Join our community and connect with us on:"
  349. support_list
  350. }
  351. cleanup() {
  352. if [ -z "${NO_CLEANUP}" ] && [ -n "${tmpdir}" ]; then
  353. cd || true
  354. DRY_RUN=0
  355. run_as_root rm -rf "${tmpdir}"
  356. fi
  357. }
  358. deferred_warnings() {
  359. if [ -n "${NETDATA_WARNINGS}" ]; then
  360. printf >&2 "%s\n" "The following non-fatal warnings or errors were encountered:"
  361. # shellcheck disable=SC2059
  362. printf >&2 "${NETDATA_WARNINGS}"
  363. printf >&2 "\n\n"
  364. fi
  365. }
  366. fatal() {
  367. deferred_warnings
  368. printf >&2 "%s\n\n" "${TPUT_BGRED}${TPUT_WHITE}${TPUT_BOLD} ABORTED ${TPUT_RESET} ${1}"
  369. printf >&2 "%s\n" "For community support, you can connect with us on:"
  370. support_list
  371. telemetry_event "INSTALL_FAILED" "${1}" "${2}"
  372. cleanup
  373. trap - EXIT
  374. exit 1
  375. }
  376. ESCAPED_PRINT_METHOD=
  377. # shellcheck disable=SC3050
  378. if printf "%s " test > /dev/null 2>&1; then
  379. ESCAPED_PRINT_METHOD="printfq"
  380. fi
  381. escaped_print() {
  382. if [ "${ESCAPED_PRINT_METHOD}" = "printfq" ]; then
  383. # shellcheck disable=SC3050
  384. printf "%s " "${@}"
  385. else
  386. printf "%s" "${*}"
  387. fi
  388. return 0
  389. }
  390. progress() {
  391. echo >&2 " --- ${TPUT_BOLD}${*}${TPUT_RESET} --- "
  392. }
  393. run_logfile="/dev/null"
  394. run() {
  395. user="${USER--}"
  396. dir="${PWD}"
  397. if [ "$(id -u)" = "0" ]; then
  398. info="[root ${dir}]# "
  399. info_console="[${TPUT_DIM}${dir}${TPUT_RESET}]# "
  400. else
  401. info="[${user} ${dir}]$ "
  402. info_console="[${TPUT_DIM}${dir}${TPUT_RESET}]$ "
  403. fi
  404. if [ "${DRY_RUN}" -eq 1 ]; then
  405. printf >&2 "%s" "Would run command:\n"
  406. fi
  407. {
  408. printf "%s" "${info}"
  409. escaped_print "${@}"
  410. printf " ... "
  411. } >> "${run_logfile}"
  412. printf >&2 "%s" "${info_console}${TPUT_BOLD}"
  413. escaped_print >&2 "${@}"
  414. printf >&2 "%s\n" "${TPUT_RESET}"
  415. if [ "${DRY_RUN}" -ne 1 ]; then
  416. "${@}"
  417. ret=$?
  418. else
  419. ret=0
  420. fi
  421. if [ ${ret} -ne 0 ]; then
  422. printf >&2 "%s\n\n" "${TPUT_BGRED}${TPUT_WHITE}${TPUT_BOLD} FAILED ${TPUT_RESET}"
  423. printf "%s\n" "FAILED with exit code ${ret}" >> "${run_logfile}"
  424. # shellcheck disable=SC2089
  425. NETDATA_WARNINGS="${NETDATA_WARNINGS}\n - Command \"${*}\" failed with exit code ${ret}."
  426. else
  427. printf >&2 "%s\n\n" "${TPUT_BGGREEN}${TPUT_WHITE}${TPUT_BOLD} OK ${TPUT_RESET}"
  428. printf "OK\n" >> "${run_logfile}"
  429. fi
  430. return ${ret}
  431. }
  432. run_as_root() {
  433. confirm_root_support
  434. if [ "$(id -u)" -ne "0" ]; then
  435. printf >&2 "Root privileges required to run %s\n" "${*}"
  436. fi
  437. run ${ROOTCMD} "${@}"
  438. }
  439. run_script() {
  440. set_tmpdir
  441. export NETDATA_SCRIPT_STATUS_PATH="${tmpdir}/.script-status"
  442. export NETDATA_SAVE_WARNINGS=1
  443. export NETDATA_PROPAGATE_WARNINGS=1
  444. # shellcheck disable=SC2090
  445. export NETDATA_WARNINGS="${NETDATA_WARNINGS}"
  446. # shellcheck disable=SC2086
  447. run ${ROOTCMD} "${@}"
  448. if [ -r "${NETDATA_SCRIPT_STATUS_PATH}" ]; then
  449. # shellcheck disable=SC1090
  450. . "${NETDATA_SCRIPT_STATUS_PATH}"
  451. rm -f "${NETDATA_SCRIPT_STATUS_PATH}"
  452. fi
  453. }
  454. warning() {
  455. printf >&2 "%s\n\n" "${TPUT_BGRED}${TPUT_WHITE}${TPUT_BOLD} WARNING ${TPUT_RESET} ${*}"
  456. NETDATA_WARNINGS="${NETDATA_WARNINGS}\n - ${*}"
  457. }
  458. _cannot_use_tmpdir() {
  459. testfile="$(TMPDIR="${1}" mktemp -q -t netdata-test.XXXXXXXXXX)"
  460. ret=0
  461. if [ -z "${testfile}" ]; then
  462. return "${ret}"
  463. fi
  464. if printf '#!/bin/sh\necho SUCCESS\n' > "${testfile}"; then
  465. if chmod +x "${testfile}"; then
  466. if [ "$("${testfile}")" = "SUCCESS" ]; then
  467. ret=1
  468. fi
  469. fi
  470. fi
  471. rm -f "${testfile}"
  472. return "${ret}"
  473. }
  474. create_tmp_directory() {
  475. if [ -z "${TMPDIR}" ] || _cannot_use_tmpdir "${TMPDIR}"; then
  476. if _cannot_use_tmpdir /tmp; then
  477. if _cannot_use_tmpdir "${PWD}"; then
  478. fatal "Unable to find a usable temporary directory. Please set \$TMPDIR to a path that is both writable and allows execution of files and try again." F0400
  479. else
  480. TMPDIR="${PWD}"
  481. fi
  482. else
  483. TMPDIR="/tmp"
  484. fi
  485. fi
  486. mktemp -d -t netdata-kickstart-XXXXXXXXXX
  487. }
  488. set_tmpdir() {
  489. if [ -z "${tmpdir}" ] || [ ! -d "${tmpdir}" ]; then
  490. tmpdir="$(create_tmp_directory)"
  491. progress "Using ${tmpdir} as a temporary directory."
  492. cd "${tmpdir}" || fatal "Failed to change current working directory to ${tmpdir}." F000A
  493. fi
  494. }
  495. check_for_remote_file() {
  496. url="${1}"
  497. if echo "${url}" | grep -Eq "^file:///"; then
  498. [ -e "${url#file://}" ] || return 1
  499. elif [ -n "${CURL}" ]; then
  500. "${CURL}" --output /dev/null --silent --head --fail "${url}" || return 1
  501. elif command -v wget > /dev/null 2>&1; then
  502. wget -S --spider "${url}" 2>&1 | grep -q 'HTTP/1.1 200 OK' || return 1
  503. else
  504. fatal "${ERROR_F0003}" F0003
  505. fi
  506. }
  507. download() {
  508. url="${1}"
  509. dest="${2}"
  510. if echo "${url}" | grep -Eq "^file:///"; then
  511. run cp "${url#file://}" "${dest}" || return 1
  512. elif [ -n "${CURL}" ]; then
  513. run "${CURL}" --fail -q -sSL --connect-timeout 10 --retry 3 --output "${dest}" "${url}" || return 1
  514. elif command -v wget > /dev/null 2>&1; then
  515. run wget -T 15 -O "${dest}" "${url}" || return 1
  516. else
  517. fatal "${ERROR_F0003}" F0003
  518. fi
  519. }
  520. get_redirect() {
  521. url="${1}"
  522. if [ -n "${CURL}" ]; then
  523. run sh -c "${CURL} ${url} -s -L -I -o /dev/null -w '%{url_effective}' | grep -o '[^/]*$'" || return 1
  524. elif command -v wget > /dev/null 2>&1; then
  525. run sh -c "wget -S -O /dev/null ${url} 2>&1 | grep -m 1 Location | grep -o '[^/]*$'" || return 1
  526. else
  527. fatal "${ERROR_F0003}" F0003
  528. fi
  529. }
  530. safe_sha256sum() {
  531. # Within the context of the installer, we only use -c option that is common between the two commands
  532. # We will have to reconsider if we start using non-common options
  533. if command -v shasum > /dev/null 2>&1; then
  534. shasum -a 256 "$@"
  535. elif command -v sha256sum > /dev/null 2>&1; then
  536. sha256sum "$@"
  537. else
  538. fatal "Could not find a usable checksum tool. Either sha256sum, or a version of shasum supporting SHA256 checksums is required to proceed with installation." F0004
  539. fi
  540. }
  541. get_system_info() {
  542. SYSARCH="$(uname -m)"
  543. case "$(uname -s)" in
  544. Linux)
  545. SYSTYPE="Linux"
  546. if [ -z "${SKIP_DISTRO_DETECTION}" ]; then
  547. os_release_file=
  548. if [ -s "/etc/os-release" ] && [ -r "/etc/os-release" ]; then
  549. os_release_file="/etc/os-release"
  550. elif [ -s "/usr/lib/os-release" ] && [ -r "/usr/lib/os-release" ]; then
  551. os_release_file="/usr/lib/os-release"
  552. else
  553. warning "Cannot find usable OS release information. Native packages will not be available for this install."
  554. fi
  555. if [ -n "${os_release_file}" ]; then
  556. # shellcheck disable=SC1090
  557. . "${os_release_file}"
  558. DISTRO="${ID}"
  559. SYSVERSION="${VERSION_ID}"
  560. SYSCODENAME="${VERSION_CODENAME}"
  561. else
  562. DISTRO="unknown"
  563. DISTRO_COMPAT_NAME="unknown"
  564. SYSVERSION="unknown"
  565. SYSCODENAME="unknown"
  566. fi
  567. else
  568. warning "Distribution auto-detection overridden by user. This is not guaranteed to work, and is not officially supported."
  569. fi
  570. supported_compat_names="debian ubuntu centos fedora opensuse ol amzn arch"
  571. if str_in_list "${DISTRO}" "${supported_compat_names}"; then
  572. DISTRO_COMPAT_NAME="${DISTRO}"
  573. else
  574. case "${DISTRO}" in
  575. opensuse-leap)
  576. DISTRO_COMPAT_NAME="opensuse"
  577. ;;
  578. opensuse-tumbleweed)
  579. DISTRO_COMPAT_NAME="opensuse"
  580. SYSVERSION="tumbleweed"
  581. ;;
  582. cloudlinux|almalinux|centos-stream|rocky|rhel)
  583. DISTRO_COMPAT_NAME="centos"
  584. ;;
  585. artix|manjaro|obarun)
  586. DISTRO_COMPAT_NAME="arch"
  587. ;;
  588. *)
  589. DISTRO_COMPAT_NAME="unknown"
  590. ;;
  591. esac
  592. fi
  593. case "${DISTRO_COMPAT_NAME}" in
  594. centos|ol) SYSVERSION=$(echo "$SYSVERSION" | cut -d'.' -f1) ;;
  595. esac
  596. ;;
  597. Darwin)
  598. SYSTYPE="Darwin"
  599. SYSVERSION="$(sw_vers -buildVersion)"
  600. ;;
  601. FreeBSD)
  602. SYSTYPE="FreeBSD"
  603. SYSVERSION="$(uname -K)"
  604. ;;
  605. *) fatal "Unsupported system type detected. Netdata cannot be installed on this system using this script." F0200 ;;
  606. esac
  607. }
  608. str_in_list() {
  609. printf "%s\n" "${2}" | tr ' ' "\n" | grep -qE "^${1}\$"
  610. return $?
  611. }
  612. confirm_root_support() {
  613. if [ "$(id -u)" -ne "0" ]; then
  614. if [ -z "${ROOTCMD}" ] && command -v sudo > /dev/null; then
  615. if [ "${INTERACTIVE}" -eq 0 ]; then
  616. ROOTCMD="sudo -n"
  617. else
  618. ROOTCMD="sudo"
  619. fi
  620. fi
  621. if [ -z "${ROOTCMD}" ] && command -v doas > /dev/null; then
  622. if [ "${INTERACTIVE}" -eq 0 ]; then
  623. ROOTCMD="doas -n"
  624. else
  625. ROOTCMD="doas"
  626. fi
  627. fi
  628. if [ -z "${ROOTCMD}" ] && command -v pkexec > /dev/null; then
  629. ROOTCMD="pkexec"
  630. fi
  631. if [ -z "${ROOTCMD}" ]; then
  632. fatal "This script needs root privileges to install Netdata, but cannot find a way to gain them (we support sudo, doas, and pkexec). Either re-run this script as root, or set \$ROOTCMD to a command that can be used to gain root privileges." F0201
  633. fi
  634. fi
  635. }
  636. confirm() {
  637. prompt="${1} [y/n]"
  638. while true; do
  639. echo "${prompt}"
  640. read -r yn
  641. case "$yn" in
  642. [Yy]*) return 0;;
  643. [Nn]*) return 1;;
  644. *) echo "Please answer yes or no.";;
  645. esac
  646. done
  647. }
  648. # ======================================================================
  649. # Existing install handling code
  650. update() {
  651. updater="${ndprefix}/usr/libexec/netdata/netdata-updater.sh"
  652. if run_as_root test -x "${updater}"; then
  653. if [ "${DRY_RUN}" -eq 1 ]; then
  654. progress "Would attempt to update existing installation by running the updater script located at: ${updater}"
  655. return 0
  656. fi
  657. if [ "${INTERACTIVE}" -eq 0 ]; then
  658. opts="--non-interactive"
  659. else
  660. opts="--interactive"
  661. fi
  662. if run_script "${updater}" ${opts} --not-running-from-cron; then
  663. progress "Updated existing install at ${ndprefix}"
  664. return 0
  665. else
  666. if [ -n "${EXIT_REASON}" ]; then
  667. fatal "Failed to update existing Netdata install at ${ndprefix}: ${EXIT_REASON}" "${EXIT_CODE}"
  668. else
  669. fatal "Failed to update existing Netdata install at ${ndprefix}: Encountered an unhandled error in the updater. Further information about this error may be displayed above." U0000
  670. fi
  671. fi
  672. else
  673. warning "Could not find a usable copy of the updater script. We are unable to update this system in place."
  674. return 1
  675. fi
  676. }
  677. uninstall() {
  678. set_tmpdir
  679. get_system_info
  680. detect_existing_install
  681. if [ -n "${OLD_INSTALL_PREFIX}" ]; then
  682. INSTALL_PREFIX="$(echo "${OLD_INSTALL_PREFIX}/" | sed 's/$/netdata/g')"
  683. else
  684. INSTALL_PREFIX="${ndprefix}"
  685. fi
  686. uninstaller="${INSTALL_PREFIX}/usr/libexec/netdata/netdata-uninstaller.sh"
  687. uninstaller_url="https://raw.githubusercontent.com/netdata/netdata/master/packaging/installer/netdata-uninstaller.sh"
  688. if [ $INTERACTIVE = 0 ]; then
  689. FLAGS="--yes --force"
  690. else
  691. FLAGS="--yes"
  692. fi
  693. if run_as_root test -x "${uninstaller}"; then
  694. if [ "${DRY_RUN}" -eq 1 ]; then
  695. progress "Would attempt to uninstall existing install with uninstaller script found at: ${uninstaller}"
  696. return 0
  697. else
  698. progress "Found existing netdata-uninstaller. Running it.."
  699. # shellcheck disable=SC2086
  700. if ! run_script "${uninstaller}" ${FLAGS}; then
  701. warning "Uninstaller failed. Some parts of Netdata may still be present on the system."
  702. fi
  703. fi
  704. else
  705. if [ "${DRY_RUN}" -eq 1 ]; then
  706. progress "Would download installer script from: ${uninstaller_url}"
  707. progress "Would attempt to uninstall existing install with downloaded uninstaller script."
  708. return 0
  709. else
  710. progress "Downloading netdata-uninstaller ..."
  711. download "${uninstaller_url}" "${tmpdir}/netdata-uninstaller.sh"
  712. chmod +x "${tmpdir}/netdata-uninstaller.sh"
  713. # shellcheck disable=SC2086
  714. if ! run_script "${tmpdir}/netdata-uninstaller.sh" ${FLAGS}; then
  715. warning "Uninstaller failed. Some parts of Netdata may still be present on the system."
  716. fi
  717. fi
  718. fi
  719. }
  720. detect_existing_install() {
  721. set_tmpdir
  722. progress "Checking for existing installations of Netdata..."
  723. if pkg_installed netdata; then
  724. ndprefix="/"
  725. EXISTING_INSTALL_IS_NATIVE="1"
  726. else
  727. EXISTING_INSTALL_IS_NATIVE="0"
  728. if [ -n "${INSTALL_PREFIX}" ]; then
  729. searchpath="${INSTALL_PREFIX}/bin:${INSTALL_PREFIX}/sbin:${INSTALL_PREFIX}/usr/bin:${INSTALL_PREFIX}/usr/sbin:${PATH}"
  730. searchpath="${INSTALL_PREFIX}/netdata/bin:${INSTALL_PREFIX}/netdata/sbin:${INSTALL_PREFIX}/netdata/usr/bin:${INSTALL_PREFIX}/netdata/usr/sbin:${searchpath}"
  731. else
  732. searchpath="${PATH}"
  733. fi
  734. ndpath="$(PATH="${searchpath}" command -v netdata 2>/dev/null)"
  735. if [ -z "$ndpath" ] && [ -x /opt/netdata/bin/netdata ]; then
  736. ndpath="/opt/netdata/bin/netdata"
  737. fi
  738. if [ -n "${ndpath}" ]; then
  739. case "${ndpath}" in
  740. */usr/bin/netdata|*/usr/sbin/netdata) ndprefix="$(dirname "$(dirname "$(dirname "${ndpath}")")")" ;;
  741. *) ndprefix="$(dirname "$(dirname "${ndpath}")")" ;;
  742. esac
  743. fi
  744. if echo "${ndprefix}" | grep -Eq '^/usr$'; then
  745. ndprefix="$(dirname "${ndprefix}")"
  746. fi
  747. fi
  748. if [ -n "${ndprefix}" ]; then
  749. typefile="${ndprefix}/etc/netdata/.install-type"
  750. if [ -r "${typefile}" ]; then
  751. # shellcheck disable=SC1090,SC1091
  752. . "${typefile}"
  753. else
  754. INSTALL_TYPE="unknown"
  755. fi
  756. envfile="${ndprefix}/etc/netdata/.environment"
  757. if [ "${INSTALL_TYPE}" = "unknown" ] || [ "${INSTALL_TYPE}" = "custom" ]; then
  758. if [ -r "${envfile}" ]; then
  759. # shellcheck disable=SC1090,SC1091
  760. . "${envfile}"
  761. if [ -n "${NETDATA_IS_STATIC_INSTALL}" ]; then
  762. if [ "${NETDATA_IS_STATIC_INSTALL}" = "yes" ]; then
  763. INSTALL_TYPE="legacy-static"
  764. else
  765. INSTALL_TYPE="legacy-build"
  766. fi
  767. fi
  768. fi
  769. fi
  770. fi
  771. }
  772. handle_existing_install() {
  773. detect_existing_install
  774. if [ -z "${ndprefix}" ] || [ -z "${INSTALL_TYPE}" ]; then
  775. progress "No existing installations of netdata found, assuming this is a fresh install."
  776. return 0
  777. fi
  778. case "${INSTALL_TYPE}" in
  779. kickstart-*|legacy-*|binpkg-*|manual-static|unknown)
  780. if [ "${INSTALL_TYPE}" = "unknown" ]; then
  781. if [ "${EXISTING_INSTALL_IS_NATIVE}" -eq 1 ]; then
  782. warning "Found an existing netdata install managed by the system package manager, but could not determine the install type. Usually this means you installed an unsupported third-party netdata package. This script supports claiming most such installs, but attempting to update or reinstall them using this script may be dangerous."
  783. else
  784. warning "Found an existing netdata install at ${ndprefix}, but could not determine the install type. Usually this means you installed Netdata through your distribution’s regular package repositories or some other unsupported method."
  785. fi
  786. else
  787. progress "Found an existing netdata install at ${ndprefix}, with installation type '${INSTALL_TYPE}'."
  788. fi
  789. if [ "${ACTION}" = "reinstall" ] || [ "${ACTION}" = "unsafe-reinstall" ]; then
  790. progress "Found an existing netdata install at ${ndprefix}, but user requested reinstall, continuing."
  791. case "${INSTALL_TYPE}" in
  792. binpkg-*) NETDATA_FORCE_METHOD='native' ;;
  793. *-build) NETDATA_FORCE_METHOD='build' ;;
  794. *-static) NETDATA_FORCE_METHOD='static' ;;
  795. *)
  796. if [ "${ACTION}" = "unsafe-reinstall" ]; then
  797. warning "Reinstalling over top of a ${INSTALL_TYPE} installation may be unsafe, but the user has requested we proceed."
  798. elif [ "${INTERACTIVE}" -eq 0 ]; then
  799. fatal "User requested reinstall, but we cannot safely reinstall over top of a ${INSTALL_TYPE} installation, exiting." F0104
  800. else
  801. if [ "${EXISTING_INSTALL_IS_NATIVE}" ]; then
  802. reinstall_prompt="Reinstalling over top of an existing install managed by the system package manager is known to cause things to break, are you sure you want to continue?"
  803. else
  804. reinstall_prompt="Reinstalling over top of a ${INSTALL_TYPE} installation may be unsafe, do you want to continue?"
  805. fi
  806. if confirm "${reinstall_prompt}"; then
  807. progress "OK, continuing."
  808. else
  809. fatal "Cancelling reinstallation at user request." F0105
  810. fi
  811. fi
  812. ;;
  813. esac
  814. return 0
  815. elif [ "${INSTALL_TYPE}" = "unknown" ]; then
  816. claimonly_notice="If you just want to claim this install, you should re-run this command with the --claim-only option instead."
  817. if [ "${EXISTING_INSTALL_IS_NATIVE}" -eq 1 ]; then
  818. failmsg="Attempting to update an installation managed by the system package manager is known to not work in most cases. If you are trying to install the latest version of Netdata, you will need to manually uninstall it through your system package manager. ${claimonly_notice}"
  819. promptmsg="Attempting to update an installation managed by the system package manager is known to not work in most cases. If you are trying to install the latest version of Netdata, you will need to manually uninstall it through your system package manager. ${claimonly_notice} Are you sure you want to continue?"
  820. else
  821. failmsg="We do not support trying to update or claim installations when we cannot determine the install type. You will need to uninstall the existing install using the same method you used to install it to proceed. ${claimonly_notice}"
  822. promptmsg="Attempting to update an existing install with an unknown installation type is not officially supported. It may work, but it also might break your system. ${claimonly_notice} Are you sure you want to continue?"
  823. fi
  824. if [ "${INTERACTIVE}" -eq 0 ] && [ "${ACTION}" != "claim" ]; then
  825. fatal "${failmsg}" F0106
  826. elif [ "${INTERACTIVE}" -eq 1 ] && [ "${ACTION}" != "claim" ]; then
  827. if confirm "${promptmsg}"; then
  828. progress "OK, continuing"
  829. else
  830. fatal "Cancelling update of unknown installation type at user request." F050C
  831. fi
  832. fi
  833. fi
  834. ret=0
  835. if [ "${ACTION}" != "claim" ]; then
  836. if ! update; then
  837. warning "Failed to update existing Netdata install at ${ndprefix}."
  838. else
  839. progress "Successfully updated existing netdata install at ${ndprefix}."
  840. fi
  841. else
  842. warning "Not updating existing install at ${ndprefix}."
  843. fi
  844. if [ -n "${NETDATA_CLAIM_TOKEN}" ]; then
  845. progress "Attempting to claim existing install at ${ndprefix}."
  846. INSTALL_PREFIX="${ndprefix}"
  847. claim
  848. ret=$?
  849. elif [ "${ACTION}" = "claim" ]; then
  850. fatal "User asked to claim, but did not provide a claiming token." F0202
  851. else
  852. progress "Not attempting to claim existing install at ${ndprefix} (no claiming token provided)."
  853. fi
  854. deferred_warnings
  855. success_banner
  856. cleanup
  857. trap - EXIT
  858. exit $ret
  859. ;;
  860. oci)
  861. fatal "This is an OCI container, use the regular container lifecycle management commands for your container tools instead of this script for managing it." F0203
  862. ;;
  863. *)
  864. if [ "${ACTION}" = "reinstall" ] || [ "${ACTION}" = "unsafe-reinstall" ]; then
  865. if [ "${ACTION}" = "unsafe-reinstall" ]; then
  866. warning "Reinstalling over top of a ${INSTALL_TYPE} installation may be unsafe, but the user has requested we proceed."
  867. elif [ "${INTERACTIVE}" -eq 0 ]; then
  868. fatal "User requested reinstall, but we cannot safely reinstall over top of a ${INSTALL_TYPE} installation, exiting." F0104
  869. else
  870. if confirm "Reinstalling over top of a ${INSTALL_TYPE} installation may be unsafe, do you want to continue?"; then
  871. progress "OK, continuing."
  872. else
  873. fatal "Cancelling reinstallation at user request." F0105
  874. fi
  875. fi
  876. else
  877. if [ -n "${NETDATA_CLAIM_TOKEN}" ]; then
  878. progress "Attempting to claim existing install at ${ndprefix}."
  879. INSTALL_PREFIX="${ndprefix}"
  880. claim
  881. ret=$?
  882. cleanup
  883. trap - EXIT
  884. exit $ret
  885. elif [ "${ACTION}" = "claim" ]; then
  886. fatal "User asked to claim, but did not provide a claiming token." F0202
  887. else
  888. fatal "Found an existing netdata install at ${ndprefix}, but the install type is '${INSTALL_TYPE}', which is not supported by this script, refusing to proceed." F0103
  889. fi
  890. fi
  891. ;;
  892. esac
  893. }
  894. soft_disable_cloud() {
  895. set_tmpdir
  896. cloud_prefix="${INSTALL_PREFIX}/var/lib/netdata/cloud.d"
  897. run_as_root mkdir -p "${cloud_prefix}"
  898. cat > "${tmpdir}/cloud.conf" << EOF
  899. [global]
  900. enabled = no
  901. EOF
  902. run_as_root cp "${tmpdir}/cloud.conf" "${cloud_prefix}/cloud.conf"
  903. if [ -z "${NETDATA_NO_START}" ]; then
  904. case "${SYSTYPE}" in
  905. Darwin) run_as_root launchctl kickstart -k com.github.netdata ;;
  906. FreeBSD) run_as_root service netdata restart ;;
  907. Linux)
  908. initpath="$(run_as_root readlink /proc/1/exe)"
  909. if command -v service > /dev/null 2>&1; then
  910. run_as_root service netdata restart
  911. elif command -v rc-service > /dev/null 2>&1; then
  912. run_as_root rc-service netdata restart
  913. elif [ "$(basename "${initpath}" 2> /dev/null)" = "systemd" ]; then
  914. run_as_root systemctl restart netdata
  915. elif [ -f /etc/init.d/netdata ]; then
  916. run_as_root /etc/init.d/netdata restart
  917. fi
  918. ;;
  919. esac
  920. fi
  921. }
  922. confirm_install_prefix() {
  923. if [ -n "${INSTALL_PREFIX}" ] && [ "${NETDATA_FORCE_METHOD}" != 'build' ]; then
  924. fatal "The --install-prefix option is only supported together with the --build-only option." F0204
  925. fi
  926. if [ -n "${INSTALL_PREFIX}" ]; then
  927. NETDATA_INSTALLER_OPTIONS="${NETDATA_INSTALLER_OPTIONS} --install-prefix ${INSTALL_PREFIX}"
  928. else
  929. case "${SYSTYPE}" in
  930. Darwin)
  931. INSTALL_PREFIX="/usr/local/netdata"
  932. NETDATA_INSTALLER_OPTIONS="${NETDATA_INSTALLER_OPTIONS} --install-no-prefix ${INSTALL_PREFIX}"
  933. ;;
  934. FreeBSD)
  935. INSTALL_PREFIX="/usr/local"
  936. NETDATA_INSTALLER_OPTIONS="${NETDATA_INSTALLER_OPTIONS} --install-no-prefix ${INSTALL_PREFIX}"
  937. ;;
  938. esac
  939. fi
  940. }
  941. # ======================================================================
  942. # Claiming support code
  943. check_claim_opts() {
  944. # shellcheck disable=SC2235,SC2030
  945. if [ -z "${NETDATA_CLAIM_TOKEN}" ] && [ -n "${NETDATA_CLAIM_ROOMS}" ]; then
  946. fatal "Invalid claiming options, claim rooms may only be specified when a token is specified." F0204
  947. elif [ -z "${NETDATA_CLAIM_TOKEN}" ] && [ -n "${NETDATA_CLAIM_EXTRA}" ]; then
  948. fatal "Invalid claiming options, a claiming token must be specified." F0204
  949. elif [ "${NETDATA_DISABLE_CLOUD}" -eq 1 ] && [ -n "${NETDATA_CLAIM_TOKEN}" ]; then
  950. fatal "Cloud explicitly disabled, but automatic claiming requested. Either enable Netdata Cloud, or remove the --claim-* options." F0204
  951. fi
  952. }
  953. is_netdata_running() {
  954. if command -v pgrep > /dev/null 2>&1; then
  955. if pgrep netdata; then
  956. return 0
  957. else
  958. return 1
  959. fi
  960. else
  961. if [ -z "${INSTALL_PREFIX}" ]; then
  962. NETDATACLI_PATH=/usr/sbin/netdatacli
  963. elif [ "${INSTALL_PREFIX}" = "/opt/netdata" ]; then
  964. NETDATACLI_PATH="/opt/netdata/bin/netdatacli"
  965. else
  966. NETDATACLI_PATH="${INSTALL_PREFIX}/netdata/usr/sbin/netdatacli"
  967. fi
  968. if "${NETDATACLI_PATH}" ping > /dev/null 2>&1; then
  969. return 0
  970. else
  971. return 1
  972. fi
  973. fi
  974. }
  975. claim() {
  976. if [ "${DRY_RUN}" -eq 1 ]; then
  977. progress "Would attempt to claim agent to ${NETDATA_CLAIM_URL}"
  978. else
  979. progress "Attempting to claim agent to ${NETDATA_CLAIM_URL}"
  980. fi
  981. if command -v netdata-claim.sh > /dev/null 2>&1; then
  982. NETDATA_CLAIM_PATH="$(command -v netdata-claim.sh)"
  983. elif [ -z "${INSTALL_PREFIX}" ] || [ "${INSTALL_PREFIX}" = "/" ]; then
  984. NETDATA_CLAIM_PATH=/usr/sbin/netdata-claim.sh
  985. elif [ "${INSTALL_PREFIX}" = "/opt/netdata" ]; then
  986. NETDATA_CLAIM_PATH="/opt/netdata/bin/netdata-claim.sh"
  987. elif [ ! -d "${INSTALL_PREFIX}/netdata" ]; then
  988. if [ -d "${INSTALL_PREFIX}/usr" ]; then
  989. NETDATA_CLAIM_PATH="${INSTALL_PREFIX}/usr/sbin/netdata-claim.sh"
  990. else
  991. NETDATA_CLAIM_PATH="${INSTALL_PREFIX}/sbin/netdata-claim.sh"
  992. fi
  993. else
  994. NETDATA_CLAIM_PATH="${INSTALL_PREFIX}/netdata/usr/sbin/netdata-claim.sh"
  995. fi
  996. err_msg=
  997. err_code=
  998. if [ -z "${NETDATA_CLAIM_PATH}" ]; then
  999. err_msg="Unable to claim node: could not find usable claiming script. Reinstalling Netdata may resolve this."
  1000. err_code=F050B
  1001. elif [ ! -e "${NETDATA_CLAIM_PATH}" ]; then
  1002. err_msg="Unable to claim node: ${NETDATA_CLAIM_PATH} does not exist."
  1003. err_code=F0512
  1004. elif [ ! -f "${NETDATA_CLAIM_PATH}" ]; then
  1005. err_msg="Unable to claim node: ${NETDATA_CLAIM_PATH} is not a file."
  1006. err_code=F0513
  1007. elif [ ! -x "${NETDATA_CLAIM_PATH}" ]; then
  1008. err_msg="Unable to claim node: claiming script at ${NETDATA_CLAIM_PATH} is not executable. Reinstalling Netdata may resolve this."
  1009. err_code=F0514
  1010. fi
  1011. if [ -n "$err_msg" ]; then
  1012. if [ "${ACTION}" = "claim" ]; then
  1013. fatal "$err_msg" "$err_code"
  1014. else
  1015. warning "$err_msg"
  1016. return 1
  1017. fi
  1018. fi
  1019. if ! is_netdata_running; then
  1020. NETDATA_CLAIM_EXTRA="${NETDATA_CLAIM_EXTRA} -daemon-not-running"
  1021. fi
  1022. # shellcheck disable=SC2086
  1023. run_as_root "${NETDATA_CLAIM_PATH}" -token="${NETDATA_CLAIM_TOKEN}" -rooms="${NETDATA_CLAIM_ROOMS}" -url="${NETDATA_CLAIM_URL}" ${NETDATA_CLAIM_EXTRA}
  1024. case $? in
  1025. 0)
  1026. progress "Successfully claimed node"
  1027. return 0
  1028. ;;
  1029. 1) warning "Unable to claim node due to invalid claiming options. If you are seeing this message, you’ve probably found a bug and should open a bug report at ${AGENT_BUG_REPORT_URL}" ;;
  1030. 2) warning "Unable to claim node due to issues creating the claiming directory or preparing the local claiming key. Make sure you have a working openssl command and that ${INSTALL_PREFIX}/var/lib/netdata/cloud.d exists, then try again." ;;
  1031. 3) warning "Unable to claim node due to missing dependencies. Usually this means that the Netdata Agent was built without support for Netdata Cloud. If you built the agent from source, please install all needed dependencies for Cloud support. If you used the regular installation script and see this error, please file a bug report at ${AGENT_BUG_REPORT_URL}." ;;
  1032. 4) warning "Failed to claim node due to inability to connect to ${NETDATA_CLAIM_URL}. Usually this either means that the specified claiming URL is wrong, or that you are having networking problems." ;;
  1033. 5)
  1034. progress "Successfully claimed node, but was not able to notify the Netdata Agent. You will need to restart the Netdata service on this node before it will show up in the Cloud."
  1035. return 0
  1036. ;;
  1037. 8) warning "Failed to claim node due to an invalid agent ID. You can usually resolve this by removing ${INSTALL_PREFIX}/var/lib/netdata/registry/netdata.public.unique.id and restarting the agent. Then try to claim it again using the same options." ;;
  1038. 9) warning "Failed to claim node due to an invalid node name. This probably means you tried to specify a custom name for this node (for example, using the --claim-hostname option), but the hostname itself was either empty or consisted solely of whitespace. You can resolve this by specifying a valid host name and trying again." ;;
  1039. 10) warning "Failed to claim node due to an invalid room ID. This issue is most likely caused by a typo. Please check if the room(s) you are trying to add appear on the list of rooms provided to the --claim-rooms option ('${NETDATA_CLAIM_ROOMS}'). Then verify if the rooms are visible in Netdata Cloud and try again." ;;
  1040. 11) warning "Failed to claim node due to an issue with the generated RSA key pair. You can usually resolve this by removing all files in ${INSTALL_PREFIX}/var/lib/netdata/cloud.d and then trying again." ;;
  1041. 12) warning "Failed to claim node due to an invalid or expired claiming token. Please check that the token specified with the --claim-token option ('${NETDATA_CLAIM_TOKEN}') matches what you see in the Cloud and try again." ;;
  1042. 13) warning "Failed to claim node because the Cloud thinks it is already claimed. If this node was created by cloning a VM or as a container from a template, please remove the file ${INSTALL_PREFIX}/var/lib/netdata/registry/netdata.public.unique.id and restart the agent. Then try to claim it again with the same options. Otherwise, if you are certain this node has never been claimed before, you can use the --claim-id option to specify a new node ID to use for claiming, for example by using the uuidgen command like so: --claim-id \"\$(uuidgen)\"" ;;
  1043. 14) warning "Failed to claim node because the node is already in the process of being claimed. You should not need to do anything to resolve this, the node should show up properly in the Cloud soon. If it does not, please report a bug at ${AGENT_BUG_REPORT_URL}." ;;
  1044. 15|16|17) warning "Failed to claim node due to an internal server error in the Cloud. Please retry claiming this node later, and if you still see this message file a bug report at ${CLOUD_BUG_REPORT_URL}." ;;
  1045. 18) warning "Unable to claim node because this Netdata installation does not have a unique ID yet. Make sure the agent is running and started up correctly, and then try again." ;;
  1046. *) warning "Failed to claim node for an unknown reason. This usually means either networking problems or a bug. Please retry claiming later, and if you still see this message file a bug report at ${AGENT_BUG_REPORT_URL}" ;;
  1047. esac
  1048. if [ "${ACTION}" = "claim" ]; then
  1049. deferred_warnings
  1050. printf >&2 "%s\n" "For community support, you can connect with us on:"
  1051. support_list
  1052. cleanup
  1053. trap - EXIT
  1054. exit 1
  1055. fi
  1056. }
  1057. # ======================================================================
  1058. # Auto-update handling code.
  1059. set_auto_updates() {
  1060. if run_as_root test -x "${INSTALL_PREFIX}/usr/libexec/netdata/netdata-updater.sh"; then
  1061. updater="${INSTALL_PREFIX}/usr/libexec/netdata/netdata-updater.sh"
  1062. elif run_as_root test -x "${INSTALL_PREFIX}/netdata/usr/libexec/netdata/netdata-updater.sh"; then
  1063. updater="${INSTALL_PREFIX}/netdata/usr/libexec/netdata/netdata-updater.sh"
  1064. else
  1065. warning "Could not find netdata-updater.sh. This means that auto-updates cannot (currently) be enabled on this system. See https://learn.netdata.cloud/docs/agent/packaging/installer/update for more information about updating Netdata."
  1066. return 0
  1067. fi
  1068. if [ "${AUTO_UPDATE}" -eq 1 ]; then
  1069. if [ "${DRY_RUN}" -eq 1 ]; then
  1070. progress "Would have attempted to enable automatic updates."
  1071. # This first case is for catching using a new kickstart script with an old build. It can be safely removed after v1.34.0 is released.
  1072. elif ! run_as_root grep -q '\-\-enable-auto-updates' "${updater}"; then
  1073. echo
  1074. elif ! run_as_root "${updater}" --enable-auto-updates "${NETDATA_AUTO_UPDATE_TYPE}"; then
  1075. warning "Failed to enable auto updates. Netdata will still work, but you will need to update manually."
  1076. fi
  1077. else
  1078. if [ "${DRY_RUN}" -eq 1 ]; then
  1079. progress "Would have attempted to disable automatic updates."
  1080. else
  1081. run_as_root "${updater}" --disable-auto-updates
  1082. fi
  1083. fi
  1084. }
  1085. # ======================================================================
  1086. # Native package install code.
  1087. # Check for an already installed package with a given name.
  1088. pkg_installed() {
  1089. case "${SYSTYPE}" in
  1090. Linux)
  1091. case "${DISTRO_COMPAT_NAME}" in
  1092. debian|ubuntu)
  1093. # shellcheck disable=SC2016
  1094. dpkg-query --show --showformat '${Status}' "${1}" 2>&1 | cut -f 1 -d ' ' | grep -q '^install$'
  1095. return $?
  1096. ;;
  1097. centos|fedora|opensuse|ol|amzn)
  1098. rpm -q "${1}" > /dev/null 2>&1
  1099. return $?
  1100. ;;
  1101. alpine)
  1102. apk -e info "${1}" > /dev/null 2>&1
  1103. return $?
  1104. ;;
  1105. arch)
  1106. pacman -Qi "${1}" > /dev/null 2>&1
  1107. return $?
  1108. ;;
  1109. *) return 1 ;;
  1110. esac
  1111. ;;
  1112. Darwin)
  1113. if command -v brew > /dev/null 2>&1; then
  1114. brew list "${1}" > /dev/null 2>&1
  1115. return $?
  1116. else
  1117. return 1
  1118. fi
  1119. ;;
  1120. FreeBSD)
  1121. if pkg -N > /dev/null 2>&1; then
  1122. pkg info "${1}" > /dev/null 2>&1
  1123. return $?
  1124. else
  1125. return 1
  1126. fi
  1127. ;;
  1128. *) return 1 ;;
  1129. esac
  1130. }
  1131. # Check for the existence of a usable netdata package in the repo.
  1132. netdata_avail_check() {
  1133. case "${DISTRO_COMPAT_NAME}" in
  1134. debian|ubuntu)
  1135. env DEBIAN_FRONTEND=noninteractive apt-cache policy netdata | grep -q repo.netdata.cloud/repos/;
  1136. return $?
  1137. ;;
  1138. centos|fedora|ol|amzn)
  1139. # shellcheck disable=SC2086
  1140. ${pm_cmd} search --nogpgcheck -v netdata | grep -qE 'Repo *: netdata(-edge)?$'
  1141. return $?
  1142. ;;
  1143. opensuse)
  1144. zypper packages -r "$(zypper repos | grep -E 'netdata |netdata-edge ' | cut -f 1 -d '|' | tr -d ' ')" | grep -E 'netdata '
  1145. return $?
  1146. ;;
  1147. *) return 1 ;;
  1148. esac
  1149. }
  1150. # Check for any distro-specific dependencies we know we need.
  1151. check_special_native_deps() {
  1152. if [ "${DISTRO_COMPAT_NAME}" = "centos" ] && [ "${SYSVERSION}" = "7" ]; then
  1153. progress "Checking for libuv availability."
  1154. if ${pm_cmd} search --nogpgcheck -v libuv | grep -q "No matches found"; then
  1155. progress "libuv not found, checking for EPEL availability."
  1156. if ${pm_cmd} search --nogpgcheck -v epel-release | grep -q "No matches found"; then
  1157. warning "Unable to find a suitable source for libuv, cannot install using native packages on this system."
  1158. return 1
  1159. else
  1160. progress "EPEL is available, attempting to install so that required dependencies are available."
  1161. # shellcheck disable=SC2086
  1162. if ! run_as_root env ${env} ${pm_cmd} ${install_subcmd} ${pkg_install_opts} epel-release; then
  1163. warning "Failed to install EPEL, even though it is required to install native packages on this system."
  1164. return 1
  1165. fi
  1166. fi
  1167. else
  1168. return 0
  1169. fi
  1170. fi
  1171. }
  1172. common_rpm_opts() {
  1173. pkg_type="rpm"
  1174. pkg_suffix=".noarch"
  1175. pkg_vsep="-"
  1176. INSTALL_TYPE="binpkg-rpm"
  1177. NATIVE_VERSION="${INSTALL_VERSION:+"-${INSTALL_VERSION}.${SYSARCH}"}"
  1178. }
  1179. common_dnf_opts() {
  1180. if [ "${INTERACTIVE}" = "0" ]; then
  1181. interactive_opts="-y"
  1182. fi
  1183. if command -v dnf > /dev/null; then
  1184. pm_cmd="dnf"
  1185. repo_subcmd="makecache"
  1186. else
  1187. pm_cmd="yum"
  1188. fi
  1189. install_subcmd="install"
  1190. pkg_install_opts="${interactive_opts}"
  1191. repo_update_opts="${interactive_opts}"
  1192. uninstall_subcmd="remove"
  1193. }
  1194. try_package_install() {
  1195. failed_refresh_msg="Failed to refresh repository metadata. ${BADNET_MSG} or incompatibilities with one or more third-party package repositories in the system package manager configuration."
  1196. if [ -z "${DISTRO_COMPAT_NAME}" ] || [ "${DISTRO_COMPAT_NAME}" = "unknown" ]; then
  1197. warning "Unable to determine Linux distribution for native packages."
  1198. return 2
  1199. elif [ -z "${SYSCODENAME}" ]; then
  1200. case "${DISTRO_COMPAT_NAME}" in
  1201. debian|ubuntu)
  1202. warning "Release codename not set. Unable to check availability of native packages for this system."
  1203. return 2
  1204. ;;
  1205. esac
  1206. fi
  1207. set_tmpdir
  1208. if [ "${DRY_RUN}" -eq 1 ]; then
  1209. progress "Would attempt to install using native packages..."
  1210. else
  1211. progress "Attempting to install using native packages..."
  1212. fi
  1213. if [ "${SELECTED_RELEASE_CHANNEL}" = "nightly" ]; then
  1214. release="-edge"
  1215. else
  1216. release=""
  1217. fi
  1218. interactive_opts=""
  1219. env=""
  1220. case "${DISTRO_COMPAT_NAME}" in
  1221. debian|ubuntu)
  1222. if [ "${INTERACTIVE}" = "0" ]; then
  1223. install_subcmd="-o Dpkg::Options::=--force-confdef -o Dpkg::Options::=--force-confold install"
  1224. interactive_opts="-y"
  1225. env="DEBIAN_FRONTEND=noninteractive"
  1226. else
  1227. install_subcmd="install"
  1228. fi
  1229. needs_early_refresh=1
  1230. pm_cmd="apt-get"
  1231. repo_subcmd="update"
  1232. pkg_type="deb"
  1233. pkg_vsep="_"
  1234. pkg_install_opts="${interactive_opts}"
  1235. repo_update_opts="${interactive_opts}"
  1236. uninstall_subcmd="purge"
  1237. repo_prefix="${DISTRO_COMPAT_NAME}/${SYSCODENAME}"
  1238. pkg_suffix="+${DISTRO_COMPAT_NAME}${SYSVERSION}_all"
  1239. INSTALL_TYPE="binpkg-deb"
  1240. NATIVE_VERSION="${INSTALL_VERSION:+"=${INSTALL_VERSION}"}"
  1241. ;;
  1242. centos)
  1243. common_rpm_opts
  1244. common_dnf_opts
  1245. repo_prefix="el/${SYSVERSION}"
  1246. # if [ "${SYSVERSION}" -lt 8 ]; then
  1247. # explicitly_install_native_plugins=1
  1248. # fi
  1249. ;;
  1250. fedora|ol)
  1251. common_rpm_opts
  1252. common_dnf_opts
  1253. repo_prefix="${DISTRO_COMPAT_NAME}/${SYSVERSION}"
  1254. ;;
  1255. opensuse)
  1256. if [ "${INTERACTIVE}" = "0" ]; then
  1257. install_subcmd="--non-interactive --no-gpg-checks install"
  1258. else
  1259. install_subcmd="--no-gpg-checks install"
  1260. fi
  1261. common_rpm_opts
  1262. pm_cmd="zypper"
  1263. repo_subcmd="--gpg-auto-import-keys refresh"
  1264. repo_prefix="opensuse/${SYSVERSION}"
  1265. pkg_install_opts="${interactive_opts} --allow-unsigned-rpm"
  1266. repo_update_opts=""
  1267. uninstall_subcmd="remove"
  1268. ;;
  1269. amzn)
  1270. common_rpm_opts
  1271. common_dnf_opts
  1272. repo_prefix="amazonlinux/${SYSVERSION}"
  1273. ;;
  1274. *)
  1275. warning "We do not provide native packages for ${DISTRO}."
  1276. return 2
  1277. ;;
  1278. esac
  1279. if [ -n "${SKIP_DISTRO_DETECTION}" ]; then
  1280. warning "Attempting to use native packages with a distro override. This is not officially supported, but may work in some cases. If your system requires a distro override to use native packages, please open an feature request at ${AGENT_BUG_REPORT_URL} about it so that we can update the installer to auto-detect this."
  1281. fi
  1282. if [ -n "${INSTALL_VERSION}" ]; then
  1283. if echo "${INSTALL_VERSION}" | grep -q "nightly"; then
  1284. new_release="-edge"
  1285. else
  1286. new_release=
  1287. fi
  1288. if { [ -n "${new_release}" ] && [ -z "${release}" ]; } || { [ -z "${new_release}" ] && [ -n "${release}" ]; }; then
  1289. warning "Selected release channel does not match this version and it will be changed automatically."
  1290. fi
  1291. release="${new_release}"
  1292. fi
  1293. repoconfig_name="netdata-repo${release}"
  1294. case "${pkg_type}" in
  1295. deb)
  1296. repoconfig_file="${repoconfig_name}${pkg_vsep}${REPOCONFIG_DEB_VERSION}${pkg_suffix}.${pkg_type}"
  1297. repoconfig_url="${REPOCONFIG_DEB_URL_PREFIX}/${repo_prefix}/${repoconfig_file}"
  1298. ;;
  1299. rpm)
  1300. repoconfig_file="${repoconfig_name}${pkg_vsep}${REPOCONFIG_RPM_VERSION}${pkg_suffix}.${pkg_type}"
  1301. repoconfig_url="${REPOCONFIG_RPM_URL_PREFIX}/${repo_prefix}/${SYSARCH}/${repoconfig_file}"
  1302. ;;
  1303. esac
  1304. if ! pkg_installed "${repoconfig_name}"; then
  1305. progress "Checking for availability of repository configuration package."
  1306. if ! check_for_remote_file "${repoconfig_url}"; then
  1307. warning "No repository configuration package available for ${DISTRO} ${SYSVERSION}. Cannot install native packages on this system."
  1308. return 2
  1309. fi
  1310. if ! download "${repoconfig_url}" "${tmpdir}/${repoconfig_file}"; then
  1311. fatal "Failed to download repository configuration package. ${BADNET_MSG}." F0209
  1312. fi
  1313. if [ -n "${needs_early_refresh}" ]; then
  1314. # shellcheck disable=SC2086
  1315. if ! run_as_root env ${env} ${pm_cmd} ${repo_subcmd} ${repo_update_opts}; then
  1316. warning "${failed_refresh_msg}"
  1317. return 2
  1318. fi
  1319. fi
  1320. # shellcheck disable=SC2086
  1321. if ! run_as_root env ${env} ${pm_cmd} ${install_subcmd} ${pkg_install_opts} "${tmpdir}/${repoconfig_file}"; then
  1322. warning "Failed to install repository configuration package."
  1323. return 2
  1324. fi
  1325. if [ -n "${repo_subcmd}" ]; then
  1326. # shellcheck disable=SC2086
  1327. if ! run_as_root env ${env} ${pm_cmd} ${repo_subcmd} ${repo_update_opts}; then
  1328. fatal "${failed_refresh_msg} In most cases, disabling any third-party repositories on the system and re-running the installer with the same options should work. If that does not work, consider using a static build with the --static-only option instead of native packages." F0205
  1329. fi
  1330. fi
  1331. else
  1332. progress "Repository configuration is already present, attempting to install netdata."
  1333. fi
  1334. if [ "${ACTION}" = "repositories-only" ]; then
  1335. progress "Successfully installed repository configuraion package."
  1336. deferred_warnings
  1337. cleanup
  1338. trap - EXIT
  1339. exit 1
  1340. fi
  1341. if ! check_special_native_deps; then
  1342. warning "Could not find secondary dependencies for ${DISTRO} on ${SYSARCH}."
  1343. if [ -z "${NO_CLEANUP}" ]; then
  1344. progress "Attempting to uninstall repository configuration package."
  1345. # shellcheck disable=SC2086
  1346. run_as_root env ${env} ${pm_cmd} ${uninstall_subcmd} ${pkg_install_opts} "${repoconfig_name}"
  1347. fi
  1348. return 2
  1349. fi
  1350. if ! netdata_avail_check "${DISTRO_COMPAT_NAME}"; then
  1351. warning "Could not find a usable native package for ${DISTRO} on ${SYSARCH}."
  1352. if [ -z "${NO_CLEANUP}" ]; then
  1353. progress "Attempting to uninstall repository configuration package."
  1354. # shellcheck disable=SC2086
  1355. run_as_root env ${env} ${pm_cmd} ${uninstall_subcmd} ${pkg_install_opts} "${repoconfig_name}"
  1356. fi
  1357. return 2
  1358. fi
  1359. if [ "${NETDATA_DISABLE_TELEMETRY}" -eq 1 ]; then
  1360. run_as_root mkdir -p "/etc/netdata"
  1361. run_as_root touch "/etc/netdata/.opt-out-from-anonymous-statistics"
  1362. fi
  1363. # shellcheck disable=SC2086
  1364. if ! run_as_root env ${env} ${pm_cmd} ${install_subcmd} ${pkg_install_opts} "netdata${NATIVE_VERSION}"; then
  1365. warning "Failed to install Netdata package."
  1366. if [ -z "${NO_CLEANUP}" ]; then
  1367. progress "Attempting to uninstall repository configuration package."
  1368. # shellcheck disable=SC2086
  1369. run_as_root env ${env} ${pm_cmd} ${uninstall_subcmd} ${pkg_install_opts} "${repoconfig_name}"
  1370. fi
  1371. return 2
  1372. fi
  1373. if [ -n "${explicitly_install_native_plugins}" ]; then
  1374. progress "Installing external plugins."
  1375. # shellcheck disable=SC2086
  1376. if ! run_as_root env ${env} ${pm_cmd} ${install_subcmd} ${DEFAULT_PLUGIN_PACKAGES}; then
  1377. warning "Failed to install external plugin packages. Some collectors may not be available."
  1378. fi
  1379. fi
  1380. }
  1381. # ======================================================================
  1382. # Static build install code
  1383. # shellcheck disable=SC2034,SC2086,SC2126
  1384. set_static_archive_urls() {
  1385. if [ -z "${2}" ]; then
  1386. arch="${SYSARCH}"
  1387. else
  1388. arch="${2}"
  1389. fi
  1390. if [ -n "${NETDATA_OFFLINE_INSTALL_SOURCE}" ]; then
  1391. path="$(cd "${NETDATA_OFFLINE_INSTALL_SOURCE}" || exit 1; pwd)"
  1392. export NETDATA_STATIC_ARCHIVE_URL="file://${path}/netdata-${arch}-latest.gz.run"
  1393. export NETDATA_STATIC_ARCHIVE_NAME="netdata-${arch}-latest.gz.run"
  1394. export NETDATA_STATIC_ARCHIVE_CHECKSUM_URL="file://${path}/sha256sums.txt"
  1395. elif [ "${1}" = "stable" ]; then
  1396. if [ -n "${INSTALL_VERSION}" ]; then
  1397. export NETDATA_STATIC_ARCHIVE_URL="https://github.com/netdata/netdata/releases/download/v${INSTALL_VERSION}/netdata-${arch}-v${INSTALL_VERSION}.gz.run"
  1398. export NETDATA_STATIC_ARCHIVE_OLD_URL="https://github.com/netdata/netdata/releases/download/v${INSTALL_VERSION}/netdata-v${INSTALL_VERSION}.gz.run"
  1399. export NETDATA_STATIC_ARCHIVE_NAME="netdata-${arch}-v${INSTALL_VERSION}.gz.run"
  1400. export NETDATA_STATIC_ARCHIVE_OLD_NAME="netdata-v${INSTALL_VERSION}.gz.run"
  1401. export NETDATA_STATIC_ARCHIVE_CHECKSUM_URL="https://github.com/netdata/netdata/releases/download/v${INSTALL_VERSION}/sha256sums.txt"
  1402. else
  1403. latest="$(get_redirect "https://github.com/netdata/netdata/releases/latest")"
  1404. export NETDATA_STATIC_ARCHIVE_URL="https://github.com/netdata/netdata/releases/download/${latest}/netdata-${arch}-latest.gz.run"
  1405. export NETDATA_STATIC_ARCHIVE_NAME="netdata-${arch}-latest.gz.run"
  1406. export NETDATA_STATIC_ARCHIVE_CHECKSUM_URL="https://github.com/netdata/netdata/releases/download/${latest}/sha256sums.txt"
  1407. fi
  1408. else
  1409. if [ -n "${INSTALL_VERSION}" ]; then
  1410. export NETDATA_STATIC_ARCHIVE_URL="${NETDATA_TARBALL_BASEURL}/download/v${INSTALL_VERSION}/netdata-${arch}-v${INSTALL_VERSION}.gz.run"
  1411. export NETDATA_STATIC_ARCHIVE_OLD_URL="${NETDATA_TARBALL_BASEURL}/download/v${INSTALL_VERSION}/netdata-v${INSTALL_VERSION}.gz.run"
  1412. export NETDATA_STATIC_ARCHIVE_NAME="netdata-${arch}-v${INSTALL_VERSION}.gz.run"
  1413. export NETDATA_STATIC_ARCHIVE_OLD_NAME="netdata-v${INSTALL_VERSION}.gz.run"
  1414. export NETDATA_STATIC_ARCHIVE_CHECKSUM_URL="${NETDATA_TARBALL_BASEURL}/download/v${INSTALL_VERSION}/sha256sums.txt"
  1415. else
  1416. tag="$(get_redirect "${NETDATA_TARBALL_BASEURL}/latest")"
  1417. export NETDATA_STATIC_ARCHIVE_URL="${NETDATA_TARBALL_BASEURL}/download/${tag}/netdata-${arch}-latest.gz.run"
  1418. export NETDATA_STATIC_ARCHIVE_NAME="netdata-${arch}-latest.gz.run"
  1419. export NETDATA_STATIC_ARCHIVE_CHECKSUM_URL="${NETDATA_TARBALL_BASEURL}/download/${tag}/sha256sums.txt"
  1420. fi
  1421. fi
  1422. }
  1423. try_static_install() {
  1424. set_static_archive_urls "${SELECTED_RELEASE_CHANNEL}"
  1425. if [ "${DRY_RUN}" -eq 1 ]; then
  1426. progress "Would attempt to install using static build..."
  1427. else
  1428. progress "Attempting to install using static build..."
  1429. fi
  1430. # Check status code first, so that we can provide nicer fallback for dry runs.
  1431. if check_for_remote_file "${NETDATA_STATIC_ARCHIVE_URL}"; then
  1432. netdata_agent="${NETDATA_STATIC_ARCHIVE_NAME}"
  1433. elif [ "${SYSARCH}" = "x86_64" ] && check_for_remote_file "${NETDATA_STATIC_ARCHIVE_OLD_URL}"; then
  1434. netdata_agent="${NETDATA_STATIC_ARCHIVE_OLD_NAME}"
  1435. export NETDATA_STATIC_ARCHIVE_URL="${NETDATA_STATIC_ARCHIVE_OLD_URL}"
  1436. else
  1437. warning "There is no static build available for ${SYSARCH} CPUs. This usually means we simply do not currently provide static builds for ${SYSARCH} CPUs."
  1438. return 2
  1439. fi
  1440. if ! download "${NETDATA_STATIC_ARCHIVE_URL}" "${tmpdir}/${netdata_agent}"; then
  1441. fatal "Unable to download static build archive for ${SYSARCH}. ${BADNET_MSG}." F0208
  1442. fi
  1443. if ! download "${NETDATA_STATIC_ARCHIVE_CHECKSUM_URL}" "${tmpdir}/sha256sum.txt"; then
  1444. fatal "Unable to fetch checksums to verify static build archive. ${BADNET_MSG}." F0206
  1445. fi
  1446. if [ "${DRY_RUN}" -eq 1 ]; then
  1447. progress "Would validate SHA256 checksum of downloaded static build archive."
  1448. else
  1449. if [ -z "${INSTALL_VERSION}" ]; then
  1450. if ! grep "${netdata_agent}" "${tmpdir}/sha256sum.txt" | safe_sha256sum -c - > /dev/null 2>&1; then
  1451. fatal "Static binary checksum validation failed. ${BADCACHE_MSG}." F0207
  1452. fi
  1453. fi
  1454. fi
  1455. if [ "${INTERACTIVE}" -eq 0 ]; then
  1456. opts="${opts} --accept"
  1457. fi
  1458. progress "Installing netdata"
  1459. # shellcheck disable=SC2086
  1460. if ! run_as_root sh "${tmpdir}/${netdata_agent}" ${opts} -- ${NETDATA_INSTALLER_OPTIONS}; then
  1461. warning "Failed to install static build of Netdata on ${SYSARCH}."
  1462. run rm -rf /opt/netdata
  1463. return 2
  1464. fi
  1465. if [ "${DRY_RUN}" -ne 1 ]; then
  1466. install_type_file="/opt/netdata/etc/netdata/.install-type"
  1467. if [ -f "${install_type_file}" ]; then
  1468. run_as_root sh -c "cat \"${install_type_file}\" > \"${tmpdir}/install-type\""
  1469. run_as_root chown "$(id -u)":"$(id -g)" "${tmpdir}/install-type"
  1470. # shellcheck disable=SC1090,SC1091
  1471. . "${tmpdir}/install-type"
  1472. cat > "${tmpdir}/install-type" <<- EOF
  1473. INSTALL_TYPE='kickstart-static'
  1474. PREBUILT_ARCH='${PREBUILT_ARCH}'
  1475. EOF
  1476. run_as_root chown netdata:netdata "${tmpdir}/install-type"
  1477. run_as_root cp "${tmpdir}/install-type" "${install_type_file}"
  1478. fi
  1479. fi
  1480. }
  1481. # ======================================================================
  1482. # Local build install code
  1483. set_source_archive_urls() {
  1484. if [ "$1" = "stable" ]; then
  1485. if [ -n "${INSTALL_VERSION}" ]; then
  1486. export NETDATA_SOURCE_ARCHIVE_URL="https://github.com/netdata/netdata/releases/download/v${INSTALL_VERSION}/netdata-v${INSTALL_VERSION}.tar.gz"
  1487. export NETDATA_SOURCE_ARCHIVE_CHECKSUM_URL="https://github.com/netdata/netdata/releases/download/v${INSTALL_VERSION}/sha256sums.txt"
  1488. else
  1489. latest="$(get_redirect "https://github.com/netdata/netdata/releases/latest")"
  1490. export NETDATA_SOURCE_ARCHIVE_URL="https://github.com/netdata/netdata/releases/download/${latest}/netdata-${latest}.tar.gz"
  1491. export NETDATA_SOURCE_ARCHIVE_CHECKSUM_URL="https://github.com/netdata/netdata/releases/download/${latest}/sha256sums.txt"
  1492. fi
  1493. else
  1494. if [ -n "${INSTALL_VERSION}" ]; then
  1495. export NETDATA_SOURCE_ARCHIVE_URL="${NETDATA_TARBALL_BASEURL}/download/v${INSTALL_VERSION}/netdata-latest.tar.gz"
  1496. export NETDATA_SOURCE_ARCHIVE_CHECKSUM_URL="${NETDATA_TARBALL_BASEURL}/download/v${INSTALL_VERSION}/sha256sums.txt"
  1497. else
  1498. tag="$(get_redirect "${NETDATA_TARBALL_BASEURL}/latest")"
  1499. export NETDATA_SOURCE_ARCHIVE_URL="${NETDATA_TARBALL_BASEURL}/download/${tag}/netdata-latest.tar.gz"
  1500. export NETDATA_SOURCE_ARCHIVE_CHECKSUM_URL="${NETDATA_TARBALL_BASEURL}/download/${tag}/sha256sums.txt"
  1501. fi
  1502. fi
  1503. }
  1504. install_local_build_dependencies() {
  1505. set_tmpdir
  1506. bash="$(command -v bash 2> /dev/null)"
  1507. if [ -z "${bash}" ] || [ ! -x "${bash}" ]; then
  1508. warning "Unable to find a usable version of \`bash\` (required for local build)."
  1509. return 1
  1510. fi
  1511. if ! download "${PACKAGES_SCRIPT}" "${tmpdir}/install-required-packages.sh"; then
  1512. fatal "Failed to download dependency handling script for local build. ${BADNET_MSG}." F000D
  1513. fi
  1514. if [ "${DRY_RUN}" -eq 1 ]; then
  1515. progress "Would run downloaded script to install required build dependencies..."
  1516. else
  1517. progress "Running downloaded script to install required build dependencies..."
  1518. fi
  1519. if [ "${INTERACTIVE}" -eq 0 ]; then
  1520. opts="--dont-wait --non-interactive"
  1521. fi
  1522. # shellcheck disable=SC2086
  1523. if ! run_as_root "${bash}" "${tmpdir}/install-required-packages.sh" ${opts} netdata; then
  1524. warning "Failed to install all required packages, but installation might still be possible."
  1525. fi
  1526. }
  1527. build_and_install() {
  1528. if [ "${DRY_RUN}" -eq 1 ]; then
  1529. progress "Would attempt to build netdata..."
  1530. else
  1531. progress "Building netdata..."
  1532. fi
  1533. echo "INSTALL_TYPE='kickstart-build'" > system/.install-type
  1534. opts="${NETDATA_INSTALLER_OPTIONS}"
  1535. if [ "${INTERACTIVE}" -eq 0 ]; then
  1536. opts="${opts} --dont-wait"
  1537. fi
  1538. if [ "${SELECTED_RELEASE_CHANNEL}" = "stable" ]; then
  1539. opts="${opts} --stable-channel"
  1540. fi
  1541. if [ "${NETDATA_REQUIRE_CLOUD}" -eq 1 ]; then
  1542. opts="${opts} --require-cloud"
  1543. elif [ "${NETDATA_DISABLE_CLOUD}" -eq 1 ]; then
  1544. opts="${opts} --disable-cloud"
  1545. fi
  1546. # shellcheck disable=SC2086
  1547. run_script ./netdata-installer.sh ${opts}
  1548. case $? in
  1549. 1)
  1550. if [ -n "${EXIT_REASON}" ]; then
  1551. fatal "netdata-installer.sh failed to run: ${EXIT_REASON}" "${EXIT_CODE}"
  1552. else
  1553. fatal "netdata-installer.sh failed to run: Encountered an unhandled error in the installer code." I0000
  1554. fi
  1555. ;;
  1556. 2) fatal "Insufficient RAM to install netdata." F0008 ;;
  1557. esac
  1558. }
  1559. try_build_install() {
  1560. set_tmpdir
  1561. if [ "${DRY_RUN}" -eq 1 ]; then
  1562. progress "Would attempt to install by building locally..."
  1563. else
  1564. progress "Attempting to install by building locally..."
  1565. fi
  1566. if ! install_local_build_dependencies; then
  1567. return 1
  1568. fi
  1569. set_source_archive_urls "${SELECTED_RELEASE_CHANNEL}"
  1570. if [ -n "${INSTALL_VERSION}" ]; then
  1571. if ! download "${NETDATA_SOURCE_ARCHIVE_URL}" "${tmpdir}/netdata-v${INSTALL_VERSION}.tar.gz"; then
  1572. fatal "Failed to download source tarball for local build. ${BADNET_MSG}." F000B
  1573. fi
  1574. elif ! download "${NETDATA_SOURCE_ARCHIVE_URL}" "${tmpdir}/netdata-latest.tar.gz"; then
  1575. fatal "Failed to download source tarball for local build. ${BADNET_MSG}." F000B
  1576. fi
  1577. if ! download "${NETDATA_SOURCE_ARCHIVE_CHECKSUM_URL}" "${tmpdir}/sha256sum.txt"; then
  1578. fatal "Failed to download checksums for source tarball verification. ${BADNET_MSG}." F000C
  1579. fi
  1580. if [ "${DRY_RUN}" -eq 1 ]; then
  1581. progress "Would validate SHA256 checksum of downloaded source archive."
  1582. else
  1583. if [ -z "${INSTALL_VERSION}" ]; then
  1584. # shellcheck disable=SC2086
  1585. if ! grep netdata-latest.tar.gz "${tmpdir}/sha256sum.txt" | safe_sha256sum -c - > /dev/null 2>&1; then
  1586. fatal "Tarball checksum validation failed. ${BADCACHE_MSG}." F0005
  1587. fi
  1588. fi
  1589. fi
  1590. if [ -n "${INSTALL_VERSION}" ]; then
  1591. run tar -xf "${tmpdir}/netdata-v${INSTALL_VERSION}.tar.gz" -C "${tmpdir}"
  1592. rm -rf "${tmpdir}/netdata-v${INSTALL_VERSION}.tar.gz" > /dev/null 2>&1
  1593. else
  1594. run tar -xf "${tmpdir}/netdata-latest.tar.gz" -C "${tmpdir}"
  1595. rm -rf "${tmpdir}/netdata-latest.tar.gz" > /dev/null 2>&1
  1596. fi
  1597. if [ "${DRY_RUN}" -ne 1 ]; then
  1598. cd "$(find "${tmpdir}" -mindepth 1 -maxdepth 1 -type d -name netdata-)" || fatal "Cannot change directory to netdata source tree" F0006
  1599. fi
  1600. if [ -x netdata-installer.sh ] || [ "${DRY_RUN}" -eq 1 ]; then
  1601. build_and_install || return 1
  1602. else
  1603. # This case is needed because some platforms produce an extra directory on the source tarball extraction.
  1604. if [ "$(find . -mindepth 1 -maxdepth 1 -type d | wc -l)" -eq 1 ] && [ -x "$(find . -mindepth 1 -maxdepth 1 -type d)/netdata-installer.sh" ]; then
  1605. cd "$(find . -mindepth 1 -maxdepth 1 -type d)" && build_and_install || return 1
  1606. else
  1607. fatal "Cannot install netdata from source (the source directory does not include netdata-installer.sh)." F0009
  1608. fi
  1609. fi
  1610. }
  1611. # ======================================================================
  1612. # Offline install support code
  1613. prepare_offline_install_source() {
  1614. if [ -e "${1}" ]; then
  1615. if [ ! -d "${1}" ]; then
  1616. fatal "${1} is not a directory, unable to prepare offline install source." F0503
  1617. fi
  1618. else
  1619. run mkdir -p "${1}" || fatal "Unable to create target directory for offline install preparation." F0504
  1620. fi
  1621. run cd "${1}" || fatal "Failed to switch to target directory for offline install preparation." F0505
  1622. case "${NETDATA_FORCE_METHOD}" in
  1623. static|'')
  1624. set_static_archive_urls "${SELECTED_RELEASE_CHANNEL}" "x86_64"
  1625. if check_for_remote_file "${NETDATA_STATIC_ARCHIVE_URL}"; then
  1626. for arch in ${STATIC_INSTALL_ARCHES}; do
  1627. set_static_archive_urls "${SELECTED_RELEASE_CHANNEL}" "${arch}"
  1628. progress "Fetching ${NETDATA_STATIC_ARCHIVE_URL}"
  1629. if ! download "${NETDATA_STATIC_ARCHIVE_URL}" "netdata-${arch}-latest.gz.run"; then
  1630. warning "Failed to download static installer archive for ${arch}. ${BADNET_MSG}."
  1631. fi
  1632. done
  1633. legacy=0
  1634. else
  1635. warning "Selected version of Netdata only provides static builds for x86_64. You will only be able to install on x86_64 systems with this offline install source."
  1636. progress "Fetching ${NETDATA_STATIC_ARCHIVE_OLD_URL}"
  1637. legacy=1
  1638. if ! download "${NETDATA_STATIC_ARCHIVE_OLD_URL}" "netdata-x86_64-latest.gz.run"; then
  1639. warning "Failed to download static installer archive for x86_64. ${BADNET_MSG}."
  1640. fi
  1641. fi
  1642. progress "Fetching ${NETDATA_STATIC_ARCHIVE_CHECKSUM_URL}"
  1643. if ! download "${NETDATA_STATIC_ARCHIVE_CHECKSUM_URL}" "sha256sums.txt"; then
  1644. fatal "Failed to download checksum file. ${BADNET_MSG}." F0506
  1645. fi
  1646. ;;
  1647. esac
  1648. if [ "${legacy:-0}" -eq 1 ]; then
  1649. sed -e 's/netdata-latest.gz.run/netdata-x86_64-latest.gz.run' sha256sums.txt > sha256sums.tmp
  1650. mv sha256sums.tmp sha256sums.txt
  1651. fi
  1652. if [ "${DRY_RUN}" -ne 1 ]; then
  1653. progress "Verifying checksums."
  1654. if ! grep -e "$(find . -name '*.gz.run')" sha256sums.txt | safe_sha256sum -c -; then
  1655. fatal "Checksums for offline install files are incorrect. ${BADCACHE_MSG}." F0507
  1656. fi
  1657. else
  1658. progress "Would verify SHA256 checksums of downloaded installation files."
  1659. fi
  1660. if [ "${DRY_RUN}" -ne 1 ]; then
  1661. progress "Preparing install script."
  1662. cat > "install.sh" <<-EOF
  1663. #!/bin/sh
  1664. dir=\$(CDPATH= cd -- "\$(dirname -- "\$0")" && pwd)
  1665. "\${dir}/kickstart.sh" --offline-install-source "\${dir}" \${@}
  1666. EOF
  1667. chmod +x "install.sh"
  1668. else
  1669. progress "Would create install script"
  1670. fi
  1671. if [ "${DRY_RUN}" -ne 1 ]; then
  1672. progress "Copying kickstart script."
  1673. cp "${KICKSTART_SOURCE}" "kickstart.sh"
  1674. chmod +x "kickstart.sh"
  1675. else
  1676. progress "Would copy kickstart.sh to offline install source directory"
  1677. fi
  1678. if [ "${DRY_RUN}" -ne 1 ]; then
  1679. progress "Saving release channel information."
  1680. echo "${SELECTED_RELEASE_CHANNEL}" > "channel"
  1681. else
  1682. progress "Would save release channel information to offline install source directory"
  1683. fi
  1684. progress "Finished preparing offline install source directory at ${1}. You can now copy this directory to a target system and then run the script ‘install.sh’ from it to install on that system."
  1685. }
  1686. # ======================================================================
  1687. # Per system-type install logic
  1688. install_on_linux() {
  1689. if [ "${NETDATA_FORCE_METHOD}" != 'static' ] && [ "${NETDATA_FORCE_METHOD}" != 'build' ] && [ -z "${NETDATA_OFFLINE_INSTALL_SOURCE}" ]; then
  1690. SELECTED_INSTALL_METHOD="native"
  1691. try_package_install
  1692. case "$?" in
  1693. 0)
  1694. NETDATA_INSTALL_SUCCESSFUL=1
  1695. INSTALL_PREFIX="/"
  1696. ;;
  1697. 1) fatal "Unable to install on this system." F0300 ;;
  1698. 2)
  1699. case "${NETDATA_FORCE_METHOD}" in
  1700. native) fatal "Could not install native binary packages." F0301 ;;
  1701. *) warning "Could not install native binary packages, falling back to alternative installation method." ;;
  1702. esac
  1703. ;;
  1704. esac
  1705. fi
  1706. if [ "${NETDATA_FORCE_METHOD}" != 'native' ] && [ "${NETDATA_FORCE_METHOD}" != 'build' ] && [ -z "${NETDATA_INSTALL_SUCCESSFUL}" ]; then
  1707. SELECTED_INSTALL_METHOD="static"
  1708. INSTALL_TYPE="kickstart-static"
  1709. try_static_install
  1710. case "$?" in
  1711. 0)
  1712. NETDATA_INSTALL_SUCCESSFUL=1
  1713. INSTALL_PREFIX="/opt/netdata"
  1714. ;;
  1715. 1) fatal "Unable to install on this system." F0302 ;;
  1716. 2)
  1717. case "${NETDATA_FORCE_METHOD}" in
  1718. static) fatal "Could not install static build." F0303 ;;
  1719. *) warning "Could not install static build, falling back to alternative installation method." ;;
  1720. esac
  1721. ;;
  1722. esac
  1723. fi
  1724. if [ "${NETDATA_FORCE_METHOD}" != 'native' ] && [ "${NETDATA_FORCE_METHOD}" != 'static' ] && [ -z "${NETDATA_INSTALL_SUCCESSFUL}" ]; then
  1725. SELECTED_INSTALL_METHOD="build"
  1726. INSTALL_TYPE="kickstart-build"
  1727. try_build_install
  1728. case "$?" in
  1729. 0) NETDATA_INSTALL_SUCCESSFUL=1 ;;
  1730. *) fatal "Unable to install on this system." F0304 ;;
  1731. esac
  1732. fi
  1733. }
  1734. install_on_macos() {
  1735. case "${NETDATA_FORCE_METHOD}" in
  1736. native) fatal "User requested native package, but native packages are not available for macOS. Try installing without \`--only-native\` option." F0305 ;;
  1737. static) fatal "User requested static build, but static builds are not available for macOS. Try installing without \`--only-static\` option." F0306 ;;
  1738. *)
  1739. SELECTED_INSTALL_METHOD="build"
  1740. INSTALL_TYPE="kickstart-build"
  1741. try_build_install
  1742. case "$?" in
  1743. 0) NETDATA_INSTALL_SUCCESSFUL=1 ;;
  1744. *) fatal "Unable to install on this system." F0307 ;;
  1745. esac
  1746. ;;
  1747. esac
  1748. }
  1749. install_on_freebsd() {
  1750. case "${NETDATA_FORCE_METHOD}" in
  1751. native) fatal "User requested native package, but native packages are not available for FreeBSD. Try installing without \`--only-native\` option." F0308 ;;
  1752. static) fatal "User requested static build, but static builds are not available for FreeBSD. Try installing without \`--only-static\` option." F0309 ;;
  1753. *)
  1754. SELECTED_INSTALL_METHOD="build"
  1755. INSTALL_TYPE="kickstart-build"
  1756. try_build_install
  1757. case "$?" in
  1758. 0) NETDATA_INSTALL_SUCCESSFUL=1 ;;
  1759. *) fatal "Unable to install on this system." F030A ;;
  1760. esac
  1761. ;;
  1762. esac
  1763. }
  1764. # ======================================================================
  1765. # Argument parsing code
  1766. validate_args() {
  1767. check_claim_opts
  1768. if [ -n "${NETDATA_FORCE_METHOD}" ]; then
  1769. SELECTED_INSTALL_METHOD="${NETDATA_FORCE_METHOD}"
  1770. fi
  1771. if [ "${ACTION}" = "repositories-only" ] && [ "${NETDATA_FORCE_METHOD}" != "native" ]; then
  1772. fatal "Repositories can only be installed for native installs." F050D
  1773. fi
  1774. if [ -n "${NETDATA_OFFLINE_INSTALL_SOURCE}" ]; then
  1775. case "${NETDATA_FORCE_METHOD}" in
  1776. native|build) fatal "Offline installs are only supported for static builds currently." F0502 ;;
  1777. esac
  1778. fi
  1779. if [ -n "${LOCAL_BUILD_OPTIONS}" ]; then
  1780. case "${NETDATA_FORCE_METHOD}" in
  1781. build) NETDATA_INSTALLER_OPTIONS="${NETDATA_INSTALLER_OPTIONS} ${LOCAL_BUILD_OPTIONS}" ;;
  1782. *) fatal "Specifying local build options is only supported when the --build-only option is also specified." F0401 ;;
  1783. esac
  1784. fi
  1785. if [ -n "${STATIC_INSTALL_OPTIONS}" ]; then
  1786. case "${NETDATA_FORCE_METHOD}" in
  1787. static) NETDATA_INSTALLER_OPTIONS="${NETDATA_INSTALLER_OPTIONS} ${STATIC_INSTALL_OPTIONS}" ;;
  1788. *) fatal "Specifying installer options options is only supported when the --static-only option is also specified." F0402 ;;
  1789. esac
  1790. fi
  1791. if [ -n "${NETDATA_OFFLINE_INSTALL_SOURCE}" ] && [ -n "${INSTALL_VERSION}" ]; then
  1792. fatal "Specifying an install version alongside an offline install source is not supported." F050A
  1793. fi
  1794. if [ "${NETDATA_AUTO_UPDATES}" = "default" ]; then
  1795. if [ -n "${NETDATA_OFFLINE_INSTALL_SOURCE}" ] || [ -n "${INSTALL_VERSION}" ]; then
  1796. AUTO_UPDATE=0
  1797. else
  1798. AUTO_UPDATE=1
  1799. fi
  1800. elif [ "${NETDATA_AUTO_UPDATES}" = 1 ]; then
  1801. AUTO_UPDATE=1
  1802. else
  1803. AUTO_UPDATE=0
  1804. fi
  1805. if [ "${RELEASE_CHANNEL}" = "default" ]; then
  1806. if [ -n "${NETDATA_OFFLINE_INSTALL_SOURCE}" ]; then
  1807. SELECTED_RELEASE_CHANNEL="$(cat "${NETDATA_OFFLINE_INSTALL_SOURCE}/channel")"
  1808. if [ -z "${SELECTED_RELEASE_CHANNEL}" ]; then
  1809. fatal "Could not find a release channel indicator in ${NETDATA_OFFLINE_INSTALL_SOURCE}." F0508
  1810. fi
  1811. else
  1812. SELECTED_RELEASE_CHANNEL="${DEFAULT_RELEASE_CHANNEL}"
  1813. fi
  1814. else
  1815. if [ -n "${NETDATA_OFFLINE_INSTALL_SOURCE}" ] && [ "${RELEASE_CHANNEL}" != "$(cat "${NETDATA_OFFLINE_INSTALL_SOURCE}/channel")" ]; then
  1816. fatal "Release channal '${RELEASE_CHANNEL}' requested, but indicated offline installation source release channel is '$(cat "${NETDATA_OFFLINE_INSTALL_SOURCE}/channel")'." F0509
  1817. fi
  1818. SELECTED_RELEASE_CHANNEL="${RELEASE_CHANNEL}"
  1819. fi
  1820. }
  1821. set_action() {
  1822. new_action="${1}"
  1823. if [ -n "${ACTION}" ]; then
  1824. warning "Ignoring previously specified '${ACTION}' operation in favor of '${new_action}' specified later on the command line."
  1825. fi
  1826. ACTION="${new_action}"
  1827. NETDATA_COMMAND="${new_action}"
  1828. }
  1829. parse_args() {
  1830. while [ -n "${1}" ]; do
  1831. case "${1}" in
  1832. "--help")
  1833. usage
  1834. cleanup
  1835. trap - EXIT
  1836. exit 0
  1837. ;;
  1838. "--no-cleanup") NO_CLEANUP=1 ;;
  1839. "--dont-wait"|"--non-interactive") INTERACTIVE=0 ;;
  1840. "--interactive") INTERACTIVE=1 ;;
  1841. "--dry-run") DRY_RUN=1 ;;
  1842. "--release-channel")
  1843. RELEASE_CHANNEL="$(echo "${2}" | tr '[:upper:]' '[:lower:]')"
  1844. case "${RELEASE_CHANNEL}" in
  1845. nightly|stable|default) shift 1 ;;
  1846. *)
  1847. echo "Unrecognized value for --release-channel. Valid release channels are: stable, nightly, default"
  1848. exit 1
  1849. ;;
  1850. esac
  1851. ;;
  1852. "--stable-channel") RELEASE_CHANNEL="stable" ;;
  1853. "--nightly-channel") RELEASE_CHANNEL="nightly" ;;
  1854. "--reinstall") set_action 'reinstall' ;;
  1855. "--reinstall-even-if-unsafe") set_action 'unsafe-reinstall' ;;
  1856. "--reinstall-clean") set_action 'reinstall-clean' ;;
  1857. "--uninstall") set_action 'uninstall' ;;
  1858. "--claim-only") set_action 'claim' ;;
  1859. "--no-updates") NETDATA_AUTO_UPDATES=0 ;;
  1860. "--auto-update") NETDATA_AUTO_UPDATES="1" ;;
  1861. "--auto-update-method")
  1862. NETDATA_AUTO_UPDATE_TYPE="$(echo "${2}" | tr '[:upper:]' '[:lower:]')"
  1863. case "${NETDATA_AUTO_UPDATE_TYPE}" in
  1864. systemd|interval|crontab) shift 1 ;;
  1865. *)
  1866. echo "Unrecognized value for --auto-update-type. Valid values are: systemd, interval, crontab"
  1867. exit 1
  1868. ;;
  1869. esac
  1870. ;;
  1871. "--disable-cloud")
  1872. NETDATA_DISABLE_CLOUD=1
  1873. NETDATA_REQUIRE_CLOUD=0
  1874. ;;
  1875. "--require-cloud")
  1876. NETDATA_DISABLE_CLOUD=0
  1877. NETDATA_REQUIRE_CLOUD=1
  1878. ;;
  1879. "--dont-start-it")
  1880. NETDATA_NO_START=1
  1881. NETDATA_INSTALLER_OPTIONS="${NETDATA_INSTALLER_OPTIONS} --dont-start-it"
  1882. ;;
  1883. "--disable-telemetry")
  1884. NETDATA_DISABLE_TELEMETRY="1"
  1885. NETDATA_INSTALLER_OPTIONS="${NETDATA_INSTALLER_OPTIONS} --disable-telemetry"
  1886. ;;
  1887. "--install-prefix")
  1888. INSTALL_PREFIX="${2}"
  1889. shift 1
  1890. ;;
  1891. "--old-install-prefix")
  1892. OLD_INSTALL_PREFIX="${2}"
  1893. shift 1
  1894. ;;
  1895. "--install-version")
  1896. INSTALL_VERSION="${2}"
  1897. AUTO_UPDATE=0
  1898. shift 1
  1899. ;;
  1900. "--distro-override")
  1901. if [ -n "${2}" ]; then
  1902. SKIP_DISTRO_DETECTION=1
  1903. DISTRO="$(echo "${2}" | cut -f 1 -d ':' | tr '[:upper:]' '[:lower:]')"
  1904. SYSVERSION="$(echo "${2}" | cut -f 2 -d ':')"
  1905. SYSCODENAME="$(echo "${2}" | cut -f 3 -d ':' | tr '[:upper:]' '[:lower:]')"
  1906. if [ -z "${SYSVERSION}" ]; then
  1907. fatal "You must specify a release as well as a distribution name." F0510
  1908. fi
  1909. shift 1
  1910. else
  1911. fatal "A distribution name and release must be specified for the --distro-override option." F050F
  1912. fi
  1913. ;;
  1914. "--repositories-only")
  1915. set_action 'repositories-only'
  1916. NETDATA_FORCE_METHOD="native"
  1917. ;;
  1918. "--native-only") NETDATA_FORCE_METHOD="native" ;;
  1919. "--static-only") NETDATA_FORCE_METHOD="static" ;;
  1920. "--build-only") NETDATA_FORCE_METHOD="build" ;;
  1921. "--claim-token")
  1922. NETDATA_CLAIM_TOKEN="${2}"
  1923. shift 1
  1924. ;;
  1925. "--claim-rooms")
  1926. NETDATA_CLAIM_ROOMS="${2}"
  1927. shift 1
  1928. ;;
  1929. "--claim-url")
  1930. NETDATA_CLAIM_URL="${2}"
  1931. shift 1
  1932. ;;
  1933. "--claim-"*)
  1934. optname="$(echo "${1}" | cut -d '-' -f 4-)"
  1935. case "${optname}" in
  1936. id|proxy|user|hostname)
  1937. NETDATA_CLAIM_EXTRA="${NETDATA_CLAIM_EXTRA} -${optname}=${2}"
  1938. shift 1
  1939. ;;
  1940. verbose|insecure|noproxy|noreload|daemon-not-running) NETDATA_CLAIM_EXTRA="${NETDATA_CLAIM_EXTRA} -${optname}" ;;
  1941. *) warning "Ignoring unrecognized claiming option ${optname}" ;;
  1942. esac
  1943. ;;
  1944. "--local-build-options")
  1945. LOCAL_BUILD_OPTIONS="${LOCAL_BUILD_OPTIONS} ${2}"
  1946. shift 1
  1947. ;;
  1948. "--static-install-options")
  1949. STATIC_INSTALL_OPTIONS="${STATIC_INSTALL_OPTIONS} ${2}"
  1950. shift 1
  1951. ;;
  1952. "--prepare-offline-install-source")
  1953. if [ -n "${2}" ]; then
  1954. set_action 'prepare-offline'
  1955. OFFLINE_TARGET="${2}"
  1956. shift 1
  1957. else
  1958. fatal "A target directory must be specified with the --prepare-offline-install-source option." F0500
  1959. fi
  1960. ;;
  1961. "--offline-install-source")
  1962. if [ -d "${2}" ]; then
  1963. NETDATA_OFFLINE_INSTALL_SOURCE="${2}"
  1964. shift 1
  1965. else
  1966. fatal "A source directory must be specified with the --offline-install-source option." F0501
  1967. fi
  1968. ;;
  1969. "--"|"all"|"--yes"|"-y"|"--force"|"--accept") warning "Option '${1}' is not recognized, ignoring it. ${BADOPT_MSG}" ;;
  1970. *) fatal "Unrecognized option '${1}'. ${BADOPT_MSG}" F050E ;;
  1971. esac
  1972. shift 1
  1973. done
  1974. validate_args
  1975. }
  1976. # ======================================================================
  1977. # Main program
  1978. setup_terminal || echo > /dev/null
  1979. # shellcheck disable=SC2068
  1980. parse_args $@
  1981. confirm_root_support
  1982. get_system_info
  1983. confirm_install_prefix
  1984. if [ -z "${ACTION}" ]; then
  1985. handle_existing_install
  1986. fi
  1987. main