functions.sh 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158
  1. #!/bin/sh
  2. # SPDX-License-Identifier: GPL-3.0-or-later
  3. # next unused error code: L0003
  4. # make sure we have a UID
  5. [ -z "${UID}" ] && UID="$(id -u)"
  6. # -----------------------------------------------------------------------------
  7. setup_terminal() {
  8. TPUT_RESET=""
  9. TPUT_BLACK=""
  10. TPUT_RED=""
  11. TPUT_GREEN=""
  12. TPUT_YELLOW=""
  13. TPUT_BLUE=""
  14. TPUT_PURPLE=""
  15. TPUT_CYAN=""
  16. TPUT_WHITE=""
  17. TPUT_BGBLACK=""
  18. TPUT_BGRED=""
  19. TPUT_BGGREEN=""
  20. TPUT_BGYELLOW=""
  21. TPUT_BGBLUE=""
  22. TPUT_BGPURPLE=""
  23. TPUT_BGCYAN=""
  24. TPUT_BGWHITE=""
  25. TPUT_BOLD=""
  26. TPUT_DIM=""
  27. TPUT_UNDERLINED=""
  28. TPUT_BLINK=""
  29. TPUT_INVERTED=""
  30. TPUT_STANDOUT=""
  31. TPUT_BELL=""
  32. TPUT_CLEAR=""
  33. # Is stderr on the terminal? If not, then fail
  34. test -t 2 || return 1
  35. if command -v tput 1> /dev/null 2>&1; then
  36. if [ $(($(tput colors 2> /dev/null))) -ge 8 ]; then
  37. # Enable colors
  38. TPUT_RESET="$(tput sgr 0)"
  39. # shellcheck disable=SC2034
  40. TPUT_BLACK="$(tput setaf 0)"
  41. # shellcheck disable=SC2034
  42. TPUT_RED="$(tput setaf 1)"
  43. TPUT_GREEN="$(tput setaf 2)"
  44. # shellcheck disable=SC2034
  45. TPUT_YELLOW="$(tput setaf 3)"
  46. # shellcheck disable=SC2034
  47. TPUT_BLUE="$(tput setaf 4)"
  48. # shellcheck disable=SC2034
  49. TPUT_PURPLE="$(tput setaf 5)"
  50. # shellcheck disable=SC2034
  51. TPUT_CYAN="$(tput setaf 6)"
  52. TPUT_WHITE="$(tput setaf 7)"
  53. # shellcheck disable=SC2034
  54. TPUT_BGBLACK="$(tput setab 0)"
  55. TPUT_BGRED="$(tput setab 1)"
  56. TPUT_BGGREEN="$(tput setab 2)"
  57. # shellcheck disable=SC2034
  58. TPUT_BGYELLOW="$(tput setab 3)"
  59. # shellcheck disable=SC2034
  60. TPUT_BGBLUE="$(tput setab 4)"
  61. # shellcheck disable=SC2034
  62. TPUT_BGPURPLE="$(tput setab 5)"
  63. # shellcheck disable=SC2034
  64. TPUT_BGCYAN="$(tput setab 6)"
  65. # shellcheck disable=SC2034
  66. TPUT_BGWHITE="$(tput setab 7)"
  67. TPUT_BOLD="$(tput bold)"
  68. TPUT_DIM="$(tput dim)"
  69. # shellcheck disable=SC2034
  70. TPUT_UNDERLINED="$(tput smul)"
  71. # shellcheck disable=SC2034
  72. TPUT_BLINK="$(tput blink)"
  73. # shellcheck disable=SC2034
  74. TPUT_INVERTED="$(tput rev)"
  75. # shellcheck disable=SC2034
  76. TPUT_STANDOUT="$(tput smso)"
  77. # shellcheck disable=SC2034
  78. TPUT_BELL="$(tput bel)"
  79. # shellcheck disable=SC2034
  80. TPUT_CLEAR="$(tput clear)"
  81. fi
  82. fi
  83. return 0
  84. }
  85. setup_terminal || echo > /dev/null
  86. progress() {
  87. echo >&2 " --- ${TPUT_DIM}${TPUT_BOLD}${*}${TPUT_RESET} --- "
  88. }
  89. check_for_curl() {
  90. if [ -z "${curl}" ]; then
  91. curl="$(PATH="${PATH}:/opt/netdata/bin" command -v curl 2>/dev/null && true)"
  92. fi
  93. }
  94. get() {
  95. url="${1}"
  96. checked=0
  97. succeeded=0
  98. check_for_curl
  99. if [ -n "${curl}" ]; then
  100. checked=1
  101. if "${curl}" -q -o - -sSL --connect-timeout 10 --retry 3 "${url}"; then
  102. succeeded=1
  103. fi
  104. fi
  105. if [ "${succeeded}" -eq 0 ]; then
  106. if command -v wget > /dev/null 2>&1; then
  107. checked=1
  108. if wget -T 15 -O - "${url}"; then
  109. succeeded=1
  110. fi
  111. fi
  112. fi
  113. if [ "${succeeded}" -eq 1 ]; then
  114. return 0
  115. elif [ "${checked}" -eq 1 ]; then
  116. return 1
  117. else
  118. fatal "I need curl or wget to proceed, but neither is available on this system." "L0002"
  119. fi
  120. }
  121. download_file() {
  122. url="${1}"
  123. dest="${2}"
  124. name="${3}"
  125. opt="${4}"
  126. check_for_curl
  127. if [ -n "${curl}" ]; then
  128. checked=1
  129. if run "${curl}" -q -sSL --connect-timeout 10 --retry 3 --output "${dest}" "${url}"; then
  130. succeeded=1
  131. else
  132. rm -f "${dest}"
  133. fi
  134. fi
  135. if [ "${succeeded}" -eq 0 ]; then
  136. if command -v wget > /dev/null 2>&1; then
  137. checked=1
  138. if run wget -T 15 -O "${dest}" "${url}"; then
  139. succeeded=1
  140. fi
  141. fi
  142. fi
  143. if [ "${succeeded}" -eq 1 ]; then
  144. return 0
  145. elif [ "${checked}" -eq 1 ]; then
  146. return 1
  147. else
  148. echo >&2
  149. echo >&2 "Downloading ${name} from '${url}' failed because of missing mandatory packages."
  150. if [ -n "$opt" ]; then
  151. echo >&2 "Either add packages or disable it by issuing '--disable-${opt}' in the installer"
  152. fi
  153. echo >&2
  154. run_failed "I need curl or wget to proceed, but neither is available on this system."
  155. fi
  156. }
  157. # -----------------------------------------------------------------------------
  158. # external component handling
  159. fetch_and_verify() {
  160. component="${1}"
  161. url="${2}"
  162. base_name="${3}"
  163. tmp="${4}"
  164. override="${5}"
  165. if [ -z "${override}" ]; then
  166. download_file "${url}" "${tmp}/${base_name}" "${component}"
  167. else
  168. progress "Using provided ${component} archive ${override}"
  169. run cp "${override}" "${tmp}/${base_name}"
  170. fi
  171. if [ ! -f "${tmp}/${base_name}" ] || [ ! -s "${tmp}/${base_name}" ]; then
  172. run_failed "Unable to find usable archive for ${component}"
  173. return 1
  174. fi
  175. grep "${base_name}\$" "${INSTALLER_DIR}/packaging/${component}.checksums" > "${tmp}/sha256sums.txt" 2> /dev/null
  176. # Checksum validation
  177. if ! (cd "${tmp}" && safe_sha256sum -c "sha256sums.txt"); then
  178. run_failed "${component} files checksum validation failed."
  179. return 1
  180. fi
  181. }
  182. # -----------------------------------------------------------------------------
  183. netdata_banner() {
  184. l1=" ^" \
  185. l2=" |.-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-" \
  186. l4=" +----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+--->" \
  187. space=" "
  188. l3f=" | '-' '-' '-' '-' '-'"
  189. l3e=" '-' '-' '-' '-' '-' "
  190. netdata="netdata"
  191. chartcolor="${TPUT_DIM}"
  192. echo >&2
  193. echo >&2 "${chartcolor}${l1}${TPUT_RESET}"
  194. echo >&2 "${chartcolor}${l2%-. .-. .-. .-. .-. .-. .-. .-}${space}${TPUT_RESET}${TPUT_BOLD}${TPUT_GREEN}${netdata}${TPUT_RESET}${chartcolor}${l2# |.-. .-. .-. .-. .-. .-. .-. }${TPUT_RESET}"
  195. echo >&2 "${chartcolor}${l3f}${l3e}${TPUT_RESET}"
  196. echo >&2 "${chartcolor}${l4}${TPUT_RESET}"
  197. echo >&2
  198. }
  199. # -----------------------------------------------------------------------------
  200. # Feature management and configuration commands
  201. enable_feature() {
  202. NETDATA_CMAKE_OPTIONS="$(echo "${NETDATA_CMAKE_OPTIONS}" | sed -e "s/-DENABLE_${1}=Off[[:space:]]*//g" -e "s/-DENABLE_${1}=On[[:space:]]*//g")"
  203. if [ "${2}" -eq 1 ]; then
  204. NETDATA_CMAKE_OPTIONS="$(echo "${NETDATA_CMAKE_OPTIONS}" | sed "s/$/ -DENABLE_${1}=On/")"
  205. else
  206. NETDATA_CMAKE_OPTIONS="$(echo "${NETDATA_CMAKE_OPTIONS}" | sed "s/$/ -DENABLE_${1}=Off/")"
  207. fi
  208. }
  209. check_for_module() {
  210. if [ -z "${pkgconf}" ]; then
  211. pkgconf="$(command -v pkgconf 2>/dev/null)"
  212. [ -z "${pkgconf}" ] && pkgconf="$(command -v pkg-config 2>/dev/null)"
  213. [ -z "${pkgconf}" ] && fatal "Unable to find a usable pkgconf/pkg-config command, cannot build Netdata." I0013
  214. fi
  215. "${pkgconf}" "${1}"
  216. return "${?}"
  217. }
  218. check_for_feature() {
  219. feature_name="${1}"
  220. feature_state="${2}"
  221. shift 2
  222. feature_modules="${*}"
  223. if [ -z "${feature_state}" ]; then
  224. # shellcheck disable=SC2086
  225. if check_for_module ${feature_modules}; then
  226. enable_feature "${feature_name}" 1
  227. else
  228. enable_feature "${feature_name}" 0
  229. fi
  230. else
  231. enable_feature "${feature_name}" "${feature_state}"
  232. fi
  233. }
  234. prepare_cmake_options() {
  235. NETDATA_CMAKE_OPTIONS="-S ./ -B ${NETDATA_BUILD_DIR} ${CMAKE_OPTS} ${NETDATA_PREFIX+-DCMAKE_INSTALL_PREFIX="${NETDATA_PREFIX}"} ${NETDATA_USER:+-DNETDATA_USER=${NETDATA_USER}} ${NETDATA_CMAKE_OPTIONS} "
  236. NEED_OLD_CXX=0
  237. if [ "${FORCE_LEGACY_CXX:-0}" -eq 1 ]; then
  238. NEED_OLD_CXX=1
  239. else
  240. if command -v gcc >/dev/null 2>&1; then
  241. if [ "$(gcc --version | head -n 1 | sed 's/(.*) //' | cut -f 2 -d ' ' | cut -f 1 -d '.')" -lt 5 ]; then
  242. NEED_OLD_CXX=1
  243. fi
  244. fi
  245. if command -v clang >/dev/null 2>&1; then
  246. if [ "$(clang --version | head -n 1 | cut -f 3 -d ' ' | cut -f 1 -d '.')" -lt 4 ]; then
  247. NEED_OLD_CXX=1
  248. fi
  249. fi
  250. fi
  251. if [ "${NEED_OLD_CXX}" -eq 1 ]; then
  252. NETDATA_CMAKE_OPTIONS="${NETDATA_CMAKE_OPTIONS} -DUSE_CXX_11=On"
  253. fi
  254. if [ "${ENABLE_GO:-1}" -eq 1 ]; then
  255. enable_feature PLUGIN_GO 1
  256. else
  257. enable_feature PLUGIN_GO 0
  258. fi
  259. if [ "${USE_SYSTEM_PROTOBUF:-0}" -eq 1 ]; then
  260. enable_feature BUNDLED_PROTOBUF 0
  261. else
  262. enable_feature BUNDLED_PROTOBUF 1
  263. fi
  264. if [ -z "${ENABLE_SYSTEMD_JOURNAL}" ]; then
  265. if check_for_module libsystemd; then
  266. if check_for_module libelogind; then
  267. ENABLE_SYSTEMD_JOURNAL=0
  268. else
  269. ENABLE_SYSTEMD_JOURNAL=1
  270. fi
  271. else
  272. ENABLE_SYSTEMD_JOURNAL=0
  273. fi
  274. fi
  275. enable_feature PLUGIN_SYSTEMD_JOURNAL "${ENABLE_SYSTEMD_JOURNAL}"
  276. if command -v cups-config >/dev/null 2>&1 || check_for_module libcups || check_for_module cups; then
  277. ENABLE_CUPS=1
  278. else
  279. ENABLE_CUPS=0
  280. fi
  281. enable_feature PLUGIN_CUPS "${ENABLE_CUPS}"
  282. IS_LINUX=0
  283. [ "$(uname -s)" = "Linux" ] && IS_LINUX=1
  284. enable_feature PLUGIN_DEBUGFS "${IS_LINUX}"
  285. enable_feature PLUGIN_PERF "${IS_LINUX}"
  286. enable_feature PLUGIN_SLABINFO "${IS_LINUX}"
  287. enable_feature PLUGIN_CGROUP_NETWORK "${IS_LINUX}"
  288. enable_feature PLUGIN_LOCAL_LISTENERS "${IS_LINUX}"
  289. enable_feature PLUGIN_NETWORK_VIEWER "${IS_LINUX}"
  290. enable_feature PLUGIN_EBPF "${ENABLE_EBPF:-0}"
  291. enable_feature ACLK "${ENABLE_CLOUD:-1}"
  292. enable_feature CLOUD "${ENABLE_CLOUD:-1}"
  293. enable_feature BUNDLED_JSONC "${NETDATA_BUILD_JSON_C:-0}"
  294. enable_feature DBENGINE "${ENABLE_DBENGINE:-1}"
  295. enable_feature H2O "${ENABLE_H2O:-1}"
  296. enable_feature ML "${NETDATA_ENABLE_ML:-1}"
  297. enable_feature PLUGIN_APPS "${ENABLE_APPS:-1}"
  298. check_for_feature EXPORTER_PROMETHEUS_REMOTE_WRITE "${EXPORTER_PROMETHEUS}" snappy
  299. check_for_feature EXPORTER_MONGODB "${EXPORTER_MONGODB}" libmongoc-1.0
  300. check_for_feature PLUGIN_FREEIPMI "${ENABLE_FREEIPMI}" libipmimonitoring
  301. check_for_feature PLUGIN_NFACCT "${ENABLE_NFACCT}" libnetfilter_acct libnml
  302. check_for_feature PLUGIN_XENSTAT "${ENABLE_XENSTAT}" xenstat xenlight
  303. }
  304. # -----------------------------------------------------------------------------
  305. # portable service command
  306. service_cmd="$(command -v service 2> /dev/null || true)"
  307. rcservice_cmd="$(command -v rc-service 2> /dev/null || true)"
  308. systemctl_cmd="$(command -v systemctl 2> /dev/null || true)"
  309. service() {
  310. cmd="${1}"
  311. action="${2}"
  312. if [ -n "${systemctl_cmd}" ]; then
  313. run "${systemctl_cmd}" "${action}" "${cmd}"
  314. return $?
  315. elif [ -n "${service_cmd}" ]; then
  316. run "${service_cmd}" "${cmd}" "${action}"
  317. return $?
  318. elif [ -n "${rcservice_cmd}" ]; then
  319. run "${rcservice_cmd}" "${cmd}" "${action}"
  320. return $?
  321. fi
  322. return 1
  323. }
  324. # -----------------------------------------------------------------------------
  325. # portable pidof
  326. safe_pidof() {
  327. pidof_cmd="$(command -v pidof 2> /dev/null)"
  328. if [ -n "${pidof_cmd}" ]; then
  329. ${pidof_cmd} "${@}"
  330. return $?
  331. else
  332. ps -acxo pid,comm |
  333. sed "s/^ *//g" |
  334. grep netdata |
  335. cut -d ' ' -f 1
  336. return $?
  337. fi
  338. }
  339. # -----------------------------------------------------------------------------
  340. find_processors() {
  341. # Most UNIX systems have `nproc` as part of their userland (including Linux and BSD)
  342. if command -v nproc > /dev/null; then
  343. nproc && return
  344. fi
  345. # macOS has no nproc but it may have gnproc installed from Homebrew or from Macports.
  346. if command -v gnproc > /dev/null; then
  347. gnproc && return
  348. fi
  349. if [ -f "/proc/cpuinfo" ]; then
  350. # linux
  351. cpus=$(grep -c ^processor /proc/cpuinfo)
  352. else
  353. # freebsd
  354. cpus=$(sysctl hw.ncpu 2> /dev/null | grep ^hw.ncpu | cut -d ' ' -f 2)
  355. fi
  356. if [ -z "${cpus}" ] || [ $((cpus)) -lt 1 ]; then
  357. echo 1
  358. else
  359. echo "${cpus}"
  360. fi
  361. }
  362. # -----------------------------------------------------------------------------
  363. exit_reason() {
  364. if [ -n "${NETDATA_SAVE_WARNINGS}" ]; then
  365. EXIT_REASON="${1}"
  366. EXIT_CODE="${2}"
  367. if [ -n "${NETDATA_PROPAGATE_WARNINGS}" ]; then
  368. if [ -n "${NETDATA_SCRIPT_STATUS_PATH}" ]; then
  369. {
  370. echo "EXIT_REASON=\"${EXIT_REASON}\""
  371. echo "EXIT_CODE=\"${EXIT_CODE}\""
  372. echo "NETDATA_WARNINGS=\"${NETDATA_WARNINGS}${SAVED_WARNINGS}\""
  373. } >> "${NETDATA_SCRIPT_STATUS_PATH}"
  374. else
  375. export EXIT_REASON
  376. export EXIT_CODE
  377. export NETDATA_WARNINGS="${NETDATA_WARNINGS}${SAVED_WARNINGS}"
  378. fi
  379. fi
  380. fi
  381. }
  382. fatal() {
  383. printf >&2 "%s ABORTED %s %s \n\n" "${TPUT_BGRED}${TPUT_WHITE}${TPUT_BOLD}" "${TPUT_RESET}" "${1}"
  384. if [ -n "${NETDATA_SAVE_WARNINGS}" ]; then
  385. SAVED_WARNINGS="${SAVED_WARNINGS}\n - ${1}"
  386. fi
  387. exit_reason "${1}" "${2}"
  388. exit 1
  389. }
  390. warning() {
  391. printf >&2 "%s WARNING %s %s\n\n" "${TPUT_BGYELLOW}${TPUT_BLACK}${TPUT_BOLD}" "${TPUT_RESET}" "${1}"
  392. if [ -n "${NETDATA_SAVE_WARNINGS}" ]; then
  393. SAVED_WARNINGS="${SAVED_WARNINGS}\n - ${1}"
  394. fi
  395. }
  396. run_ok() {
  397. printf >&2 "%s OK %s %s\n\n" "${TPUT_BGGREEN}${TPUT_WHITE}${TPUT_BOLD}" "${TPUT_RESET}" "${1:-''}"
  398. }
  399. run_failed() {
  400. printf >&2 "%s FAILED %s %s\n\n" "${TPUT_BGRED}${TPUT_WHITE}${TPUT_BOLD}" "${TPUT_RESET}" "${1:-''}"
  401. if [ -n "${NETDATA_SAVE_WARNINGS}" ] && [ -n "${1:-''}" ]; then
  402. SAVED_WARNINGS="${SAVED_WARNINGS}\n - ${1}"
  403. fi
  404. }
  405. ESCAPED_PRINT_METHOD=
  406. if printf "%s " test > /dev/null 2>&1; then
  407. ESCAPED_PRINT_METHOD="printfq"
  408. fi
  409. escaped_print() {
  410. if [ "${ESCAPED_PRINT_METHOD}" = "printfq" ]; then
  411. printf "%s " "${@}"
  412. else
  413. printf "%s" "${*}"
  414. fi
  415. return 0
  416. }
  417. run_logfile="/dev/null"
  418. run() {
  419. local_user="${USER--}"
  420. local_dir="${PWD}"
  421. if [ "${UID}" = "0" ]; then
  422. info="[root ${local_dir}]# "
  423. info_console="[${TPUT_DIM}${local_dir}${TPUT_RESET}]# "
  424. else
  425. info="[${local_user} ${local_dir}]$ "
  426. info_console="[${TPUT_DIM}${local_dir}${TPUT_RESET}]$ "
  427. fi
  428. {
  429. printf "%s" "${info}"
  430. escaped_print "${@}"
  431. printf "%s" " ... "
  432. } >> "${run_logfile}"
  433. printf >&2 "%s" "${info_console}${TPUT_BOLD}${TPUT_YELLOW}"
  434. escaped_print >&2 "${@}"
  435. printf >&2 "%s\n" "${TPUT_RESET}"
  436. "${@}"
  437. ret=$?
  438. if [ ${ret} -ne 0 ]; then
  439. run_failed
  440. printf >> "${run_logfile}" "FAILED with exit code %s\n" "${ret}"
  441. if [ -n "${NETDATA_SAVE_WARNINGS}" ]; then
  442. SAVED_WARNINGS="${SAVED_WARNINGS}\n - Command '${*}' failed with exit code ${ret}."
  443. fi
  444. else
  445. run_ok
  446. printf >> "${run_logfile}" "OK\n"
  447. fi
  448. return ${ret}
  449. }
  450. iscontainer() {
  451. # man systemd-detect-virt
  452. cmd=$(command -v systemd-detect-virt 2> /dev/null)
  453. if [ -n "${cmd}" ] && [ -x "${cmd}" ]; then
  454. "${cmd}" --container > /dev/null 2>&1 && return 0
  455. fi
  456. # /proc/1/sched exposes the host's pid of our init !
  457. # http://stackoverflow.com/a/37016302
  458. pid=$(head -n 1 /proc/1/sched 2> /dev/null | {
  459. # shellcheck disable=SC2034
  460. IFS='(),#:' read -r name pid th threads
  461. echo "$pid"
  462. })
  463. if [ -n "${pid}" ]; then
  464. pid=$((pid + 0))
  465. [ ${pid} -gt 1 ] && return 0
  466. fi
  467. # lxc sets environment variable 'container'
  468. # shellcheck disable=SC2154
  469. [ -n "${container}" ] && return 0
  470. # docker creates /.dockerenv
  471. # http://stackoverflow.com/a/25518345
  472. [ -f "/.dockerenv" ] && return 0
  473. # ubuntu and debian supply /bin/running-in-container
  474. # https://www.apt-browse.org/browse/ubuntu/trusty/main/i386/upstart/1.12.1-0ubuntu4/file/bin/running-in-container
  475. if [ -x "/bin/running-in-container" ]; then
  476. "/bin/running-in-container" > /dev/null 2>&1 && return 0
  477. fi
  478. return 1
  479. }
  480. get_os_key() {
  481. if [ -f /etc/os-release ]; then
  482. # shellcheck disable=SC1091
  483. . /etc/os-release || return 1
  484. echo "${ID}-${VERSION_ID}"
  485. elif [ -f /etc/redhat-release ]; then
  486. cat /etc/redhat-release
  487. else
  488. echo "unknown"
  489. fi
  490. }
  491. get_group(){
  492. if command -v getent > /dev/null 2>&1; then
  493. getent group "${1:-""}"
  494. else
  495. grep "^${1}:" /etc/group
  496. fi
  497. }
  498. issystemd() {
  499. pids=''
  500. p=''
  501. myns=''
  502. ns=''
  503. systemctl=''
  504. # if the directory /lib/systemd/system OR /usr/lib/systemd/system (SLES 12.x) does not exit, it is not systemd
  505. if [ ! -d /lib/systemd/system ] && [ ! -d /usr/lib/systemd/system ]; then
  506. return 1
  507. fi
  508. # if there is no systemctl command, it is not systemd
  509. systemctl=$(command -v systemctl 2> /dev/null)
  510. if [ -z "${systemctl}" ] || [ ! -x "${systemctl}" ]; then
  511. return 1
  512. fi
  513. # if pid 1 is systemd, it is systemd
  514. [ "$(basename "$(readlink /proc/1/exe)" 2> /dev/null)" = "systemd" ] && return 0
  515. # if systemd is not running, it is not systemd
  516. pids=$(safe_pidof systemd 2> /dev/null)
  517. [ -z "${pids}" ] && return 1
  518. # check if the running systemd processes are not in our namespace
  519. myns="$(readlink /proc/self/ns/pid 2> /dev/null)"
  520. for p in ${pids}; do
  521. ns="$(readlink "/proc/${p}/ns/pid" 2> /dev/null)"
  522. # if pid of systemd is in our namespace, it is systemd
  523. [ -n "${myns}" ] && [ "${myns}" = "${ns}" ] && return 0
  524. done
  525. # else, it is not systemd
  526. return 1
  527. }
  528. get_systemd_service_dir() {
  529. if [ -w "/lib/systemd/system" ]; then
  530. echo "/lib/systemd/system"
  531. elif [ -w "/usr/lib/systemd/system" ]; then
  532. echo "/usr/lib/systemd/system"
  533. elif [ -w "/etc/systemd/system" ]; then
  534. echo "/etc/systemd/system"
  535. fi
  536. }
  537. run_install_service_script() {
  538. if [ -z "${tmpdir}" ]; then
  539. tmpdir="${TMPDIR:-/tmp}"
  540. fi
  541. # shellcheck disable=SC2154
  542. save_path="${tmpdir}/netdata-service-cmds"
  543. # shellcheck disable=SC2068
  544. "${NETDATA_PREFIX}/usr/libexec/netdata/install-service.sh" --save-cmds "${save_path}" ${@}
  545. case $? in
  546. 0)
  547. if [ -r "${save_path}" ]; then
  548. # shellcheck disable=SC1090
  549. . "${save_path}"
  550. fi
  551. if [ -z "${NETDATA_INSTALLER_START_CMD}" ]; then
  552. if [ -n "${NETDATA_START_CMD}" ]; then
  553. NETDATA_INSTALLER_START_CMD="${NETDATA_START_CMD}"
  554. else
  555. NETDATA_INSTALLER_START_CMD="netdata"
  556. fi
  557. fi
  558. ;;
  559. 1)
  560. if [ -z "${NETDATA_SERVICE_WARNED_1}" ]; then
  561. warning "Intenral error encountered while attempting to install or manage Netdata as a system service. This is probably a bug."
  562. NETDATA_SERVICE_WARNED_1=1
  563. fi
  564. ;;
  565. 2)
  566. if [ -z "${NETDATA_SERVICE_WARNED_2}" ]; then
  567. warning "Failed to detect system service manager type. Cannot cleanly install or manage Netdata as a system service. If you are running this script in a container, this is expected and can safely be ignored."
  568. NETDATA_SERVICE_WARNED_2=1
  569. fi
  570. ;;
  571. 3)
  572. if [ -z "${NETDATA_SERVICE_WARNED_3}" ]; then
  573. warning "Detected an unsupported system service manager. Manual setup will be required to manage Netdata as a system service."
  574. NETDATA_SERVICE_WARNED_3=1
  575. fi
  576. ;;
  577. 4)
  578. if [ -z "${NETDATA_SERVICE_WARNED_4}" ]; then
  579. warning "Detected a supported system service manager, but failed to install Netdata as a system service. Usually this is a result of incorrect permissions. Manually running ${NETDATA_PREFIX}/usr/libexec/netdata/install-service.sh may provide more information about the exact issue."
  580. NETDATA_SERVICE_WARNED_4=1
  581. fi
  582. ;;
  583. 5)
  584. if [ -z "${NETDATA_SERVICE_WARNED_5}" ]; then
  585. warning "We do not support managing Netdata as a system service on this platform. Manual setup will be required."
  586. NETDATA_SERVICE_WARNED_5=1
  587. fi
  588. ;;
  589. esac
  590. }
  591. install_netdata_service() {
  592. if [ "${UID}" -eq 0 ]; then
  593. if [ -x "${NETDATA_PREFIX}/usr/libexec/netdata/install-service.sh" ]; then
  594. run_install_service_script && return 0
  595. else
  596. warning "Could not find service install script, not installing Netdata as a system service."
  597. fi
  598. fi
  599. return 1
  600. }
  601. # -----------------------------------------------------------------------------
  602. # stop netdata
  603. pidisnetdata() {
  604. if [ -d /proc/self ]; then
  605. if [ -z "$1" ] || [ ! -f "/proc/$1/stat" ]; then
  606. return 1
  607. fi
  608. [ "$(cut -d '(' -f 2 "/proc/$1/stat" | cut -d ')' -f 1)" = "netdata" ] && return 0
  609. return 1
  610. fi
  611. return 0
  612. }
  613. stop_netdata_on_pid() {
  614. pid="${1}"
  615. ret=0
  616. count=0
  617. pidisnetdata "${pid}" || return 0
  618. printf >&2 "Stopping netdata on pid %s ..." "${pid}"
  619. while [ -n "${pid}" ] && [ ${ret} -eq 0 ]; do
  620. if [ ${count} -gt 24 ]; then
  621. warning "Cannot stop netdata agent with PID ${pid}."
  622. return 1
  623. fi
  624. count=$((count + 1))
  625. pidisnetdata "${pid}" || ret=1
  626. if [ ${ret} -eq 1 ]; then
  627. break
  628. fi
  629. if [ ${count} -lt 12 ]; then
  630. run kill "${pid}" 2> /dev/null
  631. ret=$?
  632. else
  633. run kill -9 "${pid}" 2> /dev/null
  634. ret=$?
  635. fi
  636. test ${ret} -eq 0 && printf >&2 "." && sleep 5
  637. done
  638. echo >&2
  639. if [ ${ret} -eq 0 ]; then
  640. warning "Failed to stop netdata agent process with PID ${pid}."
  641. return 1
  642. fi
  643. echo >&2 "netdata on pid ${pid} stopped."
  644. return 0
  645. }
  646. netdata_pids() {
  647. myns="$(readlink /proc/self/ns/pid 2> /dev/null)"
  648. for p in \
  649. $(cat /var/run/netdata.pid 2> /dev/null) \
  650. $(cat /var/run/netdata/netdata.pid 2> /dev/null) \
  651. $(safe_pidof netdata 2> /dev/null); do
  652. ns="$(readlink "/proc/${p}/ns/pid" 2> /dev/null)"
  653. if [ -z "${myns}" ] || [ -z "${ns}" ] || [ "${myns}" = "${ns}" ]; then
  654. pidisnetdata "${p}" && echo "${p}"
  655. fi
  656. done
  657. }
  658. stop_all_netdata() {
  659. stop_success=0
  660. if [ -x "${NETDATA_PREFIX}/usr/libexec/netdata/install-service.sh" ]; then
  661. run_install_service_script --cmds-only
  662. fi
  663. if [ "${UID}" -eq 0 ]; then
  664. uname="$(uname 2>/dev/null)"
  665. # Any of these may fail, but we need to not bail if they do.
  666. if [ -n "${NETDATA_STOP_CMD}" ]; then
  667. if ${NETDATA_STOP_CMD}; then
  668. stop_success=1
  669. sleep 5
  670. fi
  671. elif issystemd; then
  672. if systemctl stop netdata; then
  673. stop_success=1
  674. sleep 5
  675. fi
  676. elif [ "${uname}" = "Darwin" ]; then
  677. if launchctl stop netdata; then
  678. stop_success=1
  679. sleep 5
  680. fi
  681. elif [ "${uname}" = "FreeBSD" ]; then
  682. if /etc/rc.d/netdata stop; then
  683. stop_success=1
  684. sleep 5
  685. fi
  686. else
  687. if service netdata stop; then
  688. stop_success=1
  689. sleep 5
  690. fi
  691. fi
  692. fi
  693. if [ "$stop_success" = "0" ]; then
  694. if [ -n "$(netdata_pids)" ] && [ -n "$(command -v netdatacli)" ]; then
  695. netdatacli shutdown-agent
  696. sleep 20
  697. fi
  698. for p in $(netdata_pids); do
  699. # shellcheck disable=SC2086
  700. stop_netdata_on_pid ${p}
  701. done
  702. fi
  703. }
  704. # -----------------------------------------------------------------------------
  705. # restart netdata
  706. restart_netdata() {
  707. netdata="${1}"
  708. shift
  709. started=0
  710. progress "Restarting netdata instance"
  711. if [ -x "${NETDATA_PREFIX}/usr/libexec/netdata/install-service.sh" ]; then
  712. run_install_service_script --cmds-only
  713. fi
  714. if [ -z "${NETDATA_INSTALLER_START_CMD}" ]; then
  715. if [ -n "${NETDATA_START_CMD}" ]; then
  716. NETDATA_INSTALLER_START_CMD="${NETDATA_START_CMD}"
  717. else
  718. NETDATA_INSTALLER_START_CMD="${netdata}"
  719. fi
  720. fi
  721. if [ "${UID}" -eq 0 ]; then
  722. echo >&2
  723. echo >&2 "Stopping all netdata threads"
  724. run stop_all_netdata
  725. echo >&2 "Starting netdata using command '${NETDATA_INSTALLER_START_CMD}'"
  726. # shellcheck disable=SC2086
  727. run ${NETDATA_INSTALLER_START_CMD} && started=1
  728. if [ ${started} -eq 1 ] && sleep 5 && [ -z "$(netdata_pids)" ]; then
  729. echo >&2 "Ooops! it seems netdata is not started."
  730. started=0
  731. fi
  732. if [ ${started} -eq 0 ]; then
  733. echo >&2 "Attempting another netdata start using command '${NETDATA_INSTALLER_START_CMD}'"
  734. # shellcheck disable=SC2086
  735. run ${NETDATA_INSTALLER_START_CMD} && started=1
  736. fi
  737. if [ ${started} -eq 1 ] && sleep 5 && [ -z "$(netdata_pids)" ]; then
  738. echo >&2 "Hm... it seems netdata is still not started."
  739. started=0
  740. fi
  741. fi
  742. if [ ${started} -eq 0 ]; then
  743. # still not started... another forced attempt, just run the binary
  744. warning "Netdata service still not started, attempting another forced restart by running '${netdata} ${*}'"
  745. run stop_all_netdata
  746. run "${netdata}" "${@}"
  747. return $?
  748. fi
  749. return 0
  750. }
  751. # -----------------------------------------------------------------------------
  752. # install netdata logrotate
  753. install_netdata_logrotate() {
  754. src="${NETDATA_PREFIX}/usr/lib/netdata/system/logrotate/netdata"
  755. if [ "${UID}" -eq 0 ]; then
  756. if [ -d /etc/logrotate.d ]; then
  757. if [ ! -f /etc/logrotate.d/netdata ]; then
  758. run cp "${src}" /etc/logrotate.d/netdata
  759. fi
  760. if [ -f /etc/logrotate.d/netdata ]; then
  761. run chmod 644 /etc/logrotate.d/netdata
  762. fi
  763. return 0
  764. fi
  765. fi
  766. return 1
  767. }
  768. # -----------------------------------------------------------------------------
  769. # install netdata journald configuration
  770. install_netdata_journald_conf() {
  771. src="${NETDATA_PREFIX}/usr/lib/netdata/system/systemd/journald@netdata.conf"
  772. [ ! -d /usr/lib/systemd/ ] && return 0
  773. [ "${UID}" -ne 0 ] && return 1
  774. if [ ! -d /usr/lib/systemd/journald@netdata.conf.d/ ]; then
  775. run mkdir /usr/lib/systemd/journald@netdata.conf.d/
  776. fi
  777. run cp "${src}" /usr/lib/systemd/journald@netdata.conf.d/netdata.conf
  778. if [ -f /usr/lib/systemd/journald@netdata.conf.d/netdata.conf ]; then
  779. run chmod 644 /usr/lib/systemd/journald@netdata.conf.d/netdata.conf
  780. fi
  781. return 0
  782. }
  783. # -----------------------------------------------------------------------------
  784. # create netdata.conf
  785. create_netdata_conf() {
  786. path="${1}"
  787. url="${2}"
  788. if [ -s "${path}" ]; then
  789. return 0
  790. fi
  791. if [ -n "$url" ]; then
  792. echo >&2 "Downloading default configuration from netdata..."
  793. sleep 5
  794. # remove a possibly obsolete configuration file
  795. [ -f "${path}.new" ] && rm "${path}.new"
  796. # disable a proxy to get data from the local netdata
  797. export http_proxy=
  798. export https_proxy=
  799. check_for_curl
  800. if [ -n "${curl}" ]; then
  801. run "${curl}" -sSL --connect-timeout 10 --retry 3 "${url}" > "${path}.new"
  802. elif command -v wget 1> /dev/null 2>&1; then
  803. run wget -T 15 -O - "${url}" > "${path}.new"
  804. fi
  805. if [ -s "${path}.new" ]; then
  806. run mv "${path}.new" "${path}"
  807. run_ok "New configuration saved for you to edit at ${path}"
  808. else
  809. [ -f "${path}.new" ] && rm "${path}.new"
  810. run_failed "Cannot download configuration from netdata daemon using url '${url}'"
  811. url=''
  812. fi
  813. fi
  814. if [ -z "$url" ]; then
  815. cat << EOF > "${path}"
  816. # netdata can generate its own config which is available at 'http://<IP>:19999/netdata.conf'
  817. # You can download it using:
  818. # curl -o ${path} http://localhost:19999/netdata.conf
  819. # or
  820. # wget -O ${path} http://localhost:19999/netdata.conf
  821. EOF
  822. fi
  823. }
  824. portable_add_user() {
  825. username="${1}"
  826. homedir="${2}"
  827. [ -z "${homedir}" ] && homedir="/tmp"
  828. # Check if user exists
  829. if command -v getent > /dev/null 2>&1; then
  830. if getent passwd "${username}" > /dev/null 2>&1; then
  831. echo >&2 "User '${username}' already exists."
  832. return 0
  833. fi
  834. elif command -v dscl > /dev/null 2>&1; then
  835. if dscl . read /Users/"${username}" >/dev/null 2>&1; then
  836. echo >&2 "User '${username}' already exists."
  837. return 0
  838. fi
  839. else
  840. if cut -d ':' -f 1 < /etc/passwd | grep "^${username}$" 1> /dev/null 2>&1; then
  841. echo >&2 "User '${username}' already exists."
  842. return 0
  843. fi
  844. fi
  845. echo >&2 "Adding ${username} user account with home ${homedir} ..."
  846. nologin="$(command -v nologin || echo '/bin/false')"
  847. if command -v useradd 1> /dev/null 2>&1; then
  848. run useradd -r -g "${username}" -c "${username}" -s "${nologin}" --no-create-home -d "${homedir}" "${username}" && return 0
  849. elif command -v pw 1> /dev/null 2>&1; then
  850. run pw useradd "${username}" -d "${homedir}" -g "${username}" -s "${nologin}" && return 0
  851. elif command -v adduser 1> /dev/null 2>&1; then
  852. run adduser -h "${homedir}" -s "${nologin}" -D -G "${username}" "${username}" && return 0
  853. elif command -v sysadminctl 1> /dev/null 2>&1; then
  854. gid=$(dscl . read /Groups/"${username}" 2>/dev/null | grep PrimaryGroupID | grep -Eo "[0-9]+")
  855. if run sysadminctl -addUser "${username}" -shell /usr/bin/false -home /var/empty -GID "$gid"; then
  856. # FIXME: I think the proper solution is to create a role account:
  857. # -roleAccount + name starting with _ and UID in 200-400 range.
  858. run dscl . create /Users/"${username}" IsHidden 1
  859. return 0
  860. fi
  861. fi
  862. warning "Failed to add ${username} user account!"
  863. return 1
  864. }
  865. portable_add_group() {
  866. groupname="${1}"
  867. # Check if group exist
  868. if get_group "${groupname}" > /dev/null 2>&1; then
  869. echo >&2 "Group '${groupname}' already exists."
  870. return 0
  871. fi
  872. echo >&2 "Adding ${groupname} user group ..."
  873. # Linux
  874. if command -v groupadd 1> /dev/null 2>&1; then
  875. run groupadd -r "${groupname}" && return 0
  876. elif command -v pw 1> /dev/null 2>&1; then
  877. run pw groupadd "${groupname}" && return 0
  878. elif command -v addgroup 1> /dev/null 2>&1; then
  879. run addgroup "${groupname}" && return 0
  880. elif command -v dseditgroup 1> /dev/null 2>&1; then
  881. dseditgroup -o create "${groupname}" && return 0
  882. fi
  883. warning >&2 "Failed to add ${groupname} user group !"
  884. return 1
  885. }
  886. portable_add_user_to_group() {
  887. groupname="${1}"
  888. username="${2}"
  889. # Check if group exist
  890. if ! get_group "${groupname}" > /dev/null 2>&1; then
  891. echo >&2 "Group '${groupname}' does not exist."
  892. # Don’t treat this as a failure, if the group does not exist we should not be trying to add the user to it.
  893. return 0
  894. fi
  895. # Check if user is in group
  896. if get_group "${groupname}" | cut -d ':' -f 4 | grep -wq "${username}"; then
  897. # username is already there
  898. echo >&2 "User '${username}' is already in group '${groupname}'."
  899. return 0
  900. else
  901. # username is not in group
  902. echo >&2 "Adding ${username} user to the ${groupname} group ..."
  903. # Linux
  904. if command -v usermod 1> /dev/null 2>&1; then
  905. run usermod -a -G "${groupname}" "${username}" && return 0
  906. elif command -v pw 1> /dev/null 2>&1; then
  907. run pw groupmod "${groupname}" -m "${username}" && return 0
  908. elif command -v addgroup 1> /dev/null 2>&1; then
  909. run addgroup "${username}" "${groupname}" && return 0
  910. elif command -v dseditgroup 1> /dev/null 2>&1; then
  911. dseditgroup -u "${username}" "${groupname}" && return 0
  912. fi
  913. warning >&2 "Failed to add user ${username} to group ${groupname}!"
  914. return 1
  915. fi
  916. }
  917. safe_sha256sum() {
  918. # Within the context of the installer, we only use -c option that is common between the two commands
  919. # We will have to reconsider if we start non-common options
  920. if command -v sha256sum > /dev/null 2>&1; then
  921. sha256sum "$@"
  922. elif command -v shasum > /dev/null 2>&1; then
  923. shasum -a 256 "$@"
  924. else
  925. fatal "I could not find a suitable checksum binary to use" "L0001"
  926. fi
  927. }
  928. _get_scheduler_type() {
  929. if _get_intervaldir > /dev/null ; then
  930. echo 'interval'
  931. elif issystemd ; then
  932. echo 'systemd'
  933. elif [ -d /etc/cron.d ] ; then
  934. echo 'crontab'
  935. else
  936. echo 'none'
  937. fi
  938. }
  939. _get_intervaldir() {
  940. if [ -d /etc/cron.daily ]; then
  941. echo /etc/cron.daily
  942. elif [ -d /etc/periodic/daily ]; then
  943. echo /etc/periodic/daily
  944. else
  945. return 1
  946. fi
  947. return 0
  948. }
  949. install_netdata_updater() {
  950. if [ "${INSTALLER_DIR}" ] && [ -f "${INSTALLER_DIR}/packaging/installer/netdata-updater.sh" ]; then
  951. cat "${INSTALLER_DIR}/packaging/installer/netdata-updater.sh" > "${NETDATA_PREFIX}/usr/libexec/netdata/netdata-updater.sh" || return 1
  952. fi
  953. if [ "${NETDATA_SOURCE_DIR}" ] && [ -f "${NETDATA_SOURCE_DIR}/packaging/installer/netdata-updater.sh" ]; then
  954. cat "${NETDATA_SOURCE_DIR}/packaging/installer/netdata-updater.sh" > "${NETDATA_PREFIX}/usr/libexec/netdata/netdata-updater.sh" || return 1
  955. fi
  956. # these files are installed by cmake
  957. libsysdir="${NETDATA_PREFIX}/usr/lib/netdata/system/systemd/"
  958. if [ -d "${libsysdir}" ] && issystemd && [ -n "$(get_systemd_service_dir)" ]; then
  959. cat "${libsysdir}/netdata-updater.timer" > "$(get_systemd_service_dir)/netdata-updater.timer"
  960. cat "${libsysdir}/netdata-updater.service" > "$(get_systemd_service_dir)/netdata-updater.service"
  961. fi
  962. sed -i -e "s|THIS_SHOULD_BE_REPLACED_BY_INSTALLER_SCRIPT|${NETDATA_USER_CONFIG_DIR}/.environment|" "${NETDATA_PREFIX}/usr/libexec/netdata/netdata-updater.sh" || return 1
  963. chmod 0755 "${NETDATA_PREFIX}/usr/libexec/netdata/netdata-updater.sh"
  964. echo >&2 "Update script is located at ${TPUT_GREEN}${TPUT_BOLD}${NETDATA_PREFIX}/usr/libexec/netdata/netdata-updater.sh${TPUT_RESET}"
  965. echo >&2
  966. return 0
  967. }
  968. set_netdata_updater_channel() {
  969. sed -i -e "s/^RELEASE_CHANNEL=.*/RELEASE_CHANNEL=\"${RELEASE_CHANNEL}\"/" "${NETDATA_USER_CONFIG_DIR}/.environment"
  970. }