netdata-uninstaller.sh 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  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. # 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. # username is not in group
  458. info "Deleting ${username} user from ${groupname} group ..."
  459. # Linux
  460. if command -v gpasswd 1> /dev/null 2>&1; then
  461. run gpasswd -d "netdata" "${group}" && return 0
  462. fi
  463. # FreeBSD
  464. if command -v pw 1> /dev/null 2>&1; then
  465. run pw groupmod "${groupname}" -d "${username}" && return 0
  466. fi
  467. # BusyBox
  468. if command -v delgroup 1> /dev/null 2>&1; then
  469. run delgroup "${username}" "${groupname}" && return 0
  470. fi
  471. # mac OS
  472. if command -v dseditgroup 1> /dev/null 2>&1; then
  473. run dseditgroup -o delete -u "${username}" "${groupname}" && return 0
  474. fi
  475. error "Failed to delete user ${username} from group ${groupname} !"
  476. return 1
  477. }
  478. quit_msg() {
  479. echo
  480. if [ "$FILE_REMOVAL_STATUS" -eq 0 ]; then
  481. fatal "Failed to completely remove Netdata from this system." R0004
  482. else
  483. info "Netdata files were successfully removed from your system"
  484. fi
  485. }
  486. rm_file() {
  487. FILE="$1"
  488. if [ -f "${FILE}" ]; then
  489. if user_input "Do you want to delete this file '$FILE' ? "; then
  490. run rm -v "${FILE}"
  491. fi
  492. fi
  493. }
  494. rm_dir() {
  495. DIR="$1"
  496. if [ -n "$DIR" ] && [ -d "$DIR" ]; then
  497. if user_input "Do you want to delete this directory '$DIR' ? "; then
  498. run rm -v -f -R "${DIR}"
  499. fi
  500. fi
  501. }
  502. safe_pidof() {
  503. pidof_cmd="$(command -v pidof 2> /dev/null)"
  504. if [ -n "${pidof_cmd}" ]; then
  505. ${pidof_cmd} "${@}"
  506. return $?
  507. else
  508. ps -acxo pid,comm |
  509. sed "s/^ *//g" |
  510. grep netdata |
  511. cut -d ' ' -f 1
  512. return $?
  513. fi
  514. }
  515. pidisnetdata() {
  516. if [ -d /proc/self ]; then
  517. if [ -z "$1" ] || [ ! -f "/proc/$1/stat" ]; then
  518. return 1
  519. fi
  520. [ "$(cut -d '(' -f 2 "/proc/$1/stat" | cut -d ')' -f 1)" = "netdata" ] && return 0
  521. return 1
  522. fi
  523. return 0
  524. }
  525. stop_netdata_on_pid() {
  526. pid="${1}"
  527. ret=0
  528. count=0
  529. pidisnetdata "${pid}" || return 0
  530. info "Stopping netdata on pid ${pid} ..."
  531. while [ -n "$pid" ] && [ ${ret} -eq 0 ]; do
  532. if [ ${count} -gt 24 ]; then
  533. error "Cannot stop the running netdata on pid ${pid}."
  534. return 1
  535. fi
  536. count=$((count + 1))
  537. pidisnetdata "${pid}" || ret=1
  538. if [ ${ret} -eq 1 ]; then
  539. break
  540. fi
  541. if [ ${count} -lt 12 ]; then
  542. run kill "${pid}" 2> /dev/null
  543. ret=$?
  544. else
  545. run kill -9 "${pid}" 2> /dev/null
  546. ret=$?
  547. fi
  548. test ${ret} -eq 0 && printf >&2 "." && sleep 5
  549. done
  550. echo >&2
  551. if [ ${ret} -eq 0 ]; then
  552. error "SORRY! CANNOT STOP netdata ON PID ${pid} !"
  553. return 1
  554. fi
  555. info "netdata on pid ${pid} stopped."
  556. return 0
  557. }
  558. netdata_pids() {
  559. p=''
  560. ns=''
  561. myns="$(readlink /proc/self/ns/pid 2> /dev/null)"
  562. for p in \
  563. $(cat /var/run/netdata.pid 2> /dev/null) \
  564. $(cat /var/run/netdata/netdata.pid 2> /dev/null) \
  565. $(safe_pidof netdata 2> /dev/null); do
  566. ns="$(readlink "/proc/${p}/ns/pid" 2> /dev/null)"
  567. if [ -z "${myns}" ] || [ -z "${ns}" ] || [ "${myns}" = "${ns}" ]; then
  568. pidisnetdata "${p}" && echo "${p}"
  569. fi
  570. done
  571. }
  572. stop_all_netdata() {
  573. p=''
  574. stop_success=0
  575. if [ "$(id -u)" -eq 0 ]; then
  576. uname="$(uname 2> /dev/null)"
  577. # Any of these may fail, but we need to not bail if they do.
  578. if issystemd; then
  579. if systemctl stop netdata; then
  580. stop_success=1
  581. sleep 5
  582. fi
  583. elif [ "${uname}" = "Darwin" ]; then
  584. if launchctl stop netdata; then
  585. stop_success=1
  586. sleep 5
  587. fi
  588. elif [ "${uname}" = "FreeBSD" ]; then
  589. if /etc/rc.d/netdata stop; then
  590. stop_success=1
  591. sleep 5
  592. fi
  593. else
  594. if service netdata stop; then
  595. stop_success=1
  596. sleep 5
  597. fi
  598. fi
  599. fi
  600. if [ "$stop_success" = "0" ]; then
  601. if [ -n "$(netdata_pids)" ] && [ -n "$(command -v netdatacli)" ]; then
  602. netdatacli shutdown-agent
  603. sleep 20
  604. fi
  605. for p in $(netdata_pids); do
  606. # shellcheck disable=SC2086
  607. stop_netdata_on_pid ${p}
  608. done
  609. fi
  610. }
  611. trap quit_msg EXIT
  612. # shellcheck source=/dev/null
  613. # shellcheck disable=SC1090
  614. . "${ENVIRONMENT_FILE}" || exit 1
  615. #### STOP NETDATA
  616. info "Stopping a possibly running netdata..."
  617. stop_all_netdata
  618. #### REMOVE NETDATA FILES
  619. rm_file /etc/logrotate.d/netdata
  620. rm_file /etc/systemd/system/netdata.service
  621. rm_file /lib/systemd/system/netdata.service
  622. rm_file /usr/lib/systemd/system/netdata.service
  623. rm_file /etc/systemd/system/netdata-updater.service
  624. rm_file /lib/systemd/system/netdata-updater.service
  625. rm_file /usr/lib/systemd/system/netdata-updater.service
  626. rm_file /etc/systemd/system/netdata-updater.timer
  627. rm_file /lib/systemd/system/netdata-updater.timer
  628. rm_file /usr/lib/systemd/system/netdata-updater.timer
  629. rm_file /etc/init.d/netdata
  630. rm_file /etc/periodic/daily/netdata-updater
  631. rm_file /etc/cron.daily/netdata-updater
  632. rm_file /etc/cron.d/netdata-updater
  633. if [ -n "${NETDATA_PREFIX}" ] && [ -d "${NETDATA_PREFIX}" ]; then
  634. rm_dir "${NETDATA_PREFIX}"
  635. else
  636. rm_file "/usr/sbin/netdata"
  637. rm_file "/usr/sbin/netdatacli"
  638. rm_file "/tmp/netdata-ipc"
  639. rm_file "/usr/sbin/netdata-claim.sh"
  640. rm_dir "/usr/share/netdata"
  641. rm_dir "/usr/libexec/netdata"
  642. rm_dir "/var/lib/netdata"
  643. rm_dir "/var/cache/netdata"
  644. rm_dir "/var/log/netdata"
  645. rm_dir "/etc/netdata"
  646. fi
  647. FILE_REMOVAL_STATUS=1
  648. #### REMOVE NETDATA USER FROM ADDED GROUPS
  649. if [ -n "$NETDATA_ADDED_TO_GROUPS" ]; then
  650. if user_input "Do you want to delete 'netdata' from following groups: '$NETDATA_ADDED_TO_GROUPS' ? "; then
  651. for group in $NETDATA_ADDED_TO_GROUPS; do
  652. portable_del_user_from_group "${group}" "netdata"
  653. done
  654. fi
  655. fi
  656. #### REMOVE USER
  657. if user_input "Do you want to delete 'netdata' system user ? "; then
  658. portable_del_user "netdata" || :
  659. fi
  660. ### REMOVE GROUP
  661. if user_input "Do you want to delete 'netdata' system group ? "; then
  662. portable_del_group "netdata" || :
  663. fi