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