functions.sh 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168
  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 [ "${ENABLE_PYTHON:-1}" -eq 1 ]; then
  260. enable_feature PLUGIN_PYTHON 1
  261. else
  262. enable_feature PLUGIN_PYTHON 0
  263. fi
  264. if [ "${ENABLE_CHARTS:-1}" -eq 1 ]; then
  265. enable_feature PLUGIN_CHARTS 1
  266. else
  267. enable_feature PLUGIN_CHARTS 0
  268. fi
  269. if [ "${USE_SYSTEM_PROTOBUF:-0}" -eq 1 ]; then
  270. enable_feature BUNDLED_PROTOBUF 0
  271. else
  272. enable_feature BUNDLED_PROTOBUF 1
  273. fi
  274. if [ -z "${ENABLE_SYSTEMD_JOURNAL}" ]; then
  275. if check_for_module libsystemd; then
  276. if check_for_module libelogind; then
  277. ENABLE_SYSTEMD_JOURNAL=0
  278. else
  279. ENABLE_SYSTEMD_JOURNAL=1
  280. fi
  281. else
  282. ENABLE_SYSTEMD_JOURNAL=0
  283. fi
  284. fi
  285. enable_feature PLUGIN_SYSTEMD_JOURNAL "${ENABLE_SYSTEMD_JOURNAL}"
  286. if command -v cups-config >/dev/null 2>&1 || check_for_module libcups || check_for_module cups; then
  287. ENABLE_CUPS=1
  288. else
  289. ENABLE_CUPS=0
  290. fi
  291. enable_feature PLUGIN_CUPS "${ENABLE_CUPS}"
  292. IS_LINUX=0
  293. [ "$(uname -s)" = "Linux" ] && IS_LINUX=1
  294. enable_feature PLUGIN_DEBUGFS "${IS_LINUX}"
  295. enable_feature PLUGIN_PERF "${IS_LINUX}"
  296. enable_feature PLUGIN_SLABINFO "${IS_LINUX}"
  297. enable_feature PLUGIN_CGROUP_NETWORK "${IS_LINUX}"
  298. enable_feature PLUGIN_LOCAL_LISTENERS "${IS_LINUX}"
  299. enable_feature PLUGIN_NETWORK_VIEWER "${IS_LINUX}"
  300. enable_feature PLUGIN_EBPF "${ENABLE_EBPF:-0}"
  301. enable_feature BUNDLED_JSONC "${NETDATA_BUILD_JSON_C:-0}"
  302. enable_feature DBENGINE "${ENABLE_DBENGINE:-1}"
  303. enable_feature H2O "${ENABLE_H2O:-0}"
  304. enable_feature ML "${NETDATA_ENABLE_ML:-1}"
  305. enable_feature PLUGIN_APPS "${ENABLE_APPS:-1}"
  306. check_for_feature EXPORTER_PROMETHEUS_REMOTE_WRITE "${EXPORTER_PROMETHEUS}" snappy
  307. check_for_feature EXPORTER_MONGODB "${EXPORTER_MONGODB}" libmongoc-1.0
  308. check_for_feature PLUGIN_FREEIPMI "${ENABLE_FREEIPMI}" libipmimonitoring
  309. check_for_feature PLUGIN_NFACCT "${ENABLE_NFACCT}" libnetfilter_acct libnml
  310. check_for_feature PLUGIN_XENSTAT "${ENABLE_XENSTAT}" xenstat xenlight
  311. }
  312. # -----------------------------------------------------------------------------
  313. # portable service command
  314. service_cmd="$(command -v service 2> /dev/null || true)"
  315. rcservice_cmd="$(command -v rc-service 2> /dev/null || true)"
  316. systemctl_cmd="$(command -v systemctl 2> /dev/null || true)"
  317. service() {
  318. cmd="${1}"
  319. action="${2}"
  320. if [ -n "${systemctl_cmd}" ]; then
  321. run "${systemctl_cmd}" "${action}" "${cmd}"
  322. return $?
  323. elif [ -n "${service_cmd}" ]; then
  324. run "${service_cmd}" "${cmd}" "${action}"
  325. return $?
  326. elif [ -n "${rcservice_cmd}" ]; then
  327. run "${rcservice_cmd}" "${cmd}" "${action}"
  328. return $?
  329. fi
  330. return 1
  331. }
  332. # -----------------------------------------------------------------------------
  333. # portable pidof
  334. safe_pidof() {
  335. pidof_cmd="$(command -v pidof 2> /dev/null)"
  336. if [ -n "${pidof_cmd}" ]; then
  337. ${pidof_cmd} "${@}"
  338. return $?
  339. else
  340. ps -acxo pid,comm |
  341. sed "s/^ *//g" |
  342. grep netdata |
  343. cut -d ' ' -f 1
  344. return $?
  345. fi
  346. }
  347. # -----------------------------------------------------------------------------
  348. find_processors() {
  349. # Most UNIX systems have `nproc` as part of their userland (including Linux and BSD)
  350. if command -v nproc > /dev/null; then
  351. nproc && return
  352. fi
  353. # macOS has no nproc but it may have gnproc installed from Homebrew or from Macports.
  354. if command -v gnproc > /dev/null; then
  355. gnproc && return
  356. fi
  357. if [ -f "/proc/cpuinfo" ]; then
  358. # linux
  359. cpus=$(grep -c ^processor /proc/cpuinfo)
  360. else
  361. # freebsd
  362. cpus=$(sysctl hw.ncpu 2> /dev/null | grep ^hw.ncpu | cut -d ' ' -f 2)
  363. fi
  364. if [ -z "${cpus}" ] || [ $((cpus)) -lt 1 ]; then
  365. echo 1
  366. else
  367. echo "${cpus}"
  368. fi
  369. }
  370. # -----------------------------------------------------------------------------
  371. exit_reason() {
  372. if [ -n "${NETDATA_SAVE_WARNINGS}" ]; then
  373. EXIT_REASON="${1}"
  374. EXIT_CODE="${2}"
  375. if [ -n "${NETDATA_PROPAGATE_WARNINGS}" ]; then
  376. if [ -n "${NETDATA_SCRIPT_STATUS_PATH}" ]; then
  377. {
  378. echo "EXIT_REASON=\"${EXIT_REASON}\""
  379. echo "EXIT_CODE=\"${EXIT_CODE}\""
  380. echo "NETDATA_WARNINGS=\"${NETDATA_WARNINGS}${SAVED_WARNINGS}\""
  381. } >> "${NETDATA_SCRIPT_STATUS_PATH}"
  382. else
  383. export EXIT_REASON
  384. export EXIT_CODE
  385. export NETDATA_WARNINGS="${NETDATA_WARNINGS}${SAVED_WARNINGS}"
  386. fi
  387. fi
  388. fi
  389. }
  390. fatal() {
  391. printf >&2 "%s ABORTED %s %s \n\n" "${TPUT_BGRED}${TPUT_WHITE}${TPUT_BOLD}" "${TPUT_RESET}" "${1}"
  392. if [ -n "${NETDATA_SAVE_WARNINGS}" ]; then
  393. SAVED_WARNINGS="${SAVED_WARNINGS}\n - ${1}"
  394. fi
  395. exit_reason "${1}" "${2}"
  396. exit 1
  397. }
  398. warning() {
  399. printf >&2 "%s WARNING %s %s\n\n" "${TPUT_BGYELLOW}${TPUT_BLACK}${TPUT_BOLD}" "${TPUT_RESET}" "${1}"
  400. if [ -n "${NETDATA_SAVE_WARNINGS}" ]; then
  401. SAVED_WARNINGS="${SAVED_WARNINGS}\n - ${1}"
  402. fi
  403. }
  404. run_ok() {
  405. printf >&2 "%s OK %s %s\n\n" "${TPUT_BGGREEN}${TPUT_WHITE}${TPUT_BOLD}" "${TPUT_RESET}" "${1:-''}"
  406. }
  407. run_failed() {
  408. printf >&2 "%s FAILED %s %s\n\n" "${TPUT_BGRED}${TPUT_WHITE}${TPUT_BOLD}" "${TPUT_RESET}" "${1:-''}"
  409. if [ -n "${NETDATA_SAVE_WARNINGS}" ] && [ -n "${1:-''}" ]; then
  410. SAVED_WARNINGS="${SAVED_WARNINGS}\n - ${1}"
  411. fi
  412. }
  413. ESCAPED_PRINT_METHOD=
  414. if printf "%s " test > /dev/null 2>&1; then
  415. ESCAPED_PRINT_METHOD="printfq"
  416. fi
  417. escaped_print() {
  418. if [ "${ESCAPED_PRINT_METHOD}" = "printfq" ]; then
  419. printf "%s " "${@}"
  420. else
  421. printf "%s" "${*}"
  422. fi
  423. return 0
  424. }
  425. run_logfile="/dev/null"
  426. run() {
  427. local_user="${USER--}"
  428. local_dir="${PWD}"
  429. if [ "${UID}" = "0" ]; then
  430. info="[root ${local_dir}]# "
  431. info_console="[${TPUT_DIM}${local_dir}${TPUT_RESET}]# "
  432. else
  433. info="[${local_user} ${local_dir}]$ "
  434. info_console="[${TPUT_DIM}${local_dir}${TPUT_RESET}]$ "
  435. fi
  436. {
  437. printf "%s" "${info}"
  438. escaped_print "${@}"
  439. printf "%s" " ... "
  440. } >> "${run_logfile}"
  441. printf >&2 "%s" "${info_console}${TPUT_BOLD}${TPUT_YELLOW}"
  442. escaped_print >&2 "${@}"
  443. printf >&2 "%s\n" "${TPUT_RESET}"
  444. "${@}"
  445. ret=$?
  446. if [ ${ret} -ne 0 ]; then
  447. run_failed
  448. printf >> "${run_logfile}" "FAILED with exit code %s\n" "${ret}"
  449. if [ -n "${NETDATA_SAVE_WARNINGS}" ]; then
  450. SAVED_WARNINGS="${SAVED_WARNINGS}\n - Command '${*}' failed with exit code ${ret}."
  451. fi
  452. else
  453. run_ok
  454. printf >> "${run_logfile}" "OK\n"
  455. fi
  456. return ${ret}
  457. }
  458. iscontainer() {
  459. # man systemd-detect-virt
  460. cmd=$(command -v systemd-detect-virt 2> /dev/null)
  461. if [ -n "${cmd}" ] && [ -x "${cmd}" ]; then
  462. "${cmd}" --container > /dev/null 2>&1 && return 0
  463. fi
  464. # /proc/1/sched exposes the host's pid of our init !
  465. # http://stackoverflow.com/a/37016302
  466. pid=$(head -n 1 /proc/1/sched 2> /dev/null | {
  467. # shellcheck disable=SC2034
  468. IFS='(),#:' read -r name pid th threads
  469. echo "$pid"
  470. })
  471. if [ -n "${pid}" ]; then
  472. pid=$((pid + 0))
  473. [ ${pid} -gt 1 ] && return 0
  474. fi
  475. # lxc sets environment variable 'container'
  476. # shellcheck disable=SC2154
  477. [ -n "${container}" ] && return 0
  478. # docker creates /.dockerenv
  479. # http://stackoverflow.com/a/25518345
  480. [ -f "/.dockerenv" ] && return 0
  481. # ubuntu and debian supply /bin/running-in-container
  482. # https://www.apt-browse.org/browse/ubuntu/trusty/main/i386/upstart/1.12.1-0ubuntu4/file/bin/running-in-container
  483. if [ -x "/bin/running-in-container" ]; then
  484. "/bin/running-in-container" > /dev/null 2>&1 && return 0
  485. fi
  486. return 1
  487. }
  488. get_os_key() {
  489. if [ -f /etc/os-release ]; then
  490. # shellcheck disable=SC1091
  491. . /etc/os-release || return 1
  492. echo "${ID}-${VERSION_ID}"
  493. elif [ -f /etc/redhat-release ]; then
  494. cat /etc/redhat-release
  495. else
  496. echo "unknown"
  497. fi
  498. }
  499. get_group(){
  500. if command -v getent > /dev/null 2>&1; then
  501. getent group "${1:-""}"
  502. else
  503. grep "^${1}:" /etc/group
  504. fi
  505. }
  506. issystemd() {
  507. pids=''
  508. p=''
  509. myns=''
  510. ns=''
  511. systemctl=''
  512. # if the directory /lib/systemd/system OR /usr/lib/systemd/system (SLES 12.x) does not exit, it is not systemd
  513. if [ ! -d /lib/systemd/system ] && [ ! -d /usr/lib/systemd/system ]; then
  514. return 1
  515. fi
  516. # if there is no systemctl command, it is not systemd
  517. systemctl=$(command -v systemctl 2> /dev/null)
  518. if [ -z "${systemctl}" ] || [ ! -x "${systemctl}" ]; then
  519. return 1
  520. fi
  521. # if pid 1 is systemd, it is systemd
  522. [ "$(basename "$(readlink /proc/1/exe)" 2> /dev/null)" = "systemd" ] && return 0
  523. # if systemd is not running, it is not systemd
  524. pids=$(safe_pidof systemd 2> /dev/null)
  525. [ -z "${pids}" ] && return 1
  526. # check if the running systemd processes are not in our namespace
  527. myns="$(readlink /proc/self/ns/pid 2> /dev/null)"
  528. for p in ${pids}; do
  529. ns="$(readlink "/proc/${p}/ns/pid" 2> /dev/null)"
  530. # if pid of systemd is in our namespace, it is systemd
  531. [ -n "${myns}" ] && [ "${myns}" = "${ns}" ] && return 0
  532. done
  533. # else, it is not systemd
  534. return 1
  535. }
  536. get_systemd_service_dir() {
  537. if [ -w "/lib/systemd/system" ]; then
  538. echo "/lib/systemd/system"
  539. elif [ -w "/usr/lib/systemd/system" ]; then
  540. echo "/usr/lib/systemd/system"
  541. elif [ -w "/etc/systemd/system" ]; then
  542. echo "/etc/systemd/system"
  543. fi
  544. }
  545. run_install_service_script() {
  546. if [ -z "${tmpdir}" ]; then
  547. tmpdir="${TMPDIR:-/tmp}"
  548. fi
  549. # shellcheck disable=SC2154
  550. save_path="${tmpdir}/netdata-service-cmds"
  551. # shellcheck disable=SC2068
  552. "${NETDATA_PREFIX}/usr/libexec/netdata/install-service.sh" --save-cmds "${save_path}" ${@}
  553. case $? in
  554. 0)
  555. if [ -r "${save_path}" ]; then
  556. # shellcheck disable=SC1090
  557. . "${save_path}"
  558. fi
  559. if [ -z "${NETDATA_INSTALLER_START_CMD}" ]; then
  560. if [ -n "${NETDATA_START_CMD}" ]; then
  561. NETDATA_INSTALLER_START_CMD="${NETDATA_START_CMD}"
  562. else
  563. NETDATA_INSTALLER_START_CMD="netdata"
  564. fi
  565. fi
  566. ;;
  567. 1)
  568. if [ -z "${NETDATA_SERVICE_WARNED_1}" ]; then
  569. warning "Intenral error encountered while attempting to install or manage Netdata as a system service. This is probably a bug."
  570. NETDATA_SERVICE_WARNED_1=1
  571. fi
  572. ;;
  573. 2)
  574. if [ -z "${NETDATA_SERVICE_WARNED_2}" ]; then
  575. 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."
  576. NETDATA_SERVICE_WARNED_2=1
  577. fi
  578. ;;
  579. 3)
  580. if [ -z "${NETDATA_SERVICE_WARNED_3}" ]; then
  581. warning "Detected an unsupported system service manager. Manual setup will be required to manage Netdata as a system service."
  582. NETDATA_SERVICE_WARNED_3=1
  583. fi
  584. ;;
  585. 4)
  586. if [ -z "${NETDATA_SERVICE_WARNED_4}" ]; then
  587. 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."
  588. NETDATA_SERVICE_WARNED_4=1
  589. fi
  590. ;;
  591. 5)
  592. if [ -z "${NETDATA_SERVICE_WARNED_5}" ]; then
  593. warning "We do not support managing Netdata as a system service on this platform. Manual setup will be required."
  594. NETDATA_SERVICE_WARNED_5=1
  595. fi
  596. ;;
  597. esac
  598. }
  599. install_netdata_service() {
  600. if [ "${UID}" -eq 0 ]; then
  601. if [ -x "${NETDATA_PREFIX}/usr/libexec/netdata/install-service.sh" ]; then
  602. run_install_service_script && return 0
  603. else
  604. warning "Could not find service install script, not installing Netdata as a system service."
  605. fi
  606. fi
  607. return 1
  608. }
  609. # -----------------------------------------------------------------------------
  610. # stop netdata
  611. pidisnetdata() {
  612. if [ -d /proc/self ]; then
  613. if [ -z "$1" ] || [ ! -f "/proc/$1/stat" ]; then
  614. return 1
  615. fi
  616. [ "$(cut -d '(' -f 2 "/proc/$1/stat" | cut -d ')' -f 1)" = "netdata" ] && return 0
  617. return 1
  618. fi
  619. return 0
  620. }
  621. stop_netdata_on_pid() {
  622. pid="${1}"
  623. ret=0
  624. count=0
  625. pidisnetdata "${pid}" || return 0
  626. printf >&2 "Stopping netdata on pid %s ..." "${pid}"
  627. while [ -n "${pid}" ] && [ ${ret} -eq 0 ]; do
  628. if [ ${count} -gt 24 ]; then
  629. warning "Cannot stop netdata agent with PID ${pid}."
  630. return 1
  631. fi
  632. count=$((count + 1))
  633. pidisnetdata "${pid}" || ret=1
  634. if [ ${ret} -eq 1 ]; then
  635. break
  636. fi
  637. if [ ${count} -lt 12 ]; then
  638. run kill "${pid}" 2> /dev/null
  639. ret=$?
  640. else
  641. run kill -9 "${pid}" 2> /dev/null
  642. ret=$?
  643. fi
  644. test ${ret} -eq 0 && printf >&2 "." && sleep 5
  645. done
  646. echo >&2
  647. if [ ${ret} -eq 0 ]; then
  648. warning "Failed to stop netdata agent process with PID ${pid}."
  649. return 1
  650. fi
  651. echo >&2 "netdata on pid ${pid} stopped."
  652. return 0
  653. }
  654. netdata_pids() {
  655. myns="$(readlink /proc/self/ns/pid 2> /dev/null)"
  656. for p in \
  657. $(cat /var/run/netdata.pid 2> /dev/null) \
  658. $(cat /var/run/netdata/netdata.pid 2> /dev/null) \
  659. $(safe_pidof netdata 2> /dev/null); do
  660. ns="$(readlink "/proc/${p}/ns/pid" 2> /dev/null)"
  661. if [ -z "${myns}" ] || [ -z "${ns}" ] || [ "${myns}" = "${ns}" ]; then
  662. pidisnetdata "${p}" && echo "${p}"
  663. fi
  664. done
  665. }
  666. stop_all_netdata() {
  667. stop_success=0
  668. if [ -x "${NETDATA_PREFIX}/usr/libexec/netdata/install-service.sh" ]; then
  669. run_install_service_script --cmds-only
  670. fi
  671. if [ "${UID}" -eq 0 ]; then
  672. uname="$(uname 2>/dev/null)"
  673. # Any of these may fail, but we need to not bail if they do.
  674. if [ -n "${NETDATA_STOP_CMD}" ]; then
  675. if ${NETDATA_STOP_CMD}; then
  676. stop_success=1
  677. sleep 5
  678. fi
  679. elif issystemd; then
  680. if systemctl stop netdata; then
  681. stop_success=1
  682. sleep 5
  683. fi
  684. elif [ "${uname}" = "Darwin" ]; then
  685. if launchctl stop netdata; then
  686. stop_success=1
  687. sleep 5
  688. fi
  689. elif [ "${uname}" = "FreeBSD" ]; then
  690. if /etc/rc.d/netdata stop; then
  691. stop_success=1
  692. sleep 5
  693. fi
  694. else
  695. if service netdata stop; then
  696. stop_success=1
  697. sleep 5
  698. fi
  699. fi
  700. fi
  701. if [ "$stop_success" = "0" ]; then
  702. if [ -n "$(netdata_pids)" ] && [ -n "$(command -v netdatacli)" ]; then
  703. netdatacli shutdown-agent
  704. sleep 20
  705. fi
  706. for p in $(netdata_pids); do
  707. # shellcheck disable=SC2086
  708. stop_netdata_on_pid ${p}
  709. done
  710. fi
  711. }
  712. # -----------------------------------------------------------------------------
  713. # restart netdata
  714. restart_netdata() {
  715. netdata="${1}"
  716. shift
  717. started=0
  718. progress "Restarting netdata instance"
  719. if [ -x "${NETDATA_PREFIX}/usr/libexec/netdata/install-service.sh" ]; then
  720. run_install_service_script --cmds-only
  721. fi
  722. if [ -z "${NETDATA_INSTALLER_START_CMD}" ]; then
  723. if [ -n "${NETDATA_START_CMD}" ]; then
  724. NETDATA_INSTALLER_START_CMD="${NETDATA_START_CMD}"
  725. else
  726. NETDATA_INSTALLER_START_CMD="${netdata}"
  727. fi
  728. fi
  729. if [ "${UID}" -eq 0 ]; then
  730. echo >&2
  731. echo >&2 "Stopping all netdata threads"
  732. run stop_all_netdata
  733. echo >&2 "Starting netdata using command '${NETDATA_INSTALLER_START_CMD}'"
  734. # shellcheck disable=SC2086
  735. run ${NETDATA_INSTALLER_START_CMD} && started=1
  736. if [ ${started} -eq 1 ] && sleep 5 && [ -z "$(netdata_pids)" ]; then
  737. echo >&2 "Ooops! it seems netdata is not started."
  738. started=0
  739. fi
  740. if [ ${started} -eq 0 ]; then
  741. echo >&2 "Attempting another netdata start using command '${NETDATA_INSTALLER_START_CMD}'"
  742. # shellcheck disable=SC2086
  743. run ${NETDATA_INSTALLER_START_CMD} && started=1
  744. fi
  745. if [ ${started} -eq 1 ] && sleep 5 && [ -z "$(netdata_pids)" ]; then
  746. echo >&2 "Hm... it seems netdata is still not started."
  747. started=0
  748. fi
  749. fi
  750. if [ ${started} -eq 0 ]; then
  751. # still not started... another forced attempt, just run the binary
  752. warning "Netdata service still not started, attempting another forced restart by running '${netdata} ${*}'"
  753. run stop_all_netdata
  754. run "${netdata}" "${@}"
  755. return $?
  756. fi
  757. return 0
  758. }
  759. # -----------------------------------------------------------------------------
  760. # install netdata logrotate
  761. install_netdata_logrotate() {
  762. src="${NETDATA_PREFIX}/usr/lib/netdata/system/logrotate/netdata"
  763. if [ "${UID}" -eq 0 ]; then
  764. if [ -d /etc/logrotate.d ]; then
  765. if [ ! -f /etc/logrotate.d/netdata ]; then
  766. run cp "${src}" /etc/logrotate.d/netdata
  767. fi
  768. if [ -f /etc/logrotate.d/netdata ]; then
  769. run chmod 644 /etc/logrotate.d/netdata
  770. fi
  771. return 0
  772. fi
  773. fi
  774. return 1
  775. }
  776. # -----------------------------------------------------------------------------
  777. # install netdata journald configuration
  778. install_netdata_journald_conf() {
  779. src="${NETDATA_PREFIX}/usr/lib/netdata/system/systemd/journald@netdata.conf"
  780. [ ! -d /usr/lib/systemd/ ] && return 0
  781. [ "${UID}" -ne 0 ] && return 1
  782. if [ ! -d /usr/lib/systemd/journald@netdata.conf.d/ ]; then
  783. run mkdir /usr/lib/systemd/journald@netdata.conf.d/
  784. fi
  785. run cp "${src}" /usr/lib/systemd/journald@netdata.conf.d/netdata.conf
  786. if [ -f /usr/lib/systemd/journald@netdata.conf.d/netdata.conf ]; then
  787. run chmod 644 /usr/lib/systemd/journald@netdata.conf.d/netdata.conf
  788. fi
  789. return 0
  790. }
  791. # -----------------------------------------------------------------------------
  792. # create netdata.conf
  793. create_netdata_conf() {
  794. path="${1}"
  795. url="${2}"
  796. if [ -s "${path}" ]; then
  797. return 0
  798. fi
  799. if [ -n "$url" ]; then
  800. echo >&2 "Downloading default configuration from netdata..."
  801. sleep 5
  802. # remove a possibly obsolete configuration file
  803. [ -f "${path}.new" ] && rm "${path}.new"
  804. # disable a proxy to get data from the local netdata
  805. export http_proxy=
  806. export https_proxy=
  807. check_for_curl
  808. if [ -n "${curl}" ]; then
  809. run "${curl}" -sSL --connect-timeout 10 --retry 3 "${url}" > "${path}.new"
  810. elif command -v wget 1> /dev/null 2>&1; then
  811. run wget -T 15 -O - "${url}" > "${path}.new"
  812. fi
  813. if [ -s "${path}.new" ]; then
  814. run mv "${path}.new" "${path}"
  815. run_ok "New configuration saved for you to edit at ${path}"
  816. else
  817. [ -f "${path}.new" ] && rm "${path}.new"
  818. run_failed "Cannot download configuration from netdata daemon using url '${url}'"
  819. url=''
  820. fi
  821. fi
  822. if [ -z "$url" ]; then
  823. cat << EOF > "${path}"
  824. # netdata can generate its own config which is available at 'http://<IP>:19999/netdata.conf'
  825. # You can download it using:
  826. # curl -o ${path} http://localhost:19999/netdata.conf
  827. # or
  828. # wget -O ${path} http://localhost:19999/netdata.conf
  829. EOF
  830. fi
  831. }
  832. portable_add_user() {
  833. username="${1}"
  834. homedir="${2}"
  835. [ -z "${homedir}" ] && homedir="/tmp"
  836. # Check if user exists
  837. if command -v getent > /dev/null 2>&1; then
  838. if getent passwd "${username}" > /dev/null 2>&1; then
  839. echo >&2 "User '${username}' already exists."
  840. return 0
  841. fi
  842. elif command -v dscl > /dev/null 2>&1; then
  843. if dscl . read /Users/"${username}" >/dev/null 2>&1; then
  844. echo >&2 "User '${username}' already exists."
  845. return 0
  846. fi
  847. else
  848. if cut -d ':' -f 1 < /etc/passwd | grep "^${username}$" 1> /dev/null 2>&1; then
  849. echo >&2 "User '${username}' already exists."
  850. return 0
  851. fi
  852. fi
  853. echo >&2 "Adding ${username} user account with home ${homedir} ..."
  854. nologin="$(command -v nologin || echo '/bin/false')"
  855. if command -v useradd 1> /dev/null 2>&1; then
  856. run useradd -r -g "${username}" -c "${username}" -s "${nologin}" --no-create-home -d "${homedir}" "${username}" && return 0
  857. elif command -v pw 1> /dev/null 2>&1; then
  858. run pw useradd "${username}" -d "${homedir}" -g "${username}" -s "${nologin}" && return 0
  859. elif command -v adduser 1> /dev/null 2>&1; then
  860. run adduser -h "${homedir}" -s "${nologin}" -D -G "${username}" "${username}" && return 0
  861. elif command -v sysadminctl 1> /dev/null 2>&1; then
  862. gid=$(dscl . read /Groups/"${username}" 2>/dev/null | grep PrimaryGroupID | grep -Eo "[0-9]+")
  863. if run sysadminctl -addUser "${username}" -shell /usr/bin/false -home /var/empty -GID "$gid"; then
  864. # FIXME: I think the proper solution is to create a role account:
  865. # -roleAccount + name starting with _ and UID in 200-400 range.
  866. run dscl . create /Users/"${username}" IsHidden 1
  867. return 0
  868. fi
  869. fi
  870. warning "Failed to add ${username} user account!"
  871. return 1
  872. }
  873. portable_add_group() {
  874. groupname="${1}"
  875. # Check if group exist
  876. if get_group "${groupname}" > /dev/null 2>&1; then
  877. echo >&2 "Group '${groupname}' already exists."
  878. return 0
  879. fi
  880. echo >&2 "Adding ${groupname} user group ..."
  881. # Linux
  882. if command -v groupadd 1> /dev/null 2>&1; then
  883. run groupadd -r "${groupname}" && return 0
  884. elif command -v pw 1> /dev/null 2>&1; then
  885. run pw groupadd "${groupname}" && return 0
  886. elif command -v addgroup 1> /dev/null 2>&1; then
  887. run addgroup "${groupname}" && return 0
  888. elif command -v dseditgroup 1> /dev/null 2>&1; then
  889. dseditgroup -o create "${groupname}" && return 0
  890. fi
  891. warning >&2 "Failed to add ${groupname} user group !"
  892. return 1
  893. }
  894. portable_add_user_to_group() {
  895. groupname="${1}"
  896. username="${2}"
  897. # Check if group exist
  898. if ! get_group "${groupname}" > /dev/null 2>&1; then
  899. echo >&2 "Group '${groupname}' does not exist."
  900. # Don’t treat this as a failure, if the group does not exist we should not be trying to add the user to it.
  901. return 0
  902. fi
  903. # Check if user is in group
  904. if get_group "${groupname}" | cut -d ':' -f 4 | grep -wq "${username}"; then
  905. # username is already there
  906. echo >&2 "User '${username}' is already in group '${groupname}'."
  907. return 0
  908. else
  909. # username is not in group
  910. echo >&2 "Adding ${username} user to the ${groupname} group ..."
  911. # Linux
  912. if command -v usermod 1> /dev/null 2>&1; then
  913. run usermod -a -G "${groupname}" "${username}" && return 0
  914. elif command -v pw 1> /dev/null 2>&1; then
  915. run pw groupmod "${groupname}" -m "${username}" && return 0
  916. elif command -v addgroup 1> /dev/null 2>&1; then
  917. run addgroup "${username}" "${groupname}" && return 0
  918. elif command -v dseditgroup 1> /dev/null 2>&1; then
  919. dseditgroup -u "${username}" "${groupname}" && return 0
  920. fi
  921. warning >&2 "Failed to add user ${username} to group ${groupname}!"
  922. return 1
  923. fi
  924. }
  925. safe_sha256sum() {
  926. # Within the context of the installer, we only use -c option that is common between the two commands
  927. # We will have to reconsider if we start non-common options
  928. if command -v sha256sum > /dev/null 2>&1; then
  929. sha256sum "$@"
  930. elif command -v shasum > /dev/null 2>&1; then
  931. shasum -a 256 "$@"
  932. else
  933. fatal "I could not find a suitable checksum binary to use" "L0001"
  934. fi
  935. }
  936. _get_scheduler_type() {
  937. if _get_intervaldir > /dev/null ; then
  938. echo 'interval'
  939. elif issystemd ; then
  940. echo 'systemd'
  941. elif [ -d /etc/cron.d ] ; then
  942. echo 'crontab'
  943. else
  944. echo 'none'
  945. fi
  946. }
  947. _get_intervaldir() {
  948. if [ -d /etc/cron.daily ]; then
  949. echo /etc/cron.daily
  950. elif [ -d /etc/periodic/daily ]; then
  951. echo /etc/periodic/daily
  952. else
  953. return 1
  954. fi
  955. return 0
  956. }
  957. install_netdata_updater() {
  958. if [ "${INSTALLER_DIR}" ] && [ -f "${INSTALLER_DIR}/packaging/installer/netdata-updater.sh" ]; then
  959. cat "${INSTALLER_DIR}/packaging/installer/netdata-updater.sh" > "${NETDATA_PREFIX}/usr/libexec/netdata/netdata-updater.sh" || return 1
  960. fi
  961. if [ "${NETDATA_SOURCE_DIR}" ] && [ -f "${NETDATA_SOURCE_DIR}/packaging/installer/netdata-updater.sh" ]; then
  962. cat "${NETDATA_SOURCE_DIR}/packaging/installer/netdata-updater.sh" > "${NETDATA_PREFIX}/usr/libexec/netdata/netdata-updater.sh" || return 1
  963. fi
  964. # these files are installed by cmake
  965. libsysdir="${NETDATA_PREFIX}/usr/lib/netdata/system/systemd/"
  966. if [ -d "${libsysdir}" ] && issystemd && [ -n "$(get_systemd_service_dir)" ]; then
  967. cat "${libsysdir}/netdata-updater.timer" > "$(get_systemd_service_dir)/netdata-updater.timer"
  968. cat "${libsysdir}/netdata-updater.service" > "$(get_systemd_service_dir)/netdata-updater.service"
  969. fi
  970. 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
  971. chmod 0755 "${NETDATA_PREFIX}/usr/libexec/netdata/netdata-updater.sh"
  972. echo >&2 "Update script is located at ${TPUT_GREEN}${TPUT_BOLD}${NETDATA_PREFIX}/usr/libexec/netdata/netdata-updater.sh${TPUT_RESET}"
  973. echo >&2
  974. return 0
  975. }
  976. set_netdata_updater_channel() {
  977. sed -i -e "s/^RELEASE_CHANNEL=.*/RELEASE_CHANNEL=\"${RELEASE_CHANNEL}\"/" "${NETDATA_USER_CONFIG_DIR}/.environment"
  978. }