functions.sh 20 KB

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