functions.sh 23 KB

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