functions.sh 31 KB

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