functions.sh 30 KB

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