functions.sh 23 KB

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