netdata-uninstaller.sh 21 KB

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