functions.sh 31 KB

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