netdata-uninstaller.sh 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  1. #!/bin/sh
  2. #
  3. # This is the netdata uninstaller script
  4. #
  5. # Variables needed by script and taken from '.environment' file:
  6. # - NETDATA_PREFIX
  7. # - NETDATA_ADDED_TO_GROUPS
  8. #
  9. # Copyright: SPDX-License-Identifier: GPL-3.0-or-later
  10. #
  11. # Author: Paweł Krupa <paulfantom@gmail.com>
  12. # Author: Pavlos Emm. Katsoulakis <paul@netdata.cloud>
  13. #
  14. # Next unused error code: R0005
  15. usage="$(basename "$0") [-h] [-f ] -- program to calculate the answer to life, the universe and everything
  16. where:
  17. -e, --env path to environment file (defaults to '/etc/netdata/.environment'
  18. -f, --force force uninstallation and do not ask any questions
  19. -h show this help text
  20. -y, --yes flag needs to be set to proceed with uninstallation"
  21. FILE_REMOVAL_STATUS=0
  22. ENVIRONMENT_FILE="/etc/netdata/.environment"
  23. # shellcheck disable=SC2034
  24. INTERACTIVITY="-i"
  25. YES=0
  26. while :; do
  27. case "$1" in
  28. -h | --help)
  29. echo "$usage" >&2
  30. exit 1
  31. ;;
  32. -f | --force)
  33. INTERACTIVITY="-f"
  34. shift
  35. ;;
  36. -y | --yes)
  37. YES=1
  38. FLAG=-y
  39. shift
  40. ;;
  41. -e | --env)
  42. ENVIRONMENT_FILE="$2"
  43. shift 2
  44. ;;
  45. -*)
  46. echo "$usage" >&2
  47. exit 1
  48. ;;
  49. *) break ;;
  50. esac
  51. done
  52. if [ -n "${script_source}" ]; then
  53. script_name="$(basename "${script_source}")"
  54. else
  55. script_name="netdata-uninstaller.sh"
  56. fi
  57. info() {
  58. echo >&2 "$(date) : INFO: ${script_name}: " "${1}"
  59. }
  60. error() {
  61. echo >&2 "$(date) : ERROR: ${script_name}: " "${1}"
  62. if [ -n "${NETDATA_SAVE_WARNINGS}" ]; then
  63. NETDATA_WARNINGS="${NETDATA_WARNINGS}\n - ${1}"
  64. fi
  65. }
  66. fatal() {
  67. echo >&2 "$(date) : FATAL: ${script_name}: FAILED TO UNINSTALL NETDATA: " "${1}"
  68. if [ -n "${NETDATA_SAVE_WARNINGS}" ]; then
  69. NETDATA_WARNINGS="${NETDATA_WARNINGS}\n - ${1}"
  70. fi
  71. exit_reason "${1}" "${2}"
  72. exit 1
  73. }
  74. exit_reason() {
  75. if [ -n "${NETDATA_SAVE_WARNINGS}" ]; then
  76. EXIT_REASON="${1}"
  77. EXIT_CODE="${2}"
  78. if [ -n "${NETDATA_PROPAGATE_WARNINGS}" ]; then
  79. export EXIT_REASON
  80. export EXIT_CODE
  81. export NETDATA_WARNINGS
  82. fi
  83. fi
  84. }
  85. if [ "$YES" != "1" ]; then
  86. echo >&2 "This script will REMOVE netdata from your system."
  87. echo >&2 "Run it again with --yes to do it."
  88. exit_reason "User did not accept uninstalling." R0001
  89. exit 1
  90. fi
  91. if [ "$(id -u)" -ne 0 ]; then
  92. error "This script SHOULD be run as root or otherwise it won't delete all installed components."
  93. key="n"
  94. read -r 1 -p "Do you want to continue as non-root user [y/n] ? " key
  95. if [ "$key" != "y" ] && [ "$key" != "Y" ]; then
  96. exit_reason "User cancelled uninstall." R0002
  97. exit 1
  98. fi
  99. fi
  100. user_input() {
  101. if [ "${INTERACTIVITY}" = "-i" ]; then
  102. TEXT="$1 [y/n]"
  103. while true; do
  104. echo "$TEXT"
  105. read -r yn
  106. case "$yn" in
  107. [Yy]*) return 0;;
  108. [Nn]*) return 1;;
  109. *) echo "Please answer yes or no.";;
  110. esac
  111. done
  112. fi
  113. }
  114. _cannot_use_tmpdir() {
  115. testfile="$(TMPDIR="${1}" mktemp -q -t netdata-test.XXXXXXXXXX)"
  116. ret=0
  117. if [ -z "${testfile}" ]; then
  118. return "${ret}"
  119. fi
  120. if printf '#!/bin/sh\necho SUCCESS\n' > "${testfile}"; then
  121. if chmod +x "${testfile}"; then
  122. if [ "$("${testfile}")" = "SUCCESS" ]; then
  123. ret=1
  124. fi
  125. fi
  126. fi
  127. rm -f "${testfile}"
  128. return "${ret}"
  129. }
  130. create_tmp_directory() {
  131. if [ -z "${TMPDIR}" ] || _cannot_use_tmpdir "${TMPDIR}"; then
  132. if _cannot_use_tmpdir /tmp; then
  133. if _cannot_use_tmpdir "${PWD}"; then
  134. fatal "Unable to find a usable temporary directory. Please set \$TMPDIR to a path that is both writable and allows execution of files and try again." R0003
  135. else
  136. TMPDIR="${PWD}"
  137. fi
  138. else
  139. TMPDIR="/tmp"
  140. fi
  141. fi
  142. mktemp -d -t netdata-uninstaller-XXXXXXXXXX
  143. }
  144. tmpdir="$(create_tmp_directory)"
  145. detect_existing_install() {
  146. if pkg_installed netdata; then
  147. ndprefix="/"
  148. else
  149. if [ -n "${INSTALL_PREFIX}" ]; then
  150. searchpath="${INSTALL_PREFIX}/bin:${INSTALL_PREFIX}/sbin:${INSTALL_PREFIX}/usr/bin:${INSTALL_PREFIX}/usr/sbin:${PATH}"
  151. searchpath="${INSTALL_PREFIX}/netdata/bin:${INSTALL_PREFIX}/netdata/sbin:${INSTALL_PREFIX}/netdata/usr/bin:${INSTALL_PREFIX}/netdata/usr/sbin:${searchpath}"
  152. else
  153. searchpath="${PATH}"
  154. fi
  155. ndpath="$(PATH="${searchpath}" command -v netdata 2>/dev/null)"
  156. if [ -z "$ndpath" ] && [ -x /opt/netdata/bin/netdata ]; then
  157. ndpath="/opt/netdata/bin/netdata"
  158. fi
  159. if [ -n "${ndpath}" ]; then
  160. ndprefix="$(dirname "$(dirname "${ndpath}")")"
  161. fi
  162. if echo "${ndprefix}" | grep -Eq '/usr$'; then
  163. ndprefix="$(dirname "${ndprefix}")"
  164. fi
  165. fi
  166. if [ -n "${ndprefix}" ]; then
  167. typefile="${ndprefix}/etc/netdata/.install-type"
  168. envfile="${ndprefix}/etc/netdata/.environment"
  169. if [ -r "${typefile}" ]; then
  170. ${ROOTCMD} sh -c "cat \"${typefile}\" > \"${tmpdir}/install-type\""
  171. # shellcheck disable=SC1090,SC1091
  172. . "${tmpdir}/install-type"
  173. else
  174. INSTALL_TYPE="unknown"
  175. fi
  176. if [ "${INSTALL_TYPE}" = "unknown" ] || [ "${INSTALL_TYPE}" = "custom" ]; then
  177. if [ -r "${envfile}" ]; then
  178. ${ROOTCMD} sh -c "cat \"${envfile}\" > \"${tmpdir}/environment\""
  179. # shellcheck disable=SC1091
  180. . "${tmpdir}/environment"
  181. if [ -n "${NETDATA_IS_STATIC_INSTALL}" ]; then
  182. if [ "${NETDATA_IS_STATIC_INSTALL}" = "yes" ]; then
  183. INSTALL_TYPE="legacy-static"
  184. else
  185. INSTALL_TYPE="legacy-build"
  186. fi
  187. fi
  188. fi
  189. fi
  190. fi
  191. }
  192. pkg_installed() {
  193. case "${DISTRO_COMPAT_NAME}" in
  194. debian|ubuntu)
  195. dpkg-query --show --showformat '${Status}' "${1}" 2>&1 | cut -f 1 -d ' ' | grep -q '^install$'
  196. return $?
  197. ;;
  198. centos|fedora|opensuse|ol)
  199. rpm -q "${1}" > /dev/null 2>&1
  200. return $?
  201. ;;
  202. *)
  203. return 1
  204. ;;
  205. esac
  206. }
  207. detect_existing_install
  208. if [ -x "$(command -v apt-get)" ] && [ "${INSTALL_TYPE}" = "binpkg-deb" ]; then
  209. if dpkg -s netdata > /dev/null; then
  210. echo "Found netdata native installation"
  211. if user_input "Do you want to remove netdata? "; then
  212. # shellcheck disable=SC2086
  213. apt-get remove netdata ${FLAG}
  214. fi
  215. if dpkg -s netdata-repo-edge > /dev/null; then
  216. if user_input "Do you want to remove netdata-repo-edge? "; then
  217. # shellcheck disable=SC2086
  218. apt-get remove netdata-repo-edge ${FLAG}
  219. fi
  220. fi
  221. if dpkg -s netdata-repo > /dev/null; then
  222. if user_input "Do you want to remove netdata-repo? "; then
  223. # shellcheck disable=SC2086
  224. apt-get remove netdata-repo ${FLAG}
  225. fi
  226. fi
  227. exit 0
  228. fi
  229. elif [ -x "$(command -v dnf)" ] && [ "${INSTALL_TYPE}" = "binpkg-rpm" ]; then
  230. if rpm -q netdata > /dev/null; then
  231. echo "Found netdata native installation."
  232. if user_input "Do you want to remove netdata? "; then
  233. # shellcheck disable=SC2086
  234. dnf remove netdata ${FLAG}
  235. fi
  236. if rpm -q netdata-repo-edge > /dev/null; then
  237. if user_input "Do you want to remove netdata-repo-edge? "; then
  238. # shellcheck disable=SC2086
  239. dnf remove netdata-repo-edge ${FLAG}
  240. fi
  241. fi
  242. if rpm -q netdata-repo > /dev/null; then
  243. if user_input "Do you want to remove netdata-repo? "; then
  244. # shellcheck disable=SC2086
  245. dnf remove netdata-repo ${FLAG}
  246. fi
  247. fi
  248. exit 0
  249. fi
  250. elif [ -x "$(command -v yum)" ] && [ "${INSTALL_TYPE}" = "binpkg-rpm" ]; then
  251. if rpm -q netdata > /dev/null; then
  252. echo "Found netdata native installation."
  253. if user_input "Do you want to remove netdata? "; then
  254. # shellcheck disable=SC2086
  255. yum remove netdata ${FLAG}
  256. fi
  257. if rpm -q netdata-repo-edge > /dev/null; then
  258. if user_input "Do you want to remove netdata-repo-edge? "; then
  259. # shellcheck disable=SC2086
  260. yum remove netdata-repo-edge ${FLAG}
  261. fi
  262. fi
  263. if rpm -q netdata-repo > /dev/null; then
  264. if user_input "Do you want to remove netdata-repo? "; then
  265. # shellcheck disable=SC2086
  266. yum remove netdata-repo ${FLAG}
  267. fi
  268. fi
  269. exit 0
  270. fi
  271. elif [ -x "$(command -v zypper)" ] && [ "${INSTALL_TYPE}" = "binpkg-rpm" ]; then
  272. if [ "${FLAG}" = "-y" ]; then
  273. FLAG=-n
  274. fi
  275. if zypper search -i netdata > /dev/null; then
  276. echo "Found netdata native installation."
  277. if user_input "Do you want to remove netdata? "; then
  278. # shellcheck disable=SC2086
  279. zypper ${FLAG} remove netdata
  280. fi
  281. if zypper search -i netdata-repo-edge > /dev/null; then
  282. if user_input "Do you want to remove netdata-repo-edge? "; then
  283. # shellcheck disable=SC2086
  284. zypper ${FLAG} remove netdata-repo-edge
  285. fi
  286. fi
  287. if zypper search -i netdata-repo > /dev/null; then
  288. if user_input "Do you want to remove netdata-repo? "; then
  289. # shellcheck disable=SC2086
  290. zypper ${FLAG} remove netdata-repo
  291. fi
  292. fi
  293. exit 0
  294. fi
  295. fi
  296. # -----------------------------------------------------------------------------
  297. # portable service command
  298. service_cmd="$(command -v service 2> /dev/null)"
  299. rcservice_cmd="$(command -v rc-service 2> /dev/null)"
  300. systemctl_cmd="$(command -v systemctl 2> /dev/null)"
  301. service() {
  302. cmd="${1}"
  303. action="${2}"
  304. if [ -n "${systemctl_cmd}" ]; then
  305. run "${systemctl_cmd}" "${action}" "${cmd}"
  306. return $?
  307. elif [ -n "${service_cmd}" ]; then
  308. run "${service_cmd}" "${cmd}" "${action}"
  309. return $?
  310. elif [ -n "${rcservice_cmd}" ]; then
  311. run "${rcservice_cmd}" "${cmd}" "${action}"
  312. return $?
  313. fi
  314. return 1
  315. }
  316. # -----------------------------------------------------------------------------
  317. setup_terminal() {
  318. TPUT_RESET=""
  319. TPUT_YELLOW=""
  320. TPUT_WHITE=""
  321. TPUT_BGRED=""
  322. TPUT_BGGREEN=""
  323. TPUT_BOLD=""
  324. TPUT_DIM=""
  325. # Is stderr on the terminal? If not, then fail
  326. test -t 2 || return 1
  327. if command -v tput 1> /dev/null 2>&1; then
  328. if [ $(($(tput colors 2> /dev/null))) -ge 8 ]; then
  329. # Enable colors
  330. TPUT_RESET="$(tput sgr 0)"
  331. TPUT_YELLOW="$(tput setaf 3)"
  332. TPUT_WHITE="$(tput setaf 7)"
  333. TPUT_BGRED="$(tput setab 1)"
  334. TPUT_BGGREEN="$(tput setab 2)"
  335. TPUT_BOLD="$(tput bold)"
  336. TPUT_DIM="$(tput dim)"
  337. fi
  338. fi
  339. return 0
  340. }
  341. setup_terminal || echo > /dev/null
  342. ESCAPED_PRINT_METHOD=
  343. if printf "%s " test > /dev/null 2>&1; then
  344. ESCAPED_PRINT_METHOD="printfq"
  345. fi
  346. escaped_print() {
  347. if [ "${ESCAPED_PRINT_METHOD}" = "printfq" ]; then
  348. printf "%s " "${@}"
  349. else
  350. printf "%s" "${*}"
  351. fi
  352. return 0
  353. }
  354. run_logfile="/dev/null"
  355. run() {
  356. user="${USER--}"
  357. dir="${PWD}"
  358. if [ "$(id -u)" = "0" ]; then
  359. info="[root ${dir}]# "
  360. info_console="[${TPUT_DIM}${dir}${TPUT_RESET}]# "
  361. else
  362. info="[${user} ${dir}]$ "
  363. info_console="[${TPUT_DIM}${dir}${TPUT_RESET}]$ "
  364. fi
  365. {
  366. printf "%s" "${info}"
  367. escaped_print "${@}"
  368. printf "%s" " ... "
  369. } >> "${run_logfile}"
  370. printf "%s" "${info_console}${TPUT_BOLD}${TPUT_YELLOW}" >&2
  371. escaped_print >&2 "${@}"
  372. printf "%s\n" "${TPUT_RESET}" >&2
  373. "${@}"
  374. ret=$?
  375. if [ ${ret} -ne 0 ]; then
  376. printf >&2 "%s FAILED %s\n\n" "${TPUT_BGRED}${TPUT_WHITE}${TPUT_BOLD}" "${TPUT_RESET}"
  377. printf >> "${run_logfile}" "FAILED with exit code %s\n" "${ret}"
  378. NETDATA_WARNINGS="${NETDATA_WARNINGS}\n - Command \"${*}\" failed with exit code ${ret}."
  379. else
  380. printf >&2 "%s OK %s\n\n" "${TPUT_BGGREEN}${TPUT_WHITE}${TPUT_BOLD}" "${TPUT_RESET}"
  381. printf >> "${run_logfile}" "OK\n"
  382. fi
  383. return ${ret}
  384. }
  385. portable_del_group() {
  386. groupname="${1}"
  387. # Check if group exist
  388. info "Removing ${groupname} user group ..."
  389. # Linux
  390. if command -v groupdel 1> /dev/null 2>&1; then
  391. if get_group "${groupname}" > /dev/null 2>&1; then
  392. run groupdel "${groupname}" && return 0
  393. else
  394. info "Group ${groupname} already removed in a previous step."
  395. return 0
  396. fi
  397. fi
  398. # mac OS
  399. if command -v dseditgroup 1> /dev/null 2>&1; then
  400. if dseditgroup -o read netdata 1> /dev/null 2>&1; then
  401. run dseditgroup -o delete "${groupname}" && return 0
  402. else
  403. info "Could not find group ${groupname}, nothing to do"
  404. return 0
  405. fi
  406. fi
  407. error "Group ${groupname} was not automatically removed, you might have to remove it manually"
  408. return 1
  409. }
  410. issystemd() {
  411. pids=''
  412. p=''
  413. myns=''
  414. ns=''
  415. systemctl=''
  416. # if the directory /lib/systemd/system OR /usr/lib/systemd/system (SLES 12.x) does not exit, it is not systemd
  417. if [ ! -d /lib/systemd/system ] && [ ! -d /usr/lib/systemd/system ]; then
  418. return 1
  419. fi
  420. # if there is no systemctl command, it is not systemd
  421. systemctl=$(command -v systemctl 2> /dev/null)
  422. if [ -z "${systemctl}" ] || [ ! -x "${systemctl}" ]; then
  423. return 1
  424. fi
  425. # if pid 1 is systemd, it is systemd
  426. [ "$(basename "$(readlink /proc/1/exe)" 2> /dev/null)" = "systemd" ] && return 0
  427. # if systemd is not running, it is not systemd
  428. pids=$(safe_pidof systemd 2> /dev/null)
  429. [ -z "${pids}" ] && return 1
  430. # check if the running systemd processes are not in our namespace
  431. myns="$(readlink /proc/self/ns/pid 2> /dev/null)"
  432. for p in ${pids}; do
  433. ns="$(readlink "/proc/${p}/ns/pid" 2> /dev/null)"
  434. # if pid of systemd is in our namespace, it is systemd
  435. [ -n "${myns}" ] && [ "${myns}" = "${ns}" ] && return 0
  436. done
  437. # else, it is not systemd
  438. return 1
  439. }
  440. portable_del_user() {
  441. username="${1}"
  442. info "Deleting ${username} user account ..."
  443. # Linux
  444. if command -v userdel 1> /dev/null 2>&1; then
  445. run userdel -f "${username}" && return 0
  446. fi
  447. # mac OS
  448. if command -v sysadminctl 1> /dev/null 2>&1; then
  449. run sysadminctl -deleteUser "${username}" && return 0
  450. fi
  451. error "User ${username} could not be deleted from system, you might have to remove it manually"
  452. return 1
  453. }
  454. portable_del_user_from_group() {
  455. groupname="${1}"
  456. username="${2}"
  457. if command -v getent > /dev/null 2>&1; then
  458. getent group "${1:-""}" | grep -q "${2}"
  459. else
  460. grep "^${1}:" /etc/group | grep -q "${2}"
  461. fi
  462. ret=$?
  463. [ "${ret}" != "0" ] && return 0
  464. # username is not in group
  465. info "Deleting ${username} user from ${groupname} group ..."
  466. # Linux
  467. if command -v gpasswd 1> /dev/null 2>&1; then
  468. run gpasswd -d "netdata" "${group}" && return 0
  469. fi
  470. # FreeBSD
  471. if command -v pw 1> /dev/null 2>&1; then
  472. run pw groupmod "${groupname}" -d "${username}" && return 0
  473. fi
  474. # BusyBox
  475. if command -v delgroup 1> /dev/null 2>&1; then
  476. run delgroup "${username}" "${groupname}" && return 0
  477. fi
  478. # mac OS
  479. if command -v dseditgroup 1> /dev/null 2>&1; then
  480. run dseditgroup -o delete -u "${username}" "${groupname}" && return 0
  481. fi
  482. error "Failed to delete user ${username} from group ${groupname} !"
  483. return 1
  484. }
  485. quit_msg() {
  486. echo
  487. if [ "$FILE_REMOVAL_STATUS" -eq 0 ]; then
  488. fatal "Failed to completely remove Netdata from this system." R0004
  489. else
  490. info "Netdata files were successfully removed from your system"
  491. fi
  492. }
  493. rm_file() {
  494. FILE="$1"
  495. if [ -f "${FILE}" ]; then
  496. if user_input "Do you want to delete this file '$FILE' ? "; then
  497. run rm -v "${FILE}"
  498. fi
  499. fi
  500. }
  501. rm_dir() {
  502. DIR="$1"
  503. if [ -n "$DIR" ] && [ -d "$DIR" ]; then
  504. if user_input "Do you want to delete this directory '$DIR' ? "; then
  505. run rm -v -f -R "${DIR}"
  506. fi
  507. fi
  508. }
  509. safe_pidof() {
  510. pidof_cmd="$(command -v pidof 2> /dev/null)"
  511. if [ -n "${pidof_cmd}" ]; then
  512. ${pidof_cmd} "${@}"
  513. return $?
  514. else
  515. ps -acxo pid,comm |
  516. sed "s/^ *//g" |
  517. grep netdata |
  518. cut -d ' ' -f 1
  519. return $?
  520. fi
  521. }
  522. pidisnetdata() {
  523. if [ -d /proc/self ]; then
  524. if [ -z "$1" ] || [ ! -f "/proc/$1/stat" ]; then
  525. return 1
  526. fi
  527. [ "$(cut -d '(' -f 2 "/proc/$1/stat" | cut -d ')' -f 1)" = "netdata" ] && return 0
  528. return 1
  529. fi
  530. return 0
  531. }
  532. stop_netdata_on_pid() {
  533. pid="${1}"
  534. ret=0
  535. count=0
  536. pidisnetdata "${pid}" || return 0
  537. info "Stopping netdata on pid ${pid} ..."
  538. while [ -n "$pid" ] && [ ${ret} -eq 0 ]; do
  539. if [ ${count} -gt 24 ]; then
  540. error "Cannot stop the running netdata on pid ${pid}."
  541. return 1
  542. fi
  543. count=$((count + 1))
  544. pidisnetdata "${pid}" || ret=1
  545. if [ ${ret} -eq 1 ]; then
  546. break
  547. fi
  548. if [ ${count} -lt 12 ]; then
  549. run kill "${pid}" 2> /dev/null
  550. ret=$?
  551. else
  552. run kill -9 "${pid}" 2> /dev/null
  553. ret=$?
  554. fi
  555. test ${ret} -eq 0 && printf >&2 "." && sleep 5
  556. done
  557. echo >&2
  558. if [ ${ret} -eq 0 ]; then
  559. error "SORRY! CANNOT STOP netdata ON PID ${pid} !"
  560. return 1
  561. fi
  562. info "netdata on pid ${pid} stopped."
  563. return 0
  564. }
  565. netdata_pids() {
  566. p=''
  567. ns=''
  568. myns="$(readlink /proc/self/ns/pid 2> /dev/null)"
  569. for p in \
  570. $(cat /var/run/netdata.pid 2> /dev/null) \
  571. $(cat /var/run/netdata/netdata.pid 2> /dev/null) \
  572. $(safe_pidof netdata 2> /dev/null); do
  573. ns="$(readlink "/proc/${p}/ns/pid" 2> /dev/null)"
  574. if [ -z "${myns}" ] || [ -z "${ns}" ] || [ "${myns}" = "${ns}" ]; then
  575. pidisnetdata "${p}" && echo "${p}"
  576. fi
  577. done
  578. }
  579. stop_all_netdata() {
  580. p=''
  581. stop_success=0
  582. if [ "$(id -u)" -eq 0 ]; then
  583. uname="$(uname 2> /dev/null)"
  584. # Any of these may fail, but we need to not bail if they do.
  585. if issystemd; then
  586. if systemctl stop netdata; then
  587. stop_success=1
  588. sleep 5
  589. fi
  590. elif [ "${uname}" = "Darwin" ]; then
  591. if launchctl stop netdata; then
  592. stop_success=1
  593. sleep 5
  594. fi
  595. elif [ "${uname}" = "FreeBSD" ]; then
  596. if /etc/rc.d/netdata stop; then
  597. stop_success=1
  598. sleep 5
  599. fi
  600. else
  601. if service netdata stop; then
  602. stop_success=1
  603. sleep 5
  604. fi
  605. fi
  606. fi
  607. if [ "$stop_success" = "0" ]; then
  608. if [ -n "$(netdata_pids)" ] && [ -n "$(command -v netdatacli)" ]; then
  609. netdatacli shutdown-agent
  610. sleep 20
  611. fi
  612. for p in $(netdata_pids); do
  613. # shellcheck disable=SC2086
  614. stop_netdata_on_pid ${p}
  615. done
  616. fi
  617. }
  618. trap quit_msg EXIT
  619. # shellcheck source=/dev/null
  620. # shellcheck disable=SC1090
  621. . "${ENVIRONMENT_FILE}" || exit 1
  622. #### STOP NETDATA
  623. info "Stopping a possibly running netdata..."
  624. stop_all_netdata
  625. if [ "$(uname -s)" = "Darwin" ]; then
  626. launchctl unload /Library/LaunchDaemons/com.github.netdata.plist 2>/dev/null
  627. fi
  628. #### REMOVE NETDATA FILES
  629. rm_file /etc/logrotate.d/netdata
  630. rm_file /usr/lib/systemd/journald@netdata.conf.d/netdata.conf
  631. rm_file /etc/systemd/system/netdata.service
  632. rm_file /lib/systemd/system/netdata.service
  633. rm_file /usr/lib/systemd/system/netdata.service
  634. rm_file /etc/systemd/system/netdata-updater.service
  635. rm_file /lib/systemd/system/netdata-updater.service
  636. rm_file /usr/lib/systemd/system/netdata-updater.service
  637. rm_file /etc/systemd/system/netdata-updater.timer
  638. rm_file /lib/systemd/system/netdata-updater.timer
  639. rm_file /usr/lib/systemd/system/netdata-updater.timer
  640. rm_file /usr/lib/systemd/system-preset/50-netdata.preset
  641. rm_file /lib/systemd/system-preset/50-netdata.preset
  642. rm_file /etc/init.d/netdata
  643. rm_file /etc/periodic/daily/netdata-updater
  644. rm_file /etc/cron.daily/netdata-updater
  645. rm_file /etc/cron.d/netdata-updater
  646. rm_file /etc/cron.d/netdata-updater-daily
  647. rm_file /Library/LaunchDaemons/com.github.netdata.plist
  648. if [ -n "${NETDATA_PREFIX}" ] && [ -d "${NETDATA_PREFIX}" ] && [ "netdata" = "$(basename "$NETDATA_PREFIX")" ] ; then
  649. rm_dir "${NETDATA_PREFIX}"
  650. else
  651. rm_file "${NETDATA_PREFIX}/usr/sbin/netdata"
  652. rm_file "${NETDATA_PREFIX}/usr/sbin/netdatacli"
  653. rm_file "${NETDATA_PREFIX}/usr/sbin/netdata-claim.sh"
  654. rm_file "${NETDATA_PREFIX}/usr/sbin/log2journal"
  655. rm_file "${NETDATA_PREFIX}/usr/sbin/systemd-cat-native"
  656. rm_file "/tmp/netdata-ipc"
  657. rm_file "/tmp/netdata-service-cmds"
  658. rm_dir "${NETDATA_PREFIX}/usr/share/netdata"
  659. rm_dir "${NETDATA_PREFIX}/usr/libexec/netdata"
  660. rm_dir "${NETDATA_PREFIX}/var/lib/netdata"
  661. rm_dir "${NETDATA_PREFIX}/var/cache/netdata"
  662. rm_dir "${NETDATA_PREFIX}/var/log/netdata"
  663. rm_dir "${NETDATA_PREFIX}/etc/netdata"
  664. rm_dir /usr/lib/systemd/journald@netdata.conf.d/
  665. fi
  666. if [ -n "${tmpdir}" ]; then
  667. run rm -rf "${tmpdir}" || true
  668. fi
  669. FILE_REMOVAL_STATUS=1
  670. #### REMOVE USER
  671. if user_input "Do you want to delete 'netdata' system user ? "; then
  672. portable_del_user "netdata" || :
  673. elif [ -n "$NETDATA_ADDED_TO_GROUPS" ]; then
  674. if user_input "Do you want to delete 'netdata' from following groups: '$NETDATA_ADDED_TO_GROUPS' ? "; then
  675. for group in $NETDATA_ADDED_TO_GROUPS; do
  676. portable_del_user_from_group "${group}" "netdata"
  677. done
  678. fi
  679. fi
  680. ### REMOVE GROUP
  681. if user_input "Do you want to delete 'netdata' system group ? "; then
  682. portable_del_group "netdata" || :
  683. fi