functions.sh 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073
  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. issystemd() {
  348. pids=''
  349. p=''
  350. myns=''
  351. ns=''
  352. systemctl=''
  353. # if the directory /lib/systemd/system OR /usr/lib/systemd/system (SLES 12.x) does not exit, it is not systemd
  354. if [ ! -d /lib/systemd/system ] && [ ! -d /usr/lib/systemd/system ]; then
  355. return 1
  356. fi
  357. # if there is no systemctl command, it is not systemd
  358. systemctl=$(command -v systemctl 2> /dev/null)
  359. if [ -z "${systemctl}" ] || [ ! -x "${systemctl}" ]; then
  360. return 1
  361. fi
  362. # if pid 1 is systemd, it is systemd
  363. [ "$(basename "$(readlink /proc/1/exe)" 2> /dev/null)" = "systemd" ] && return 0
  364. # if systemd is not running, it is not systemd
  365. pids=$(safe_pidof systemd 2> /dev/null)
  366. [ -z "${pids}" ] && return 1
  367. # check if the running systemd processes are not in our namespace
  368. myns="$(readlink /proc/self/ns/pid 2> /dev/null)"
  369. for p in ${pids}; do
  370. ns="$(readlink "/proc/${p}/ns/pid" 2> /dev/null)"
  371. # if pid of systemd is in our namespace, it is systemd
  372. [ -n "${myns}" ] && [ "${myns}" = "${ns}" ] && return 0
  373. done
  374. # else, it is not systemd
  375. return 1
  376. }
  377. get_systemd_service_dir() {
  378. if [ -w "/lib/systemd/system" ]; then
  379. echo "/lib/systemd/system"
  380. elif [ -w "/usr/lib/systemd/system" ]; then
  381. echo "/usr/lib/systemd/system"
  382. elif [ -w "/etc/systemd/system" ]; then
  383. echo "/etc/systemd/system"
  384. fi
  385. }
  386. install_non_systemd_init() {
  387. [ "${UID}" != 0 ] && return 1
  388. key="$(get_os_key)"
  389. if [ -d /etc/init.d ] && [ ! -f /etc/init.d/netdata ]; then
  390. if expr "${key}" : "^(gentoo|alpine).*"; then
  391. echo >&2 "Installing OpenRC init file..."
  392. run cp system/netdata-openrc /etc/init.d/netdata &&
  393. run chmod 755 /etc/init.d/netdata &&
  394. run rc-update add netdata default &&
  395. return 0
  396. elif expr "${key}" : "^devuan*" || [ "${key}" = "debian-7" ] || [ "${key}" = "ubuntu-12.04" ] || [ "${key}" = "ubuntu-14.04" ]; then
  397. echo >&2 "Installing LSB init file..."
  398. run cp system/netdata-lsb /etc/init.d/netdata &&
  399. run chmod 755 /etc/init.d/netdata &&
  400. run update-rc.d netdata defaults &&
  401. run update-rc.d netdata enable &&
  402. return 0
  403. 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
  404. echo >&2 "Installing init.d file..."
  405. run cp system/netdata-init-d /etc/init.d/netdata &&
  406. run chmod 755 /etc/init.d/netdata &&
  407. run chkconfig netdata on &&
  408. return 0
  409. else
  410. warning "Could not determine what type of init script to install on this system."
  411. return 1
  412. fi
  413. elif [ -f /etc/init.d/netdata ]; then
  414. echo >&2 "file '/etc/init.d/netdata' already exists."
  415. return 0
  416. else
  417. warning "Could not determine what type of init script to install on this system."
  418. fi
  419. return 1
  420. }
  421. run_install_service_script() {
  422. if [ -z "${tmpdir}" ]; then
  423. tmpdir="${TMPDIR:-/tmp}"
  424. fi
  425. # shellcheck disable=SC2154
  426. save_path="${tmpdir}/netdata-service-cmds"
  427. # shellcheck disable=SC2068
  428. "${NETDATA_PREFIX}/usr/libexec/netdata/install-service.sh" --save-cmds "${save_path}" ${@}
  429. case $? in
  430. 0)
  431. if [ -r "${save_path}" ]; then
  432. # shellcheck disable=SC1090
  433. . "${save_path}"
  434. fi
  435. if [ -z "${NETDATA_INSTALLER_START_CMD}" ]; then
  436. if [ -n "${NETDATA_START_CMD}" ]; then
  437. NETDATA_INSTALLER_START_CMD="${NETDATA_START_CMD}"
  438. else
  439. NETDATA_INSTALLER_START_CMD="netdata"
  440. fi
  441. fi
  442. ;;
  443. 1)
  444. if [ -z "${NETDATA_SERVICE_WARNED_1}" ]; then
  445. warning "Intenral error encountered while attempting to install or manage Netdata as a system service. This is probably a bug."
  446. NETDATA_SERVICE_WARNED_1=1
  447. fi
  448. ;;
  449. 2)
  450. if [ -z "${NETDATA_SERVICE_WARNED_2}" ]; then
  451. 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."
  452. NETDATA_SERVICE_WARNED_2=1
  453. fi
  454. ;;
  455. 3)
  456. if [ -z "${NETDATA_SERVICE_WARNED_3}" ]; then
  457. warning "Detected an unsupported system service manager. Manual setup will be required to manage Netdata as a system service."
  458. NETDATA_SERVICE_WARNED_3=1
  459. fi
  460. ;;
  461. 4)
  462. if [ -z "${NETDATA_SERVICE_WARNED_4}" ]; then
  463. 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."
  464. NETDATA_SERVICE_WARNED_4=1
  465. fi
  466. ;;
  467. 5)
  468. if [ -z "${NETDATA_SERVICE_WARNED_5}" ]; then
  469. warning "We do not support managing Netdata as a system service on this platform. Manual setup will be required."
  470. NETDATA_SERVICE_WARNED_5=1
  471. fi
  472. ;;
  473. esac
  474. }
  475. install_netdata_service() {
  476. if [ "${UID}" -eq 0 ]; then
  477. if [ -x "${NETDATA_PREFIX}/usr/libexec/netdata/install-service.sh" ]; then
  478. run_install_service_script && return 0
  479. else
  480. # This is used by netdata-installer.sh
  481. # shellcheck disable=SC2034
  482. NETDATA_STOP_CMD="netdatacli shutdown-agent"
  483. NETDATA_START_CMD="netdata"
  484. NETDATA_INSTALLER_START_CMD=""
  485. uname="$(uname 2> /dev/null)"
  486. if [ "${uname}" = "Darwin" ]; then
  487. if [ -f "/Library/LaunchDaemons/com.github.netdata.plist" ]; then
  488. echo >&2 "file '/Library/LaunchDaemons/com.github.netdata.plist' already exists."
  489. return 0
  490. else
  491. echo >&2 "Installing MacOS X plist file..."
  492. # This is used by netdata-installer.sh
  493. # shellcheck disable=SC2034
  494. run cp system/netdata.plist /Library/LaunchDaemons/com.github.netdata.plist &&
  495. run launchctl load /Library/LaunchDaemons/com.github.netdata.plist &&
  496. NETDATA_START_CMD="launchctl start com.github.netdata" &&
  497. NETDATA_STOP_CMD="launchctl stop com.github.netdata"
  498. return 0
  499. fi
  500. elif [ "${uname}" = "FreeBSD" ]; then
  501. # This is used by netdata-installer.sh
  502. # shellcheck disable=SC2034
  503. run cp system/netdata-freebsd /etc/rc.d/netdata && NETDATA_START_CMD="service netdata start" &&
  504. NETDATA_STOP_CMD="service netdata stop" &&
  505. NETDATA_INSTALLER_START_CMD="service netdata onestart" &&
  506. myret=$?
  507. echo >&2 "Note: To explicitly enable netdata automatic start, set 'netdata_enable' to 'YES' in /etc/rc.conf"
  508. echo >&2 ""
  509. return ${myret}
  510. elif issystemd; then
  511. # systemd is running on this system
  512. NETDATA_START_CMD="systemctl start netdata"
  513. # This is used by netdata-installer.sh
  514. # shellcheck disable=SC2034
  515. NETDATA_STOP_CMD="systemctl stop netdata"
  516. NETDATA_INSTALLER_START_CMD="${NETDATA_START_CMD}"
  517. SYSTEMD_DIRECTORY="$(get_systemd_service_dir)"
  518. if [ "${SYSTEMD_DIRECTORY}x" != "x" ]; then
  519. ENABLE_NETDATA_IF_PREVIOUSLY_ENABLED="run systemctl enable netdata"
  520. IS_NETDATA_ENABLED="$(systemctl is-enabled netdata 2> /dev/null || echo "Netdata not there")"
  521. if [ "${IS_NETDATA_ENABLED}" = "disabled" ]; then
  522. echo >&2 "Netdata was there and disabled, make sure we don't re-enable it ourselves"
  523. ENABLE_NETDATA_IF_PREVIOUSLY_ENABLED="true"
  524. fi
  525. echo >&2 "Installing systemd service..."
  526. run cp system/netdata.service "${SYSTEMD_DIRECTORY}/netdata.service" &&
  527. run systemctl daemon-reload &&
  528. ${ENABLE_NETDATA_IF_PREVIOUSLY_ENABLED} &&
  529. return 0
  530. else
  531. warning "Could not find a systemd service directory, unable to install Netdata systemd service."
  532. fi
  533. else
  534. install_non_systemd_init
  535. ret=$?
  536. if [ ${ret} -eq 0 ]; then
  537. if [ -n "${service_cmd}" ]; then
  538. NETDATA_START_CMD="service netdata start"
  539. # This is used by netdata-installer.sh
  540. # shellcheck disable=SC2034
  541. NETDATA_STOP_CMD="service netdata stop"
  542. elif [ -n "${rcservice_cmd}" ]; then
  543. NETDATA_START_CMD="rc-service netdata start"
  544. # This is used by netdata-installer.sh
  545. # shellcheck disable=SC2034
  546. NETDATA_STOP_CMD="rc-service netdata stop"
  547. fi
  548. NETDATA_INSTALLER_START_CMD="${NETDATA_START_CMD}"
  549. fi
  550. return ${ret}
  551. fi
  552. fi
  553. fi
  554. return 1
  555. }
  556. # -----------------------------------------------------------------------------
  557. # stop netdata
  558. pidisnetdata() {
  559. if [ -d /proc/self ]; then
  560. if [ -z "$1" ] || [ ! -f "/proc/$1/stat" ]; then
  561. return 1
  562. fi
  563. [ "$(cut -d '(' -f 2 "/proc/$1/stat" | cut -d ')' -f 1)" = "netdata" ] && return 0
  564. return 1
  565. fi
  566. return 0
  567. }
  568. stop_netdata_on_pid() {
  569. pid="${1}"
  570. ret=0
  571. count=0
  572. pidisnetdata "${pid}" || return 0
  573. printf >&2 "Stopping netdata on pid %s ..." "${pid}"
  574. while [ -n "${pid}" ] && [ ${ret} -eq 0 ]; do
  575. if [ ${count} -gt 24 ]; then
  576. warning "Cannot stop netdata agent with PID ${pid}."
  577. return 1
  578. fi
  579. count=$((count + 1))
  580. pidisnetdata "${pid}" || ret=1
  581. if [ ${ret} -eq 1 ]; then
  582. break
  583. fi
  584. if [ ${count} -lt 12 ]; then
  585. run kill "${pid}" 2> /dev/null
  586. ret=$?
  587. else
  588. run kill -9 "${pid}" 2> /dev/null
  589. ret=$?
  590. fi
  591. test ${ret} -eq 0 && printf >&2 "." && sleep 5
  592. done
  593. echo >&2
  594. if [ ${ret} -eq 0 ]; then
  595. warning "Failed to stop netdata agent process with PID ${pid}."
  596. return 1
  597. fi
  598. echo >&2 "netdata on pid ${pid} stopped."
  599. return 0
  600. }
  601. netdata_pids() {
  602. myns="$(readlink /proc/self/ns/pid 2> /dev/null)"
  603. for p in \
  604. $(cat /var/run/netdata.pid 2> /dev/null) \
  605. $(cat /var/run/netdata/netdata.pid 2> /dev/null) \
  606. $(safe_pidof netdata 2> /dev/null); do
  607. ns="$(readlink "/proc/${p}/ns/pid" 2> /dev/null)"
  608. if [ -z "${myns}" ] || [ -z "${ns}" ] || [ "${myns}" = "${ns}" ]; then
  609. pidisnetdata "${p}" && echo "${p}"
  610. fi
  611. done
  612. }
  613. stop_all_netdata() {
  614. stop_success=0
  615. if [ -x "${NETDATA_PREFIX}/usr/libexec/netdata/install-service.sh" ]; then
  616. run_install_service_script --cmds-only
  617. fi
  618. if [ "${UID}" -eq 0 ]; then
  619. uname="$(uname 2>/dev/null)"
  620. # Any of these may fail, but we need to not bail if they do.
  621. if [ -n "${NETDATA_STOP_CMD}" ]; then
  622. if ${NETDATA_STOP_CMD}; then
  623. stop_success=1
  624. sleep 5
  625. fi
  626. elif issystemd; then
  627. if systemctl stop netdata; then
  628. stop_success=1
  629. sleep 5
  630. fi
  631. elif [ "${uname}" = "Darwin" ]; then
  632. if launchctl stop netdata; then
  633. stop_success=1
  634. sleep 5
  635. fi
  636. elif [ "${uname}" = "FreeBSD" ]; then
  637. if /etc/rc.d/netdata stop; then
  638. stop_success=1
  639. sleep 5
  640. fi
  641. else
  642. if service netdata stop; then
  643. stop_success=1
  644. sleep 5
  645. fi
  646. fi
  647. fi
  648. if [ "$stop_success" = "0" ]; then
  649. if [ -n "$(netdata_pids)" ] && [ -n "$(command -v netdatacli)" ]; then
  650. netdatacli shutdown-agent
  651. sleep 20
  652. fi
  653. for p in $(netdata_pids); do
  654. # shellcheck disable=SC2086
  655. stop_netdata_on_pid ${p}
  656. done
  657. fi
  658. }
  659. # -----------------------------------------------------------------------------
  660. # restart netdata
  661. restart_netdata() {
  662. netdata="${1}"
  663. shift
  664. started=0
  665. progress "Restarting netdata instance"
  666. if [ -x "${NETDATA_PREFIX}/usr/libexec/netdata/install-service.sh" ]; then
  667. run_install_service_script --cmds-only
  668. fi
  669. if [ -z "${NETDATA_INSTALLER_START_CMD}" ]; then
  670. if [ -n "${NETDATA_START_CMD}" ]; then
  671. NETDATA_INSTALLER_START_CMD="${NETDATA_START_CMD}"
  672. else
  673. NETDATA_INSTALLER_START_CMD="${netdata}"
  674. fi
  675. fi
  676. if [ "${UID}" -eq 0 ]; then
  677. echo >&2
  678. echo >&2 "Stopping all netdata threads"
  679. run stop_all_netdata
  680. echo >&2 "Starting netdata using command '${NETDATA_INSTALLER_START_CMD}'"
  681. # shellcheck disable=SC2086
  682. run ${NETDATA_INSTALLER_START_CMD} && started=1
  683. if [ ${started} -eq 1 ] && sleep 5 && [ -z "$(netdata_pids)" ]; then
  684. echo >&2 "Ooops! it seems netdata is not started."
  685. started=0
  686. fi
  687. if [ ${started} -eq 0 ]; then
  688. echo >&2 "Attempting another netdata start using command '${NETDATA_INSTALLER_START_CMD}'"
  689. # shellcheck disable=SC2086
  690. run ${NETDATA_INSTALLER_START_CMD} && started=1
  691. fi
  692. if [ ${started} -eq 1 ] && sleep 5 && [ -z "$(netdata_pids)" ]; then
  693. echo >&2 "Hm... it seems netdata is still not started."
  694. started=0
  695. fi
  696. fi
  697. if [ ${started} -eq 0 ]; then
  698. # still not started... another forced attempt, just run the binary
  699. warning "Netdata service still not started, attempting another forced restart by running '${netdata} ${*}'"
  700. run stop_all_netdata
  701. run "${netdata}" "${@}"
  702. return $?
  703. fi
  704. return 0
  705. }
  706. # -----------------------------------------------------------------------------
  707. # install netdata logrotate
  708. install_netdata_logrotate() {
  709. if [ "${UID}" -eq 0 ]; then
  710. if [ -d /etc/logrotate.d ]; then
  711. if [ ! -f /etc/logrotate.d/netdata ]; then
  712. run cp system/netdata.logrotate /etc/logrotate.d/netdata
  713. fi
  714. if [ -f /etc/logrotate.d/netdata ]; then
  715. run chmod 644 /etc/logrotate.d/netdata
  716. fi
  717. return 0
  718. fi
  719. fi
  720. return 1
  721. }
  722. # -----------------------------------------------------------------------------
  723. # create netdata.conf
  724. create_netdata_conf() {
  725. path="${1}"
  726. url="${2}"
  727. if [ -s "${path}" ]; then
  728. return 0
  729. fi
  730. if [ -n "$url" ]; then
  731. echo >&2 "Downloading default configuration from netdata..."
  732. sleep 5
  733. # remove a possibly obsolete configuration file
  734. [ -f "${path}.new" ] && rm "${path}.new"
  735. # disable a proxy to get data from the local netdata
  736. export http_proxy=
  737. export https_proxy=
  738. if command -v curl 1> /dev/null 2>&1; then
  739. run curl -sSL --connect-timeout 10 --retry 3 "${url}" > "${path}.new"
  740. elif command -v wget 1> /dev/null 2>&1; then
  741. run wget -T 15 -O - "${url}" > "${path}.new"
  742. fi
  743. if [ -s "${path}.new" ]; then
  744. run mv "${path}.new" "${path}"
  745. run_ok "New configuration saved for you to edit at ${path}"
  746. else
  747. [ -f "${path}.new" ] && rm "${path}.new"
  748. run_failed "Cannot download configuration from netdata daemon using url '${url}'"
  749. url=''
  750. fi
  751. fi
  752. if [ -z "$url" ]; then
  753. echo "# netdata can generate its own config which is available at 'http://<netdata_ip>/netdata.conf'" > "${path}"
  754. echo "# You can download it with command like: 'wget -O ${path} http://localhost:19999/netdata.conf'" >> "${path}"
  755. fi
  756. }
  757. portable_add_user() {
  758. username="${1}"
  759. homedir="${2}"
  760. [ -z "${homedir}" ] && homedir="/tmp"
  761. # Check if user exists
  762. if cut -d ':' -f 1 < /etc/passwd | grep "^${username}$" 1> /dev/null 2>&1; then
  763. echo >&2 "User '${username}' already exists."
  764. return 0
  765. fi
  766. echo >&2 "Adding ${username} user account with home ${homedir} ..."
  767. nologin="$(command -v nologin || echo '/bin/false')"
  768. # Linux
  769. if command -v useradd 1> /dev/null 2>&1; then
  770. run useradd -r -g "${username}" -c "${username}" -s "${nologin}" --no-create-home -d "${homedir}" "${username}" && return 0
  771. fi
  772. # FreeBSD
  773. if command -v pw 1> /dev/null 2>&1; then
  774. run pw useradd "${username}" -d "${homedir}" -g "${username}" -s "${nologin}" && return 0
  775. fi
  776. # BusyBox
  777. if command -v adduser 1> /dev/null 2>&1; then
  778. run adduser -h "${homedir}" -s "${nologin}" -D -G "${username}" "${username}" && return 0
  779. fi
  780. # mac OS
  781. if command -v sysadminctl 1> /dev/null 2>&1; then
  782. run sysadminctl -addUser "${username}" && return 0
  783. fi
  784. warning "Failed to add ${username} user account!"
  785. return 1
  786. }
  787. portable_add_group() {
  788. groupname="${1}"
  789. # Check if group exist
  790. if cut -d ':' -f 1 < /etc/group | grep "^${groupname}$" 1> /dev/null 2>&1; then
  791. echo >&2 "Group '${groupname}' already exists."
  792. return 0
  793. fi
  794. echo >&2 "Adding ${groupname} user group ..."
  795. # Linux
  796. if command -v groupadd 1> /dev/null 2>&1; then
  797. run groupadd -r "${groupname}" && return 0
  798. fi
  799. # FreeBSD
  800. if command -v pw 1> /dev/null 2>&1; then
  801. run pw groupadd "${groupname}" && return 0
  802. fi
  803. # BusyBox
  804. if command -v addgroup 1> /dev/null 2>&1; then
  805. run addgroup "${groupname}" && return 0
  806. fi
  807. # mac OS
  808. if command -v dseditgroup 1> /dev/null 2>&1; then
  809. dseditgroup -o create "${groupname}" && return 0
  810. fi
  811. warning >&2 "Failed to add ${groupname} user group !"
  812. return 1
  813. }
  814. portable_add_user_to_group() {
  815. groupname="${1}"
  816. username="${2}"
  817. # Check if group exist
  818. if ! cut -d ':' -f 1 < /etc/group | grep "^${groupname}$" > /dev/null 2>&1; then
  819. echo >&2 "Group '${groupname}' does not exist."
  820. # Don’t treat this as a failure, if the group does not exist we should not be trying to add the user to it.
  821. return 0
  822. fi
  823. # Check if user is in group
  824. if expr ",$(grep "^${groupname}:" < /etc/group | cut -d ':' -f 4)," : ",""${username}"","; then
  825. # username is already there
  826. echo >&2 "User '${username}' is already in group '${groupname}'."
  827. return 0
  828. else
  829. # username is not in group
  830. echo >&2 "Adding ${username} user to the ${groupname} group ..."
  831. # Linux
  832. if command -v usermod 1> /dev/null 2>&1; then
  833. run usermod -a -G "${groupname}" "${username}" && return 0
  834. fi
  835. # FreeBSD
  836. if command -v pw 1> /dev/null 2>&1; then
  837. run pw groupmod "${groupname}" -m "${username}" && return 0
  838. fi
  839. # BusyBox
  840. if command -v addgroup 1> /dev/null 2>&1; then
  841. run addgroup "${username}" "${groupname}" && return 0
  842. fi
  843. # mac OS
  844. if command -v dseditgroup 1> /dev/null 2>&1; then
  845. dseditgroup -u "${username}" "${groupname}" && return 0
  846. fi
  847. warning >&2 "Failed to add user ${username} to group ${groupname}!"
  848. return 1
  849. fi
  850. }
  851. safe_sha256sum() {
  852. # Within the context of the installer, we only use -c option that is common between the two commands
  853. # We will have to reconsider if we start non-common options
  854. if command -v sha256sum > /dev/null 2>&1; then
  855. sha256sum "$@"
  856. elif command -v shasum > /dev/null 2>&1; then
  857. shasum -a 256 "$@"
  858. else
  859. fatal "I could not find a suitable checksum binary to use" "L0001"
  860. fi
  861. }
  862. _get_scheduler_type() {
  863. if _get_intervaldir > /dev/null ; then
  864. echo 'interval'
  865. elif issystemd ; then
  866. echo 'systemd'
  867. elif [ -d /etc/cron.d ] ; then
  868. echo 'crontab'
  869. else
  870. echo 'none'
  871. fi
  872. }
  873. _get_intervaldir() {
  874. if [ -d /etc/cron.daily ]; then
  875. echo /etc/cron.daily
  876. elif [ -d /etc/periodic/daily ]; then
  877. echo /etc/periodic/daily
  878. else
  879. return 1
  880. fi
  881. return 0
  882. }
  883. install_netdata_updater() {
  884. if [ "${INSTALLER_DIR}" ] && [ -f "${INSTALLER_DIR}/packaging/installer/netdata-updater.sh" ]; then
  885. cat "${INSTALLER_DIR}/packaging/installer/netdata-updater.sh" > "${NETDATA_PREFIX}/usr/libexec/netdata/netdata-updater.sh" || return 1
  886. fi
  887. if [ "${NETDATA_SOURCE_DIR}" ] && [ -f "${NETDATA_SOURCE_DIR}/packaging/installer/netdata-updater.sh" ]; then
  888. cat "${NETDATA_SOURCE_DIR}/packaging/installer/netdata-updater.sh" > "${NETDATA_PREFIX}/usr/libexec/netdata/netdata-updater.sh" || return 1
  889. fi
  890. if issystemd && [ -n "$(get_systemd_service_dir)" ]; then
  891. cat "${NETDATA_SOURCE_DIR}/system/netdata-updater.timer" > "$(get_systemd_service_dir)/netdata-updater.timer"
  892. cat "${NETDATA_SOURCE_DIR}/system/netdata-updater.service" > "$(get_systemd_service_dir)/netdata-updater.service"
  893. fi
  894. 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
  895. chmod 0755 "${NETDATA_PREFIX}/usr/libexec/netdata/netdata-updater.sh"
  896. echo >&2 "Update script is located at ${TPUT_GREEN}${TPUT_BOLD}${NETDATA_PREFIX}/usr/libexec/netdata/netdata-updater.sh${TPUT_RESET}"
  897. echo >&2
  898. return 0
  899. }
  900. set_netdata_updater_channel() {
  901. sed -i -e "s/^RELEASE_CHANNEL=.*/RELEASE_CHANNEL=\"${RELEASE_CHANNEL}\"/" "${NETDATA_USER_CONFIG_DIR}/.environment"
  902. }