functions.sh 32 KB

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