functions.sh 27 KB

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