kickstart.sh 83 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273
  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. run_as_root rm -rf "${tmpdir}"
  355. fi
  356. }
  357. deferred_warnings() {
  358. if [ -n "${NETDATA_WARNINGS}" ]; then
  359. printf >&2 "%s\n" "The following non-fatal warnings or errors were encountered:"
  360. # shellcheck disable=SC2059
  361. printf >&2 "${NETDATA_WARNINGS}"
  362. printf >&2 "\n\n"
  363. fi
  364. }
  365. fatal() {
  366. deferred_warnings
  367. printf >&2 "%s\n\n" "${TPUT_BGRED}${TPUT_WHITE}${TPUT_BOLD} ABORTED ${TPUT_RESET} ${1}"
  368. printf >&2 "%s\n" "For community support, you can connect with us on:"
  369. support_list
  370. telemetry_event "INSTALL_FAILED" "${1}" "${2}"
  371. cleanup
  372. trap - EXIT
  373. exit 1
  374. }
  375. ESCAPED_PRINT_METHOD=
  376. # shellcheck disable=SC3050
  377. if printf "%s " test > /dev/null 2>&1; then
  378. ESCAPED_PRINT_METHOD="printfq"
  379. fi
  380. escaped_print() {
  381. if [ "${ESCAPED_PRINT_METHOD}" = "printfq" ]; then
  382. # shellcheck disable=SC3050
  383. printf "%s " "${@}"
  384. else
  385. printf "%s" "${*}"
  386. fi
  387. return 0
  388. }
  389. progress() {
  390. echo >&2 " --- ${TPUT_BOLD}${*}${TPUT_RESET} --- "
  391. }
  392. run_logfile="/dev/null"
  393. run() {
  394. user="${USER--}"
  395. dir="${PWD}"
  396. if [ "$(id -u)" = "0" ]; then
  397. info="[root ${dir}]# "
  398. info_console="[${TPUT_DIM}${dir}${TPUT_RESET}]# "
  399. else
  400. info="[${user} ${dir}]$ "
  401. info_console="[${TPUT_DIM}${dir}${TPUT_RESET}]$ "
  402. fi
  403. if [ "${DRY_RUN}" -eq 1 ]; then
  404. printf >&2 "%s" "Would run command:\n"
  405. fi
  406. {
  407. printf "%s" "${info}"
  408. escaped_print "${@}"
  409. printf " ... "
  410. } >> "${run_logfile}"
  411. printf >&2 "%s" "${info_console}${TPUT_BOLD}"
  412. escaped_print >&2 "${@}"
  413. printf >&2 "%s\n" "${TPUT_RESET}"
  414. if [ "${DRY_RUN}" -ne 1 ]; then
  415. "${@}"
  416. ret=$?
  417. else
  418. ret=0
  419. fi
  420. if [ ${ret} -ne 0 ]; then
  421. printf >&2 "%s\n\n" "${TPUT_BGRED}${TPUT_WHITE}${TPUT_BOLD} FAILED ${TPUT_RESET}"
  422. printf "%s\n" "FAILED with exit code ${ret}" >> "${run_logfile}"
  423. # shellcheck disable=SC2089
  424. NETDATA_WARNINGS="${NETDATA_WARNINGS}\n - Command \"${*}\" failed with exit code ${ret}."
  425. else
  426. printf >&2 "%s\n\n" "${TPUT_BGGREEN}${TPUT_WHITE}${TPUT_BOLD} OK ${TPUT_RESET}"
  427. printf "OK\n" >> "${run_logfile}"
  428. fi
  429. return ${ret}
  430. }
  431. run_as_root() {
  432. confirm_root_support
  433. if [ "$(id -u)" -ne "0" ]; then
  434. printf >&2 "Root privileges required to run %s\n" "${*}"
  435. fi
  436. run ${ROOTCMD} "${@}"
  437. }
  438. run_script() {
  439. set_tmpdir
  440. export NETDATA_SCRIPT_STATUS_PATH="${tmpdir}/.script-status"
  441. export NETDATA_SAVE_WARNINGS=1
  442. export NETDATA_PROPAGATE_WARNINGS=1
  443. # shellcheck disable=SC2090
  444. export NETDATA_WARNINGS="${NETDATA_WARNINGS}"
  445. # shellcheck disable=SC2086
  446. run ${ROOTCMD} "${@}"
  447. if [ -r "${NETDATA_SCRIPT_STATUS_PATH}" ]; then
  448. # shellcheck disable=SC1090
  449. . "${NETDATA_SCRIPT_STATUS_PATH}"
  450. rm -f "${NETDATA_SCRIPT_STATUS_PATH}"
  451. fi
  452. }
  453. warning() {
  454. printf >&2 "%s\n\n" "${TPUT_BGRED}${TPUT_WHITE}${TPUT_BOLD} WARNING ${TPUT_RESET} ${*}"
  455. NETDATA_WARNINGS="${NETDATA_WARNINGS}\n - ${*}"
  456. }
  457. _cannot_use_tmpdir() {
  458. testfile="$(TMPDIR="${1}" mktemp -q -t netdata-test.XXXXXXXXXX)"
  459. ret=0
  460. if [ -z "${testfile}" ]; then
  461. return "${ret}"
  462. fi
  463. if printf '#!/bin/sh\necho SUCCESS\n' > "${testfile}"; then
  464. if chmod +x "${testfile}"; then
  465. if [ "$("${testfile}")" = "SUCCESS" ]; then
  466. ret=1
  467. fi
  468. fi
  469. fi
  470. rm -f "${testfile}"
  471. return "${ret}"
  472. }
  473. create_tmp_directory() {
  474. if [ -z "${TMPDIR}" ] || _cannot_use_tmpdir "${TMPDIR}"; then
  475. if _cannot_use_tmpdir /tmp; then
  476. if _cannot_use_tmpdir "${PWD}"; then
  477. 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
  478. else
  479. TMPDIR="${PWD}"
  480. fi
  481. else
  482. TMPDIR="/tmp"
  483. fi
  484. fi
  485. mktemp -d -t netdata-kickstart-XXXXXXXXXX
  486. }
  487. set_tmpdir() {
  488. if [ -z "${tmpdir}" ] || [ ! -d "${tmpdir}" ]; then
  489. tmpdir="$(create_tmp_directory)"
  490. progress "Using ${tmpdir} as a temporary directory."
  491. cd "${tmpdir}" || fatal "Failed to change current working directory to ${tmpdir}." F000A
  492. fi
  493. }
  494. check_for_remote_file() {
  495. url="${1}"
  496. if echo "${url}" | grep -Eq "^file:///"; then
  497. [ -e "${url#file://}" ] || return 1
  498. elif [ -n "${CURL}" ]; then
  499. "${CURL}" --output /dev/null --silent --head --fail "${url}" || return 1
  500. elif command -v wget > /dev/null 2>&1; then
  501. wget -S --spider "${url}" 2>&1 | grep -q 'HTTP/1.1 200 OK' || return 1
  502. else
  503. fatal "${ERROR_F0003}" F0003
  504. fi
  505. }
  506. download() {
  507. url="${1}"
  508. dest="${2}"
  509. if echo "${url}" | grep -Eq "^file:///"; then
  510. run cp "${url#file://}" "${dest}" || return 1
  511. elif [ -n "${CURL}" ]; then
  512. run "${CURL}" --fail -q -sSL --connect-timeout 10 --retry 3 --output "${dest}" "${url}" || return 1
  513. elif command -v wget > /dev/null 2>&1; then
  514. run wget -T 15 -O "${dest}" "${url}" || return 1
  515. else
  516. fatal "${ERROR_F0003}" F0003
  517. fi
  518. }
  519. get_redirect() {
  520. url="${1}"
  521. if [ -n "${CURL}" ]; then
  522. run sh -c "${CURL} ${url} -s -L -I -o /dev/null -w '%{url_effective}' | grep -o '[^/]*$'" || return 1
  523. elif command -v wget > /dev/null 2>&1; then
  524. run sh -c "wget -S -O /dev/null ${url} 2>&1 | grep -m 1 Location | grep -o '[^/]*$'" || return 1
  525. else
  526. fatal "${ERROR_F0003}" F0003
  527. fi
  528. }
  529. safe_sha256sum() {
  530. # Within the context of the installer, we only use -c option that is common between the two commands
  531. # We will have to reconsider if we start using non-common options
  532. if command -v shasum > /dev/null 2>&1; then
  533. shasum -a 256 "$@"
  534. elif command -v sha256sum > /dev/null 2>&1; then
  535. sha256sum "$@"
  536. else
  537. 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
  538. fi
  539. }
  540. get_system_info() {
  541. SYSARCH="$(uname -m)"
  542. case "$(uname -s)" in
  543. Linux)
  544. SYSTYPE="Linux"
  545. if [ -z "${SKIP_DISTRO_DETECTION}" ]; then
  546. os_release_file=
  547. if [ -s "/etc/os-release" ] && [ -r "/etc/os-release" ]; then
  548. os_release_file="/etc/os-release"
  549. elif [ -s "/usr/lib/os-release" ] && [ -r "/usr/lib/os-release" ]; then
  550. os_release_file="/usr/lib/os-release"
  551. else
  552. warning "Cannot find usable OS release information. Native packages will not be available for this install."
  553. fi
  554. if [ -n "${os_release_file}" ]; then
  555. # shellcheck disable=SC1090
  556. . "${os_release_file}"
  557. DISTRO="${ID}"
  558. SYSVERSION="${VERSION_ID}"
  559. SYSCODENAME="${VERSION_CODENAME}"
  560. else
  561. DISTRO="unknown"
  562. DISTRO_COMPAT_NAME="unknown"
  563. SYSVERSION="unknown"
  564. SYSCODENAME="unknown"
  565. fi
  566. else
  567. warning "Distribution auto-detection overridden by user. This is not guaranteed to work, and is not officially supported."
  568. fi
  569. supported_compat_names="debian ubuntu centos fedora opensuse ol amzn arch"
  570. if str_in_list "${DISTRO}" "${supported_compat_names}"; then
  571. DISTRO_COMPAT_NAME="${DISTRO}"
  572. else
  573. case "${DISTRO}" in
  574. opensuse-leap)
  575. DISTRO_COMPAT_NAME="opensuse"
  576. ;;
  577. opensuse-tumbleweed)
  578. DISTRO_COMPAT_NAME="opensuse"
  579. SYSVERSION="tumbleweed"
  580. ;;
  581. cloudlinux|almalinux|rocky|rhel)
  582. DISTRO_COMPAT_NAME="centos"
  583. ;;
  584. artix|manjaro|obarun)
  585. DISTRO_COMPAT_NAME="arch"
  586. ;;
  587. *)
  588. DISTRO_COMPAT_NAME="unknown"
  589. ;;
  590. esac
  591. fi
  592. case "${DISTRO_COMPAT_NAME}" in
  593. centos|ol) SYSVERSION=$(echo "$SYSVERSION" | cut -d'.' -f1) ;;
  594. esac
  595. ;;
  596. Darwin)
  597. SYSTYPE="Darwin"
  598. SYSVERSION="$(sw_vers -buildVersion)"
  599. ;;
  600. FreeBSD)
  601. SYSTYPE="FreeBSD"
  602. SYSVERSION="$(uname -K)"
  603. ;;
  604. *) fatal "Unsupported system type detected. Netdata cannot be installed on this system using this script." F0200 ;;
  605. esac
  606. }
  607. str_in_list() {
  608. printf "%s\n" "${2}" | tr ' ' "\n" | grep -qE "^${1}\$"
  609. return $?
  610. }
  611. confirm_root_support() {
  612. if [ "$(id -u)" -ne "0" ]; then
  613. if [ -z "${ROOTCMD}" ] && command -v sudo > /dev/null; then
  614. if [ "${INTERACTIVE}" -eq 0 ]; then
  615. ROOTCMD="sudo -n"
  616. else
  617. ROOTCMD="sudo"
  618. fi
  619. fi
  620. if [ -z "${ROOTCMD}" ] && command -v doas > /dev/null; then
  621. if [ "${INTERACTIVE}" -eq 0 ]; then
  622. ROOTCMD="doas -n"
  623. else
  624. ROOTCMD="doas"
  625. fi
  626. fi
  627. if [ -z "${ROOTCMD}" ] && command -v pkexec > /dev/null; then
  628. ROOTCMD="pkexec"
  629. fi
  630. if [ -z "${ROOTCMD}" ]; then
  631. 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
  632. fi
  633. fi
  634. }
  635. confirm() {
  636. prompt="${1} [y/n]"
  637. while true; do
  638. echo "${prompt}"
  639. read -r yn
  640. case "$yn" in
  641. [Yy]*) return 0;;
  642. [Nn]*) return 1;;
  643. *) echo "Please answer yes or no.";;
  644. esac
  645. done
  646. }
  647. # ======================================================================
  648. # Existing install handling code
  649. update() {
  650. updater="${ndprefix}/usr/libexec/netdata/netdata-updater.sh"
  651. if run_as_root test -x "${updater}"; then
  652. if [ "${DRY_RUN}" -eq 1 ]; then
  653. progress "Would attempt to update existing installation by running the updater script located at: ${updater}"
  654. return 0
  655. fi
  656. if [ "${INTERACTIVE}" -eq 0 ]; then
  657. opts="--non-interactive"
  658. else
  659. opts="--interactive"
  660. fi
  661. if run_script "${updater}" ${opts} --not-running-from-cron; then
  662. progress "Updated existing install at ${ndprefix}"
  663. return 0
  664. else
  665. if [ -n "${EXIT_REASON}" ]; then
  666. fatal "Failed to update existing Netdata install at ${ndprefix}: ${EXIT_REASON}" "${EXIT_CODE}"
  667. else
  668. 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
  669. fi
  670. fi
  671. else
  672. warning "Could not find a usable copy of the updater script. We are unable to update this system in place."
  673. return 1
  674. fi
  675. }
  676. uninstall() {
  677. set_tmpdir
  678. get_system_info
  679. detect_existing_install
  680. if [ -n "${OLD_INSTALL_PREFIX}" ]; then
  681. INSTALL_PREFIX="$(echo "${OLD_INSTALL_PREFIX}/" | sed 's/$/netdata/g')"
  682. else
  683. INSTALL_PREFIX="${ndprefix}"
  684. fi
  685. uninstaller="${INSTALL_PREFIX}/usr/libexec/netdata/netdata-uninstaller.sh"
  686. uninstaller_url="https://raw.githubusercontent.com/netdata/netdata/master/packaging/installer/netdata-uninstaller.sh"
  687. if [ $INTERACTIVE = 0 ]; then
  688. FLAGS="--yes --force"
  689. else
  690. FLAGS="--yes"
  691. fi
  692. if [ -x "${uninstaller}" ]; then
  693. if [ "${DRY_RUN}" -eq 1 ]; then
  694. progress "Would attempt to uninstall existing install with uninstaller script found at: ${uninstaller}"
  695. return 0
  696. else
  697. progress "Found existing netdata-uninstaller. Running it.."
  698. # shellcheck disable=SC2086
  699. if ! run_script "${uninstaller}" ${FLAGS}; then
  700. warning "Uninstaller failed. Some parts of Netdata may still be present on the system."
  701. fi
  702. fi
  703. else
  704. if [ "${DRY_RUN}" -eq 1 ]; then
  705. progress "Would download installer script from: ${uninstaller_url}"
  706. progress "Would attempt to uninstall existing install with downloaded uninstaller script."
  707. return 0
  708. else
  709. progress "Downloading netdata-uninstaller ..."
  710. download "${uninstaller_url}" "${tmpdir}/netdata-uninstaller.sh"
  711. chmod +x "${tmpdir}/netdata-uninstaller.sh"
  712. # shellcheck disable=SC2086
  713. if ! run_script "${tmpdir}/netdata-uninstaller.sh" ${FLAGS}; then
  714. warning "Uninstaller failed. Some parts of Netdata may still be present on the system."
  715. fi
  716. fi
  717. fi
  718. }
  719. detect_existing_install() {
  720. set_tmpdir
  721. progress "Checking for existing installations of Netdata..."
  722. if pkg_installed netdata; then
  723. ndprefix="/"
  724. EXISTING_INSTALL_IS_NATIVE="1"
  725. else
  726. EXISTING_INSTALL_IS_NATIVE="0"
  727. if [ -n "${INSTALL_PREFIX}" ]; then
  728. searchpath="${INSTALL_PREFIX}/bin:${INSTALL_PREFIX}/sbin:${INSTALL_PREFIX}/usr/bin:${INSTALL_PREFIX}/usr/sbin:${PATH}"
  729. searchpath="${INSTALL_PREFIX}/netdata/bin:${INSTALL_PREFIX}/netdata/sbin:${INSTALL_PREFIX}/netdata/usr/bin:${INSTALL_PREFIX}/netdata/usr/sbin:${searchpath}"
  730. else
  731. searchpath="${PATH}"
  732. fi
  733. ndpath="$(PATH="${searchpath}" command -v netdata 2>/dev/null)"
  734. if [ -z "$ndpath" ] && [ -x /opt/netdata/bin/netdata ]; then
  735. ndpath="/opt/netdata/bin/netdata"
  736. fi
  737. if [ -n "${ndpath}" ]; then
  738. case "${ndpath}" in
  739. */usr/bin/netdata|*/usr/sbin/netdata) ndprefix="$(dirname "$(dirname "$(dirname "${ndpath}")")")" ;;
  740. *) ndprefix="$(dirname "$(dirname "${ndpath}")")" ;;
  741. esac
  742. fi
  743. if echo "${ndprefix}" | grep -Eq '^/usr$'; then
  744. ndprefix="$(dirname "${ndprefix}")"
  745. fi
  746. fi
  747. if [ -n "${ndprefix}" ]; then
  748. typefile="${ndprefix}/etc/netdata/.install-type"
  749. if [ -r "${typefile}" ]; then
  750. run_as_root sh -c "cat \"${typefile}\" > \"${tmpdir}/install-type\""
  751. # shellcheck disable=SC1090,SC1091
  752. . "${tmpdir}/install-type"
  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. run_as_root sh -c "cat \"${envfile}\" > \"${tmpdir}/environment\""
  760. # shellcheck disable=SC1091
  761. . "${tmpdir}/environment"
  762. if [ -n "${NETDATA_IS_STATIC_INSTALL}" ]; then
  763. if [ "${NETDATA_IS_STATIC_INSTALL}" = "yes" ]; then
  764. INSTALL_TYPE="legacy-static"
  765. else
  766. INSTALL_TYPE="legacy-build"
  767. fi
  768. fi
  769. fi
  770. fi
  771. fi
  772. }
  773. handle_existing_install() {
  774. detect_existing_install
  775. if [ -z "${ndprefix}" ] || [ -z "${INSTALL_TYPE}" ]; then
  776. progress "No existing installations of netdata found, assuming this is a fresh install."
  777. return 0
  778. fi
  779. case "${INSTALL_TYPE}" in
  780. kickstart-*|legacy-*|binpkg-*|manual-static|unknown)
  781. if [ "${INSTALL_TYPE}" = "unknown" ]; then
  782. if [ "${EXISTING_INSTALL_IS_NATIVE}" -eq 1 ]; then
  783. 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."
  784. else
  785. 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."
  786. fi
  787. else
  788. progress "Found an existing netdata install at ${ndprefix}, with installation type '${INSTALL_TYPE}'."
  789. fi
  790. if [ "${ACTION}" = "reinstall" ] || [ "${ACTION}" = "unsafe-reinstall" ]; then
  791. progress "Found an existing netdata install at ${ndprefix}, but user requested reinstall, continuing."
  792. case "${INSTALL_TYPE}" in
  793. binpkg-*) NETDATA_FORCE_METHOD='native' ;;
  794. *-build) NETDATA_FORCE_METHOD='build' ;;
  795. *-static) NETDATA_FORCE_METHOD='static' ;;
  796. *)
  797. if [ "${ACTION}" = "unsafe-reinstall" ]; then
  798. warning "Reinstalling over top of a ${INSTALL_TYPE} installation may be unsafe, but the user has requested we proceed."
  799. elif [ "${INTERACTIVE}" -eq 0 ]; then
  800. fatal "User requested reinstall, but we cannot safely reinstall over top of a ${INSTALL_TYPE} installation, exiting." F0104
  801. else
  802. if [ "${EXISTING_INSTALL_IS_NATIVE}" ]; then
  803. 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?"
  804. else
  805. reinstall_prompt="Reinstalling over top of a ${INSTALL_TYPE} installation may be unsafe, do you want to continue?"
  806. fi
  807. if confirm "${reinstall_prompt}"; then
  808. progress "OK, continuing."
  809. else
  810. fatal "Cancelling reinstallation at user request." F0105
  811. fi
  812. fi
  813. ;;
  814. esac
  815. return 0
  816. elif [ "${INSTALL_TYPE}" = "unknown" ]; then
  817. claimonly_notice="If you just want to claim this install, you should re-run this command with the --claim-only option instead."
  818. if [ "${EXISTING_INSTALL_IS_NATIVE}" -eq 1 ]; then
  819. 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}"
  820. 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?"
  821. else
  822. 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}"
  823. 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?"
  824. fi
  825. if [ "${INTERACTIVE}" -eq 0 ] && [ "${ACTION}" != "claim" ]; then
  826. fatal "${failmsg}" F0106
  827. elif [ "${INTERACTIVE}" -eq 1 ] && [ "${ACTION}" != "claim" ]; then
  828. if confirm "${promptmsg}"; then
  829. progress "OK, continuing"
  830. else
  831. fatal "Cancelling update of unknown installation type at user request." F050C
  832. fi
  833. fi
  834. fi
  835. ret=0
  836. if [ "${ACTION}" != "claim" ]; then
  837. if ! update; then
  838. warning "Failed to update existing Netdata install at ${ndprefix}."
  839. else
  840. progress "Successfully updated existing netdata install at ${ndprefix}."
  841. fi
  842. else
  843. warning "Not updating existing install at ${ndprefix}."
  844. fi
  845. if [ -n "${NETDATA_CLAIM_TOKEN}" ]; then
  846. progress "Attempting to claim existing install at ${ndprefix}."
  847. INSTALL_PREFIX="${ndprefix}"
  848. claim
  849. ret=$?
  850. elif [ "${ACTION}" = "claim" ]; then
  851. fatal "User asked to claim, but did not provide a claiming token." F0202
  852. else
  853. progress "Not attempting to claim existing install at ${ndprefix} (no claiming token provided)."
  854. fi
  855. deferred_warnings
  856. success_banner
  857. cleanup
  858. trap - EXIT
  859. exit $ret
  860. ;;
  861. oci)
  862. 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
  863. ;;
  864. *)
  865. if [ "${ACTION}" = "reinstall" ] || [ "${ACTION}" = "unsafe-reinstall" ]; then
  866. if [ "${ACTION}" = "unsafe-reinstall" ]; then
  867. warning "Reinstalling over top of a ${INSTALL_TYPE} installation may be unsafe, but the user has requested we proceed."
  868. elif [ "${INTERACTIVE}" -eq 0 ]; then
  869. fatal "User requested reinstall, but we cannot safely reinstall over top of a ${INSTALL_TYPE} installation, exiting." F0104
  870. else
  871. if confirm "Reinstalling over top of a ${INSTALL_TYPE} installation may be unsafe, do you want to continue?"; then
  872. progress "OK, continuing."
  873. else
  874. fatal "Cancelling reinstallation at user request." F0105
  875. fi
  876. fi
  877. else
  878. if [ -n "${NETDATA_CLAIM_TOKEN}" ]; then
  879. progress "Attempting to claim existing install at ${ndprefix}."
  880. INSTALL_PREFIX="${ndprefix}"
  881. claim
  882. ret=$?
  883. cleanup
  884. trap - EXIT
  885. exit $ret
  886. elif [ "${ACTION}" = "claim" ]; then
  887. fatal "User asked to claim, but did not provide a claiming token." F0202
  888. else
  889. 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
  890. fi
  891. fi
  892. ;;
  893. esac
  894. }
  895. soft_disable_cloud() {
  896. set_tmpdir
  897. cloud_prefix="${INSTALL_PREFIX}/var/lib/netdata/cloud.d"
  898. run_as_root mkdir -p "${cloud_prefix}"
  899. cat > "${tmpdir}/cloud.conf" << EOF
  900. [global]
  901. enabled = no
  902. EOF
  903. run_as_root cp "${tmpdir}/cloud.conf" "${cloud_prefix}/cloud.conf"
  904. if [ -z "${NETDATA_NO_START}" ]; then
  905. case "${SYSTYPE}" in
  906. Darwin) run_as_root launchctl kickstart -k com.github.netdata ;;
  907. FreeBSD) run_as_root service netdata restart ;;
  908. Linux)
  909. initpath="$(run_as_root readlink /proc/1/exe)"
  910. if command -v service > /dev/null 2>&1; then
  911. run_as_root service netdata restart
  912. elif command -v rc-service > /dev/null 2>&1; then
  913. run_as_root rc-service netdata restart
  914. elif [ "$(basename "${initpath}" 2> /dev/null)" = "systemd" ]; then
  915. run_as_root systemctl restart netdata
  916. elif [ -f /etc/init.d/netdata ]; then
  917. run_as_root /etc/init.d/netdata restart
  918. fi
  919. ;;
  920. esac
  921. fi
  922. }
  923. confirm_install_prefix() {
  924. if [ -n "${INSTALL_PREFIX}" ] && [ "${NETDATA_FORCE_METHOD}" != 'build' ]; then
  925. fatal "The --install-prefix option is only supported together with the --build-only option." F0204
  926. fi
  927. if [ -n "${INSTALL_PREFIX}" ]; then
  928. NETDATA_INSTALLER_OPTIONS="${NETDATA_INSTALLER_OPTIONS} --install-prefix ${INSTALL_PREFIX}"
  929. else
  930. case "${SYSTYPE}" in
  931. Darwin)
  932. INSTALL_PREFIX="/usr/local/netdata"
  933. NETDATA_INSTALLER_OPTIONS="${NETDATA_INSTALLER_OPTIONS} --install-no-prefix ${INSTALL_PREFIX}"
  934. ;;
  935. FreeBSD)
  936. INSTALL_PREFIX="/usr/local"
  937. NETDATA_INSTALLER_OPTIONS="${NETDATA_INSTALLER_OPTIONS} --install-no-prefix ${INSTALL_PREFIX}"
  938. ;;
  939. esac
  940. fi
  941. }
  942. # ======================================================================
  943. # Claiming support code
  944. check_claim_opts() {
  945. # shellcheck disable=SC2235,SC2030
  946. if [ -z "${NETDATA_CLAIM_TOKEN}" ] && [ -n "${NETDATA_CLAIM_ROOMS}" ]; then
  947. fatal "Invalid claiming options, claim rooms may only be specified when a token is specified." F0204
  948. elif [ -z "${NETDATA_CLAIM_TOKEN}" ] && [ -n "${NETDATA_CLAIM_EXTRA}" ]; then
  949. fatal "Invalid claiming options, a claiming token must be specified." F0204
  950. elif [ "${NETDATA_DISABLE_CLOUD}" -eq 1 ] && [ -n "${NETDATA_CLAIM_TOKEN}" ]; then
  951. fatal "Cloud explicitly disabled, but automatic claiming requested. Either enable Netdata Cloud, or remove the --claim-* options." F0204
  952. fi
  953. }
  954. is_netdata_running() {
  955. if command -v pgrep > /dev/null 2>&1; then
  956. if pgrep netdata; then
  957. return 0
  958. else
  959. return 1
  960. fi
  961. else
  962. if [ -z "${INSTALL_PREFIX}" ]; then
  963. NETDATACLI_PATH=/usr/sbin/netdatacli
  964. elif [ "${INSTALL_PREFIX}" = "/opt/netdata" ]; then
  965. NETDATACLI_PATH="/opt/netdata/bin/netdatacli"
  966. else
  967. NETDATACLI_PATH="${INSTALL_PREFIX}/netdata/usr/sbin/netdatacli"
  968. fi
  969. if "${NETDATACLI_PATH}" ping > /dev/null 2>&1; then
  970. return 0
  971. else
  972. return 1
  973. fi
  974. fi
  975. }
  976. claim() {
  977. if [ "${DRY_RUN}" -eq 1 ]; then
  978. progress "Would attempt to claim agent to ${NETDATA_CLAIM_URL}"
  979. else
  980. progress "Attempting to claim agent to ${NETDATA_CLAIM_URL}"
  981. fi
  982. if command -v netdata-claim.sh > /dev/null 2>&1; then
  983. NETDATA_CLAIM_PATH="$(command -v netdata-claim.sh)"
  984. elif [ -z "${INSTALL_PREFIX}" ] || [ "${INSTALL_PREFIX}" = "/" ]; then
  985. NETDATA_CLAIM_PATH=/usr/sbin/netdata-claim.sh
  986. elif [ "${INSTALL_PREFIX}" = "/opt/netdata" ]; then
  987. NETDATA_CLAIM_PATH="/opt/netdata/bin/netdata-claim.sh"
  988. elif [ ! -d "${INSTALL_PREFIX}/netdata" ]; then
  989. if [ -d "${INSTALL_PREFIX}/usr" ]; then
  990. NETDATA_CLAIM_PATH="${INSTALL_PREFIX}/usr/sbin/netdata-claim.sh"
  991. else
  992. NETDATA_CLAIM_PATH="${INSTALL_PREFIX}/sbin/netdata-claim.sh"
  993. fi
  994. else
  995. NETDATA_CLAIM_PATH="${INSTALL_PREFIX}/netdata/usr/sbin/netdata-claim.sh"
  996. fi
  997. err_msg=
  998. err_code=
  999. if [ -z "${NETDATA_CLAIM_PATH}" ]; then
  1000. err_msg="Unable to claim node: could not find usable claiming script. Reinstalling Netdata may resolve this."
  1001. err_code=F050B
  1002. elif [ ! -e "${NETDATA_CLAIM_PATH}" ]; then
  1003. err_msg="Unable to claim node: ${NETDATA_CLAIM_PATH} does not exist."
  1004. err_code=F0512
  1005. elif [ ! -f "${NETDATA_CLAIM_PATH}" ]; then
  1006. err_msg="Unable to claim node: ${NETDATA_CLAIM_PATH} is not a file."
  1007. err_code=F0513
  1008. elif [ ! -x "${NETDATA_CLAIM_PATH}" ]; then
  1009. err_msg="Unable to claim node: claiming script at ${NETDATA_CLAIM_PATH} is not executable. Reinstalling Netdata may resolve this."
  1010. err_code=F0514
  1011. fi
  1012. if [ -n "$err_msg" ]; then
  1013. if [ "${ACTION}" = "claim" ]; then
  1014. fatal "$err_msg" "$err_code"
  1015. else
  1016. warning "$err_msg"
  1017. return 1
  1018. fi
  1019. fi
  1020. if ! is_netdata_running; then
  1021. NETDATA_CLAIM_EXTRA="${NETDATA_CLAIM_EXTRA} -daemon-not-running"
  1022. fi
  1023. # shellcheck disable=SC2086
  1024. run_as_root "${NETDATA_CLAIM_PATH}" -token="${NETDATA_CLAIM_TOKEN}" -rooms="${NETDATA_CLAIM_ROOMS}" -url="${NETDATA_CLAIM_URL}" ${NETDATA_CLAIM_EXTRA}
  1025. case $? in
  1026. 0)
  1027. progress "Successfully claimed node"
  1028. return 0
  1029. ;;
  1030. 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}" ;;
  1031. 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." ;;
  1032. 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}." ;;
  1033. 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." ;;
  1034. 5)
  1035. 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."
  1036. return 0
  1037. ;;
  1038. 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." ;;
  1039. 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." ;;
  1040. 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." ;;
  1041. 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." ;;
  1042. 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." ;;
  1043. 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)\"" ;;
  1044. 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}." ;;
  1045. 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}." ;;
  1046. 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." ;;
  1047. *) 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}" ;;
  1048. esac
  1049. if [ "${ACTION}" = "claim" ]; then
  1050. deferred_warnings
  1051. printf >&2 "%s\n" "For community support, you can connect with us on:"
  1052. support_list
  1053. cleanup
  1054. trap - EXIT
  1055. exit 1
  1056. fi
  1057. }
  1058. # ======================================================================
  1059. # Auto-update handling code.
  1060. set_auto_updates() {
  1061. if run_as_root test -x "${INSTALL_PREFIX}/usr/libexec/netdata/netdata-updater.sh"; then
  1062. updater="${INSTALL_PREFIX}/usr/libexec/netdata/netdata-updater.sh"
  1063. elif run_as_root test -x "${INSTALL_PREFIX}/netdata/usr/libexec/netdata/netdata-updater.sh"; then
  1064. updater="${INSTALL_PREFIX}/netdata/usr/libexec/netdata/netdata-updater.sh"
  1065. else
  1066. 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."
  1067. return 0
  1068. fi
  1069. if [ "${AUTO_UPDATE}" -eq 1 ]; then
  1070. if [ "${DRY_RUN}" -eq 1 ]; then
  1071. progress "Would have attempted to enable automatic updates."
  1072. # 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.
  1073. elif ! run_as_root grep -q '\-\-enable-auto-updates' "${updater}"; then
  1074. echo
  1075. elif ! run_as_root "${updater}" --enable-auto-updates "${NETDATA_AUTO_UPDATE_TYPE}"; then
  1076. warning "Failed to enable auto updates. Netdata will still work, but you will need to update manually."
  1077. fi
  1078. else
  1079. if [ "${DRY_RUN}" -eq 1 ]; then
  1080. progress "Would have attempted to disable automatic updates."
  1081. else
  1082. run_as_root "${updater}" --disable-auto-updates
  1083. fi
  1084. fi
  1085. }
  1086. # ======================================================================
  1087. # Native package install code.
  1088. # Check for an already installed package with a given name.
  1089. pkg_installed() {
  1090. case "${SYSTYPE}" in
  1091. Linux)
  1092. case "${DISTRO_COMPAT_NAME}" in
  1093. debian|ubuntu)
  1094. # shellcheck disable=SC2016
  1095. dpkg-query --show --showformat '${Status}' "${1}" 2>&1 | cut -f 1 -d ' ' | grep -q '^install$'
  1096. return $?
  1097. ;;
  1098. centos|fedora|opensuse|ol|amzn)
  1099. rpm -q "${1}" > /dev/null 2>&1
  1100. return $?
  1101. ;;
  1102. alpine)
  1103. apk -e info "${1}" > /dev/null 2>&1
  1104. return $?
  1105. ;;
  1106. arch)
  1107. pacman -Qi "${1}" > /dev/null 2>&1
  1108. return $?
  1109. ;;
  1110. *) return 1 ;;
  1111. esac
  1112. ;;
  1113. Darwin)
  1114. if command -v brew > /dev/null 2>&1; then
  1115. brew list "${1}" > /dev/null 2>&1
  1116. return $?
  1117. else
  1118. return 1
  1119. fi
  1120. ;;
  1121. FreeBSD)
  1122. if pkg -N > /dev/null 2>&1; then
  1123. pkg info "${1}" > /dev/null 2>&1
  1124. return $?
  1125. else
  1126. return 1
  1127. fi
  1128. ;;
  1129. *) return 1 ;;
  1130. esac
  1131. }
  1132. # Check for the existence of a usable netdata package in the repo.
  1133. netdata_avail_check() {
  1134. case "${DISTRO_COMPAT_NAME}" in
  1135. debian|ubuntu)
  1136. env DEBIAN_FRONTEND=noninteractive apt-cache policy netdata | grep -q repo.netdata.cloud/repos/;
  1137. return $?
  1138. ;;
  1139. centos|fedora|ol|amzn)
  1140. # shellcheck disable=SC2086
  1141. ${pm_cmd} search --nogpgcheck -v netdata | grep -qE 'Repo *: netdata(-edge)?$'
  1142. return $?
  1143. ;;
  1144. opensuse)
  1145. zypper packages -r "$(zypper repos | grep -E 'netdata |netdata-edge ' | cut -f 1 -d '|' | tr -d ' ')" | grep -E 'netdata '
  1146. return $?
  1147. ;;
  1148. *) return 1 ;;
  1149. esac
  1150. }
  1151. # Check for any distro-specific dependencies we know we need.
  1152. check_special_native_deps() {
  1153. if [ "${DISTRO_COMPAT_NAME}" = "centos" ] && [ "${SYSVERSION}" = "7" ]; then
  1154. progress "Checking for libuv availability."
  1155. if ${pm_cmd} search --nogpgcheck -v libuv | grep -q "No matches found"; then
  1156. progress "libuv not found, checking for EPEL availability."
  1157. if ${pm_cmd} search --nogpgcheck -v epel-release | grep -q "No matches found"; then
  1158. warning "Unable to find a suitable source for libuv, cannot install using native packages on this system."
  1159. return 1
  1160. else
  1161. progress "EPEL is available, attempting to install so that required dependencies are available."
  1162. # shellcheck disable=SC2086
  1163. if ! run_as_root env ${env} ${pm_cmd} ${install_subcmd} ${pkg_install_opts} epel-release; then
  1164. warning "Failed to install EPEL, even though it is required to install native packages on this system."
  1165. return 1
  1166. fi
  1167. fi
  1168. else
  1169. return 0
  1170. fi
  1171. fi
  1172. }
  1173. common_rpm_opts() {
  1174. pkg_type="rpm"
  1175. pkg_suffix=".noarch"
  1176. pkg_vsep="-"
  1177. INSTALL_TYPE="binpkg-rpm"
  1178. NATIVE_VERSION="${INSTALL_VERSION:+"-${INSTALL_VERSION}.${SYSARCH}"}"
  1179. }
  1180. common_dnf_opts() {
  1181. if [ "${INTERACTIVE}" = "0" ]; then
  1182. interactive_opts="-y"
  1183. fi
  1184. if command -v dnf > /dev/null; then
  1185. pm_cmd="dnf"
  1186. repo_subcmd="makecache"
  1187. else
  1188. pm_cmd="yum"
  1189. fi
  1190. install_subcmd="install"
  1191. pkg_install_opts="${interactive_opts}"
  1192. repo_update_opts="${interactive_opts}"
  1193. uninstall_subcmd="remove"
  1194. }
  1195. try_package_install() {
  1196. 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."
  1197. if [ -z "${DISTRO_COMPAT_NAME}" ] || [ "${DISTRO_COMPAT_NAME}" = "unknown" ]; then
  1198. warning "Unable to determine Linux distribution for native packages."
  1199. return 2
  1200. elif [ -z "${SYSCODENAME}" ]; then
  1201. case "${DISTRO_COMPAT_NAME}" in
  1202. debian|ubuntu)
  1203. warning "Release codename not set. Unable to check availability of native packages for this system."
  1204. return 2
  1205. ;;
  1206. esac
  1207. fi
  1208. set_tmpdir
  1209. if [ "${DRY_RUN}" -eq 1 ]; then
  1210. progress "Would attempt to install using native packages..."
  1211. else
  1212. progress "Attempting to install using native packages..."
  1213. fi
  1214. if [ "${SELECTED_RELEASE_CHANNEL}" = "nightly" ]; then
  1215. release="-edge"
  1216. else
  1217. release=""
  1218. fi
  1219. interactive_opts=""
  1220. env=""
  1221. case "${DISTRO_COMPAT_NAME}" in
  1222. debian|ubuntu)
  1223. if [ "${INTERACTIVE}" = "0" ]; then
  1224. install_subcmd="-o Dpkg::Options::=--force-confdef -o Dpkg::Options::=--force-confold install"
  1225. interactive_opts="-y"
  1226. env="DEBIAN_FRONTEND=noninteractive"
  1227. else
  1228. install_subcmd="install"
  1229. fi
  1230. needs_early_refresh=1
  1231. pm_cmd="apt-get"
  1232. repo_subcmd="update"
  1233. pkg_type="deb"
  1234. pkg_vsep="_"
  1235. pkg_install_opts="${interactive_opts}"
  1236. repo_update_opts="${interactive_opts}"
  1237. uninstall_subcmd="purge"
  1238. repo_prefix="${DISTRO_COMPAT_NAME}/${SYSCODENAME}"
  1239. pkg_suffix="+${DISTRO_COMPAT_NAME}${SYSVERSION}_all"
  1240. INSTALL_TYPE="binpkg-deb"
  1241. NATIVE_VERSION="${INSTALL_VERSION:+"=${INSTALL_VERSION}"}"
  1242. ;;
  1243. centos)
  1244. common_rpm_opts
  1245. common_dnf_opts
  1246. repo_prefix="el/${SYSVERSION}"
  1247. # if [ "${SYSVERSION}" -lt 8 ]; then
  1248. # explicitly_install_native_plugins=1
  1249. # fi
  1250. ;;
  1251. fedora|ol)
  1252. common_rpm_opts
  1253. common_dnf_opts
  1254. repo_prefix="${DISTRO_COMPAT_NAME}/${SYSVERSION}"
  1255. ;;
  1256. opensuse)
  1257. if [ "${INTERACTIVE}" = "0" ]; then
  1258. install_subcmd="--non-interactive --no-gpg-checks install"
  1259. else
  1260. install_subcmd="--no-gpg-checks install"
  1261. fi
  1262. common_rpm_opts
  1263. pm_cmd="zypper"
  1264. repo_subcmd="--gpg-auto-import-keys refresh"
  1265. repo_prefix="opensuse/${SYSVERSION}"
  1266. pkg_install_opts="${interactive_opts} --allow-unsigned-rpm"
  1267. repo_update_opts=""
  1268. uninstall_subcmd="remove"
  1269. ;;
  1270. amzn)
  1271. common_rpm_opts
  1272. common_dnf_opts
  1273. repo_prefix="amazonlinux/${SYSVERSION}"
  1274. ;;
  1275. *)
  1276. warning "We do not provide native packages for ${DISTRO}."
  1277. return 2
  1278. ;;
  1279. esac
  1280. if [ -n "${SKIP_DISTRO_DETECTION}" ]; then
  1281. 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."
  1282. fi
  1283. if [ -n "${INSTALL_VERSION}" ]; then
  1284. if echo "${INSTALL_VERSION}" | grep -q "nightly"; then
  1285. new_release="-edge"
  1286. else
  1287. new_release=
  1288. fi
  1289. if { [ -n "${new_release}" ] && [ -z "${release}" ]; } || { [ -z "${new_release}" ] && [ -n "${release}" ]; }; then
  1290. warning "Selected release channel does not match this version and it will be changed automatically."
  1291. fi
  1292. release="${new_release}"
  1293. fi
  1294. repoconfig_name="netdata-repo${release}"
  1295. case "${pkg_type}" in
  1296. deb)
  1297. repoconfig_file="${repoconfig_name}${pkg_vsep}${REPOCONFIG_DEB_VERSION}${pkg_suffix}.${pkg_type}"
  1298. repoconfig_url="${REPOCONFIG_DEB_URL_PREFIX}/${repo_prefix}/${repoconfig_file}"
  1299. ;;
  1300. rpm)
  1301. repoconfig_file="${repoconfig_name}${pkg_vsep}${REPOCONFIG_RPM_VERSION}${pkg_suffix}.${pkg_type}"
  1302. repoconfig_url="${REPOCONFIG_RPM_URL_PREFIX}/${repo_prefix}/${SYSARCH}/${repoconfig_file}"
  1303. ;;
  1304. esac
  1305. if ! pkg_installed "${repoconfig_name}"; then
  1306. progress "Checking for availability of repository configuration package."
  1307. if ! check_for_remote_file "${repoconfig_url}"; then
  1308. warning "No repository configuration package available for ${DISTRO} ${SYSVERSION}. Cannot install native packages on this system."
  1309. return 2
  1310. fi
  1311. if ! download "${repoconfig_url}" "${tmpdir}/${repoconfig_file}"; then
  1312. fatal "Failed to download repository configuration package. ${BADNET_MSG}." F0209
  1313. fi
  1314. if [ -n "${needs_early_refresh}" ]; then
  1315. # shellcheck disable=SC2086
  1316. if ! run_as_root env ${env} ${pm_cmd} ${repo_subcmd} ${repo_update_opts}; then
  1317. warning "${failed_refresh_msg}"
  1318. return 2
  1319. fi
  1320. fi
  1321. # shellcheck disable=SC2086
  1322. if ! run_as_root env ${env} ${pm_cmd} ${install_subcmd} ${pkg_install_opts} "${tmpdir}/${repoconfig_file}"; then
  1323. warning "Failed to install repository configuration package."
  1324. return 2
  1325. fi
  1326. if [ -n "${repo_subcmd}" ]; then
  1327. # shellcheck disable=SC2086
  1328. if ! run_as_root env ${env} ${pm_cmd} ${repo_subcmd} ${repo_update_opts}; then
  1329. 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
  1330. fi
  1331. fi
  1332. else
  1333. progress "Repository configuration is already present, attempting to install netdata."
  1334. fi
  1335. if [ "${ACTION}" = "repositories-only" ]; then
  1336. progress "Successfully installed repository configuraion package."
  1337. deferred_warnings
  1338. cleanup
  1339. trap - EXIT
  1340. exit 1
  1341. fi
  1342. if ! check_special_native_deps; then
  1343. warning "Could not find secondary dependencies for ${DISTRO} on ${SYSARCH}."
  1344. if [ -z "${NO_CLEANUP}" ]; then
  1345. progress "Attempting to uninstall repository configuration package."
  1346. # shellcheck disable=SC2086
  1347. run_as_root env ${env} ${pm_cmd} ${uninstall_subcmd} ${pkg_install_opts} "${repoconfig_name}"
  1348. fi
  1349. return 2
  1350. fi
  1351. if ! netdata_avail_check "${DISTRO_COMPAT_NAME}"; then
  1352. warning "Could not find a usable native package for ${DISTRO} on ${SYSARCH}."
  1353. if [ -z "${NO_CLEANUP}" ]; then
  1354. progress "Attempting to uninstall repository configuration package."
  1355. # shellcheck disable=SC2086
  1356. run_as_root env ${env} ${pm_cmd} ${uninstall_subcmd} ${pkg_install_opts} "${repoconfig_name}"
  1357. fi
  1358. return 2
  1359. fi
  1360. if [ "${NETDATA_DISABLE_TELEMETRY}" -eq 1 ]; then
  1361. run_as_root mkdir -p "/etc/netdata"
  1362. run_as_root touch "/etc/netdata/.opt-out-from-anonymous-statistics"
  1363. fi
  1364. # shellcheck disable=SC2086
  1365. if ! run_as_root env ${env} ${pm_cmd} ${install_subcmd} ${pkg_install_opts} "netdata${NATIVE_VERSION}"; then
  1366. warning "Failed to install Netdata package."
  1367. if [ -z "${NO_CLEANUP}" ]; then
  1368. progress "Attempting to uninstall repository configuration package."
  1369. # shellcheck disable=SC2086
  1370. run_as_root env ${env} ${pm_cmd} ${uninstall_subcmd} ${pkg_install_opts} "${repoconfig_name}"
  1371. fi
  1372. return 2
  1373. fi
  1374. if [ -n "${explicitly_install_native_plugins}" ]; then
  1375. progress "Installing external plugins."
  1376. # shellcheck disable=SC2086
  1377. if ! run_as_root env ${env} ${pm_cmd} ${install_subcmd} ${DEFAULT_PLUGIN_PACKAGES}; then
  1378. warning "Failed to install external plugin packages. Some collectors may not be available."
  1379. fi
  1380. fi
  1381. }
  1382. # ======================================================================
  1383. # Static build install code
  1384. # shellcheck disable=SC2034,SC2086,SC2126
  1385. set_static_archive_urls() {
  1386. if [ -z "${2}" ]; then
  1387. arch="${SYSARCH}"
  1388. else
  1389. arch="${2}"
  1390. fi
  1391. if [ -n "${NETDATA_OFFLINE_INSTALL_SOURCE}" ]; then
  1392. path="$(cd "${NETDATA_OFFLINE_INSTALL_SOURCE}" || exit 1; pwd)"
  1393. export NETDATA_STATIC_ARCHIVE_URL="file://${path}/netdata-${arch}-latest.gz.run"
  1394. export NETDATA_STATIC_ARCHIVE_NAME="netdata-${arch}-latest.gz.run"
  1395. export NETDATA_STATIC_ARCHIVE_CHECKSUM_URL="file://${path}/sha256sums.txt"
  1396. elif [ "${1}" = "stable" ]; then
  1397. if [ -n "${INSTALL_VERSION}" ]; then
  1398. export NETDATA_STATIC_ARCHIVE_URL="https://github.com/netdata/netdata/releases/download/v${INSTALL_VERSION}/netdata-${arch}-v${INSTALL_VERSION}.gz.run"
  1399. export NETDATA_STATIC_ARCHIVE_OLD_URL="https://github.com/netdata/netdata/releases/download/v${INSTALL_VERSION}/netdata-v${INSTALL_VERSION}.gz.run"
  1400. export NETDATA_STATIC_ARCHIVE_NAME="netdata-${arch}-v${INSTALL_VERSION}.gz.run"
  1401. export NETDATA_STATIC_ARCHIVE_OLD_NAME="netdata-v${INSTALL_VERSION}.gz.run"
  1402. export NETDATA_STATIC_ARCHIVE_CHECKSUM_URL="https://github.com/netdata/netdata/releases/download/v${INSTALL_VERSION}/sha256sums.txt"
  1403. else
  1404. latest="$(get_redirect "https://github.com/netdata/netdata/releases/latest")"
  1405. export NETDATA_STATIC_ARCHIVE_URL="https://github.com/netdata/netdata/releases/download/${latest}/netdata-${arch}-latest.gz.run"
  1406. export NETDATA_STATIC_ARCHIVE_NAME="netdata-${arch}-latest.gz.run"
  1407. export NETDATA_STATIC_ARCHIVE_CHECKSUM_URL="https://github.com/netdata/netdata/releases/download/${latest}/sha256sums.txt"
  1408. fi
  1409. else
  1410. if [ -n "${INSTALL_VERSION}" ]; then
  1411. export NETDATA_STATIC_ARCHIVE_URL="${NETDATA_TARBALL_BASEURL}/download/v${INSTALL_VERSION}/netdata-${arch}-v${INSTALL_VERSION}.gz.run"
  1412. export NETDATA_STATIC_ARCHIVE_OLD_URL="${NETDATA_TARBALL_BASEURL}/download/v${INSTALL_VERSION}/netdata-v${INSTALL_VERSION}.gz.run"
  1413. export NETDATA_STATIC_ARCHIVE_NAME="netdata-${arch}-v${INSTALL_VERSION}.gz.run"
  1414. export NETDATA_STATIC_ARCHIVE_OLD_NAME="netdata-v${INSTALL_VERSION}.gz.run"
  1415. export NETDATA_STATIC_ARCHIVE_CHECKSUM_URL="${NETDATA_TARBALL_BASEURL}/download/v${INSTALL_VERSION}/sha256sums.txt"
  1416. else
  1417. tag="$(get_redirect "${NETDATA_TARBALL_BASEURL}/latest")"
  1418. export NETDATA_STATIC_ARCHIVE_URL="${NETDATA_TARBALL_BASEURL}/download/${tag}/netdata-${arch}-latest.gz.run"
  1419. export NETDATA_STATIC_ARCHIVE_NAME="netdata-${arch}-latest.gz.run"
  1420. export NETDATA_STATIC_ARCHIVE_CHECKSUM_URL="${NETDATA_TARBALL_BASEURL}/download/${tag}/sha256sums.txt"
  1421. fi
  1422. fi
  1423. }
  1424. try_static_install() {
  1425. set_static_archive_urls "${SELECTED_RELEASE_CHANNEL}"
  1426. if [ "${DRY_RUN}" -eq 1 ]; then
  1427. progress "Would attempt to install using static build..."
  1428. else
  1429. progress "Attempting to install using static build..."
  1430. fi
  1431. # Check status code first, so that we can provide nicer fallback for dry runs.
  1432. if check_for_remote_file "${NETDATA_STATIC_ARCHIVE_URL}"; then
  1433. netdata_agent="${NETDATA_STATIC_ARCHIVE_NAME}"
  1434. elif [ "${SYSARCH}" = "x86_64" ] && check_for_remote_file "${NETDATA_STATIC_ARCHIVE_OLD_URL}"; then
  1435. netdata_agent="${NETDATA_STATIC_ARCHIVE_OLD_NAME}"
  1436. export NETDATA_STATIC_ARCHIVE_URL="${NETDATA_STATIC_ARCHIVE_OLD_URL}"
  1437. else
  1438. warning "There is no static build available for ${SYSARCH} CPUs. This usually means we simply do not currently provide static builds for ${SYSARCH} CPUs."
  1439. return 2
  1440. fi
  1441. if ! download "${NETDATA_STATIC_ARCHIVE_URL}" "${tmpdir}/${netdata_agent}"; then
  1442. fatal "Unable to download static build archive for ${SYSARCH}. ${BADNET_MSG}." F0208
  1443. fi
  1444. if ! download "${NETDATA_STATIC_ARCHIVE_CHECKSUM_URL}" "${tmpdir}/sha256sum.txt"; then
  1445. fatal "Unable to fetch checksums to verify static build archive. ${BADNET_MSG}." F0206
  1446. fi
  1447. if [ "${DRY_RUN}" -eq 1 ]; then
  1448. progress "Would validate SHA256 checksum of downloaded static build archive."
  1449. else
  1450. if [ -z "${INSTALL_VERSION}" ]; then
  1451. if ! grep "${netdata_agent}" "${tmpdir}/sha256sum.txt" | safe_sha256sum -c - > /dev/null 2>&1; then
  1452. fatal "Static binary checksum validation failed. ${BADCACHE_MSG}." F0207
  1453. fi
  1454. fi
  1455. fi
  1456. if [ "${INTERACTIVE}" -eq 0 ]; then
  1457. opts="${opts} --accept"
  1458. fi
  1459. progress "Installing netdata"
  1460. # shellcheck disable=SC2086
  1461. if ! run_as_root sh "${tmpdir}/${netdata_agent}" ${opts} -- ${NETDATA_INSTALLER_OPTIONS}; then
  1462. warning "Failed to install static build of Netdata on ${SYSARCH}."
  1463. run rm -rf /opt/netdata
  1464. return 2
  1465. fi
  1466. if [ "${DRY_RUN}" -ne 1 ]; then
  1467. install_type_file="/opt/netdata/etc/netdata/.install-type"
  1468. if [ -f "${install_type_file}" ]; then
  1469. run_as_root sh -c "cat \"${install_type_file}\" > \"${tmpdir}/install-type\""
  1470. run_as_root chown "$(id -u)":"$(id -g)" "${tmpdir}/install-type"
  1471. # shellcheck disable=SC1090,SC1091
  1472. . "${tmpdir}/install-type"
  1473. cat > "${tmpdir}/install-type" <<- EOF
  1474. INSTALL_TYPE='kickstart-static'
  1475. PREBUILT_ARCH='${PREBUILT_ARCH}'
  1476. EOF
  1477. run_as_root chown netdata:netdata "${tmpdir}/install-type"
  1478. run_as_root cp "${tmpdir}/install-type" "${install_type_file}"
  1479. fi
  1480. fi
  1481. }
  1482. # ======================================================================
  1483. # Local build install code
  1484. set_source_archive_urls() {
  1485. if [ "$1" = "stable" ]; then
  1486. if [ -n "${INSTALL_VERSION}" ]; then
  1487. export NETDATA_SOURCE_ARCHIVE_URL="https://github.com/netdata/netdata/releases/download/v${INSTALL_VERSION}/netdata-v${INSTALL_VERSION}.tar.gz"
  1488. export NETDATA_SOURCE_ARCHIVE_CHECKSUM_URL="https://github.com/netdata/netdata/releases/download/v${INSTALL_VERSION}/sha256sums.txt"
  1489. else
  1490. latest="$(get_redirect "https://github.com/netdata/netdata/releases/latest")"
  1491. export NETDATA_SOURCE_ARCHIVE_URL="https://github.com/netdata/netdata/releases/download/${latest}/netdata-${latest}.tar.gz"
  1492. export NETDATA_SOURCE_ARCHIVE_CHECKSUM_URL="https://github.com/netdata/netdata/releases/download/${latest}/sha256sums.txt"
  1493. fi
  1494. else
  1495. if [ -n "${INSTALL_VERSION}" ]; then
  1496. export NETDATA_SOURCE_ARCHIVE_URL="${NETDATA_TARBALL_BASEURL}/download/v${INSTALL_VERSION}/netdata-latest.tar.gz"
  1497. export NETDATA_SOURCE_ARCHIVE_CHECKSUM_URL="${NETDATA_TARBALL_BASEURL}/download/v${INSTALL_VERSION}/sha256sums.txt"
  1498. else
  1499. tag="$(get_redirect "${NETDATA_TARBALL_BASEURL}/latest")"
  1500. export NETDATA_SOURCE_ARCHIVE_URL="${NETDATA_TARBALL_BASEURL}/download/${tag}/netdata-latest.tar.gz"
  1501. export NETDATA_SOURCE_ARCHIVE_CHECKSUM_URL="${NETDATA_TARBALL_BASEURL}/download/${tag}/sha256sums.txt"
  1502. fi
  1503. fi
  1504. }
  1505. install_local_build_dependencies() {
  1506. set_tmpdir
  1507. bash="$(command -v bash 2> /dev/null)"
  1508. if [ -z "${bash}" ] || [ ! -x "${bash}" ]; then
  1509. warning "Unable to find a usable version of \`bash\` (required for local build)."
  1510. return 1
  1511. fi
  1512. if ! download "${PACKAGES_SCRIPT}" "${tmpdir}/install-required-packages.sh"; then
  1513. fatal "Failed to download dependency handling script for local build. ${BADNET_MSG}." F000D
  1514. fi
  1515. if [ "${DRY_RUN}" -eq 1 ]; then
  1516. progress "Would run downloaded script to install required build dependencies..."
  1517. else
  1518. progress "Running downloaded script to install required build dependencies..."
  1519. fi
  1520. if [ "${INTERACTIVE}" -eq 0 ]; then
  1521. opts="--dont-wait --non-interactive"
  1522. fi
  1523. # shellcheck disable=SC2086
  1524. if ! run_as_root "${bash}" "${tmpdir}/install-required-packages.sh" ${opts} netdata; then
  1525. warning "Failed to install all required packages, but installation might still be possible."
  1526. fi
  1527. }
  1528. build_and_install() {
  1529. if [ "${DRY_RUN}" -eq 1 ]; then
  1530. progress "Would attempt to build netdata..."
  1531. else
  1532. progress "Building netdata..."
  1533. fi
  1534. echo "INSTALL_TYPE='kickstart-build'" > system/.install-type
  1535. opts="${NETDATA_INSTALLER_OPTIONS}"
  1536. if [ "${INTERACTIVE}" -eq 0 ]; then
  1537. opts="${opts} --dont-wait"
  1538. fi
  1539. if [ "${SELECTED_RELEASE_CHANNEL}" = "stable" ]; then
  1540. opts="${opts} --stable-channel"
  1541. fi
  1542. if [ "${NETDATA_REQUIRE_CLOUD}" -eq 1 ]; then
  1543. opts="${opts} --require-cloud"
  1544. elif [ "${NETDATA_DISABLE_CLOUD}" -eq 1 ]; then
  1545. opts="${opts} --disable-cloud"
  1546. fi
  1547. # shellcheck disable=SC2086
  1548. run_script ./netdata-installer.sh ${opts}
  1549. case $? in
  1550. 1)
  1551. if [ -n "${EXIT_REASON}" ]; then
  1552. fatal "netdata-installer.sh failed to run: ${EXIT_REASON}" "${EXIT_CODE}"
  1553. else
  1554. fatal "netdata-installer.sh failed to run: Encountered an unhandled error in the installer code." I0000
  1555. fi
  1556. ;;
  1557. 2) fatal "Insufficient RAM to install netdata." F0008 ;;
  1558. esac
  1559. }
  1560. try_build_install() {
  1561. set_tmpdir
  1562. if [ "${DRY_RUN}" -eq 1 ]; then
  1563. progress "Would attempt to install by building locally..."
  1564. else
  1565. progress "Attempting to install by building locally..."
  1566. fi
  1567. if ! install_local_build_dependencies; then
  1568. return 1
  1569. fi
  1570. set_source_archive_urls "${SELECTED_RELEASE_CHANNEL}"
  1571. if [ -n "${INSTALL_VERSION}" ]; then
  1572. if ! download "${NETDATA_SOURCE_ARCHIVE_URL}" "${tmpdir}/netdata-v${INSTALL_VERSION}.tar.gz"; then
  1573. fatal "Failed to download source tarball for local build. ${BADNET_MSG}." F000B
  1574. fi
  1575. elif ! download "${NETDATA_SOURCE_ARCHIVE_URL}" "${tmpdir}/netdata-latest.tar.gz"; then
  1576. fatal "Failed to download source tarball for local build. ${BADNET_MSG}." F000B
  1577. fi
  1578. if ! download "${NETDATA_SOURCE_ARCHIVE_CHECKSUM_URL}" "${tmpdir}/sha256sum.txt"; then
  1579. fatal "Failed to download checksums for source tarball verification. ${BADNET_MSG}." F000C
  1580. fi
  1581. if [ "${DRY_RUN}" -eq 1 ]; then
  1582. progress "Would validate SHA256 checksum of downloaded source archive."
  1583. else
  1584. if [ -z "${INSTALL_VERSION}" ]; then
  1585. # shellcheck disable=SC2086
  1586. if ! grep netdata-latest.tar.gz "${tmpdir}/sha256sum.txt" | safe_sha256sum -c - > /dev/null 2>&1; then
  1587. fatal "Tarball checksum validation failed. ${BADCACHE_MSG}." F0005
  1588. fi
  1589. fi
  1590. fi
  1591. if [ -n "${INSTALL_VERSION}" ]; then
  1592. run tar -xf "${tmpdir}/netdata-v${INSTALL_VERSION}.tar.gz" -C "${tmpdir}"
  1593. rm -rf "${tmpdir}/netdata-v${INSTALL_VERSION}.tar.gz" > /dev/null 2>&1
  1594. else
  1595. run tar -xf "${tmpdir}/netdata-latest.tar.gz" -C "${tmpdir}"
  1596. rm -rf "${tmpdir}/netdata-latest.tar.gz" > /dev/null 2>&1
  1597. fi
  1598. if [ "${DRY_RUN}" -ne 1 ]; then
  1599. cd "$(find "${tmpdir}" -mindepth 1 -maxdepth 1 -type d -name netdata-)" || fatal "Cannot change directory to netdata source tree" F0006
  1600. fi
  1601. if [ -x netdata-installer.sh ] || [ "${DRY_RUN}" -eq 1 ]; then
  1602. build_and_install || return 1
  1603. else
  1604. # This case is needed because some platforms produce an extra directory on the source tarball extraction.
  1605. if [ "$(find . -mindepth 1 -maxdepth 1 -type d | wc -l)" -eq 1 ] && [ -x "$(find . -mindepth 1 -maxdepth 1 -type d)/netdata-installer.sh" ]; then
  1606. cd "$(find . -mindepth 1 -maxdepth 1 -type d)" && build_and_install || return 1
  1607. else
  1608. fatal "Cannot install netdata from source (the source directory does not include netdata-installer.sh)." F0009
  1609. fi
  1610. fi
  1611. }
  1612. # ======================================================================
  1613. # Offline install support code
  1614. prepare_offline_install_source() {
  1615. if [ -e "${1}" ]; then
  1616. if [ ! -d "${1}" ]; then
  1617. fatal "${1} is not a directory, unable to prepare offline install source." F0503
  1618. fi
  1619. else
  1620. run mkdir -p "${1}" || fatal "Unable to create target directory for offline install preparation." F0504
  1621. fi
  1622. run cd "${1}" || fatal "Failed to switch to target directory for offline install preparation." F0505
  1623. case "${NETDATA_FORCE_METHOD}" in
  1624. static|'')
  1625. set_static_archive_urls "${SELECTED_RELEASE_CHANNEL}" "x86_64"
  1626. if check_for_remote_file "${NETDATA_STATIC_ARCHIVE_URL}"; then
  1627. for arch in ${STATIC_INSTALL_ARCHES}; do
  1628. set_static_archive_urls "${SELECTED_RELEASE_CHANNEL}" "${arch}"
  1629. progress "Fetching ${NETDATA_STATIC_ARCHIVE_URL}"
  1630. if ! download "${NETDATA_STATIC_ARCHIVE_URL}" "netdata-${arch}-latest.gz.run"; then
  1631. warning "Failed to download static installer archive for ${arch}. ${BADNET_MSG}."
  1632. fi
  1633. done
  1634. legacy=0
  1635. else
  1636. 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."
  1637. progress "Fetching ${NETDATA_STATIC_ARCHIVE_OLD_URL}"
  1638. legacy=1
  1639. if ! download "${NETDATA_STATIC_ARCHIVE_OLD_URL}" "netdata-x86_64-latest.gz.run"; then
  1640. warning "Failed to download static installer archive for x86_64. ${BADNET_MSG}."
  1641. fi
  1642. fi
  1643. progress "Fetching ${NETDATA_STATIC_ARCHIVE_CHECKSUM_URL}"
  1644. if ! download "${NETDATA_STATIC_ARCHIVE_CHECKSUM_URL}" "sha256sums.txt"; then
  1645. fatal "Failed to download checksum file. ${BADNET_MSG}." F0506
  1646. fi
  1647. ;;
  1648. esac
  1649. if [ "${legacy:-0}" -eq 1 ]; then
  1650. sed -e 's/netdata-latest.gz.run/netdata-x86_64-latest.gz.run' sha256sums.txt > sha256sums.tmp
  1651. mv sha256sums.tmp sha256sums.txt
  1652. fi
  1653. if [ "${DRY_RUN}" -ne 1 ]; then
  1654. progress "Verifying checksums."
  1655. if ! grep -e "$(find . -name '*.gz.run')" sha256sums.txt | safe_sha256sum -c -; then
  1656. fatal "Checksums for offline install files are incorrect. ${BADCACHE_MSG}." F0507
  1657. fi
  1658. else
  1659. progress "Would verify SHA256 checksums of downloaded installation files."
  1660. fi
  1661. if [ "${DRY_RUN}" -ne 1 ]; then
  1662. progress "Preparing install script."
  1663. cat > "install.sh" <<-EOF
  1664. #!/bin/sh
  1665. dir=\$(CDPATH= cd -- "\$(dirname -- "\$0")" && pwd)
  1666. "\${dir}/kickstart.sh" --offline-install-source "\${dir}" \${@}
  1667. EOF
  1668. chmod +x "install.sh"
  1669. else
  1670. progress "Would create install script"
  1671. fi
  1672. if [ "${DRY_RUN}" -ne 1 ]; then
  1673. progress "Copying kickstart script."
  1674. cp "${KICKSTART_SOURCE}" "kickstart.sh"
  1675. chmod +x "kickstart.sh"
  1676. else
  1677. progress "Would copy kickstart.sh to offline install source directory"
  1678. fi
  1679. if [ "${DRY_RUN}" -ne 1 ]; then
  1680. progress "Saving release channel information."
  1681. echo "${SELECTED_RELEASE_CHANNEL}" > "channel"
  1682. else
  1683. progress "Would save release channel information to offline install source directory"
  1684. fi
  1685. 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."
  1686. }
  1687. # ======================================================================
  1688. # Per system-type install logic
  1689. install_on_linux() {
  1690. if [ "${NETDATA_FORCE_METHOD}" != 'static' ] && [ "${NETDATA_FORCE_METHOD}" != 'build' ] && [ -z "${NETDATA_OFFLINE_INSTALL_SOURCE}" ]; then
  1691. SELECTED_INSTALL_METHOD="native"
  1692. try_package_install
  1693. case "$?" in
  1694. 0)
  1695. NETDATA_INSTALL_SUCCESSFUL=1
  1696. INSTALL_PREFIX="/"
  1697. ;;
  1698. 1) fatal "Unable to install on this system." F0300 ;;
  1699. 2)
  1700. case "${NETDATA_FORCE_METHOD}" in
  1701. native) fatal "Could not install native binary packages." F0301 ;;
  1702. *) warning "Could not install native binary packages, falling back to alternative installation method." ;;
  1703. esac
  1704. ;;
  1705. esac
  1706. fi
  1707. if [ "${NETDATA_FORCE_METHOD}" != 'native' ] && [ "${NETDATA_FORCE_METHOD}" != 'build' ] && [ -z "${NETDATA_INSTALL_SUCCESSFUL}" ]; then
  1708. SELECTED_INSTALL_METHOD="static"
  1709. INSTALL_TYPE="kickstart-static"
  1710. try_static_install
  1711. case "$?" in
  1712. 0)
  1713. NETDATA_INSTALL_SUCCESSFUL=1
  1714. INSTALL_PREFIX="/opt/netdata"
  1715. ;;
  1716. 1) fatal "Unable to install on this system." F0302 ;;
  1717. 2)
  1718. case "${NETDATA_FORCE_METHOD}" in
  1719. static) fatal "Could not install static build." F0303 ;;
  1720. *) warning "Could not install static build, falling back to alternative installation method." ;;
  1721. esac
  1722. ;;
  1723. esac
  1724. fi
  1725. if [ "${NETDATA_FORCE_METHOD}" != 'native' ] && [ "${NETDATA_FORCE_METHOD}" != 'static' ] && [ -z "${NETDATA_INSTALL_SUCCESSFUL}" ]; then
  1726. SELECTED_INSTALL_METHOD="build"
  1727. INSTALL_TYPE="kickstart-build"
  1728. try_build_install
  1729. case "$?" in
  1730. 0) NETDATA_INSTALL_SUCCESSFUL=1 ;;
  1731. *) fatal "Unable to install on this system." F0304 ;;
  1732. esac
  1733. fi
  1734. }
  1735. install_on_macos() {
  1736. case "${NETDATA_FORCE_METHOD}" in
  1737. native) fatal "User requested native package, but native packages are not available for macOS. Try installing without \`--only-native\` option." F0305 ;;
  1738. static) fatal "User requested static build, but static builds are not available for macOS. Try installing without \`--only-static\` option." F0306 ;;
  1739. *)
  1740. SELECTED_INSTALL_METHOD="build"
  1741. INSTALL_TYPE="kickstart-build"
  1742. try_build_install
  1743. case "$?" in
  1744. 0) NETDATA_INSTALL_SUCCESSFUL=1 ;;
  1745. *) fatal "Unable to install on this system." F0307 ;;
  1746. esac
  1747. ;;
  1748. esac
  1749. }
  1750. install_on_freebsd() {
  1751. case "${NETDATA_FORCE_METHOD}" in
  1752. native) fatal "User requested native package, but native packages are not available for FreeBSD. Try installing without \`--only-native\` option." F0308 ;;
  1753. static) fatal "User requested static build, but static builds are not available for FreeBSD. Try installing without \`--only-static\` option." F0309 ;;
  1754. *)
  1755. SELECTED_INSTALL_METHOD="build"
  1756. INSTALL_TYPE="kickstart-build"
  1757. try_build_install
  1758. case "$?" in
  1759. 0) NETDATA_INSTALL_SUCCESSFUL=1 ;;
  1760. *) fatal "Unable to install on this system." F030A ;;
  1761. esac
  1762. ;;
  1763. esac
  1764. }
  1765. # ======================================================================
  1766. # Argument parsing code
  1767. validate_args() {
  1768. check_claim_opts
  1769. if [ -n "${NETDATA_FORCE_METHOD}" ]; then
  1770. SELECTED_INSTALL_METHOD="${NETDATA_FORCE_METHOD}"
  1771. fi
  1772. if [ "${ACTION}" = "repositories-only" ] && [ "${NETDATA_FORCE_METHOD}" != "native" ]; then
  1773. fatal "Repositories can only be installed for native installs." F050D
  1774. fi
  1775. if [ -n "${NETDATA_OFFLINE_INSTALL_SOURCE}" ]; then
  1776. case "${NETDATA_FORCE_METHOD}" in
  1777. native|build) fatal "Offline installs are only supported for static builds currently." F0502 ;;
  1778. esac
  1779. fi
  1780. if [ -n "${LOCAL_BUILD_OPTIONS}" ]; then
  1781. case "${NETDATA_FORCE_METHOD}" in
  1782. build) NETDATA_INSTALLER_OPTIONS="${NETDATA_INSTALLER_OPTIONS} ${LOCAL_BUILD_OPTIONS}" ;;
  1783. *) fatal "Specifying local build options is only supported when the --build-only option is also specified." F0401 ;;
  1784. esac
  1785. fi
  1786. if [ -n "${STATIC_INSTALL_OPTIONS}" ]; then
  1787. case "${NETDATA_FORCE_METHOD}" in
  1788. static) NETDATA_INSTALLER_OPTIONS="${NETDATA_INSTALLER_OPTIONS} ${STATIC_INSTALL_OPTIONS}" ;;
  1789. *) fatal "Specifying installer options options is only supported when the --static-only option is also specified." F0402 ;;
  1790. esac
  1791. fi
  1792. if [ -n "${NETDATA_OFFLINE_INSTALL_SOURCE}" ] && [ -n "${INSTALL_VERSION}" ]; then
  1793. fatal "Specifying an install version alongside an offline install source is not supported." F050A
  1794. fi
  1795. if [ "${NETDATA_AUTO_UPDATES}" = "default" ]; then
  1796. if [ -n "${NETDATA_OFFLINE_INSTALL_SOURCE}" ] || [ -n "${INSTALL_VERSION}" ]; then
  1797. AUTO_UPDATE=0
  1798. else
  1799. AUTO_UPDATE=1
  1800. fi
  1801. elif [ "${NETDATA_AUTO_UPDATES}" = 1 ]; then
  1802. AUTO_UPDATE=1
  1803. else
  1804. AUTO_UPDATE=0
  1805. fi
  1806. if [ "${RELEASE_CHANNEL}" = "default" ]; then
  1807. if [ -n "${NETDATA_OFFLINE_INSTALL_SOURCE}" ]; then
  1808. SELECTED_RELEASE_CHANNEL="$(cat "${NETDATA_OFFLINE_INSTALL_SOURCE}/channel")"
  1809. if [ -z "${SELECTED_RELEASE_CHANNEL}" ]; then
  1810. fatal "Could not find a release channel indicator in ${NETDATA_OFFLINE_INSTALL_SOURCE}." F0508
  1811. fi
  1812. else
  1813. SELECTED_RELEASE_CHANNEL="${DEFAULT_RELEASE_CHANNEL}"
  1814. fi
  1815. else
  1816. if [ -n "${NETDATA_OFFLINE_INSTALL_SOURCE}" ] && [ "${RELEASE_CHANNEL}" != "$(cat "${NETDATA_OFFLINE_INSTALL_SOURCE}/channel")" ]; then
  1817. fatal "Release channal '${RELEASE_CHANNEL}' requested, but indicated offline installation source release channel is '$(cat "${NETDATA_OFFLINE_INSTALL_SOURCE}/channel")'." F0509
  1818. fi
  1819. SELECTED_RELEASE_CHANNEL="${RELEASE_CHANNEL}"
  1820. fi
  1821. }
  1822. set_action() {
  1823. new_action="${1}"
  1824. if [ -n "${ACTION}" ]; then
  1825. warning "Ignoring previously specified '${ACTION}' operation in favor of '${new_action}' specified later on the command line."
  1826. fi
  1827. ACTION="${new_action}"
  1828. NETDATA_COMMAND="${new_action}"
  1829. }
  1830. parse_args() {
  1831. while [ -n "${1}" ]; do
  1832. case "${1}" in
  1833. "--help")
  1834. usage
  1835. cleanup
  1836. trap - EXIT
  1837. exit 0
  1838. ;;
  1839. "--no-cleanup") NO_CLEANUP=1 ;;
  1840. "--dont-wait"|"--non-interactive") INTERACTIVE=0 ;;
  1841. "--interactive") INTERACTIVE=1 ;;
  1842. "--dry-run") DRY_RUN=1 ;;
  1843. "--release-channel")
  1844. RELEASE_CHANNEL="$(echo "${2}" | tr '[:upper:]' '[:lower:]')"
  1845. case "${RELEASE_CHANNEL}" in
  1846. nightly|stable|default) shift 1 ;;
  1847. *)
  1848. echo "Unrecognized value for --release-channel. Valid release channels are: stable, nightly, default"
  1849. exit 1
  1850. ;;
  1851. esac
  1852. ;;
  1853. "--stable-channel") RELEASE_CHANNEL="stable" ;;
  1854. "--nightly-channel") RELEASE_CHANNEL="nightly" ;;
  1855. "--reinstall") set_action 'reinstall' ;;
  1856. "--reinstall-even-if-unsafe") set_action 'unsafe-reinstall' ;;
  1857. "--reinstall-clean") set_action 'reinstall-clean' ;;
  1858. "--uninstall") set_action 'uninstall' ;;
  1859. "--claim-only") set_action 'claim' ;;
  1860. "--no-updates") NETDATA_AUTO_UPDATES=0 ;;
  1861. "--auto-update") NETDATA_AUTO_UPDATES="1" ;;
  1862. "--auto-update-method")
  1863. NETDATA_AUTO_UPDATE_TYPE="$(echo "${2}" | tr '[:upper:]' '[:lower:]')"
  1864. case "${NETDATA_AUTO_UPDATE_TYPE}" in
  1865. systemd|interval|crontab) shift 1 ;;
  1866. *)
  1867. echo "Unrecognized value for --auto-update-type. Valid values are: systemd, interval, crontab"
  1868. exit 1
  1869. ;;
  1870. esac
  1871. ;;
  1872. "--disable-cloud")
  1873. NETDATA_DISABLE_CLOUD=1
  1874. NETDATA_REQUIRE_CLOUD=0
  1875. ;;
  1876. "--require-cloud")
  1877. NETDATA_DISABLE_CLOUD=0
  1878. NETDATA_REQUIRE_CLOUD=1
  1879. ;;
  1880. "--dont-start-it")
  1881. NETDATA_NO_START=1
  1882. NETDATA_INSTALLER_OPTIONS="${NETDATA_INSTALLER_OPTIONS} --dont-start-it"
  1883. ;;
  1884. "--disable-telemetry")
  1885. NETDATA_DISABLE_TELEMETRY="1"
  1886. NETDATA_INSTALLER_OPTIONS="${NETDATA_INSTALLER_OPTIONS} --disable-telemetry"
  1887. ;;
  1888. "--install-prefix")
  1889. INSTALL_PREFIX="${2}"
  1890. shift 1
  1891. ;;
  1892. "--old-install-prefix")
  1893. OLD_INSTALL_PREFIX="${2}"
  1894. shift 1
  1895. ;;
  1896. "--install-version")
  1897. INSTALL_VERSION="${2}"
  1898. AUTO_UPDATE=0
  1899. shift 1
  1900. ;;
  1901. "--distro-override")
  1902. if [ -n "${2}" ]; then
  1903. SKIP_DISTRO_DETECTION=1
  1904. DISTRO="$(echo "${2}" | cut -f 1 -d ':' | tr '[:upper:]' '[:lower:]')"
  1905. SYSVERSION="$(echo "${2}" | cut -f 2 -d ':')"
  1906. SYSCODENAME="$(echo "${2}" | cut -f 3 -d ':' | tr '[:upper:]' '[:lower:]')"
  1907. if [ -z "${SYSVERSION}" ]; then
  1908. fatal "You must specify a release as well as a distribution name." F0510
  1909. fi
  1910. shift 1
  1911. else
  1912. fatal "A distribution name and release must be specified for the --distro-override option." F050F
  1913. fi
  1914. ;;
  1915. "--repositories-only")
  1916. set_action 'repositories-only'
  1917. NETDATA_FORCE_METHOD="native"
  1918. ;;
  1919. "--native-only") NETDATA_FORCE_METHOD="native" ;;
  1920. "--static-only") NETDATA_FORCE_METHOD="static" ;;
  1921. "--build-only") NETDATA_FORCE_METHOD="build" ;;
  1922. "--claim-token")
  1923. NETDATA_CLAIM_TOKEN="${2}"
  1924. shift 1
  1925. ;;
  1926. "--claim-rooms")
  1927. NETDATA_CLAIM_ROOMS="${2}"
  1928. shift 1
  1929. ;;
  1930. "--claim-url")
  1931. NETDATA_CLAIM_URL="${2}"
  1932. shift 1
  1933. ;;
  1934. "--claim-"*)
  1935. optname="$(echo "${1}" | cut -d '-' -f 4-)"
  1936. case "${optname}" in
  1937. id|proxy|user|hostname)
  1938. NETDATA_CLAIM_EXTRA="${NETDATA_CLAIM_EXTRA} -${optname}=${2}"
  1939. shift 1
  1940. ;;
  1941. verbose|insecure|noproxy|noreload|daemon-not-running) NETDATA_CLAIM_EXTRA="${NETDATA_CLAIM_EXTRA} -${optname}" ;;
  1942. *) warning "Ignoring unrecognized claiming option ${optname}" ;;
  1943. esac
  1944. ;;
  1945. "--local-build-options")
  1946. LOCAL_BUILD_OPTIONS="${LOCAL_BUILD_OPTIONS} ${2}"
  1947. shift 1
  1948. ;;
  1949. "--static-install-options")
  1950. STATIC_INSTALL_OPTIONS="${STATIC_INSTALL_OPTIONS} ${2}"
  1951. shift 1
  1952. ;;
  1953. "--prepare-offline-install-source")
  1954. if [ -n "${2}" ]; then
  1955. set_action 'prepare-offline'
  1956. OFFLINE_TARGET="${2}"
  1957. shift 1
  1958. else
  1959. fatal "A target directory must be specified with the --prepare-offline-install-source option." F0500
  1960. fi
  1961. ;;
  1962. "--offline-install-source")
  1963. if [ -d "${2}" ]; then
  1964. NETDATA_OFFLINE_INSTALL_SOURCE="${2}"
  1965. shift 1
  1966. else
  1967. fatal "A source directory must be specified with the --offline-install-source option." F0501
  1968. fi
  1969. ;;
  1970. "--"|"all"|"--yes"|"-y"|"--force"|"--accept") warning "Option '${1}' is not recognized, ignoring it. ${BADOPT_MSG}" ;;
  1971. *) fatal "Unrecognized option '${1}'. ${BADOPT_MSG}" F050E ;;
  1972. esac
  1973. shift 1
  1974. done
  1975. validate_args
  1976. }
  1977. # ======================================================================
  1978. # Main program
  1979. setup_terminal || echo > /dev/null
  1980. # shellcheck disable=SC2068
  1981. parse_args $@
  1982. confirm_root_support
  1983. get_system_info
  1984. confirm_install_prefix
  1985. if [ -z "${ACTION}" ]; then
  1986. handle_existing_install
  1987. fi
  1988. main