functions.sh 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  1. # no shebang necessary - this is a library to be sourced
  2. # SPDX-License-Identifier: GPL-3.0-or-later
  3. # shellcheck disable=SC1091,SC1117,SC2002,SC2004,SC2034,SC2046,SC2059,SC2086,SC2129,SC2148,SC2154,SC2155,SC2162,SC2166,SC2181,SC2193
  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. TPUT_BLACK="$(tput setaf 0)"
  40. TPUT_RED="$(tput setaf 1)"
  41. TPUT_GREEN="$(tput setaf 2)"
  42. TPUT_YELLOW="$(tput setaf 3)"
  43. TPUT_BLUE="$(tput setaf 4)"
  44. TPUT_PURPLE="$(tput setaf 5)"
  45. TPUT_CYAN="$(tput setaf 6)"
  46. TPUT_WHITE="$(tput setaf 7)"
  47. TPUT_BGBLACK="$(tput setab 0)"
  48. TPUT_BGRED="$(tput setab 1)"
  49. TPUT_BGGREEN="$(tput setab 2)"
  50. TPUT_BGYELLOW="$(tput setab 3)"
  51. TPUT_BGBLUE="$(tput setab 4)"
  52. TPUT_BGPURPLE="$(tput setab 5)"
  53. TPUT_BGCYAN="$(tput setab 6)"
  54. TPUT_BGWHITE="$(tput setab 7)"
  55. TPUT_BOLD="$(tput bold)"
  56. TPUT_DIM="$(tput dim)"
  57. TPUT_UNDERLINED="$(tput smul)"
  58. TPUT_BLINK="$(tput blink)"
  59. TPUT_INVERTED="$(tput rev)"
  60. TPUT_STANDOUT="$(tput smso)"
  61. TPUT_BELL="$(tput bel)"
  62. TPUT_CLEAR="$(tput clear)"
  63. fi
  64. fi
  65. return 0
  66. }
  67. setup_terminal || echo >/dev/null
  68. progress() {
  69. echo >&2 " --- ${TPUT_DIM}${TPUT_BOLD}${*}${TPUT_RESET} --- "
  70. }
  71. # -----------------------------------------------------------------------------
  72. netdata_banner() {
  73. local l1=" ^" \
  74. l2=" |.-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-" \
  75. l3=" | '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' " \
  76. l4=" +----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+--->" \
  77. sp=" " \
  78. netdata="netdata" start end msg="${*}" chartcolor="${TPUT_DIM}"
  79. [ ${#msg} -lt ${#netdata} ] && msg="${msg}${sp:0:$((${#netdata} - ${#msg}))}"
  80. [ ${#msg} -gt $((${#l2} - 20)) ] && msg="${msg:0:$((${#l2} - 23))}..."
  81. start="$((${#l2} / 2 - 4))"
  82. [ $((start + ${#msg} + 4)) -gt ${#l2} ] && start=$((${#l2} - ${#msg} - 4))
  83. end=$((start + ${#msg} + 4))
  84. echo >&2
  85. echo >&2 "${chartcolor}${l1}${TPUT_RESET}"
  86. 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}"
  87. 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}"
  88. echo >&2 "${chartcolor}${l4}${TPUT_RESET}"
  89. echo >&2
  90. }
  91. # -----------------------------------------------------------------------------
  92. # portable service command
  93. service_cmd="$(command -v service 2>/dev/null)"
  94. rcservice_cmd="$(command -v rc-service 2>/dev/null)"
  95. systemctl_cmd="$(command -v systemctl 2>/dev/null)"
  96. service() {
  97. local cmd="${1}" action="${2}"
  98. if [ -n "${systemctl_cmd}" ]; then
  99. run "${systemctl_cmd}" "${action}" "${cmd}"
  100. return $?
  101. elif [ -n "${service_cmd}" ]; then
  102. run "${service_cmd}" "${cmd}" "${action}"
  103. return $?
  104. elif [ -n "${rcservice_cmd}" ]; then
  105. run "${rcservice_cmd}" "${cmd}" "${action}"
  106. return $?
  107. fi
  108. return 1
  109. }
  110. # -----------------------------------------------------------------------------
  111. # portable pidof
  112. safe_pidof() {
  113. local pidof_cmd="$(command -v pidof 2>/dev/null)"
  114. if [ -n "${pidof_cmd}" ]; then
  115. ${pidof_cmd} "${@}"
  116. return $?
  117. else
  118. ps -acxo pid,comm |
  119. sed "s/^ *//g" |
  120. grep netdata |
  121. cut -d ' ' -f 1
  122. return $?
  123. fi
  124. }
  125. # -----------------------------------------------------------------------------
  126. find_processors() {
  127. local cpus
  128. if [ -f "/proc/cpuinfo" ]; then
  129. # linux
  130. cpus=$(grep -c ^processor /proc/cpuinfo)
  131. else
  132. # freebsd
  133. cpus=$(sysctl hw.ncpu 2>/dev/null | grep ^hw.ncpu | cut -d ' ' -f 2)
  134. fi
  135. if [ -z "${cpus}" ] || [ $((cpus)) -lt 1 ]; then
  136. echo 1
  137. else
  138. echo "${cpus}"
  139. fi
  140. }
  141. # -----------------------------------------------------------------------------
  142. fatal() {
  143. printf >&2 "${TPUT_BGRED}${TPUT_WHITE}${TPUT_BOLD} ABORTED ${TPUT_RESET} ${*} \n\n"
  144. exit 1
  145. }
  146. run_ok() {
  147. printf >&2 "${TPUT_BGGREEN}${TPUT_WHITE}${TPUT_BOLD} OK ${TPUT_RESET} ${*} \n\n"
  148. }
  149. run_failed() {
  150. printf >&2 "${TPUT_BGRED}${TPUT_WHITE}${TPUT_BOLD} FAILED ${TPUT_RESET} ${*} \n\n"
  151. }
  152. ESCAPED_PRINT_METHOD=
  153. printf "%q " test >/dev/null 2>&1
  154. [ $? -eq 0 ] && ESCAPED_PRINT_METHOD="printfq"
  155. escaped_print() {
  156. if [ "${ESCAPED_PRINT_METHOD}" = "printfq" ]; then
  157. printf "%q " "${@}"
  158. else
  159. printf "%s" "${*}"
  160. fi
  161. return 0
  162. }
  163. run_logfile="/dev/null"
  164. run() {
  165. local user="${USER--}" dir="${PWD}" info info_console
  166. if [ "${UID}" = "0" ]; then
  167. info="[root ${dir}]# "
  168. info_console="[${TPUT_DIM}${dir}${TPUT_RESET}]# "
  169. else
  170. info="[${user} ${dir}]$ "
  171. info_console="[${TPUT_DIM}${dir}${TPUT_RESET}]$ "
  172. fi
  173. printf >>"${run_logfile}" "${info}"
  174. escaped_print >>"${run_logfile}" "${@}"
  175. printf >>"${run_logfile}" " ... "
  176. printf >&2 "${info_console}${TPUT_BOLD}${TPUT_YELLOW}"
  177. escaped_print >&2 "${@}"
  178. printf >&2 "${TPUT_RESET}\n"
  179. "${@}"
  180. local ret=$?
  181. if [ ${ret} -ne 0 ]; then
  182. run_failed
  183. printf >>"${run_logfile}" "FAILED with exit code ${ret}\n"
  184. else
  185. run_ok
  186. printf >>"${run_logfile}" "OK\n"
  187. fi
  188. return ${ret}
  189. }
  190. iscontainer() {
  191. # man systemd-detect-virt
  192. local cmd=$(command -v systemd-detect-virt 2>/dev/null)
  193. if [ -n "${cmd}" ] && [ -x "${cmd}" ]; then
  194. "${cmd}" --container >/dev/null 2>&1 && return 0
  195. fi
  196. # /proc/1/sched exposes the host's pid of our init !
  197. # http://stackoverflow.com/a/37016302
  198. local pid=$(cat /proc/1/sched 2>/dev/null | head -n 1 | {
  199. IFS='(),#:' read name pid th threads
  200. echo "$pid"
  201. })
  202. if [ -n "${pid}" ]; then
  203. pid=$(( pid + 0 ))
  204. [ ${pid} -gt 1 ] && return 0
  205. fi
  206. # lxc sets environment variable 'container'
  207. [ -n "${container}" ] && return 0
  208. # docker creates /.dockerenv
  209. # http://stackoverflow.com/a/25518345
  210. [ -f "/.dockerenv" ] && return 0
  211. # ubuntu and debian supply /bin/running-in-container
  212. # https://www.apt-browse.org/browse/ubuntu/trusty/main/i386/upstart/1.12.1-0ubuntu4/file/bin/running-in-container
  213. if [ -x "/bin/running-in-container" ]; then
  214. "/bin/running-in-container" >/dev/null 2>&1 && return 0
  215. fi
  216. return 1
  217. }
  218. issystemd() {
  219. local pids p myns ns systemctl
  220. # if the directory /lib/systemd/system OR /usr/lib/systemd/system (SLES 12.x) does not exit, it is not systemd
  221. [ ! -d /lib/systemd/system -a ! -d /usr/lib/systemd/system ] && return 1
  222. # if there is no systemctl command, it is not systemd
  223. # shellcheck disable=SC2230
  224. systemctl=$(command -v systemctl 2>/dev/null)
  225. [ -z "${systemctl}" -o ! -x "${systemctl}" ] && return 1
  226. # if pid 1 is systemd, it is systemd
  227. [ "$(basename $(readlink /proc/1/exe) 2>/dev/null)" = "systemd" ] && return 0
  228. # if systemd is not running, it is not systemd
  229. pids=$(safe_pidof systemd 2>/dev/null)
  230. [ -z "${pids}" ] && return 1
  231. # check if the running systemd processes are not in our namespace
  232. myns="$(readlink /proc/self/ns/pid 2>/dev/null)"
  233. for p in ${pids}; do
  234. ns="$(readlink "/proc/${p}/ns/pid" 2>/dev/null)"
  235. # if pid of systemd is in our namespace, it is systemd
  236. [ -n "${myns}" ] && [ "${myns}" = "${ns}" ] && return 0
  237. done
  238. # else, it is not systemd
  239. return 1
  240. }
  241. install_non_systemd_init() {
  242. [ "${UID}" != 0 ] && return 1
  243. local key="unknown"
  244. if [ -f /etc/os-release ]; then
  245. source /etc/os-release || return 1
  246. key="${ID}-${VERSION_ID}"
  247. elif [ -f /etc/redhat-release ]; then
  248. key=$(</etc/redhat-release)
  249. fi
  250. if [ -d /etc/init.d ] && [ ! -f /etc/init.d/netdata ]; then
  251. if [[ ${key} =~ ^(gentoo|alpine).* ]]; then
  252. echo >&2 "Installing OpenRC init file..."
  253. run cp system/netdata-openrc /etc/init.d/netdata &&
  254. run chmod 755 /etc/init.d/netdata &&
  255. run rc-update add netdata default &&
  256. return 0
  257. elif [ "${key}" = "debian-7" ] || [ "${key}" = "ubuntu-12.04" ] || [ "${key}" = "ubuntu-14.04" ]; then
  258. echo >&2 "Installing LSB init file..."
  259. run cp system/netdata-lsb /etc/init.d/netdata &&
  260. run chmod 755 /etc/init.d/netdata &&
  261. run update-rc.d netdata defaults &&
  262. run update-rc.d netdata enable &&
  263. return 0
  264. 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
  265. echo >&2 "Installing init.d file..."
  266. run cp system/netdata-init-d /etc/init.d/netdata &&
  267. run chmod 755 /etc/init.d/netdata &&
  268. run chkconfig netdata on &&
  269. return 0
  270. else
  271. echo >&2 "I don't know what init file to install on system '${key}'. Open a github issue to help us fix it."
  272. return 1
  273. fi
  274. elif [ -f /etc/init.d/netdata ]; then
  275. echo >&2 "file '/etc/init.d/netdata' already exists."
  276. return 0
  277. else
  278. echo >&2 "I don't know what init file to install on system '${key}'. Open a github issue to help us fix it."
  279. fi
  280. return 1
  281. }
  282. NETDATA_START_CMD="netdata"
  283. NETDATA_STOP_CMD="killall netdata"
  284. NETDATA_INSTALLER_START_CMD="${NETDATA_START_CMD}"
  285. NETDATA_INSTALLER_STOP_CMD="${NETDATA_STOP_CMD}"
  286. install_netdata_service() {
  287. local uname="$(uname 2>/dev/null)"
  288. if [ "${UID}" -eq 0 ]; then
  289. if [ "${uname}" = "Darwin" ]; then
  290. if [ -f "/Library/LaunchDaemons/com.github.netdata.plist" ]; then
  291. echo >&2 "file '/Library/LaunchDaemons/com.github.netdata.plist' already exists."
  292. return 0
  293. else
  294. echo >&2 "Installing MacOS X plist file..."
  295. run cp system/netdata.plist /Library/LaunchDaemons/com.github.netdata.plist &&
  296. run launchctl load /Library/LaunchDaemons/com.github.netdata.plist &&
  297. return 0
  298. fi
  299. elif [ "${uname}" = "FreeBSD" ]; then
  300. run cp system/netdata-freebsd /etc/rc.d/netdata && NETDATA_START_CMD="service netdata start" &&
  301. NETDATA_STOP_CMD="service netdata stop" &&
  302. NETDATA_INSTALLER_START_CMD="service netdata onestart" &&
  303. NETDATA_INSTALLER_STOP_CMD="${NETDATA_STOP_CMD}"
  304. myret=$?
  305. echo >&2 "Note: To explicitly enable netdata automatic start, set 'netdata_enable' to 'YES' in /etc/rc.conf"
  306. echo >&2 ""
  307. return ${myret}
  308. elif issystemd; then
  309. # systemd is running on this system
  310. NETDATA_START_CMD="systemctl start netdata"
  311. NETDATA_STOP_CMD="systemctl stop netdata"
  312. NETDATA_INSTALLER_START_CMD="${NETDATA_START_CMD}"
  313. NETDATA_INSTALLER_STOP_CMD="${NETDATA_STOP_CMD}"
  314. SYSTEMD_DIRECTORY=""
  315. if [ -d "/lib/systemd/system" ]; then
  316. SYSTEMD_DIRECTORY="/lib/systemd/system"
  317. elif [ -d "/usr/lib/systemd/system" ]; then
  318. SYSTEMD_DIRECTORY="/usr/lib/systemd/system"
  319. fi
  320. if [ "${SYSTEMD_DIRECTORY}x" != "x" ]; then
  321. echo >&2 "Installing systemd service..."
  322. run cp system/netdata.service "${SYSTEMD_DIRECTORY}/netdata.service" &&
  323. run systemctl daemon-reload &&
  324. run systemctl enable netdata &&
  325. return 0
  326. else
  327. echo >&2 "no systemd directory; cannot install netdata.service"
  328. fi
  329. else
  330. install_non_systemd_init
  331. local ret=$?
  332. if [ ${ret} -eq 0 ]; then
  333. if [ -n "${service_cmd}" ]; then
  334. NETDATA_START_CMD="service netdata start"
  335. NETDATA_STOP_CMD="service netdata stop"
  336. elif [ -n "${rcservice_cmd}" ]; then
  337. NETDATA_START_CMD="rc-service netdata start"
  338. NETDATA_STOP_CMD="rc-service netdata stop"
  339. fi
  340. NETDATA_INSTALLER_START_CMD="${NETDATA_START_CMD}"
  341. NETDATA_INSTALLER_STOP_CMD="${NETDATA_STOP_CMD}"
  342. fi
  343. return ${ret}
  344. fi
  345. fi
  346. return 1
  347. }
  348. # -----------------------------------------------------------------------------
  349. # stop netdata
  350. pidisnetdata() {
  351. if [ -d /proc/self ]; then
  352. [ -z "$1" -o ! -f "/proc/$1/stat" ] && return 1
  353. [ "$(cat "/proc/$1/stat" | cut -d '(' -f 2 | cut -d ')' -f 1)" = "netdata" ] && return 0
  354. return 1
  355. fi
  356. return 0
  357. }
  358. stop_netdata_on_pid() {
  359. local pid="${1}" ret=0 count=0
  360. pidisnetdata "${pid}" || return 0
  361. printf >&2 "Stopping netdata on pid %s ..." "${pid}"
  362. while [ -n "$pid" ] && [ ${ret} -eq 0 ]; do
  363. if [ ${count} -gt 45 ]; then
  364. echo >&2 "Cannot stop the running netdata on pid ${pid}."
  365. return 1
  366. fi
  367. count=$((count + 1))
  368. run kill "${pid}" 2>/dev/null
  369. ret=$?
  370. test ${ret} -eq 0 && printf >&2 "." && sleep 2
  371. done
  372. echo >&2
  373. if [ ${ret} -eq 0 ]; then
  374. echo >&2 "SORRY! CANNOT STOP netdata ON PID ${pid} !"
  375. return 1
  376. fi
  377. echo >&2 "netdata on pid ${pid} stopped."
  378. return 0
  379. }
  380. netdata_pids() {
  381. local p myns ns
  382. myns="$(readlink /proc/self/ns/pid 2>/dev/null)"
  383. for p in \
  384. $(cat /var/run/netdata.pid 2>/dev/null) \
  385. $(cat /var/run/netdata/netdata.pid 2>/dev/null) \
  386. $(safe_pidof netdata 2>/dev/null); do
  387. ns="$(readlink "/proc/${p}/ns/pid" 2>/dev/null)"
  388. if [ -z "${myns}" ] || [ -z "${ns}" ] || [ "${myns}" = "${ns}" ]; then
  389. pidisnetdata "${p}" && echo "${p}"
  390. fi
  391. done
  392. }
  393. stop_all_netdata() {
  394. local p
  395. for p in $(netdata_pids); do
  396. # shellcheck disable=SC2086
  397. stop_netdata_on_pid ${p}
  398. done
  399. }
  400. # -----------------------------------------------------------------------------
  401. # restart netdata
  402. restart_netdata() {
  403. local netdata="${1}"
  404. shift
  405. local started=0
  406. progress "Restarting netdata instance"
  407. if [ "${UID}" -eq 0 ]; then
  408. echo >&2
  409. echo >&2 "Stopping all netdata threads"
  410. run stop_all_netdata
  411. echo >&2 "Starting netdata using command '${NETDATA_INSTALLER_START_CMD}'"
  412. run ${NETDATA_INSTALLER_START_CMD} && started=1
  413. if [ ${started} -eq 1 ] && [ -z "$(netdata_pids)" ]; then
  414. echo >&2 "Ooops! it seems netdata is not started."
  415. started=0
  416. fi
  417. if [ ${started} -eq 0 ]; then
  418. echo >&2 "Attempting another netdata start using command '${NETDATA_INSTALLER_START_CMD}'"
  419. run ${NETDATA_INSTALLER_START_CMD} && started=1
  420. fi
  421. fi
  422. if [ ${started} -eq 1 ] && [ -z "$(netdata_pids)" ]; then
  423. echo >&2 "Hm... it seems netdata is still not started."
  424. started=0
  425. fi
  426. if [ ${started} -eq 0 ]; then
  427. # still not started... another forced attempt, just run the binary
  428. echo >&2 "Netdata service still not started, attempting another forced restart by running '${netdata} ${@}'"
  429. run stop_all_netdata
  430. run "${netdata}" "${@}"
  431. return $?
  432. fi
  433. return 0
  434. }
  435. # -----------------------------------------------------------------------------
  436. # install netdata logrotate
  437. install_netdata_logrotate() {
  438. if [ "${UID}" -eq 0 ]; then
  439. if [ -d /etc/logrotate.d ]; then
  440. if [ ! -f /etc/logrotate.d/netdata ]; then
  441. run cp system/netdata.logrotate /etc/logrotate.d/netdata
  442. fi
  443. if [ -f /etc/logrotate.d/netdata ]; then
  444. run chmod 644 /etc/logrotate.d/netdata
  445. fi
  446. return 0
  447. fi
  448. fi
  449. return 1
  450. }
  451. # -----------------------------------------------------------------------------
  452. # create netdata.conf
  453. create_netdata_conf() {
  454. local path="${1}" url="${2}"
  455. if [ -s "${path}" ]; then
  456. return 0
  457. fi
  458. if [ -n "$url" ]; then
  459. echo >&2 "Downloading default configuration from netdata..."
  460. sleep 5
  461. # remove a possibly obsolete configuration file
  462. [ -f "${path}.new" ] && rm "${path}.new"
  463. # disable a proxy to get data from the local netdata
  464. export http_proxy=
  465. export https_proxy=
  466. if command -v curl 1>/dev/null 2>&1; then
  467. run curl -sSL --connect-timeout 10 --retry 3 "${url}" >"${path}.new"
  468. elif command -v wget 1>/dev/null 2>&1; then
  469. run wget -T 15 -O - "${url}" >"${path}.new"
  470. fi
  471. if [ -s "${path}.new" ]; then
  472. run mv "${path}.new" "${path}"
  473. run_ok "New configuration saved for you to edit at ${path}"
  474. else
  475. [ -f "${path}.new" ] && rm "${path}.new"
  476. run_failed "Cannnot download configuration from netdata daemon using url '${url}'"
  477. url=''
  478. fi
  479. fi
  480. if [ -z "$url" ]; then
  481. echo "# netdata can generate its own config which is available at 'http://<netdata_ip>/netdata.conf'" >"${path}"
  482. echo "# You can download it with command like: 'wget -O ${path} http://localhost:19999/netdata.conf'" >>"${path}"
  483. fi
  484. }
  485. portable_add_user() {
  486. local username="${1}" homedir="${2}"
  487. [ -z "${homedir}" ] && homedir="/tmp"
  488. # Check if user exists
  489. if cut -d ':' -f 1 </etc/passwd | grep "^${username}$" 1>/dev/null 2>&1; then
  490. echo >&2 "User '${username}' already exists."
  491. return 0
  492. fi
  493. echo >&2 "Adding ${username} user account with home ${homedir} ..."
  494. # shellcheck disable=SC2230
  495. local nologin="$(command -v nologin >/dev/null 2>&1 || echo '/bin/false')"
  496. # Linux
  497. if command -v useradd 1>/dev/null 2>&1; then
  498. run useradd -r -g "${username}" -c "${username}" -s "${nologin}" --no-create-home -d "${homedir}" "${username}" && return 0
  499. fi
  500. # FreeBSD
  501. if command -v pw 1>/dev/null 2>&1; then
  502. run pw useradd "${username}" -d "${homedir}" -g "${username}" -s "${nologin}" && return 0
  503. fi
  504. # BusyBox
  505. if command -v adduser 1>/dev/null 2>&1; then
  506. run adduser -h "${homedir}" -s "${nologin}" -D -G "${username}" "${username}" && return 0
  507. fi
  508. # mac OS
  509. if command -v sysadminctl 1> /dev/null 2>&1; then
  510. run sysadminctl -addUser ${username} && return 0
  511. fi
  512. echo >&2 "Failed to add ${username} user account !"
  513. return 1
  514. }
  515. portable_add_group() {
  516. local groupname="${1}"
  517. # Check if group exist
  518. if cut -d ':' -f 1 </etc/group | grep "^${groupname}$" 1>/dev/null 2>&1; then
  519. echo >&2 "Group '${groupname}' already exists."
  520. return 0
  521. fi
  522. echo >&2 "Adding ${groupname} user group ..."
  523. # Linux
  524. if command -v groupadd 1>/dev/null 2>&1; then
  525. run groupadd -r "${groupname}" && return 0
  526. fi
  527. # FreeBSD
  528. if command -v pw 1>/dev/null 2>&1; then
  529. run pw groupadd "${groupname}" && return 0
  530. fi
  531. # BusyBox
  532. if command -v addgroup 1>/dev/null 2>&1; then
  533. run addgroup "${groupname}" && return 0
  534. fi
  535. # mac OS
  536. if command -v dseditgroup 1> /dev/null 2>&1; then
  537. dseditgroup -o create "${groupname}" && return 0
  538. fi
  539. echo >&2 "Failed to add ${groupname} user group !"
  540. return 1
  541. }
  542. portable_add_user_to_group() {
  543. local groupname="${1}" username="${2}"
  544. # Check if group exist
  545. if ! cut -d ':' -f 1 </etc/group | grep "^${groupname}$" >/dev/null 2>&1; then
  546. echo >&2 "Group '${groupname}' does not exist."
  547. return 1
  548. fi
  549. # Check if user is in group
  550. if [[ ",$(grep "^${groupname}:" </etc/group | cut -d ':' -f 4)," =~ ,${username}, ]]; then
  551. # username is already there
  552. echo >&2 "User '${username}' is already in group '${groupname}'."
  553. return 0
  554. else
  555. # username is not in group
  556. echo >&2 "Adding ${username} user to the ${groupname} group ..."
  557. # Linux
  558. if command -v usermod 1>/dev/null 2>&1; then
  559. run usermod -a -G "${groupname}" "${username}" && return 0
  560. fi
  561. # FreeBSD
  562. if command -v pw 1>/dev/null 2>&1; then
  563. run pw groupmod "${groupname}" -m "${username}" && return 0
  564. fi
  565. # BusyBox
  566. if command -v addgroup 1>/dev/null 2>&1; then
  567. run addgroup "${username}" "${groupname}" && return 0
  568. fi
  569. # mac OS
  570. if command -v dseditgroup 1> /dev/null 2>&1; then
  571. dseditgroup -u "${username}" "${groupname}" && return 0
  572. fi
  573. echo >&2 "Failed to add user ${username} to group ${groupname} !"
  574. return 1
  575. fi
  576. }
  577. safe_sha256sum() {
  578. # Within the contexct of the installer, we only use -c option that is common between the two commands
  579. # We will have to reconsider if we start non-common options
  580. if command -v sha256sum >/dev/null 2>&1; then
  581. sha256sum $@
  582. elif command -v shasum >/dev/null 2>&1; then
  583. shasum -a 256 $@
  584. else
  585. fatal "I could not find a suitable checksum binary to use"
  586. fi
  587. }