functions.sh 31 KB

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