functions.sh 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080
  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. cat << EOF > "${path}"
  770. # netdata can generate its own config which is available at 'http://<IP>:19999/netdata.conf'
  771. # You can download it using:
  772. # curl -o ${path} http://localhost:19999/netdata.conf
  773. # or
  774. # wget -O ${path} http://localhost:19999/netdata.conf
  775. EOF
  776. fi
  777. }
  778. portable_add_user() {
  779. username="${1}"
  780. homedir="${2}"
  781. [ -z "${homedir}" ] && homedir="/tmp"
  782. # Check if user exists
  783. if command -v getent > /dev/null 2>&1; then
  784. if getent passwd "${username}" > /dev/null 2>&1; then
  785. echo >&2 "User '${username}' already exists."
  786. return 0
  787. fi
  788. else
  789. if cut -d ':' -f 1 < /etc/passwd | grep "^${username}$" 1> /dev/null 2>&1; then
  790. echo >&2 "User '${username}' already exists."
  791. return 0
  792. fi
  793. fi
  794. echo >&2 "Adding ${username} user account with home ${homedir} ..."
  795. nologin="$(command -v nologin || echo '/bin/false')"
  796. if command -v useradd 1> /dev/null 2>&1; then
  797. run useradd -r -g "${username}" -c "${username}" -s "${nologin}" --no-create-home -d "${homedir}" "${username}" && return 0
  798. elif command -v pw 1> /dev/null 2>&1; then
  799. run pw useradd "${username}" -d "${homedir}" -g "${username}" -s "${nologin}" && return 0
  800. elif command -v adduser 1> /dev/null 2>&1; then
  801. run adduser -h "${homedir}" -s "${nologin}" -D -G "${username}" "${username}" && return 0
  802. elif command -v sysadminctl 1> /dev/null 2>&1; then
  803. run sysadminctl -addUser "${username}" && return 0
  804. fi
  805. warning "Failed to add ${username} user account!"
  806. return 1
  807. }
  808. portable_add_group() {
  809. groupname="${1}"
  810. # Check if group exist
  811. if get_group "${groupname}" > /dev/null 2>&1; then
  812. echo >&2 "Group '${groupname}' already exists."
  813. return 0
  814. fi
  815. echo >&2 "Adding ${groupname} user group ..."
  816. # Linux
  817. if command -v groupadd 1> /dev/null 2>&1; then
  818. run groupadd -r "${groupname}" && return 0
  819. elif command -v pw 1> /dev/null 2>&1; then
  820. run pw groupadd "${groupname}" && return 0
  821. elif command -v addgroup 1> /dev/null 2>&1; then
  822. run addgroup "${groupname}" && return 0
  823. elif command -v dseditgroup 1> /dev/null 2>&1; then
  824. dseditgroup -o create "${groupname}" && return 0
  825. fi
  826. warning >&2 "Failed to add ${groupname} user group !"
  827. return 1
  828. }
  829. portable_add_user_to_group() {
  830. groupname="${1}"
  831. username="${2}"
  832. # Check if group exist
  833. if ! get_group "${groupname}" > /dev/null 2>&1; then
  834. echo >&2 "Group '${groupname}' does not exist."
  835. # Don’t treat this as a failure, if the group does not exist we should not be trying to add the user to it.
  836. return 0
  837. fi
  838. # Check if user is in group
  839. if get_group "${groupname}" | cut -d ':' -f 4 | grep -wq "${username}"; then
  840. # username is already there
  841. echo >&2 "User '${username}' is already in group '${groupname}'."
  842. return 0
  843. else
  844. # username is not in group
  845. echo >&2 "Adding ${username} user to the ${groupname} group ..."
  846. # Linux
  847. if command -v usermod 1> /dev/null 2>&1; then
  848. run usermod -a -G "${groupname}" "${username}" && return 0
  849. elif command -v pw 1> /dev/null 2>&1; then
  850. run pw groupmod "${groupname}" -m "${username}" && return 0
  851. elif command -v addgroup 1> /dev/null 2>&1; then
  852. run addgroup "${username}" "${groupname}" && return 0
  853. elif command -v dseditgroup 1> /dev/null 2>&1; then
  854. dseditgroup -u "${username}" "${groupname}" && return 0
  855. fi
  856. warning >&2 "Failed to add user ${username} to group ${groupname}!"
  857. return 1
  858. fi
  859. }
  860. safe_sha256sum() {
  861. # Within the context of the installer, we only use -c option that is common between the two commands
  862. # We will have to reconsider if we start non-common options
  863. if command -v sha256sum > /dev/null 2>&1; then
  864. sha256sum "$@"
  865. elif command -v shasum > /dev/null 2>&1; then
  866. shasum -a 256 "$@"
  867. else
  868. fatal "I could not find a suitable checksum binary to use" "L0001"
  869. fi
  870. }
  871. _get_scheduler_type() {
  872. if _get_intervaldir > /dev/null ; then
  873. echo 'interval'
  874. elif issystemd ; then
  875. echo 'systemd'
  876. elif [ -d /etc/cron.d ] ; then
  877. echo 'crontab'
  878. else
  879. echo 'none'
  880. fi
  881. }
  882. _get_intervaldir() {
  883. if [ -d /etc/cron.daily ]; then
  884. echo /etc/cron.daily
  885. elif [ -d /etc/periodic/daily ]; then
  886. echo /etc/periodic/daily
  887. else
  888. return 1
  889. fi
  890. return 0
  891. }
  892. install_netdata_updater() {
  893. if [ "${INSTALLER_DIR}" ] && [ -f "${INSTALLER_DIR}/packaging/installer/netdata-updater.sh" ]; then
  894. cat "${INSTALLER_DIR}/packaging/installer/netdata-updater.sh" > "${NETDATA_PREFIX}/usr/libexec/netdata/netdata-updater.sh" || return 1
  895. fi
  896. if [ "${NETDATA_SOURCE_DIR}" ] && [ -f "${NETDATA_SOURCE_DIR}/packaging/installer/netdata-updater.sh" ]; then
  897. cat "${NETDATA_SOURCE_DIR}/packaging/installer/netdata-updater.sh" > "${NETDATA_PREFIX}/usr/libexec/netdata/netdata-updater.sh" || return 1
  898. fi
  899. if issystemd && [ -n "$(get_systemd_service_dir)" ]; then
  900. cat "${NETDATA_SOURCE_DIR}/system/systemd/netdata-updater.timer" > "$(get_systemd_service_dir)/netdata-updater.timer"
  901. cat "${NETDATA_SOURCE_DIR}/system/systemd/netdata-updater.service" > "$(get_systemd_service_dir)/netdata-updater.service"
  902. fi
  903. 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
  904. chmod 0755 "${NETDATA_PREFIX}/usr/libexec/netdata/netdata-updater.sh"
  905. echo >&2 "Update script is located at ${TPUT_GREEN}${TPUT_BOLD}${NETDATA_PREFIX}/usr/libexec/netdata/netdata-updater.sh${TPUT_RESET}"
  906. echo >&2
  907. return 0
  908. }
  909. set_netdata_updater_channel() {
  910. sed -i -e "s/^RELEASE_CHANNEL=.*/RELEASE_CHANNEL=\"${RELEASE_CHANNEL}\"/" "${NETDATA_USER_CONFIG_DIR}/.environment"
  911. }