netdata-uninstaller.sh 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  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-kickstart-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. apt-get remove netdata ${FLAG}
  213. fi
  214. if dpkg -s netdata-repo-edge > /dev/null; then
  215. if user_input "Do you want to remove netdata-repo-edge? "; then
  216. apt-get remove netdata-repo-edge ${FLAG}
  217. fi
  218. fi
  219. if dpkg -s netdata-repo > /dev/null; then
  220. if user_input "Do you want to remove netdata-repo? "; then
  221. apt-get remove netdata-repo ${FLAG}
  222. fi
  223. fi
  224. exit 0
  225. fi
  226. elif [ -x "$(command -v dnf)" ] && [ "${INSTALL_TYPE}" = "binpkg-rpm" ]; then
  227. if rpm -q netdata > /dev/null; then
  228. echo "Found netdata native installation."
  229. if user_input "Do you want to remove netdata? "; then
  230. dnf remove netdata ${FLAG}
  231. fi
  232. if rpm -q netdata-repo-edge > /dev/null; then
  233. if user_input "Do you want to remove netdata-repo-edge? "; then
  234. dnf remove netdata-repo-edge ${FLAG}
  235. fi
  236. fi
  237. if rpm -q netdata-repo > /dev/null; then
  238. if user_input "Do you want to remove netdata-repo? "; then
  239. dnf remove netdata-repo ${FLAG}
  240. fi
  241. fi
  242. exit 0
  243. fi
  244. elif [ -x "$(command -v yum)" ] && [ "${INSTALL_TYPE}" = "binpkg-rpm" ]; then
  245. if rpm -q netdata > /dev/null; then
  246. echo "Found netdata native installation."
  247. if user_input "Do you want to remove netdata? "; then
  248. yum remove netdata ${FLAG}
  249. fi
  250. if rpm -q netdata-repo-edge > /dev/null; then
  251. if user_input "Do you want to remove netdata-repo-edge? "; then
  252. yum remove netdata-repo-edge ${FLAG}
  253. fi
  254. fi
  255. if rpm -q netdata-repo > /dev/null; then
  256. if user_input "Do you want to remove netdata-repo? "; then
  257. yum remove netdata-repo ${FLAG}
  258. fi
  259. fi
  260. exit 0
  261. fi
  262. elif [ -x "$(command -v zypper)" ] && [ "${INSTALL_TYPE}" = "binpkg-rpm" ]; then
  263. if [ "${FLAG}" = "-y" ]; then
  264. FLAG=-n
  265. fi
  266. if zypper search -i netdata > /dev/null; then
  267. echo "Found netdata native installation."
  268. if user_input "Do you want to remove netdata? "; then
  269. zypper ${FLAG} remove netdata
  270. fi
  271. if zypper search -i netdata-repo-edge > /dev/null; then
  272. if user_input "Do you want to remove netdata-repo-edge? "; then
  273. zypper ${FLAG} remove netdata-repo-edge
  274. fi
  275. fi
  276. if zypper search -i netdata-repo > /dev/null; then
  277. if user_input "Do you want to remove netdata-repo? "; then
  278. zypper ${FLAG} remove netdata-repo
  279. fi
  280. fi
  281. exit 0
  282. fi
  283. fi
  284. # -----------------------------------------------------------------------------
  285. # portable service command
  286. service_cmd="$(command -v service 2> /dev/null)"
  287. rcservice_cmd="$(command -v rc-service 2> /dev/null)"
  288. systemctl_cmd="$(command -v systemctl 2> /dev/null)"
  289. service() {
  290. cmd="${1}"
  291. action="${2}"
  292. if [ -n "${systemctl_cmd}" ]; then
  293. run "${systemctl_cmd}" "${action}" "${cmd}"
  294. return $?
  295. elif [ -n "${service_cmd}" ]; then
  296. run "${service_cmd}" "${cmd}" "${action}"
  297. return $?
  298. elif [ -n "${rcservice_cmd}" ]; then
  299. run "${rcservice_cmd}" "${cmd}" "${action}"
  300. return $?
  301. fi
  302. return 1
  303. }
  304. # -----------------------------------------------------------------------------
  305. setup_terminal() {
  306. TPUT_RESET=""
  307. TPUT_YELLOW=""
  308. TPUT_WHITE=""
  309. TPUT_BGRED=""
  310. TPUT_BGGREEN=""
  311. TPUT_BOLD=""
  312. TPUT_DIM=""
  313. # Is stderr on the terminal? If not, then fail
  314. test -t 2 || return 1
  315. if command -v tput 1> /dev/null 2>&1; then
  316. if [ $(($(tput colors 2> /dev/null))) -ge 8 ]; then
  317. # Enable colors
  318. TPUT_RESET="$(tput sgr 0)"
  319. TPUT_YELLOW="$(tput setaf 3)"
  320. TPUT_WHITE="$(tput setaf 7)"
  321. TPUT_BGRED="$(tput setab 1)"
  322. TPUT_BGGREEN="$(tput setab 2)"
  323. TPUT_BOLD="$(tput bold)"
  324. TPUT_DIM="$(tput dim)"
  325. fi
  326. fi
  327. return 0
  328. }
  329. setup_terminal || echo > /dev/null
  330. ESCAPED_PRINT_METHOD=
  331. if printf "%s " test > /dev/null 2>&1; then
  332. ESCAPED_PRINT_METHOD="printfq"
  333. fi
  334. escaped_print() {
  335. if [ "${ESCAPED_PRINT_METHOD}" = "printfq" ]; then
  336. printf "%s " "${@}"
  337. else
  338. printf "%s" "${*}"
  339. fi
  340. return 0
  341. }
  342. run_logfile="/dev/null"
  343. run() {
  344. user="${USER--}"
  345. dir="${PWD}"
  346. if [ "$(id -u)" = "0" ]; then
  347. info="[root ${dir}]# "
  348. info_console="[${TPUT_DIM}${dir}${TPUT_RESET}]# "
  349. else
  350. info="[${user} ${dir}]$ "
  351. info_console="[${TPUT_DIM}${dir}${TPUT_RESET}]$ "
  352. fi
  353. {
  354. printf "%s" "${info}"
  355. escaped_print "${@}"
  356. printf "%s" " ... "
  357. } >> "${run_logfile}"
  358. printf "%s" "${info_console}${TPUT_BOLD}${TPUT_YELLOW}" >&2
  359. escaped_print >&2 "${@}"
  360. printf "%s\n" "${TPUT_RESET}" >&2
  361. "${@}"
  362. ret=$?
  363. if [ ${ret} -ne 0 ]; then
  364. printf >&2 "%s FAILED %s\n\n" "${TPUT_BGRED}${TPUT_WHITE}${TPUT_BOLD}" "${TPUT_RESET}"
  365. printf >> "${run_logfile}" "FAILED with exit code %s\n" "${ret}"
  366. NETDATA_WARNINGS="${NETDATA_WARNINGS}\n - Command \"${*}\" failed with exit code ${ret}."
  367. else
  368. printf >&2 "%s OK %s\n\n" "${TPUT_BGGREEN}${TPUT_WHITE}${TPUT_BOLD}" "${TPUT_RESET}"
  369. printf >> "${run_logfile}" "OK\n"
  370. fi
  371. return ${ret}
  372. }
  373. portable_del_group() {
  374. groupname="${1}"
  375. # Check if group exist
  376. info "Removing ${groupname} user group ..."
  377. # Linux
  378. if command -v groupdel 1> /dev/null 2>&1; then
  379. if grep -q "${groupname}" /etc/group; then
  380. run groupdel "${groupname}" && return 0
  381. else
  382. info "Group ${groupname} already removed in a previous step."
  383. return 0
  384. fi
  385. fi
  386. # mac OS
  387. if command -v dseditgroup 1> /dev/null 2>&1; then
  388. if dseditgroup -o read netdata 1> /dev/null 2>&1; then
  389. run dseditgroup -o delete "${groupname}" && return 0
  390. else
  391. info "Could not find group ${groupname}, nothing to do"
  392. return 0
  393. fi
  394. fi
  395. error "Group ${groupname} was not automatically removed, you might have to remove it manually"
  396. return 1
  397. }
  398. issystemd() {
  399. pids=''
  400. p=''
  401. myns=''
  402. ns=''
  403. systemctl=''
  404. # if the directory /lib/systemd/system OR /usr/lib/systemd/system (SLES 12.x) does not exit, it is not systemd
  405. if [ ! -d /lib/systemd/system ] && [ ! -d /usr/lib/systemd/system ]; then
  406. return 1
  407. fi
  408. # if there is no systemctl command, it is not systemd
  409. systemctl=$(command -v systemctl 2> /dev/null)
  410. if [ -z "${systemctl}" ] || [ ! -x "${systemctl}" ]; then
  411. return 1
  412. fi
  413. # if pid 1 is systemd, it is systemd
  414. [ "$(basename "$(readlink /proc/1/exe)" 2> /dev/null)" = "systemd" ] && return 0
  415. # if systemd is not running, it is not systemd
  416. pids=$(safe_pidof systemd 2> /dev/null)
  417. [ -z "${pids}" ] && return 1
  418. # check if the running systemd processes are not in our namespace
  419. myns="$(readlink /proc/self/ns/pid 2> /dev/null)"
  420. for p in ${pids}; do
  421. ns="$(readlink "/proc/${p}/ns/pid" 2> /dev/null)"
  422. # if pid of systemd is in our namespace, it is systemd
  423. [ -n "${myns}" ] && [ "${myns}" = "${ns}" ] && return 0
  424. done
  425. # else, it is not systemd
  426. return 1
  427. }
  428. portable_del_user() {
  429. username="${1}"
  430. info "Deleting ${username} user account ..."
  431. # Linux
  432. if command -v userdel 1> /dev/null 2>&1; then
  433. run userdel -f "${username}" && return 0
  434. fi
  435. # mac OS
  436. if command -v sysadminctl 1> /dev/null 2>&1; then
  437. run sysadminctl -deleteUser "${username}" && return 0
  438. fi
  439. error "User ${username} could not be deleted from system, you might have to remove it manually"
  440. return 1
  441. }
  442. portable_del_user_from_group() {
  443. groupname="${1}"
  444. username="${2}"
  445. # username is not in group
  446. info "Deleting ${username} user from ${groupname} group ..."
  447. # Linux
  448. if command -v gpasswd 1> /dev/null 2>&1; then
  449. run gpasswd -d "netdata" "${group}" && return 0
  450. fi
  451. # FreeBSD
  452. if command -v pw 1> /dev/null 2>&1; then
  453. run pw groupmod "${groupname}" -d "${username}" && return 0
  454. fi
  455. # BusyBox
  456. if command -v delgroup 1> /dev/null 2>&1; then
  457. run delgroup "${username}" "${groupname}" && return 0
  458. fi
  459. # mac OS
  460. if command -v dseditgroup 1> /dev/null 2>&1; then
  461. run dseditgroup -o delete -u "${username}" "${groupname}" && return 0
  462. fi
  463. error "Failed to delete user ${username} from group ${groupname} !"
  464. return 1
  465. }
  466. quit_msg() {
  467. echo
  468. if [ "$FILE_REMOVAL_STATUS" -eq 0 ]; then
  469. fatal "Failed to completely remove Netdata from this system." R0004
  470. else
  471. info "Netdata files were successfully removed from your system"
  472. fi
  473. }
  474. rm_file() {
  475. FILE="$1"
  476. if [ -f "${FILE}" ]; then
  477. if user_input "Do you want to delete this file '$FILE' ? "; then
  478. run rm -v "${FILE}"
  479. fi
  480. fi
  481. }
  482. rm_dir() {
  483. DIR="$1"
  484. if [ -n "$DIR" ] && [ -d "$DIR" ]; then
  485. if user_input "Do you want to delete this directory '$DIR' ? "; then
  486. run rm -v -f -R "${DIR}"
  487. fi
  488. fi
  489. }
  490. safe_pidof() {
  491. pidof_cmd="$(command -v pidof 2> /dev/null)"
  492. if [ -n "${pidof_cmd}" ]; then
  493. ${pidof_cmd} "${@}"
  494. return $?
  495. else
  496. ps -acxo pid,comm |
  497. sed "s/^ *//g" |
  498. grep netdata |
  499. cut -d ' ' -f 1
  500. return $?
  501. fi
  502. }
  503. pidisnetdata() {
  504. if [ -d /proc/self ]; then
  505. if [ -z "$1" ] || [ ! -f "/proc/$1/stat" ]; then
  506. return 1
  507. fi
  508. [ "$(cut -d '(' -f 2 "/proc/$1/stat" | cut -d ')' -f 1)" = "netdata" ] && return 0
  509. return 1
  510. fi
  511. return 0
  512. }
  513. stop_netdata_on_pid() {
  514. pid="${1}"
  515. ret=0
  516. count=0
  517. pidisnetdata "${pid}" || return 0
  518. info "Stopping netdata on pid ${pid} ..."
  519. while [ -n "$pid" ] && [ ${ret} -eq 0 ]; do
  520. if [ ${count} -gt 24 ]; then
  521. error "Cannot stop the running netdata on pid ${pid}."
  522. return 1
  523. fi
  524. count=$((count + 1))
  525. pidisnetdata "${pid}" || ret=1
  526. if [ ${ret} -eq 1 ]; then
  527. break
  528. fi
  529. if [ ${count} -lt 12 ]; then
  530. run kill "${pid}" 2> /dev/null
  531. ret=$?
  532. else
  533. run kill -9 "${pid}" 2> /dev/null
  534. ret=$?
  535. fi
  536. test ${ret} -eq 0 && printf >&2 "." && sleep 5
  537. done
  538. echo >&2
  539. if [ ${ret} -eq 0 ]; then
  540. error "SORRY! CANNOT STOP netdata ON PID ${pid} !"
  541. return 1
  542. fi
  543. info "netdata on pid ${pid} stopped."
  544. return 0
  545. }
  546. netdata_pids() {
  547. p=''
  548. ns=''
  549. myns="$(readlink /proc/self/ns/pid 2> /dev/null)"
  550. for p in \
  551. $(cat /var/run/netdata.pid 2> /dev/null) \
  552. $(cat /var/run/netdata/netdata.pid 2> /dev/null) \
  553. $(safe_pidof netdata 2> /dev/null); do
  554. ns="$(readlink "/proc/${p}/ns/pid" 2> /dev/null)"
  555. if [ -z "${myns}" ] || [ -z "${ns}" ] || [ "${myns}" = "${ns}" ]; then
  556. pidisnetdata "${p}" && echo "${p}"
  557. fi
  558. done
  559. }
  560. stop_all_netdata() {
  561. p=''
  562. stop_success=0
  563. if [ "$(id -u)" -eq 0 ]; then
  564. uname="$(uname 2> /dev/null)"
  565. # Any of these may fail, but we need to not bail if they do.
  566. if issystemd; then
  567. if systemctl stop netdata; then
  568. stop_success=1
  569. sleep 5
  570. fi
  571. elif [ "${uname}" = "Darwin" ]; then
  572. if launchctl stop netdata; then
  573. stop_success=1
  574. sleep 5
  575. fi
  576. elif [ "${uname}" = "FreeBSD" ]; then
  577. if /etc/rc.d/netdata stop; then
  578. stop_success=1
  579. sleep 5
  580. fi
  581. else
  582. if service netdata stop; then
  583. stop_success=1
  584. sleep 5
  585. fi
  586. fi
  587. fi
  588. if [ "$stop_success" = "0" ]; then
  589. if [ -n "$(netdata_pids)" ] && [ -n "$(command -v netdatacli)" ]; then
  590. netdatacli shutdown-agent
  591. sleep 20
  592. fi
  593. for p in $(netdata_pids); do
  594. # shellcheck disable=SC2086
  595. stop_netdata_on_pid ${p}
  596. done
  597. fi
  598. }
  599. trap quit_msg EXIT
  600. # shellcheck source=/dev/null
  601. # shellcheck disable=SC1090
  602. . "${ENVIRONMENT_FILE}" || exit 1
  603. #### STOP NETDATA
  604. info "Stopping a possibly running netdata..."
  605. stop_all_netdata
  606. #### REMOVE NETDATA FILES
  607. rm_file /etc/logrotate.d/netdata
  608. rm_file /etc/systemd/system/netdata.service
  609. rm_file /lib/systemd/system/netdata.service
  610. rm_file /usr/lib/systemd/system/netdata.service
  611. rm_file /etc/systemd/system/netdata-updater.service
  612. rm_file /lib/systemd/system/netdata-updater.service
  613. rm_file /usr/lib/systemd/system/netdata-updater.service
  614. rm_file /etc/systemd/system/netdata-updater.timer
  615. rm_file /lib/systemd/system/netdata-updater.timer
  616. rm_file /usr/lib/systemd/system/netdata-updater.timer
  617. rm_file /etc/init.d/netdata
  618. rm_file /etc/periodic/daily/netdata-updater
  619. rm_file /etc/cron.daily/netdata-updater
  620. rm_file /etc/cron.d/netdata-updater
  621. if [ -n "${NETDATA_PREFIX}" ] && [ -d "${NETDATA_PREFIX}" ]; then
  622. rm_dir "${NETDATA_PREFIX}"
  623. else
  624. rm_file "/usr/sbin/netdata"
  625. rm_file "/usr/sbin/netdatacli"
  626. rm_file "/tmp/netdata-ipc"
  627. rm_file "/usr/sbin/netdata-claim.sh"
  628. rm_dir "/usr/share/netdata"
  629. rm_dir "/usr/libexec/netdata"
  630. rm_dir "/var/lib/netdata"
  631. rm_dir "/var/cache/netdata"
  632. rm_dir "/var/log/netdata"
  633. rm_dir "/etc/netdata"
  634. fi
  635. FILE_REMOVAL_STATUS=1
  636. #### REMOVE NETDATA USER FROM ADDED GROUPS
  637. if [ -n "$NETDATA_ADDED_TO_GROUPS" ]; then
  638. if user_input "Do you want to delete 'netdata' from following groups: '$NETDATA_ADDED_TO_GROUPS' ? "; then
  639. for group in $NETDATA_ADDED_TO_GROUPS; do
  640. portable_del_user_from_group "${group}" "netdata"
  641. done
  642. fi
  643. fi
  644. #### REMOVE USER
  645. if user_input "Do you want to delete 'netdata' system user ? "; then
  646. portable_del_user "netdata" || :
  647. fi
  648. ### REMOVE GROUP
  649. if user_input "Do you want to delete 'netdata' system group ? "; then
  650. portable_del_group "netdata" || :
  651. fi