functions.sh 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  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}" =~ ^devuan* ] || [ "${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. ENABLE_NETDATA_IF_PREVIOUSLY_ENABLED="run systemctl enable netdata"
  322. IS_NETDATA_ENABLED="$(systemctl is-enabled netdata 2> /dev/null || echo "Netdata not there")"
  323. if [ "${IS_NETDATA_ENABLED}" == "disabled" ]; then
  324. echo >&2 "Netdata was there and disabled, make sure we don't re-enable it ourselves"
  325. ENABLE_NETDATA_IF_PREVIOUSLY_ENABLED="true"
  326. fi
  327. echo >&2 "Installing systemd service..."
  328. run cp system/netdata.service "${SYSTEMD_DIRECTORY}/netdata.service" &&
  329. run systemctl daemon-reload &&
  330. ${ENABLE_NETDATA_IF_PREVIOUSLY_ENABLED} &&
  331. return 0
  332. else
  333. echo >&2 "no systemd directory; cannot install netdata.service"
  334. fi
  335. else
  336. install_non_systemd_init
  337. local ret=$?
  338. if [ ${ret} -eq 0 ]; then
  339. if [ -n "${service_cmd}" ]; then
  340. NETDATA_START_CMD="service netdata start"
  341. NETDATA_STOP_CMD="service netdata stop"
  342. elif [ -n "${rcservice_cmd}" ]; then
  343. NETDATA_START_CMD="rc-service netdata start"
  344. NETDATA_STOP_CMD="rc-service netdata stop"
  345. fi
  346. NETDATA_INSTALLER_START_CMD="${NETDATA_START_CMD}"
  347. NETDATA_INSTALLER_STOP_CMD="${NETDATA_STOP_CMD}"
  348. fi
  349. return ${ret}
  350. fi
  351. fi
  352. return 1
  353. }
  354. # -----------------------------------------------------------------------------
  355. # stop netdata
  356. pidisnetdata() {
  357. if [ -d /proc/self ]; then
  358. [ -z "$1" -o ! -f "/proc/$1/stat" ] && return 1
  359. [ "$(cat "/proc/$1/stat" | cut -d '(' -f 2 | cut -d ')' -f 1)" = "netdata" ] && return 0
  360. return 1
  361. fi
  362. return 0
  363. }
  364. stop_netdata_on_pid() {
  365. local pid="${1}" ret=0 count=0
  366. pidisnetdata "${pid}" || return 0
  367. printf >&2 "Stopping netdata on pid %s ..." "${pid}"
  368. while [ -n "$pid" ] && [ ${ret} -eq 0 ]; do
  369. if [ ${count} -gt 45 ]; then
  370. echo >&2 "Cannot stop the running netdata on pid ${pid}."
  371. return 1
  372. fi
  373. count=$((count + 1))
  374. run kill "${pid}" 2>/dev/null
  375. ret=$?
  376. test ${ret} -eq 0 && printf >&2 "." && sleep 2
  377. done
  378. echo >&2
  379. if [ ${ret} -eq 0 ]; then
  380. echo >&2 "SORRY! CANNOT STOP netdata ON PID ${pid} !"
  381. return 1
  382. fi
  383. echo >&2 "netdata on pid ${pid} stopped."
  384. return 0
  385. }
  386. netdata_pids() {
  387. local p myns ns
  388. myns="$(readlink /proc/self/ns/pid 2>/dev/null)"
  389. for p in \
  390. $(cat /var/run/netdata.pid 2>/dev/null) \
  391. $(cat /var/run/netdata/netdata.pid 2>/dev/null) \
  392. $(safe_pidof netdata 2>/dev/null); do
  393. ns="$(readlink "/proc/${p}/ns/pid" 2>/dev/null)"
  394. if [ -z "${myns}" ] || [ -z "${ns}" ] || [ "${myns}" = "${ns}" ]; then
  395. pidisnetdata "${p}" && echo "${p}"
  396. fi
  397. done
  398. }
  399. stop_all_netdata() {
  400. local p
  401. for p in $(netdata_pids); do
  402. # shellcheck disable=SC2086
  403. stop_netdata_on_pid ${p}
  404. done
  405. }
  406. # -----------------------------------------------------------------------------
  407. # restart netdata
  408. restart_netdata() {
  409. local netdata="${1}"
  410. shift
  411. local started=0
  412. progress "Restarting netdata instance"
  413. if [ "${UID}" -eq 0 ]; then
  414. echo >&2
  415. echo >&2 "Stopping all netdata threads"
  416. run stop_all_netdata
  417. echo >&2 "Starting netdata using command '${NETDATA_INSTALLER_START_CMD}'"
  418. run ${NETDATA_INSTALLER_START_CMD} && started=1
  419. if [ ${started} -eq 1 ] && [ -z "$(netdata_pids)" ]; then
  420. echo >&2 "Ooops! it seems netdata is not started."
  421. started=0
  422. fi
  423. if [ ${started} -eq 0 ]; then
  424. echo >&2 "Attempting another netdata start using command '${NETDATA_INSTALLER_START_CMD}'"
  425. run ${NETDATA_INSTALLER_START_CMD} && started=1
  426. fi
  427. fi
  428. if [ ${started} -eq 1 ] && [ -z "$(netdata_pids)" ]; then
  429. echo >&2 "Hm... it seems netdata is still not started."
  430. started=0
  431. fi
  432. if [ ${started} -eq 0 ]; then
  433. # still not started... another forced attempt, just run the binary
  434. echo >&2 "Netdata service still not started, attempting another forced restart by running '${netdata} ${@}'"
  435. run stop_all_netdata
  436. run "${netdata}" "${@}"
  437. return $?
  438. fi
  439. return 0
  440. }
  441. # -----------------------------------------------------------------------------
  442. # install netdata logrotate
  443. install_netdata_logrotate() {
  444. if [ "${UID}" -eq 0 ]; then
  445. if [ -d /etc/logrotate.d ]; then
  446. if [ ! -f /etc/logrotate.d/netdata ]; then
  447. run cp system/netdata.logrotate /etc/logrotate.d/netdata
  448. fi
  449. if [ -f /etc/logrotate.d/netdata ]; then
  450. run chmod 644 /etc/logrotate.d/netdata
  451. fi
  452. return 0
  453. fi
  454. fi
  455. return 1
  456. }
  457. # -----------------------------------------------------------------------------
  458. # create netdata.conf
  459. create_netdata_conf() {
  460. local path="${1}" url="${2}"
  461. if [ -s "${path}" ]; then
  462. return 0
  463. fi
  464. if [ -n "$url" ]; then
  465. echo >&2 "Downloading default configuration from netdata..."
  466. sleep 5
  467. # remove a possibly obsolete configuration file
  468. [ -f "${path}.new" ] && rm "${path}.new"
  469. # disable a proxy to get data from the local netdata
  470. export http_proxy=
  471. export https_proxy=
  472. if command -v curl 1>/dev/null 2>&1; then
  473. run curl -sSL --connect-timeout 10 --retry 3 "${url}" >"${path}.new"
  474. elif command -v wget 1>/dev/null 2>&1; then
  475. run wget -T 15 -O - "${url}" >"${path}.new"
  476. fi
  477. if [ -s "${path}.new" ]; then
  478. run mv "${path}.new" "${path}"
  479. run_ok "New configuration saved for you to edit at ${path}"
  480. else
  481. [ -f "${path}.new" ] && rm "${path}.new"
  482. run_failed "Cannnot download configuration from netdata daemon using url '${url}'"
  483. url=''
  484. fi
  485. fi
  486. if [ -z "$url" ]; then
  487. echo "# netdata can generate its own config which is available at 'http://<netdata_ip>/netdata.conf'" >"${path}"
  488. echo "# You can download it with command like: 'wget -O ${path} http://localhost:19999/netdata.conf'" >>"${path}"
  489. fi
  490. }
  491. portable_add_user() {
  492. local username="${1}" homedir="${2}"
  493. [ -z "${homedir}" ] && homedir="/tmp"
  494. # Check if user exists
  495. if cut -d ':' -f 1 </etc/passwd | grep "^${username}$" 1>/dev/null 2>&1; then
  496. echo >&2 "User '${username}' already exists."
  497. return 0
  498. fi
  499. echo >&2 "Adding ${username} user account with home ${homedir} ..."
  500. # shellcheck disable=SC2230
  501. local nologin="$(command -v nologin >/dev/null 2>&1 || echo '/bin/false')"
  502. # Linux
  503. if command -v useradd 1>/dev/null 2>&1; then
  504. run useradd -r -g "${username}" -c "${username}" -s "${nologin}" --no-create-home -d "${homedir}" "${username}" && return 0
  505. fi
  506. # FreeBSD
  507. if command -v pw 1>/dev/null 2>&1; then
  508. run pw useradd "${username}" -d "${homedir}" -g "${username}" -s "${nologin}" && return 0
  509. fi
  510. # BusyBox
  511. if command -v adduser 1>/dev/null 2>&1; then
  512. run adduser -h "${homedir}" -s "${nologin}" -D -G "${username}" "${username}" && return 0
  513. fi
  514. # mac OS
  515. if command -v sysadminctl 1> /dev/null 2>&1; then
  516. run sysadminctl -addUser ${username} && return 0
  517. fi
  518. echo >&2 "Failed to add ${username} user account !"
  519. return 1
  520. }
  521. portable_add_group() {
  522. local groupname="${1}"
  523. # Check if group exist
  524. if cut -d ':' -f 1 </etc/group | grep "^${groupname}$" 1>/dev/null 2>&1; then
  525. echo >&2 "Group '${groupname}' already exists."
  526. return 0
  527. fi
  528. echo >&2 "Adding ${groupname} user group ..."
  529. # Linux
  530. if command -v groupadd 1>/dev/null 2>&1; then
  531. run groupadd -r "${groupname}" && return 0
  532. fi
  533. # FreeBSD
  534. if command -v pw 1>/dev/null 2>&1; then
  535. run pw groupadd "${groupname}" && return 0
  536. fi
  537. # BusyBox
  538. if command -v addgroup 1>/dev/null 2>&1; then
  539. run addgroup "${groupname}" && return 0
  540. fi
  541. # mac OS
  542. if command -v dseditgroup 1> /dev/null 2>&1; then
  543. dseditgroup -o create "${groupname}" && return 0
  544. fi
  545. echo >&2 "Failed to add ${groupname} user group !"
  546. return 1
  547. }
  548. portable_add_user_to_group() {
  549. local groupname="${1}" username="${2}"
  550. # Check if group exist
  551. if ! cut -d ':' -f 1 </etc/group | grep "^${groupname}$" >/dev/null 2>&1; then
  552. echo >&2 "Group '${groupname}' does not exist."
  553. return 1
  554. fi
  555. # Check if user is in group
  556. if [[ ",$(grep "^${groupname}:" </etc/group | cut -d ':' -f 4)," =~ ,${username}, ]]; then
  557. # username is already there
  558. echo >&2 "User '${username}' is already in group '${groupname}'."
  559. return 0
  560. else
  561. # username is not in group
  562. echo >&2 "Adding ${username} user to the ${groupname} group ..."
  563. # Linux
  564. if command -v usermod 1>/dev/null 2>&1; then
  565. run usermod -a -G "${groupname}" "${username}" && return 0
  566. fi
  567. # FreeBSD
  568. if command -v pw 1>/dev/null 2>&1; then
  569. run pw groupmod "${groupname}" -m "${username}" && return 0
  570. fi
  571. # BusyBox
  572. if command -v addgroup 1>/dev/null 2>&1; then
  573. run addgroup "${username}" "${groupname}" && return 0
  574. fi
  575. # mac OS
  576. if command -v dseditgroup 1> /dev/null 2>&1; then
  577. dseditgroup -u "${username}" "${groupname}" && return 0
  578. fi
  579. echo >&2 "Failed to add user ${username} to group ${groupname} !"
  580. return 1
  581. fi
  582. }
  583. safe_sha256sum() {
  584. # Within the contexct of the installer, we only use -c option that is common between the two commands
  585. # We will have to reconsider if we start non-common options
  586. if command -v sha256sum >/dev/null 2>&1; then
  587. sha256sum $@
  588. elif command -v shasum >/dev/null 2>&1; then
  589. shasum -a 256 $@
  590. else
  591. fatal "I could not find a suitable checksum binary to use"
  592. fi
  593. }