functions.sh 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094
  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. if [ "${UID}" -eq 0 ]; then
  724. if [ -d /etc/logrotate.d ]; then
  725. if [ ! -f /etc/logrotate.d/netdata ]; then
  726. run cp system/logrotate/netdata /etc/logrotate.d/netdata
  727. fi
  728. if [ -f /etc/logrotate.d/netdata ]; then
  729. run chmod 644 /etc/logrotate.d/netdata
  730. fi
  731. return 0
  732. fi
  733. fi
  734. return 1
  735. }
  736. # -----------------------------------------------------------------------------
  737. # create netdata.conf
  738. create_netdata_conf() {
  739. path="${1}"
  740. url="${2}"
  741. if [ -s "${path}" ]; then
  742. return 0
  743. fi
  744. if [ -n "$url" ]; then
  745. echo >&2 "Downloading default configuration from netdata..."
  746. sleep 5
  747. # remove a possibly obsolete configuration file
  748. [ -f "${path}.new" ] && rm "${path}.new"
  749. # disable a proxy to get data from the local netdata
  750. export http_proxy=
  751. export https_proxy=
  752. check_for_curl
  753. if [ -n "${curl}" ]; then
  754. run "${curl}" -sSL --connect-timeout 10 --retry 3 "${url}" > "${path}.new"
  755. elif command -v wget 1> /dev/null 2>&1; then
  756. run wget -T 15 -O - "${url}" > "${path}.new"
  757. fi
  758. if [ -s "${path}.new" ]; then
  759. run mv "${path}.new" "${path}"
  760. run_ok "New configuration saved for you to edit at ${path}"
  761. else
  762. [ -f "${path}.new" ] && rm "${path}.new"
  763. run_failed "Cannot download configuration from netdata daemon using url '${url}'"
  764. url=''
  765. fi
  766. fi
  767. if [ -z "$url" ]; then
  768. echo "# netdata can generate its own config which is available at 'http://<netdata_ip>/netdata.conf'" > "${path}"
  769. echo "# You can download it with command like: 'wget -O ${path} http://localhost:19999/netdata.conf'" >> "${path}"
  770. fi
  771. }
  772. portable_add_user() {
  773. username="${1}"
  774. homedir="${2}"
  775. [ -z "${homedir}" ] && homedir="/tmp"
  776. # Check if user exists
  777. if cut -d ':' -f 1 < /etc/passwd | grep "^${username}$" 1> /dev/null 2>&1; then
  778. echo >&2 "User '${username}' already exists."
  779. return 0
  780. fi
  781. echo >&2 "Adding ${username} user account with home ${homedir} ..."
  782. nologin="$(command -v nologin || echo '/bin/false')"
  783. # Linux
  784. if command -v useradd 1> /dev/null 2>&1; then
  785. run useradd -r -g "${username}" -c "${username}" -s "${nologin}" --no-create-home -d "${homedir}" "${username}" && return 0
  786. fi
  787. # FreeBSD
  788. if command -v pw 1> /dev/null 2>&1; then
  789. run pw useradd "${username}" -d "${homedir}" -g "${username}" -s "${nologin}" && return 0
  790. fi
  791. # BusyBox
  792. if command -v adduser 1> /dev/null 2>&1; then
  793. run adduser -h "${homedir}" -s "${nologin}" -D -G "${username}" "${username}" && return 0
  794. fi
  795. # mac OS
  796. if command -v sysadminctl 1> /dev/null 2>&1; then
  797. run sysadminctl -addUser "${username}" && return 0
  798. fi
  799. warning "Failed to add ${username} user account!"
  800. return 1
  801. }
  802. portable_add_group() {
  803. groupname="${1}"
  804. # Check if group exist
  805. if get_group "${groupname}" > /dev/null 2>&1; then
  806. echo >&2 "Group '${groupname}' already exists."
  807. return 0
  808. fi
  809. echo >&2 "Adding ${groupname} user group ..."
  810. # Linux
  811. if command -v groupadd 1> /dev/null 2>&1; then
  812. run groupadd -r "${groupname}" && return 0
  813. fi
  814. # FreeBSD
  815. if command -v pw 1> /dev/null 2>&1; then
  816. run pw groupadd "${groupname}" && return 0
  817. fi
  818. # BusyBox
  819. if command -v addgroup 1> /dev/null 2>&1; then
  820. run addgroup "${groupname}" && return 0
  821. fi
  822. # mac OS
  823. if 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. fi
  850. # FreeBSD
  851. if command -v pw 1> /dev/null 2>&1; then
  852. run pw groupmod "${groupname}" -m "${username}" && return 0
  853. fi
  854. # BusyBox
  855. if command -v addgroup 1> /dev/null 2>&1; then
  856. run addgroup "${username}" "${groupname}" && return 0
  857. fi
  858. # mac OS
  859. if command -v dseditgroup 1> /dev/null 2>&1; then
  860. dseditgroup -u "${username}" "${groupname}" && return 0
  861. fi
  862. warning >&2 "Failed to add user ${username} to group ${groupname}!"
  863. return 1
  864. fi
  865. }
  866. safe_sha256sum() {
  867. # Within the context of the installer, we only use -c option that is common between the two commands
  868. # We will have to reconsider if we start non-common options
  869. if command -v sha256sum > /dev/null 2>&1; then
  870. sha256sum "$@"
  871. elif command -v shasum > /dev/null 2>&1; then
  872. shasum -a 256 "$@"
  873. else
  874. fatal "I could not find a suitable checksum binary to use" "L0001"
  875. fi
  876. }
  877. _get_scheduler_type() {
  878. if _get_intervaldir > /dev/null ; then
  879. echo 'interval'
  880. elif issystemd ; then
  881. echo 'systemd'
  882. elif [ -d /etc/cron.d ] ; then
  883. echo 'crontab'
  884. else
  885. echo 'none'
  886. fi
  887. }
  888. _get_intervaldir() {
  889. if [ -d /etc/cron.daily ]; then
  890. echo /etc/cron.daily
  891. elif [ -d /etc/periodic/daily ]; then
  892. echo /etc/periodic/daily
  893. else
  894. return 1
  895. fi
  896. return 0
  897. }
  898. install_netdata_updater() {
  899. if [ "${INSTALLER_DIR}" ] && [ -f "${INSTALLER_DIR}/packaging/installer/netdata-updater.sh" ]; then
  900. cat "${INSTALLER_DIR}/packaging/installer/netdata-updater.sh" > "${NETDATA_PREFIX}/usr/libexec/netdata/netdata-updater.sh" || return 1
  901. fi
  902. if [ "${NETDATA_SOURCE_DIR}" ] && [ -f "${NETDATA_SOURCE_DIR}/packaging/installer/netdata-updater.sh" ]; then
  903. cat "${NETDATA_SOURCE_DIR}/packaging/installer/netdata-updater.sh" > "${NETDATA_PREFIX}/usr/libexec/netdata/netdata-updater.sh" || return 1
  904. fi
  905. if issystemd && [ -n "$(get_systemd_service_dir)" ]; then
  906. cat "${NETDATA_SOURCE_DIR}/system/systemd/netdata-updater.timer" > "$(get_systemd_service_dir)/netdata-updater.timer"
  907. cat "${NETDATA_SOURCE_DIR}/system/systemd/netdata-updater.service" > "$(get_systemd_service_dir)/netdata-updater.service"
  908. fi
  909. 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
  910. chmod 0755 "${NETDATA_PREFIX}/usr/libexec/netdata/netdata-updater.sh"
  911. echo >&2 "Update script is located at ${TPUT_GREEN}${TPUT_BOLD}${NETDATA_PREFIX}/usr/libexec/netdata/netdata-updater.sh${TPUT_RESET}"
  912. echo >&2
  913. return 0
  914. }
  915. set_netdata_updater_channel() {
  916. sed -i -e "s/^RELEASE_CHANNEL=.*/RELEASE_CHANNEL=\"${RELEASE_CHANNEL}\"/" "${NETDATA_USER_CONFIG_DIR}/.environment"
  917. }