functions.sh 27 KB

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