functions.sh 25 KB

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