functions.sh 30 KB

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