kickstart.sh 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793
  1. #!/bin/sh
  2. #
  3. # SPDX-License-Identifier: GPL-3.0-or-later
  4. # ======================================================================
  5. # Constants
  6. AGENT_BUG_REPORT_URL="https://github.com/netdata/netdata/issues/new/choose"
  7. CLOUD_BUG_REPORT_URL="https://github.com/netdata/netdata-cloud/issues/new/choose"
  8. DISCUSSIONS_URL="https://github.com/netdata/netdata/discussions"
  9. DISCORD_INVITE="https://discord.gg/5ygS846fR6"
  10. DOCS_URL="https://learn.netdata.cloud/docs/"
  11. FORUM_URL="https://community.netdata.cloud/"
  12. KICKSTART_OPTIONS="${*}"
  13. PACKAGES_SCRIPT="https://raw.githubusercontent.com/netdata/netdata/master/packaging/installer/install-required-packages.sh"
  14. PATH="${PATH}:/usr/local/bin:/usr/local/sbin"
  15. PUBLIC_CLOUD_URL="https://app.netdata.cloud"
  16. REPOCONFIG_URL_PREFIX="https://packagecloud.io/netdata/netdata-repoconfig/packages"
  17. REPOCONFIG_VERSION="1-1"
  18. TELEMETRY_URL="https://posthog.netdata.cloud/capture/"
  19. START_TIME="$(date +%s)"
  20. # ======================================================================
  21. # Defaults for environment variables
  22. DRY_RUN=0
  23. SELECTED_INSTALL_METHOD="none"
  24. INSTALL_TYPE="unknown"
  25. INSTALL_PREFIX=""
  26. NETDATA_AUTO_UPDATES="1"
  27. NETDATA_CLAIM_ONLY=0
  28. NETDATA_CLAIM_URL="${PUBLIC_CLOUD_URL}"
  29. NETDATA_DISABLE_CLOUD=0
  30. NETDATA_ONLY_BUILD=0
  31. NETDATA_ONLY_NATIVE=0
  32. NETDATA_ONLY_STATIC=0
  33. NETDATA_REQUIRE_CLOUD=1
  34. RELEASE_CHANNEL="nightly"
  35. WARNINGS=""
  36. if [ -n "$DISABLE_TELEMETRY" ]; then
  37. NETDATA_DISABLE_TELEMETRY="${DISABLE_TELEMETRY}"
  38. elif [ -n "$DO_NOT_TRACK" ]; then
  39. NETDATA_DISABLE_TELEMETRY="${DO_NOT_TRACK}"
  40. else
  41. NETDATA_DISABLE_TELEMETRY=0
  42. fi
  43. NETDATA_TARBALL_BASEURL="${NETDATA_TARBALL_BASEURL:-https://storage.googleapis.com/netdata-nightlies}"
  44. NETDATA_INSTALLER_OPTIONS="${NETDATA_INSTALLER_OPTIONS:-""}"
  45. TELEMETRY_API_KEY="${NETDATA_POSTHOG_API_KEY:-mqkwGT0JNFqO-zX2t0mW6Tec9yooaVu7xCBlXtHnt5Y}"
  46. if echo "${0}" | grep -q 'kickstart-static64'; then
  47. NETDATA_ONLY_STATIC=1
  48. fi
  49. if [ ! -t 1 ]; then
  50. INTERACTIVE=0
  51. else
  52. INTERACTIVE=1
  53. fi
  54. main() {
  55. if [ "${ACTION}" = "uninstall" ]; then
  56. uninstall
  57. printf >&2 "Finished uninstalling the Netdata Agent."
  58. deferred_warnings
  59. cleanup
  60. trap - EXIT
  61. exit 0
  62. fi
  63. if [ "${ACTION}" = "reinstall-clean" ]; then
  64. NEW_INSTALL_PREFIX="${INSTALL_PREFIX}"
  65. uninstall
  66. cleanup
  67. ACTION=
  68. INSTALL_PREFIX="${NEW_INSTALL_PREFIX}"
  69. # shellcheck disable=SC2086
  70. main
  71. trap - EXIT
  72. exit 0
  73. fi
  74. tmpdir="$(create_tmp_directory)"
  75. progress "Using ${tmpdir} as a temporary directory."
  76. cd "${tmpdir}" || fatal "Failed to change current working directory to ${tmpdir}." F000A
  77. case "${SYSTYPE}" in
  78. Linux) install_on_linux ;;
  79. Darwin) install_on_macos ;;
  80. FreeBSD) install_on_freebsd ;;
  81. esac
  82. if [ -n "${NETDATA_CLAIM_TOKEN}" ]; then
  83. claim
  84. elif [ "${NETDATA_DISABLE_CLOUD}" -eq 1 ]; then
  85. soft_disable_cloud
  86. fi
  87. set_auto_updates
  88. printf >&2 "%s\n\n" "Successfully installed the Netdata Agent."
  89. deferred_warnings
  90. success_banner
  91. telemetry_event INSTALL_SUCCESS "" ""
  92. cleanup
  93. trap - EXIT
  94. }
  95. # ======================================================================
  96. # Usage info
  97. usage() {
  98. cat << HEREDOC
  99. USAGE: kickstart.sh [options]
  100. where options include:
  101. --non-interactive Do not prompt for user input. (default: prompt if there is a controlling terminal)
  102. --interactive Prompt for user input even if there is no controlling terminal.
  103. --dont-start-it Do not start the agent by default (only for static installs or local builds)
  104. --dry-run Report what we would do with the given options on this system, but don’t actually do anything.
  105. --stable-channel Install a stable version instead of a nightly build (default: install a nightly build)
  106. --nightly-channel Install a nightly build instead of a stable version
  107. --no-updates Do not enable automatic updates (default: enable automatic updates using the best supported scheduling method)
  108. --auto-update Enable automatic updates.
  109. --auto-update-type Specify a particular scheduling type for auto-updates (valid types: systemd, interval, crontab)
  110. --disable-telemetry Opt-out of anonymous statistics.
  111. --native-only Only install if native binary packages are available.
  112. --static-only Only install if a static build is available.
  113. --build-only Only install using a local build.
  114. --reinstall Explicitly reinstall instead of updating any existing install.
  115. --reinstall-even-if-unsafe Even try to reinstall if we don't think we can do so safely (implies --reinstall).
  116. --disable-cloud Disable support for Netdata Cloud (default: detect)
  117. --require-cloud Only install if Netdata Cloud can be enabled. Overrides --disable-cloud.
  118. --install <path> Specify an installation prefix for local builds (default: autodetect based on system type).
  119. --old-install-prefix <path> Specify an old local builds installation prefix for uninstall/reinstall (if it's not default).
  120. --claim-token Use a specified token for claiming to Netdata Cloud.
  121. --claim-rooms When claiming, add the node to the specified rooms.
  122. --claim-only If there is an existing install, only try to claim it, not update it.
  123. --claim-* Specify other options for the claiming script.
  124. --no-cleanup Don't do any cleanup steps. This is intended to help with debugging the installer.
  125. --uninstall Uninstall an existing installation of Netdata.
  126. --reinstall-clean Clean reinstall Netdata.
  127. Additionally, this script may use the following environment variables:
  128. TMPDIR: Used to specify where to put temporary files. On most systems, the default we select
  129. automatically should be fine. The user running the script needs to both be able to
  130. write files to the temporary directory, and run files from that location.
  131. ROOTCMD: Used to specify a command to use to run another command with root privileges if needed. By
  132. default we try to use sudo, doas, or pkexec (in that order of preference), but if
  133. you need special options for one of those to work, or have a different tool to do
  134. the same thing on your system, you can specify it here.
  135. DISABLE_TELEMETRY If set to a value other than 0, behave as if \`--disable-telemetry\` was specified.
  136. NETDATA_INSTALLER_OPTIONS: Specifies extra options to pass to the static installer or local build script.
  137. HEREDOC
  138. }
  139. # ======================================================================
  140. # Telemetry functions
  141. telemetry_event() {
  142. if [ "${NETDATA_DISABLE_TELEMETRY}" -eq 1 ] || [ "${DRY_RUN}" -eq 1 ]; then
  143. return 0
  144. fi
  145. now="$(date +%s)"
  146. total_duration="$((now - START_TIME))"
  147. if [ -e "/etc/os-release" ]; then
  148. eval "$(grep -E "^(NAME|ID|ID_LIKE|VERSION|VERSION_ID)=" < /etc/os-release | sed 's/^/HOST_/')"
  149. fi
  150. if [ -z "${HOST_NAME}" ] || [ -z "${HOST_VERSION}" ] || [ -z "${HOST_ID}" ]; then
  151. if [ -f "/etc/lsb-release" ]; then
  152. DISTRIB_ID="unknown"
  153. DISTRIB_RELEASE="unknown"
  154. DISTRIB_CODENAME="unknown"
  155. eval "$(grep -E "^(DISTRIB_ID|DISTRIB_RELEASE|DISTRIB_CODENAME)=" < /etc/lsb-release)"
  156. if [ -z "${HOST_NAME}" ]; then HOST_NAME="${DISTRIB_ID}"; fi
  157. if [ -z "${HOST_VERSION}" ]; then HOST_VERSION="${DISTRIB_RELEASE}"; fi
  158. if [ -z "${HOST_ID}" ]; then HOST_ID="${DISTRIB_CODENAME}"; fi
  159. fi
  160. fi
  161. KERNEL_NAME="$(uname -s)"
  162. if [ "${KERNEL_NAME}" = FreeBSD ]; then
  163. TOTAL_RAM="$(sysctl -n hw.physmem)"
  164. elif [ "${KERNEL_NAME}" = Darwin ]; then
  165. TOTAL_RAM="$(sysctl -n hw.memsize)"
  166. elif [ -r /proc/meminfo ]; then
  167. TOTAL_RAM="$(grep -F MemTotal /proc/meminfo | cut -f 2 -d ':' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' | cut -f 1 -d ' ')"
  168. TOTAL_RAM="$((TOTAL_RAM * 1024))"
  169. fi
  170. if [ -f /etc/machine-id ]; then
  171. DISTINCT_ID="$(cat /etc/machine-id)"
  172. elif command -v uuidgen > /dev/null 2>&1; then
  173. DISTINCT_ID="$(uuidgen | tr '[:upper:]' '[:lower:]')"
  174. else
  175. DISTINCT_ID="null"
  176. fi
  177. REQ_BODY="$(cat << EOF
  178. {
  179. "api_key": "${TELEMETRY_API_KEY}",
  180. "event": "${1}",
  181. "properties": {
  182. "distinct_id": "${DISTINCT_ID}",
  183. "event_source": "agent installer",
  184. "\$current_url": "agent installer",
  185. "\$pathname": "netdata-installer",
  186. "\$host": "installer.netdata.io",
  187. "\$ip": "127.0.0.1",
  188. "script_variant": "kickstart-ng",
  189. "error_code": "${3}",
  190. "error_message": "${2}",
  191. "install_options": "${KICKSTART_OPTIONS}",
  192. "install_interactivity": "${INTERACTIVE}",
  193. "total_runtime": "${total_duration}",
  194. "selected_install_method": "${SELECTED_INSTALL_METHOD}",
  195. "netdata_release_channel": "${RELEASE_CHANNEL:-null}",
  196. "netdata_install_type": "${INSTALL_TYPE}",
  197. "host_os_name": "${HOST_NAME:-unknown}",
  198. "host_os_id": "${HOST_ID:-unknown}",
  199. "host_os_id_like": "${HOST_ID_LIKE:-unknown}",
  200. "host_os_version": "${HOST_VERSION:-unknown}",
  201. "host_os_version_id": "${HOST_VERSION_ID:-unknown}",
  202. "system_kernel_name": "${KERNEL_NAME}",
  203. "system_kernel_version": "$(uname -r)",
  204. "system_architecture": "$(uname -m)",
  205. "system_total_ram": "${TOTAL_RAM:-unknown}"
  206. }
  207. }
  208. EOF
  209. )"
  210. if command -v curl > /dev/null 2>&1; then
  211. curl --silent -o /dev/null -X POST --max-time 2 --header "Content-Type: application/json" -d "${REQ_BODY}" "${TELEMETRY_URL}" > /dev/null
  212. elif command -v wget > /dev/null 2>&1; then
  213. wget -q -O - --no-check-certificate \
  214. --method POST \
  215. --timeout=1 \
  216. --header 'Content-Type: application/json' \
  217. --body-data "${REQ_BODY}" \
  218. "${TELEMETRY_URL}" > /dev/null
  219. fi
  220. }
  221. trap_handler() {
  222. code="${1}"
  223. lineno="${2}"
  224. deferred_warnings
  225. printf >&2 "%s\n\n" "${TPUT_BGRED}${TPUT_WHITE}${TPUT_BOLD} ERROR ${TPUT_RESET} Installer exited unexpectedly (${code}-${lineno})"
  226. case "${code}" in
  227. 0)
  228. 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}."
  229. ;;
  230. *)
  231. printf >&2 "%s\n" "This is probably a result of a transient issue on your system. Things should work correctly if you try again."
  232. printf >&2 "%s\n" "If you continue to experience this issue, you can reacn out to us for support on:"
  233. support_list
  234. ;;
  235. esac
  236. telemetry_event INSTALL_CRASH "Installer exited unexpectedly (${code}-${lineno})" "E${code}-${lineno}"
  237. trap - EXIT
  238. cleanup
  239. exit 1
  240. }
  241. trap 'trap_handler 0 ${LINENO}' EXIT
  242. trap 'trap_handler 1 0' HUP
  243. trap 'trap_handler 2 0' INT
  244. trap 'trap_handler 3 0' QUIT
  245. trap 'trap_handler 13 0' PIPE
  246. trap 'trap_handler 15 0' TERM
  247. # ======================================================================
  248. # Utility functions
  249. setup_terminal() {
  250. TPUT_RESET=""
  251. TPUT_WHITE=""
  252. TPUT_BGRED=""
  253. TPUT_BGGREEN=""
  254. TPUT_BOLD=""
  255. TPUT_DIM=""
  256. # Is stderr on the terminal? If not, then fail
  257. test -t 2 || return 1
  258. if command -v tput > /dev/null 2>&1; then
  259. if [ $(($(tput colors 2> /dev/null))) -ge 8 ]; then
  260. # Enable colors
  261. TPUT_RESET="$(tput sgr 0)"
  262. TPUT_WHITE="$(tput setaf 7)"
  263. TPUT_BGRED="$(tput setab 1)"
  264. TPUT_BGGREEN="$(tput setab 2)"
  265. TPUT_BOLD="$(tput bold)"
  266. TPUT_DIM="$(tput dim)"
  267. fi
  268. fi
  269. echo "${TPUT_RESET}"
  270. return 0
  271. }
  272. support_list() {
  273. printf >&2 "%s\n" " - GitHub: ${DISCUSSIONS_URL}"
  274. printf >&2 "%s\n" " - Discord: ${DISCORD_INVITE}"
  275. printf >&2 "%s\n" " - Our community forums: ${FORUM_URL}"
  276. }
  277. success_banner() {
  278. printf >&2 "%s\n\n" "Official documentation can be found online at ${DOCS_URL}."
  279. if [ -z "${CLAIM_TOKEN}" ]; then
  280. printf >&2 "%s\n\n" "Looking to monitor all of your infrastructure with Netdata? Check out Netdata Cloud at ${PUBLIC_CLOUD_URL}."
  281. fi
  282. printf >&2 "%s\n" "Join our community and connect with us on:"
  283. support_list
  284. }
  285. cleanup() {
  286. if [ -z "${NO_CLEANUP}" ]; then
  287. ${ROOTCMD} rm -rf "${tmpdir}"
  288. fi
  289. }
  290. deferred_warnings() {
  291. if [ -n "${WARNINGS}" ]; then
  292. printf >&2 "%s\n" "The following non-fatal warnings or errors were encountered:"
  293. echo >&2 "${WARNINGS}"
  294. printf >&2 "\n"
  295. fi
  296. }
  297. fatal() {
  298. deferred_warnings
  299. printf >&2 "%s\n\n" "${TPUT_BGRED}${TPUT_WHITE}${TPUT_BOLD} ABORTED ${TPUT_RESET} ${1}"
  300. printf >&2 "%s\n" "For community support, you can connect with us on:"
  301. support_list
  302. telemetry_event "INSTALL_FAILED" "${1}" "${2}"
  303. cleanup
  304. trap - EXIT
  305. exit 1
  306. }
  307. ESCAPED_PRINT_METHOD=
  308. # shellcheck disable=SC3050
  309. if printf "%s " test > /dev/null 2>&1; then
  310. ESCAPED_PRINT_METHOD="printfq"
  311. fi
  312. escaped_print() {
  313. if [ "${ESCAPED_PRINT_METHOD}" = "printfq" ]; then
  314. # shellcheck disable=SC3050
  315. printf "%s " "${@}"
  316. else
  317. printf "%s" "${*}"
  318. fi
  319. return 0
  320. }
  321. progress() {
  322. echo >&2 " --- ${TPUT_BOLD}${*}${TPUT_RESET} --- "
  323. }
  324. run_logfile="/dev/null"
  325. run() {
  326. user="${USER--}"
  327. dir="${PWD}"
  328. if [ "$(id -u)" = "0" ]; then
  329. info="[root ${dir}]# "
  330. info_console="[${TPUT_DIM}${dir}${TPUT_RESET}]# "
  331. else
  332. info="[${user} ${dir}]$ "
  333. info_console="[${TPUT_DIM}${dir}${TPUT_RESET}]$ "
  334. fi
  335. if [ "${DRY_RUN}" -eq 1 ]; then
  336. printf >&2 "%s" "Would run command:\n"
  337. fi
  338. {
  339. printf "%s" "${info}"
  340. escaped_print "${@}"
  341. printf " ... "
  342. } >> "${run_logfile}"
  343. printf >&2 "%s" "${info_console}${TPUT_BOLD}"
  344. escaped_print >&2 "${@}"
  345. printf >&2 "%s\n" "${TPUT_RESET}"
  346. if [ "${DRY_RUN}" -ne 1 ]; then
  347. "${@}"
  348. ret=$?
  349. else
  350. ret=0
  351. fi
  352. if [ ${ret} -ne 0 ]; then
  353. printf >&2 "%s\n\n" "${TPUT_BGRED}${TPUT_WHITE}${TPUT_BOLD} FAILED ${TPUT_RESET}"
  354. printf "%s\n" "FAILED with exit code ${ret}" >> "${run_logfile}"
  355. WARNINGS="${WARNINGS}\n - Command \"${*}\" failed with exit code ${ret}."
  356. else
  357. printf >&2 "%s\n\n" "${TPUT_BGGREEN}${TPUT_WHITE}${TPUT_BOLD} OK ${TPUT_RESET}"
  358. printf "OK\n" >> "${run_logfile}"
  359. fi
  360. return ${ret}
  361. }
  362. warning() {
  363. printf >&2 "%s\n\n" "${TPUT_BGRED}${TPUT_WHITE}${TPUT_BOLD} WARNING ${TPUT_RESET} ${*}"
  364. WARNINGS="${WARNINGS}\n - ${*}"
  365. }
  366. _cannot_use_tmpdir() {
  367. testfile="$(TMPDIR="${1}" mktemp -q -t netdata-test.XXXXXXXXXX)"
  368. ret=0
  369. if [ -z "${testfile}" ]; then
  370. return "${ret}"
  371. fi
  372. if printf '#!/bin/sh\necho SUCCESS\n' > "${testfile}"; then
  373. if chmod +x "${testfile}"; then
  374. if [ "$("${testfile}")" = "SUCCESS" ]; then
  375. ret=1
  376. fi
  377. fi
  378. fi
  379. rm -f "${testfile}"
  380. return "${ret}"
  381. }
  382. create_tmp_directory() {
  383. if [ -z "${TMPDIR}" ] || _cannot_use_tmpdir "${TMPDIR}"; then
  384. if _cannot_use_tmpdir /tmp; then
  385. if _cannot_use_tmpdir "${PWD}"; then
  386. 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
  387. else
  388. TMPDIR="${PWD}"
  389. fi
  390. else
  391. TMPDIR="/tmp"
  392. fi
  393. fi
  394. mktemp -d -t netdata-kickstart-XXXXXXXXXX
  395. }
  396. check_for_remote_file() {
  397. url="${1}"
  398. if command -v curl > /dev/null 2>&1; then
  399. curl --output /dev/null --silent --head --fail "${url}" || return 1
  400. elif command -v wget > /dev/null 2>&1; then
  401. wget -S --spider "${url}" 2>&1 | grep -q 'HTTP/1.1 200 OK' || return 1
  402. else
  403. fatal "I need curl or wget to proceed, but neither of them are available on this system." F0003
  404. fi
  405. }
  406. download() {
  407. url="${1}"
  408. dest="${2}"
  409. if command -v curl > /dev/null 2>&1; then
  410. run curl --fail -q -sSL --connect-timeout 10 --retry 3 --output "${dest}" "${url}" || return 1
  411. elif command -v wget > /dev/null 2>&1; then
  412. run wget -T 15 -O "${dest}" "${url}" || return 1
  413. else
  414. fatal "I need curl or wget to proceed, but neither of them are available on this system." F0003
  415. fi
  416. }
  417. get_redirect() {
  418. url="${1}"
  419. if command -v curl > /dev/null 2>&1; then
  420. run sh -c "curl ${url} -s -L -I -o /dev/null -w '%{url_effective}' | grep -o '[^/]*$'" || return 1
  421. elif command -v wget > /dev/null 2>&1; then
  422. run sh -c "wget --max-redirect=0 ${url} 2>&1 | grep Location | cut -d ' ' -f2 | grep -o '[^/]*$'" || return 1
  423. else
  424. fatal "I need curl or wget to proceed, but neither of them are available on this system." F0003
  425. fi
  426. }
  427. safe_sha256sum() {
  428. # Within the context of the installer, we only use -c option that is common between the two commands
  429. # We will have to reconsider if we start using non-common options
  430. if command -v shasum > /dev/null 2>&1; then
  431. shasum -a 256 "$@"
  432. elif command -v sha256sum > /dev/null 2>&1; then
  433. sha256sum "$@"
  434. else
  435. fatal "I could not find a suitable checksum binary to use" F0004
  436. fi
  437. }
  438. get_system_info() {
  439. case "$(uname -s)" in
  440. Linux)
  441. SYSTYPE="Linux"
  442. os_release_file=
  443. if [ -s "/etc/os-release" ] && [ -r "/etc/os-release" ]; then
  444. os_release_file="/etc/os-release"
  445. elif [ -s "/usr/lib/os-release" ] && [ -r "/usr/lib/os-release" ]; then
  446. os_release_file="/usr/lib/os-release"
  447. else
  448. warning "Cannot find an os-release file ..."
  449. fi
  450. if [ -n "${os_release_file}" ]; then
  451. # shellcheck disable=SC1090
  452. . "${os_release_file}"
  453. DISTRO="${ID}"
  454. SYSVERSION="${VERSION_ID}"
  455. SYSCODENAME="${VERSION_CODENAME}"
  456. SYSARCH="$(uname -m)"
  457. supported_compat_names="debian ubuntu centos fedora opensuse ol"
  458. if str_in_list "${DISTRO}" "${supported_compat_names}"; then
  459. DISTRO_COMPAT_NAME="${DISTRO}"
  460. else
  461. case "${DISTRO}" in
  462. opensuse-leap)
  463. DISTRO_COMPAT_NAME="opensuse"
  464. ;;
  465. almalinux|rocky|rhel)
  466. DISTRO_COMPAT_NAME="centos"
  467. ;;
  468. *)
  469. DISTRO_COMPAT_NAME="unknown"
  470. ;;
  471. esac
  472. fi
  473. case "${DISTRO_COMPAT_NAME}" in
  474. centos|ol)
  475. SYSVERSION=$(echo "$SYSVERSION" | cut -d'.' -f1)
  476. ;;
  477. esac
  478. else
  479. DISTRO="unknown"
  480. DISTRO_COMPAT_NAME="unknown"
  481. SYSVERSION="unknown"
  482. SYSCODENAME="unknown"
  483. SYSARCH="$(uname -m)"
  484. fi
  485. ;;
  486. Darwin)
  487. SYSTYPE="Darwin"
  488. SYSVERSION="$(sw_vers -buildVersion)"
  489. SYSARCH="$(uname -m)"
  490. ;;
  491. FreeBSD)
  492. SYSTYPE="FreeBSD"
  493. SYSVERSION="$(uname -K)"
  494. SYSARCH="$(uname -m)"
  495. ;;
  496. *)
  497. fatal "Unsupported system type detected. Netdata cannot be installed on this system using this script." F0200
  498. ;;
  499. esac
  500. }
  501. str_in_list() {
  502. printf "%s\n" "${2}" | tr ' ' "\n" | grep -qE "^${1}\$"
  503. return $?
  504. }
  505. confirm_root_support() {
  506. if [ "$(id -u)" -ne "0" ]; then
  507. if [ -z "${ROOTCMD}" ] && command -v sudo > /dev/null; then
  508. if [ "${INTERACTIVE}" -eq 0 ]; then
  509. ROOTCMD="sudo -n"
  510. else
  511. ROOTCMD="sudo"
  512. fi
  513. fi
  514. if [ -z "${ROOTCMD}" ] && command -v doas > /dev/null; then
  515. if [ "${INTERACTIVE}" -eq 0 ]; then
  516. ROOTCMD="doas -n"
  517. else
  518. ROOTCMD="doas"
  519. fi
  520. fi
  521. if [ -z "${ROOTCMD}" ] && command -v pkexec > /dev/null; then
  522. ROOTCMD="pkexec"
  523. fi
  524. if [ -z "${ROOTCMD}" ]; then
  525. fatal "We need root privileges to continue, but cannot find a way to gain them. Either re-run this script as root, or set \$ROOTCMD to a command that can be used to gain root privileges" F0201
  526. fi
  527. fi
  528. }
  529. confirm() {
  530. prompt="${1} [y/n]"
  531. while true; do
  532. echo "${prompt}"
  533. read -r yn
  534. case "$yn" in
  535. [Yy]*) return 0;;
  536. [Nn]*) return 1;;
  537. *) echo "Please answer yes or no.";;
  538. esac
  539. done
  540. }
  541. # ======================================================================
  542. # Existing install handling code
  543. update() {
  544. updater="${ndprefix}/usr/libexec/netdata/netdata-updater.sh"
  545. if [ -x "${updater}" ]; then
  546. if [ "${DRY_RUN}" -eq 1 ]; then
  547. progress "Would attempt to update existing installation by running the updater script located at: ${updater}"
  548. return 0
  549. fi
  550. if run ${ROOTCMD} "${updater}" --not-running-from-cron; then
  551. progress "Updated existing install at ${ndprefix}"
  552. return 0
  553. else
  554. fatal "Failed to update existing Netdata install at ${ndprefix}" F0100
  555. fi
  556. else
  557. warning "Could not find a usable copy of the updater script."
  558. return 1
  559. fi
  560. }
  561. uninstall() {
  562. get_system_info
  563. detect_existing_install
  564. if [ -n "${OLD_INSTALL_PREFIX}" ]; then
  565. INSTALL_PREFIX="$(echo "${OLD_INSTALL_PREFIX}/" | sed 's/$/netdata/g')"
  566. else
  567. INSTALL_PREFIX="${ndprefix}"
  568. fi
  569. uninstaller="${INSTALL_PREFIX}/usr/libexec/netdata/netdata-uninstaller.sh"
  570. uninstaller_url="https://raw.githubusercontent.com/netdata/netdata/master/packaging/installer/netdata-uninstaller.sh"
  571. if [ $INTERACTIVE = 0 ]; then
  572. FLAGS="--yes --force"
  573. else
  574. FLAGS="--yes"
  575. fi
  576. if [ -x "${uninstaller}" ]; then
  577. if [ "${DRY_RUN}" -eq 1 ]; then
  578. progress "Would attempt to uninstall existing install with uninstaller script found at: ${uninstaller}"
  579. return 0
  580. else
  581. progress "Found existing netdata-uninstaller. Running it.."
  582. if ! run ${ROOTCMD} "${uninstaller}" $FLAGS; then
  583. warning "Uninstaller failed. Some parts of Netdata may still be present on the system."
  584. fi
  585. fi
  586. else
  587. if [ "${DRY_RUN}" -eq 1 ]; then
  588. progress "Would download installer script from: ${uninstaller_url}"
  589. progress "Would attempt to uninstall existing install with downloaded uninstaller script."
  590. return 0
  591. else
  592. progress "Downloading netdata-uninstaller ..."
  593. download "${uninstaller_url}" "${tmpdir}/netdata-uninstaller.sh"
  594. chmod +x "${tmpdir}/netdata-uninstaller.sh"
  595. if ! run ${ROOTCMD} "${tmpdir}/netdata-uninstaller.sh" $FLAGS; then
  596. warning "Uninstaller failed. Some parts of Netdata may still be present on the system."
  597. fi
  598. fi
  599. fi
  600. }
  601. detect_existing_install() {
  602. progress "Checking for existing installations of Netdata..."
  603. if pkg_installed netdata; then
  604. ndprefix="/"
  605. else
  606. if [ -n "${INSTALL_PREFIX}" ]; then
  607. searchpath="${INSTALL_PREFIX}/bin:${INSTALL_PREFIX}/sbin:${INSTALL_PREFIX}/usr/bin:${INSTALL_PREFIX}/usr/sbin:${PATH}"
  608. searchpath="${INSTALL_PREFIX}/netdata/bin:${INSTALL_PREFIX}/netdata/sbin:${INSTALL_PREFIX}/netdata/usr/bin:${INSTALL_PREFIX}/netdata/usr/sbin:${searchpath}"
  609. else
  610. searchpath="${PATH}"
  611. fi
  612. ndpath="$(PATH="${searchpath}" command -v netdata 2>/dev/null)"
  613. if [ -z "$ndpath" ] && [ -x /opt/netdata/bin/netdata ]; then
  614. ndpath="/opt/netdata/bin/netdata"
  615. fi
  616. if [ -n "${ndpath}" ]; then
  617. ndprefix="$(dirname "$(dirname "${ndpath}")")"
  618. fi
  619. if echo "${ndprefix}" | grep -Eq '^/usr$'; then
  620. ndprefix="$(dirname "${ndprefix}")"
  621. fi
  622. fi
  623. if [ -n "${ndprefix}" ]; then
  624. typefile="${ndprefix}/etc/netdata/.install-type"
  625. if [ -r "${typefile}" ]; then
  626. ${ROOTCMD} sh -c "cat \"${typefile}\" > \"${tmpdir}/install-type\""
  627. # shellcheck disable=SC1090,SC1091
  628. . "${tmpdir}/install-type"
  629. else
  630. INSTALL_TYPE="unknown"
  631. fi
  632. envfile="${ndprefix}/etc/netdata/.environment"
  633. if [ "${INSTALL_TYPE}" = "unknown" ] || [ "${INSTALL_TYPE}" = "custom" ]; then
  634. if [ -r "${envfile}" ]; then
  635. ${ROOTCMD} sh -c "cat \"${envfile}\" > \"${tmpdir}/environment\""
  636. # shellcheck disable=SC1091
  637. . "${tmpdir}/environment"
  638. if [ -n "${NETDATA_IS_STATIC_INSTALL}" ]; then
  639. if [ "${NETDATA_IS_STATIC_INSTALL}" = "yes" ]; then
  640. INSTALL_TYPE="legacy-static"
  641. else
  642. INSTALL_TYPE="legacy-build"
  643. fi
  644. fi
  645. fi
  646. fi
  647. fi
  648. }
  649. handle_existing_install() {
  650. detect_existing_install
  651. if [ -z "${ndprefix}" ] || [ -z "${INSTALL_TYPE}" ]; then
  652. progress "No existing installations of netdata found, assuming this is a fresh install."
  653. return 0
  654. fi
  655. case "${INSTALL_TYPE}" in
  656. kickstart-*|legacy-*|binpkg-*|manual-static|unknown)
  657. if [ "${INSTALL_TYPE}" = "unknown" ]; then
  658. warning "Found an existing netdata install at ${ndprefix}, but could not determine the install type."
  659. warning "Usually this means you installed Netdata through your distribution’s regular package repositories or some other unsupported method."
  660. else
  661. progress "Found an existing netdata install at ${ndprefix}, with installation type '${INSTALL_TYPE}'."
  662. fi
  663. if [ -n "${NETDATA_REINSTALL}" ] || [ -n "${NETDATA_UNSAFE_REINSTALL}" ]; then
  664. progress "Found an existing netdata install at ${ndprefix}, but user requested reinstall, continuing."
  665. case "${INSTALL_TYPE}" in
  666. binpkg-*) NETDATA_ONLY_NATIVE=1 ;;
  667. *-build) NETDATA_ONLY_BUILD=1 ;;
  668. *-static) NETDATA_ONLY_STATIC=1 ;;
  669. *)
  670. if [ -n "${NETDATA_UNSAFE_REINSTALL}" ]; then
  671. warning "Reinstalling over top of a ${INSTALL_TYPE} installation may be unsafe, but the user has requested we proceed."
  672. elif [ "${INTERACTIVE}" -eq 0 ]; then
  673. fatal "User requested reinstall, but we cannot safely reinstall over top of a ${INSTALL_TYPE} installation, exiting." F0104
  674. else
  675. if confirm "Reinstalling over top of a ${INSTALL_TYPE} installation may be unsafe, do you want to continue?"; then
  676. progress "OK, continuing."
  677. else
  678. fatal "Cancelling reinstallation at user request." F0105
  679. fi
  680. fi
  681. ;;
  682. esac
  683. return 0
  684. elif [ "${INSTALL_TYPE}" = "unknown" ]; then
  685. fatal "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." F0106
  686. fi
  687. ret=0
  688. if [ "${NETDATA_CLAIM_ONLY}" -eq 0 ]; then
  689. if ! update; then
  690. warning "Failed to update existing Netdata install at ${ndprefix}."
  691. else
  692. progress "Successfully updated existing netdata install at ${ndprefix}."
  693. fi
  694. else
  695. warning "Not updating existing install at ${ndprefix}."
  696. fi
  697. if [ -n "${NETDATA_CLAIM_TOKEN}" ]; then
  698. progress "Attempting to claim existing install at ${ndprefix}."
  699. INSTALL_PREFIX="${ndprefix}"
  700. claim
  701. ret=$?
  702. elif [ "${NETDATA_CLAIM_ONLY}" -eq 1 ]; then
  703. fatal "User asked to claim, but did not proide a claiming token." F0202
  704. else
  705. progress "Not attempting to claim existing install at ${ndprefix} (no claiming token provided)."
  706. fi
  707. deferred_warnings
  708. success_banner
  709. cleanup
  710. trap - EXIT
  711. exit $ret
  712. ;;
  713. oci)
  714. 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
  715. ;;
  716. *)
  717. if [ -n "${NETDATA_REINSTALL}" ] || [ -n "${NETDATA_UNSAFE_REINSTALL}" ]; then
  718. if [ -n "${NETDATA_UNSAFE_REINSTALL}" ]; then
  719. warning "Reinstalling over top of a ${INSTALL_TYPE} installation may be unsafe, but the user has requested we proceed."
  720. elif [ "${INTERACTIVE}" -eq 0 ]; then
  721. fatal "User requested reinstall, but we cannot safely reinstall over top of a ${INSTALL_TYPE} installation, exiting." F0104
  722. else
  723. if confirm "Reinstalling over top of a ${INSTALL_TYPE} installation may be unsafe, do you want to continue?"; then
  724. progress "OK, continuing."
  725. else
  726. fatal "Cancelling reinstallation at user request." F0105
  727. fi
  728. fi
  729. else
  730. if [ -n "${NETDATA_CLAIM_TOKEN}" ]; then
  731. progress "Attempting to claim existing install at ${ndprefix}."
  732. INSTALL_PREFIX="${ndprefix}"
  733. claim
  734. ret=$?
  735. cleanup
  736. trap - EXIT
  737. exit $ret
  738. elif [ "${NETDATA_CLAIM_ONLY}" -eq 1 ]; then
  739. fatal "User asked to claim, but did not proide a claiming token." F0202
  740. else
  741. fatal "Found an existing netdata install at ${ndprefix}, but the install type is '${INSTALL_TYPE}', which is not supported, refusing to proceed." F0103
  742. fi
  743. fi
  744. ;;
  745. esac
  746. }
  747. soft_disable_cloud() {
  748. cloud_prefix="${INSTALL_PREFIX}/var/lib/netdata/cloud.d"
  749. run ${ROOTCMD} mkdir -p "${cloud_prefix}"
  750. cat > "${tmpdir}/cloud.conf" << EOF
  751. [global]
  752. enabled = no
  753. EOF
  754. run ${ROOTCMD} cp "${tmpdir}/cloud.conf" "${cloud_prefix}/cloud.conf"
  755. if [ -z "${NETDATA_NO_START}" ]; then
  756. case "${SYSTYPE}" in
  757. Darwin) run ${ROOTCMD} launchctl kickstart -k com.github.netdata ;;
  758. FreeBSD) run ${ROOTCMD} service netdata restart ;;
  759. Linux)
  760. initpath="$(${ROOTCMD} readlink /proc/1/exe)"
  761. if command -v service > /dev/null 2>&1; then
  762. run ${ROOTCMD} service netdata restart
  763. elif command -v rc-service > /dev/null 2>&1; then
  764. run ${ROOTCMD} rc-service netdata restart
  765. elif [ "$(basename "${initpath}" 2> /dev/null)" = "systemd" ]; then
  766. run ${ROOTCMD} systemctl restart netdata
  767. elif [ -f /etc/init.d/netdata ]; then
  768. run ${ROOTCMD} /etc/init.d/netdata restart
  769. fi
  770. ;;
  771. esac
  772. fi
  773. }
  774. confirm_install_prefix() {
  775. if [ -n "${INSTALL_PREFIX}" ] && [ "${NETDATA_ONLY_BUILD}" -ne 1 ]; then
  776. fatal "The \`--install\` option is only supported together with the \`--build-only\` option." F0204
  777. fi
  778. if [ -n "${INSTALL_PREFIX}" ]; then
  779. NETDATA_INSTALLER_OPTIONS="${NETDATA_INSTALLER_OPTIONS} --install ${INSTALL_PREFIX}"
  780. else
  781. case "${SYSTYPE}" in
  782. Darwin)
  783. INSTALL_PREFIX="/usr/local/netdata"
  784. NETDATA_INSTALLER_OPTIONS="${NETDATA_INSTALLER_OPTIONS} --install-no-prefix ${INSTALL_PREFIX}"
  785. ;;
  786. FreeBSD)
  787. INSTALL_PREFIX="/usr/local"
  788. NETDATA_INSTALLER_OPTIONS="${NETDATA_INSTALLER_OPTIONS} --install-no-prefix ${INSTALL_PREFIX}"
  789. ;;
  790. esac
  791. fi
  792. }
  793. # ======================================================================
  794. # Claiming support code
  795. check_claim_opts() {
  796. # shellcheck disable=SC2235,SC2030
  797. if [ -z "${NETDATA_CLAIM_TOKEN}" ] && [ -n "${NETDATA_CLAIM_ROOMS}" ]; then
  798. fatal "Invalid claiming options, claim rooms may only be specified when a token is specified." F0204
  799. elif [ -z "${NETDATA_CLAIM_TOKEN}" ] && [ -n "${NETDATA_CLAIM_EXTRA}" ]; then
  800. fatal "Invalid claiming options, a claiming token must be specified." F0204
  801. elif [ "${NETDATA_DISABLE_CLOUD}" -eq 1 ] && [ -n "${NETDATA_CLAIM_TOKEN}" ]; then
  802. fatal "Cloud explicitly disabled, but automatic claiming requested. Either enable Netdata Cloud, or remove the --claim-* options." F0204
  803. fi
  804. }
  805. is_netdata_running() {
  806. if command -v pgrep > /dev/null 2>&1; then
  807. if pgrep netdata; then
  808. return 0
  809. else
  810. return 1
  811. fi
  812. else
  813. if [ -z "${INSTALL_PREFIX}" ]; then
  814. NETDATACLI_PATH=/usr/sbin/netdatacli
  815. elif [ "${INSTALL_PREFIX}" = "/opt/netdata" ]; then
  816. NETDATACLI_PATH="/opt/netdata/bin/netdatacli"
  817. else
  818. NETDATACLI_PATH="${INSTALL_PREFIX}/netdata/usr/sbin/netdatacli"
  819. fi
  820. if "${NETDATACLI_PATH}" ping > /dev/null 2>&1; then
  821. return 0
  822. else
  823. return 1
  824. fi
  825. fi
  826. }
  827. claim() {
  828. if [ "${DRY_RUN}" -eq 1 ]; then
  829. progress "Would attempt to claim agent to ${NETDATA_CLAIM_URL}"
  830. else
  831. progress "Attempting to claim agent to ${NETDATA_CLAIM_URL}"
  832. fi
  833. progress "Attempting to claim agent to ${NETDATA_CLAIM_URL}"
  834. if command -v netdata-claim.sh > /dev/null 2>&1; then
  835. NETDATA_CLAIM_PATH="$(command -v netdata-claim.sh)"
  836. elif [ -z "${INSTALL_PREFIX}" ] || [ "${INSTALL_PREFIX}" = "/" ]; then
  837. NETDATA_CLAIM_PATH=/usr/sbin/netdata-claim.sh
  838. elif [ "${INSTALL_PREFIX}" = "/opt/netdata" ]; then
  839. NETDATA_CLAIM_PATH="/opt/netdata/bin/netdata-claim.sh"
  840. elif [ ! -d "${INSTALL_PREFIX}/netdata" ]; then
  841. if [ -d "${INSTALL_PREFIX}/usr" ]; then
  842. NETDATA_CLAIM_PATH="${INSTALL_PREFIX}/usr/sbin/netdata-claim.sh"
  843. else
  844. NETDATA_CLAIM_PATH="${INSTALL_PREFIX}/sbin/netdata-claim.sh"
  845. fi
  846. else
  847. NETDATA_CLAIM_PATH="${INSTALL_PREFIX}/netdata/usr/sbin/netdata-claim.sh"
  848. fi
  849. if [ ! -x "${NETDATA_CLAIM_PATH}" ]; then
  850. fatal "Unable to find usable claiming script." F0106
  851. fi
  852. if ! is_netdata_running; then
  853. NETDATA_CLAIM_EXTRA="${NETDATA_CLAIM_EXTRA} -daemon-not-running"
  854. fi
  855. # shellcheck disable=SC2086
  856. run ${ROOTCMD} "${NETDATA_CLAIM_PATH}" -token="${NETDATA_CLAIM_TOKEN}" -rooms="${NETDATA_CLAIM_ROOMS}" -url="${NETDATA_CLAIM_URL}" ${NETDATA_CLAIM_EXTRA}
  857. case $? in
  858. 0)
  859. progress "Successfully claimed node"
  860. return 0
  861. ;;
  862. 1)
  863. 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}"
  864. ;;
  865. 2)
  866. 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."
  867. ;;
  868. 3)
  869. 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."
  870. ;;
  871. 4)
  872. 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."
  873. ;;
  874. 5)
  875. 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."
  876. return 0
  877. ;;
  878. 8)
  879. 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."
  880. ;;
  881. 9)
  882. 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."
  883. ;;
  884. 10)
  885. 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."
  886. ;;
  887. 11)
  888. 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."
  889. ;;
  890. 12)
  891. 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."
  892. ;;
  893. 13)
  894. 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)\""
  895. ;;
  896. 14)
  897. 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}."
  898. ;;
  899. 15|16|17)
  900. 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}."
  901. ;;
  902. 18)
  903. 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."
  904. ;;
  905. *)
  906. 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}"
  907. ;;
  908. esac
  909. if [ -z "${NETDATA_NEW_INSTALL}" ]; then
  910. deferred_warnings
  911. printf >&2 "%s\n" "For community support, you can connect with us on:"
  912. support_list
  913. cleanup
  914. trap - EXIT
  915. exit 1
  916. fi
  917. }
  918. # ======================================================================
  919. # Auto-update handling code.
  920. set_auto_updates() {
  921. if [ -x "${INSTALL_PREFIX}/usr/libexec/netdata/netdata-updater.sh" ]; then
  922. updater="${INSTALL_PREFIX}/usr/libexec/netdata/netdata-updater.sh"
  923. elif [ -x "${INSTALL_PREFIX}/netdata/usr/libexec/netdata/netdata-updater.sh" ]; then
  924. updater="${INSTALL_PREFIX}/netdata/usr/libexec/netdata/netdata-updater.sh"
  925. else
  926. 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."
  927. return 0
  928. fi
  929. if [ "${NETDATA_AUTO_UPDATES}" = "1" ]; then
  930. if [ "${DRY_RUN}" -eq 1 ]; then
  931. progress "Would have attempted to enable automatic updates."
  932. # 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.
  933. elif ! grep -q '\-\-enable-auto-updates' ${updater}; then
  934. echo
  935. elif ! ${ROOTCMD} ${updater} --enable-auto-updates "${NETDATA_AUTO_UPDATE_TYPE}"; then
  936. warning "Failed to enable auto updates. Netdata will still work, but you will need to update manually."
  937. fi
  938. else
  939. if [ "${DRY_RUN}" -eq 1 ]; then
  940. progress "Would have attempted to disable automatic updates."
  941. else
  942. ${ROOTCMD} ${updater} --disable-auto-updates
  943. fi
  944. fi
  945. }
  946. # ======================================================================
  947. # Native package install code.
  948. # Check for an already installed package with a given name.
  949. pkg_installed() {
  950. case "${DISTRO_COMPAT_NAME}" in
  951. debian|ubuntu)
  952. dpkg-query --show --showformat '${Status}' "${1}" 2>&1 | cut -f 1 -d ' ' | grep -q '^install$'
  953. return $?
  954. ;;
  955. centos|fedora|opensuse|ol)
  956. rpm -q "${1}" > /dev/null 2>&1
  957. return $?
  958. ;;
  959. *)
  960. return 1
  961. ;;
  962. esac
  963. }
  964. # Check for the existence of a usable netdata package in the repo.
  965. netdata_avail_check() {
  966. case "${DISTRO_COMPAT_NAME}" in
  967. debian|ubuntu)
  968. env DEBIAN_FRONTEND=noninteractive apt-cache policy netdata | grep -q packagecloud.io/netdata/netdata;
  969. return $?
  970. ;;
  971. centos|fedora|ol)
  972. # shellcheck disable=SC2086
  973. ${pm_cmd} search --nogpgcheck -v netdata | grep -qE 'Repo *: netdata(-edge)?$'
  974. return $?
  975. ;;
  976. opensuse)
  977. zypper packages -r "$(zypper repos | grep -E 'netdata |netdata-edge ' | cut -f 1 -d '|' | tr -d ' ')" | grep -E 'netdata '
  978. return $?
  979. ;;
  980. *)
  981. return 1
  982. ;;
  983. esac
  984. }
  985. # Check for any distro-specific dependencies we know we need.
  986. check_special_native_deps() {
  987. if [ "${DISTRO_COMPAT_NAME}" = "centos" ] && [ "${SYSVERSION}" = "7" ]; then
  988. progress "Checking for libuv availability."
  989. if ${pm_cmd} search --nogpgcheck -v libuv | grep -q "No matches found"; then
  990. progress "libuv not found, checking for EPEL availability."
  991. if ${pm_cmd} search --nogpgcheck -v epel-release | grep -q "No matches found"; then
  992. warning "Unable to find a suitable source for libuv, cannot install using native packages on this system."
  993. return 1
  994. else
  995. progress "EPEL is available, attempting to install so that required dependencies are available."
  996. # shellcheck disable=SC2086
  997. if ! run ${ROOTCMD} env ${env} ${pm_cmd} install ${pkg_install_opts} epel-release; then
  998. warning "Failed to install EPEL."
  999. return 1
  1000. fi
  1001. fi
  1002. else
  1003. return 0
  1004. fi
  1005. fi
  1006. }
  1007. try_package_install() {
  1008. if [ -z "${DISTRO}" ] || [ "${DISTRO}" = "unknown" ]; then
  1009. warning "Unable to determine Linux distribution for native packages."
  1010. return 2
  1011. fi
  1012. if [ "${DRY_RUN}" -eq 1 ]; then
  1013. progress "Would attempt to install using native packages..."
  1014. else
  1015. progress "Attempting to install using native packages..."
  1016. fi
  1017. if [ "${RELEASE_CHANNEL}" = "nightly" ]; then
  1018. release="-edge"
  1019. else
  1020. release=""
  1021. fi
  1022. if [ "${INTERACTIVE}" = "0" ]; then
  1023. interactive_opts="-y"
  1024. env="DEBIAN_FRONTEND=noninteractive"
  1025. else
  1026. interactive_opts=""
  1027. env=""
  1028. fi
  1029. case "${DISTRO_COMPAT_NAME}" in
  1030. debian)
  1031. needs_early_refresh=1
  1032. pm_cmd="apt-get"
  1033. repo_subcmd="update"
  1034. repo_prefix="debian/${SYSCODENAME}"
  1035. pkg_type="deb"
  1036. pkg_suffix="_all"
  1037. pkg_vsep="_"
  1038. pkg_install_opts="${interactive_opts}"
  1039. repo_update_opts="${interactive_opts}"
  1040. uninstall_subcmd="purge"
  1041. INSTALL_TYPE="binpkg-deb"
  1042. ;;
  1043. ubuntu)
  1044. needs_early_refresh=1
  1045. pm_cmd="apt-get"
  1046. repo_subcmd="update"
  1047. repo_prefix="ubuntu/${SYSCODENAME}"
  1048. pkg_type="deb"
  1049. pkg_suffix="_all"
  1050. pkg_vsep="_"
  1051. pkg_install_opts="${interactive_opts}"
  1052. repo_update_opts="${interactive_opts}"
  1053. uninstall_subcmd="purge"
  1054. INSTALL_TYPE="binpkg-deb"
  1055. ;;
  1056. centos)
  1057. if command -v dnf > /dev/null; then
  1058. pm_cmd="dnf"
  1059. repo_subcmd="makecache"
  1060. else
  1061. pm_cmd="yum"
  1062. fi
  1063. repo_prefix="el/${SYSVERSION}"
  1064. pkg_type="rpm"
  1065. pkg_suffix=".noarch"
  1066. pkg_vsep="-"
  1067. pkg_install_opts="${interactive_opts}"
  1068. repo_update_opts="${interactive_opts}"
  1069. uninstall_subcmd="remove"
  1070. INSTALL_TYPE="binpkg-rpm"
  1071. ;;
  1072. fedora)
  1073. if command -v dnf > /dev/null; then
  1074. pm_cmd="dnf"
  1075. repo_subcmd="makecache"
  1076. else
  1077. pm_cmd="yum"
  1078. fi
  1079. repo_prefix="fedora/${SYSVERSION}"
  1080. pkg_type="rpm"
  1081. pkg_suffix=".noarch"
  1082. pkg_vsep="-"
  1083. pkg_install_opts="${interactive_opts}"
  1084. repo_update_opts="${interactive_opts}"
  1085. uninstall_subcmd="remove"
  1086. INSTALL_TYPE="binpkg-rpm"
  1087. ;;
  1088. opensuse)
  1089. pm_cmd="zypper"
  1090. repo_subcmd="--gpg-auto-import-keys refresh"
  1091. repo_prefix="opensuse/${SYSVERSION}"
  1092. pkg_type="rpm"
  1093. pkg_suffix=".noarch"
  1094. pkg_vsep="-"
  1095. pkg_install_opts="${interactive_opts} --allow-unsigned-rpm"
  1096. repo_update_opts=""
  1097. uninstall_subcmd="remove"
  1098. INSTALL_TYPE="binpkg-rpm"
  1099. ;;
  1100. ol)
  1101. if command -v dnf > /dev/null; then
  1102. pm_cmd="dnf"
  1103. repo_subcmd="makecache"
  1104. else
  1105. pm_cmd="yum"
  1106. fi
  1107. repo_prefix="ol/${SYSVERSION}"
  1108. pkg_type="rpm"
  1109. pkg_suffix=".noarch"
  1110. pkg_vsep="-"
  1111. pkg_install_opts="${interactive_opts}"
  1112. repo_update_opts="${interactive_opts}"
  1113. uninstall_subcmd="remove"
  1114. INSTALL_TYPE="binpkg-rpm"
  1115. ;;
  1116. *)
  1117. warning "We do not provide native packages for ${DISTRO}."
  1118. return 2
  1119. ;;
  1120. esac
  1121. repoconfig_name="netdata-repo${release}"
  1122. repoconfig_file="${repoconfig_name}${pkg_vsep}${REPOCONFIG_VERSION}${pkg_suffix}.${pkg_type}"
  1123. repoconfig_url="${REPOCONFIG_URL_PREFIX}/${repo_prefix}/${repoconfig_file}/download.${pkg_type}"
  1124. if ! pkg_installed "${repoconfig_name}"; then
  1125. progress "Checking for availability of repository configuration package."
  1126. if ! check_for_remote_file "${repoconfig_url}"; then
  1127. warning "No repository configuration package available for ${DISTRO} ${SYSVERSION}."
  1128. return 2
  1129. fi
  1130. if ! download "${repoconfig_url}" "${tmpdir}/${repoconfig_file}"; then
  1131. fatal "Failed to download repository configuration package." F0209
  1132. fi
  1133. if [ -n "${needs_early_refresh}" ]; then
  1134. # shellcheck disable=SC2086
  1135. if ! run ${ROOTCMD} env ${env} ${pm_cmd} ${repo_subcmd} ${repo_update_opts}; then
  1136. warning "Failed to refresh repository metadata."
  1137. return 2
  1138. fi
  1139. fi
  1140. # shellcheck disable=SC2086
  1141. if ! run ${ROOTCMD} env ${env} ${pm_cmd} install ${pkg_install_opts} "${tmpdir}/${repoconfig_file}"; then
  1142. warning "Failed to install repository configuration package."
  1143. return 2
  1144. fi
  1145. if [ -n "${repo_subcmd}" ]; then
  1146. # shellcheck disable=SC2086
  1147. if ! run ${ROOTCMD} env ${env} ${pm_cmd} ${repo_subcmd} ${repo_update_opts}; then
  1148. fatal "Failed to refresh repository metadata." F0205
  1149. fi
  1150. fi
  1151. else
  1152. progress "Repository configuration is already present, attempting to install netdata."
  1153. fi
  1154. if ! check_special_native_deps; then
  1155. warning "Could not find secondary dependencies for ${DISTRO} on ${SYSARCH}."
  1156. if [ -z "${NO_CLEANUP}" ]; then
  1157. progress "Attempting to uninstall repository configuration package."
  1158. # shellcheck disable=SC2086
  1159. run ${ROOTCMD} env ${env} ${pm_cmd} ${uninstall_subcmd} ${pkg_install_opts} "${repoconfig_name}"
  1160. fi
  1161. return 2
  1162. fi
  1163. if ! netdata_avail_check "${DISTRO_COMPAT_NAME}"; then
  1164. warning "Could not find a usable native package for ${DISTRO} on ${SYSARCH}."
  1165. if [ -z "${NO_CLEANUP}" ]; then
  1166. progress "Attempting to uninstall repository configuration package."
  1167. # shellcheck disable=SC2086
  1168. run ${ROOTCMD} env ${env} ${pm_cmd} ${uninstall_subcmd} ${pkg_install_opts} "${repoconfig_name}"
  1169. fi
  1170. return 2
  1171. fi
  1172. if [ "${NETDATA_DISABLE_TELEMETRY}" -eq 1 ]; then
  1173. run ${ROOTCMD} mkdir -p "/etc/netdata"
  1174. run ${ROOTCMD} touch "/etc/netdata/.opt-out-from-anonymous-statistics"
  1175. fi
  1176. # shellcheck disable=SC2086
  1177. if ! run ${ROOTCMD} env ${env} ${pm_cmd} install ${pkg_install_opts} netdata; then
  1178. warning "Failed to install Netdata package."
  1179. if [ -z "${NO_CLEANUP}" ]; then
  1180. progress "Attempting to uninstall repository configuration package."
  1181. # shellcheck disable=SC2086
  1182. run ${ROOTCMD} env ${env} ${pm_cmd} ${uninstall_subcmd} ${pkg_install_opts} "${repoconfig_name}"
  1183. fi
  1184. return 2
  1185. fi
  1186. }
  1187. # ======================================================================
  1188. # Static build install code
  1189. set_static_archive_urls() {
  1190. if [ "${RELEASE_CHANNEL}" = "stable" ]; then
  1191. latest="$(get_redirect "https://github.com/netdata/netdata/releases/latest")"
  1192. export NETDATA_STATIC_ARCHIVE_URL="https://github.com/netdata/netdata/releases/download/${latest}/netdata-${SYSARCH}-latest.gz.run"
  1193. export NETDATA_STATIC_ARCHIVE_CHECKSUM_URL="https://github.com/netdata/netdata/releases/download/${latest}/sha256sums.txt"
  1194. else
  1195. export NETDATA_STATIC_ARCHIVE_URL="${NETDATA_TARBALL_BASEURL}/netdata-${SYSARCH}-latest.gz.run"
  1196. export NETDATA_STATIC_ARCHIVE_CHECKSUM_URL="${NETDATA_TARBALL_BASEURL}/sha256sums.txt"
  1197. fi
  1198. }
  1199. try_static_install() {
  1200. set_static_archive_urls "${RELEASE_CHANNEL}"
  1201. if [ "${DRY_RUN}" -eq 1 ]; then
  1202. progress "Would attempt to install using static build..."
  1203. else
  1204. progress "Attempting to install using static build..."
  1205. fi
  1206. # Check status code first, so that we can provide nicer fallback for dry runs.
  1207. if ! check_for_remote_file "${NETDATA_STATIC_ARCHIVE_URL}"; then
  1208. warning "No static build available for ${SYSARCH} CPUs."
  1209. return 2
  1210. fi
  1211. if ! download "${NETDATA_STATIC_ARCHIVE_URL}" "${tmpdir}/netdata-${SYSARCH}-latest.gz.run"; then
  1212. fatal "Unable to download static build archive for ${SYSARCH}." F0208
  1213. fi
  1214. if ! download "${NETDATA_STATIC_ARCHIVE_CHECKSUM_URL}" "${tmpdir}/sha256sum.txt"; then
  1215. fatal "Unable to fetch checksums to verify static build archive." F0206
  1216. fi
  1217. if [ "${DRY_RUN}" -eq 1 ]; then
  1218. progress "Would validate SHA256 checksum of downloaded static build archive."
  1219. else
  1220. if ! grep "netdata-${SYSARCH}-latest.gz.run" "${tmpdir}/sha256sum.txt" | safe_sha256sum -c - > /dev/null 2>&1; then
  1221. fatal "Static binary checksum validation failed. 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." F0207
  1222. fi
  1223. fi
  1224. if [ "${INTERACTIVE}" -eq 0 ]; then
  1225. opts="${opts} --accept"
  1226. fi
  1227. progress "Installing netdata"
  1228. # shellcheck disable=SC2086
  1229. if ! run ${ROOTCMD} sh "${tmpdir}/netdata-${SYSARCH}-latest.gz.run" ${opts} -- ${NETDATA_AUTO_UPDATES:+--auto-update} ${NETDATA_INSTALLER_OPTIONS}; then
  1230. warning "Failed to install static build of Netdata on ${SYSARCH}."
  1231. run rm -rf /opt/netdata
  1232. return 2
  1233. fi
  1234. if [ "${DRY_RUN}" -ne 1 ]; then
  1235. install_type_file="/opt/netdata/etc/netdata/.install-type"
  1236. if [ -f "${install_type_file}" ]; then
  1237. ${ROOTCMD} sh -c "cat \"${install_type_file}\" > \"${tmpdir}/install-type\""
  1238. ${ROOTCMD} chown "$(id -u)":"$(id -g)" "${tmpdir}/install-type"
  1239. # shellcheck disable=SC1090,SC1091
  1240. . "${tmpdir}/install-type"
  1241. cat > "${tmpdir}/install-type" <<- EOF
  1242. INSTALL_TYPE='kickstart-static'
  1243. PREBUILT_ARCH='${PREBUILT_ARCH}'
  1244. EOF
  1245. ${ROOTCMD} chown netdata:netdata "${tmpdir}/install-type"
  1246. ${ROOTCMD} cp "${tmpdir}/install-type" "${install_type_file}"
  1247. fi
  1248. fi
  1249. }
  1250. # ======================================================================
  1251. # Local build install code
  1252. set_source_archive_urls() {
  1253. if [ "$1" = "stable" ]; then
  1254. latest="$(get_redirect "https://github.com/netdata/netdata/releases/latest")"
  1255. export NETDATA_SOURCE_ARCHIVE_URL="https://github.com/netdata/netdata/releases/download/${latest}/netdata-${latest}.tar.gz"
  1256. export NETDATA_SOURCE_ARCHIVE_CHECKSUM_URL="https://github.com/netdata/netdata/releases/download/${latest}/sha256sums.txt"
  1257. else
  1258. export NETDATA_SOURCE_ARCHIVE_URL="${NETDATA_TARBALL_BASEURL}/netdata-latest.tar.gz"
  1259. export NETDATA_SOURCE_ARCHIVE_CHECKSUM_URL="${NETDATA_TARBALL_BASEURL}/sha256sums.txt"
  1260. fi
  1261. }
  1262. install_local_build_dependencies() {
  1263. bash="$(command -v bash 2> /dev/null)"
  1264. if [ -z "${bash}" ] || [ ! -x "${bash}" ]; then
  1265. warning "Unable to find a usable version of \`bash\` (required for local build)."
  1266. return 1
  1267. fi
  1268. if ! download "${PACKAGES_SCRIPT}" "${tmpdir}/install-required-packages.sh"; then
  1269. fatal "Failed to download dependency handling script for local build." F000D
  1270. fi
  1271. if [ "${DRY_RUN}" -eq 1 ]; then
  1272. progress "Would run downloaded script to install required build dependencies..."
  1273. else
  1274. progress "Running downloaded script to install required build dependencies..."
  1275. fi
  1276. if [ "${INTERACTIVE}" -eq 0 ]; then
  1277. opts="--dont-wait --non-interactive"
  1278. fi
  1279. if [ "${SYSTYPE}" = "Darwin" ]; then
  1280. sudo=""
  1281. else
  1282. sudo="${ROOTCMD}"
  1283. fi
  1284. # shellcheck disable=SC2086
  1285. if ! run ${sudo} "${bash}" "${tmpdir}/install-required-packages.sh" ${opts} netdata; then
  1286. warning "Failed to install all required packages, but installation might still be possible."
  1287. fi
  1288. }
  1289. build_and_install() {
  1290. if [ "${DRY_RUN}" -eq 1 ]; then
  1291. progress "Would attempt to build netdata..."
  1292. else
  1293. progress "Building netdata..."
  1294. fi
  1295. echo "INSTALL_TYPE='kickstart-build'" > system/.install-type
  1296. opts="${NETDATA_INSTALLER_OPTIONS}"
  1297. if [ "${INTERACTIVE}" -eq 0 ]; then
  1298. opts="${opts} --dont-wait"
  1299. fi
  1300. if [ "${NETDATA_AUTO_UPDATES}" -eq 1 ]; then
  1301. opts="${opts} --auto-update"
  1302. fi
  1303. if [ "${RELEASE_CHANNEL}" = "stable" ]; then
  1304. opts="${opts} --stable-channel"
  1305. fi
  1306. if [ "${NETDATA_REQUIRE_CLOUD}" -eq 1 ]; then
  1307. opts="${opts} --require-cloud"
  1308. elif [ "${NETDATA_DISABLE_CLOUD}" -eq 1 ]; then
  1309. opts="${opts} --disable-cloud"
  1310. fi
  1311. # shellcheck disable=SC2086
  1312. run ${ROOTCMD} ./netdata-installer.sh ${opts}
  1313. case $? in
  1314. 1)
  1315. fatal "netdata-installer.sh failed to run correctly." F0007
  1316. ;;
  1317. 2)
  1318. fatal "Insufficient RAM to install netdata." F0008
  1319. ;;
  1320. esac
  1321. }
  1322. try_build_install() {
  1323. if [ "${DRY_RUN}" -eq 1 ]; then
  1324. progress "Would attempt to install by building locally..."
  1325. else
  1326. progress "Attempting to install by building locally..."
  1327. fi
  1328. if ! install_local_build_dependencies; then
  1329. return 1
  1330. fi
  1331. set_source_archive_urls "${RELEASE_CHANNEL}"
  1332. if ! download "${NETDATA_SOURCE_ARCHIVE_URL}" "${tmpdir}/netdata-latest.tar.gz"; then
  1333. fatal "Failed to download source tarball for local build." F000B
  1334. fi
  1335. if ! download "${NETDATA_SOURCE_ARCHIVE_CHECKSUM_URL}" "${tmpdir}/sha256sum.txt"; then
  1336. fatal "Failed to download checksums for source tarball verification." F000C
  1337. fi
  1338. if [ "${DRY_RUN}" -eq 1 ]; then
  1339. progress "Would validate SHA256 checksum of downloaded source archive."
  1340. else
  1341. if ! grep netdata-latest.tar.gz "${tmpdir}/sha256sum.txt" | safe_sha256sum -c - > /dev/null 2>&1; then
  1342. fatal "Tarball checksum validation failed. 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." F0005
  1343. fi
  1344. fi
  1345. run tar -xf "${tmpdir}/netdata-latest.tar.gz" -C "${tmpdir}"
  1346. rm -rf "${tmpdir}/netdata-latest.tar.gz" > /dev/null 2>&1
  1347. if [ "${DRY_RUN}" -ne 1 ]; then
  1348. cd "$(find "${tmpdir}" -mindepth 1 -maxdepth 1 -type d -name netdata-)" || fatal "Cannot change directory to netdata source tree" F0006
  1349. fi
  1350. if [ -x netdata-installer.sh ] || [ "${DRY_RUN}" -eq 1 ]; then
  1351. build_and_install || return 1
  1352. else
  1353. # This case is needed because some platforms produce an extra directory on the source tarball extraction.
  1354. if [ "$(find . -mindepth 1 -maxdepth 1 -type d | wc -l)" -eq 1 ] && [ -x "$(find . -mindepth 1 -maxdepth 1 -type d)/netdata-installer.sh" ]; then
  1355. cd "$(find . -mindepth 1 -maxdepth 1 -type d)" && build_and_install || return 1
  1356. else
  1357. fatal "Cannot install netdata from source (the source directory does not include netdata-installer.sh)." F0009
  1358. fi
  1359. fi
  1360. }
  1361. # ======================================================================
  1362. # Per system-type install logic
  1363. install_on_linux() {
  1364. if [ "${NETDATA_ONLY_STATIC}" -ne 1 ] && [ "${NETDATA_ONLY_BUILD}" -ne 1 ]; then
  1365. SELECTED_INSTALL_METHOD="native"
  1366. try_package_install
  1367. case "$?" in
  1368. 0)
  1369. NETDATA_INSTALL_SUCCESSFUL=1
  1370. INSTALL_PREFIX="/"
  1371. ;;
  1372. 1)
  1373. fatal "Unable to install on this system." F0300
  1374. ;;
  1375. 2)
  1376. if [ "${NETDATA_ONLY_NATIVE}" -eq 1 ]; then
  1377. fatal "Could not install native binary packages." F0301
  1378. else
  1379. warning "Could not install native binary packages, falling back to alternative installation method."
  1380. fi
  1381. ;;
  1382. esac
  1383. fi
  1384. if [ "${NETDATA_ONLY_NATIVE}" -ne 1 ] && [ "${NETDATA_ONLY_BUILD}" -ne 1 ] && [ -z "${NETDATA_INSTALL_SUCCESSFUL}" ]; then
  1385. SELECTED_INSTALL_METHOD="static"
  1386. INSTALL_TYPE="kickstart-static"
  1387. try_static_install
  1388. case "$?" in
  1389. 0)
  1390. NETDATA_INSTALL_SUCCESSFUL=1
  1391. INSTALL_PREFIX="/opt/netdata"
  1392. ;;
  1393. 1)
  1394. fatal "Unable to install on this system." F0302
  1395. ;;
  1396. 2)
  1397. if [ "${NETDATA_ONLY_STATIC}" -eq 1 ]; then
  1398. fatal "Could not install static build." F0303
  1399. else
  1400. warning "Could not install static build, falling back to alternative installation method."
  1401. fi
  1402. ;;
  1403. esac
  1404. fi
  1405. if [ "${NETDATA_ONLY_NATIVE}" -ne 1 ] && [ "${NETDATA_ONLY_STATIC}" -ne 1 ] && [ -z "${NETDATA_INSTALL_SUCCESSFUL}" ]; then
  1406. SELECTED_INSTALL_METHOD="build"
  1407. INSTALL_TYPE="kickstart-build"
  1408. try_build_install
  1409. case "$?" in
  1410. 0)
  1411. NETDATA_INSTALL_SUCCESSFUL=1
  1412. ;;
  1413. *)
  1414. fatal "Unable to install on this system." F0304
  1415. ;;
  1416. esac
  1417. fi
  1418. }
  1419. install_on_macos() {
  1420. if [ "${NETDATA_ONLY_NATIVE}" -eq 1 ]; then
  1421. fatal "User requested native package, but native packages are not available for macOS. Try installing without \`--only-native\` option." F0305
  1422. elif [ "${NETDATA_ONLY_STATIC}" -eq 1 ]; then
  1423. fatal "User requested static build, but static builds are not available for macOS. Try installing without \`--only-static\` option." F0306
  1424. else
  1425. SELECTED_INSTALL_METHOD="build"
  1426. INSTALL_TYPE="kickstart-build"
  1427. try_build_install
  1428. case "$?" in
  1429. 0)
  1430. NETDATA_INSTALL_SUCCESSFUL=1
  1431. ;;
  1432. *)
  1433. fatal "Unable to install on this system." F0307
  1434. ;;
  1435. esac
  1436. fi
  1437. }
  1438. install_on_freebsd() {
  1439. if [ "${NETDATA_ONLY_NATIVE}" -eq 1 ]; then
  1440. fatal "User requested native package, but native packages are not available for FreeBSD. Try installing without \`--only-native\` option." F0308
  1441. elif [ "${NETDATA_ONLY_STATIC}" -eq 1 ]; then
  1442. fatal "User requested static build, but static builds are not available for FreeBSD. Try installing without \`--only-static\` option." F0309
  1443. else
  1444. SELECTED_INSTALL_METHOD="build"
  1445. INSTALL_TYPE="kickstart-build"
  1446. try_build_install
  1447. case "$?" in
  1448. 0)
  1449. NETDATA_INSTALL_SUCCESSFUL=1
  1450. ;;
  1451. *)
  1452. fatal "Unable to install on this system." F030A
  1453. ;;
  1454. esac
  1455. fi
  1456. }
  1457. # ======================================================================
  1458. # Main program
  1459. setup_terminal || echo > /dev/null
  1460. while [ -n "${1}" ]; do
  1461. case "${1}" in
  1462. "--help")
  1463. usage
  1464. cleanup
  1465. trap - EXIT
  1466. exit 0
  1467. ;;
  1468. "--no-cleanup") NO_CLEANUP=1 ;;
  1469. "--dont-wait"|"--non-interactive") INTERACTIVE=0 ;;
  1470. "--interactive") INTERACTIVE=1 ;;
  1471. "--dry-run") DRY_RUN=1 ;;
  1472. "--stable-channel") RELEASE_CHANNEL="stable" ;;
  1473. "--no-updates") NETDATA_AUTO_UPDATES=0 ;;
  1474. "--auto-update") NETDATA_AUTO_UPDATES="1" ;;
  1475. "--auto-update-method")
  1476. NETDATA_AUTO_UPDATE_TYPE="$(echo "${2}" | tr '[:upper:]' '[:lower:]')"
  1477. case "${NETDATA_AUTO_UPDATE_TYPE}" in
  1478. systemd|interval|crontab)
  1479. shift 1
  1480. ;;
  1481. *)
  1482. echo "Unrecognized value for --auto-update-type. Valid values are: systemd, interval, crontab"
  1483. exit 1
  1484. ;;
  1485. esac
  1486. ;;
  1487. "--reinstall") NETDATA_REINSTALL=1 ;;
  1488. "--reinstall-even-if-unsafe") NETDATA_UNSAFE_REINSTALL=1 ;;
  1489. "--claim-only") NETDATA_CLAIM_ONLY=1 ;;
  1490. "--disable-cloud")
  1491. NETDATA_DISABLE_CLOUD=1
  1492. NETDATA_REQUIRE_CLOUD=0
  1493. ;;
  1494. "--require-cloud")
  1495. NETDATA_DISABLE_CLOUD=0
  1496. NETDATA_REQUIRE_CLOUD=1
  1497. ;;
  1498. "--dont-start-it")
  1499. NETDATA_NO_START=1
  1500. NETDATA_INSTALLER_OPTIONS="${NETDATA_INSTALLER_OPTIONS} --dont-start-it"
  1501. ;;
  1502. "--disable-telemetry")
  1503. NETDATA_DISABLE_TELEMETRY="1"
  1504. NETDATA_INSTALLER_OPTIONS="${NETDATA_INSTALLER_OPTIONS} --disable-telemetry"
  1505. ;;
  1506. "--install")
  1507. INSTALL_PREFIX="${2}"
  1508. shift 1
  1509. ;;
  1510. "--old-install-prefix")
  1511. OLD_INSTALL_PREFIX="${2}"
  1512. shift 1
  1513. ;;
  1514. "--uninstall")
  1515. ACTION="uninstall"
  1516. ;;
  1517. "--reinstall-clean")
  1518. ACTION="reinstall-clean"
  1519. ;;
  1520. "--native-only")
  1521. NETDATA_ONLY_NATIVE=1
  1522. NETDATA_ONLY_STATIC=0
  1523. NETDATA_ONLY_BUILD=0
  1524. SELECTED_INSTALL_METHOD="native"
  1525. ;;
  1526. "--static-only")
  1527. NETDATA_ONLY_STATIC=1
  1528. NETDATA_ONLY_NATIVE=0
  1529. NETDATA_ONLY_BUILD=0
  1530. SELECTED_INSTALL_METHOD="static"
  1531. ;;
  1532. "--build-only")
  1533. NETDATA_ONLY_BUILD=1
  1534. NETDATA_ONLY_NATIVE=0
  1535. NETDATA_ONLY_STATIC=0
  1536. SELECTED_INSTALL_METHOD="build"
  1537. ;;
  1538. "--claim-token")
  1539. NETDATA_CLAIM_TOKEN="${2}"
  1540. shift 1
  1541. ;;
  1542. "--claim-rooms")
  1543. NETDATA_CLAIM_ROOMS="${2}"
  1544. shift 1
  1545. ;;
  1546. "--claim-url")
  1547. NETDATA_CLAIM_URL="${2}"
  1548. shift 1
  1549. ;;
  1550. "--claim-"*)
  1551. optname="$(echo "${1}" | cut -d '-' -f 4-)"
  1552. case "${optname}" in
  1553. id|proxy|user|hostname)
  1554. NETDATA_CLAIM_EXTRA="${NETDATA_CLAIM_EXTRA} -${optname}=${2}"
  1555. shift 1
  1556. ;;
  1557. verbose|insecure|noproxy|noreload|daemon-not-running)
  1558. NETDATA_CLAIM_EXTRA="${NETDATA_CLAIM_EXTRA} -${optname}"
  1559. ;;
  1560. *)
  1561. warning "Ignoring unrecognized claiming option ${optname}"
  1562. ;;
  1563. esac
  1564. ;;
  1565. *)
  1566. warning "Passing unrecognized option '${1}' to installer script. If this is intended, please add it to \$NETDATA_INSTALLER_OPTIONS instead."
  1567. NETDATA_INSTALLER_OPTIONS="${NETDATA_INSTALLER_OPTIONS} ${1}"
  1568. ;;
  1569. esac
  1570. shift 1
  1571. done
  1572. check_claim_opts
  1573. confirm_root_support
  1574. get_system_info
  1575. confirm_install_prefix
  1576. if [ -z "${ACTION}" ]; then
  1577. handle_existing_install
  1578. fi
  1579. main