#!/usr/bin/env bash version="2023.04.18.01" install_dotnet_zypper() { zypper -n install libicu rpm --import https://packages.microsoft.com/keys/microsoft.asc wget https://packages.microsoft.com/config/opensuse/15/prod.repo mv prod.repo /etc/zypp/repos.d/microsoft-prod.repo chown root:root /etc/zypp/repos.d/microsoft-prod.repo zypper -n install dotnet-runtime-6.0 } install_dotnet_apt() { #curl https://packages.microsoft.com/config/ubuntu/20.04/packages-microsoft-prod.deb -o packages-microsoft-prod.deb #dpkg -i packages-microsoft-prod.deb apt update apt install dotnet-runtime-6.0 -y } install_dotnet_yum() { rpm -Uvh https://packages.microsoft.com/config/centos/7/packages-microsoft-prod.rpm yum -y install dotnet-runtime-6.0.x86_64 } verify_install() { all_good="yes" echo "Install Status:" chk=`systemctl | grep discoveryagent | grep -v updater | wc -l` if [ $chk -eq 1 ] then echo " discoveryagent: Installed" else echo " discoveryagent: NOT INSTALLED" all_good="no" fi chk=`systemctl | grep discoveryagentupdater | wc -l` if [ $chk -eq 1 ] then echo " discoveryagentupdater: Installed" else echo " discoveryagentupdater: NOT INSTALLED" all_good="no" fi echo "Running Status:" chk=`systemctl is-active discoveryagent` if [ $chk = "active" ] then echo " discoveryagent: Running" else echo " discoveryagent: NOT RUNNING" all_good="no" fi chk=`systemctl is-active discoveryagentupdater` if [ $chk = "active" ] then echo " discoveryagentupdater: Running" else echo " discoveryagentupdater: NOT RUNNING" all_good="no" fi if [ $all_good = "yes" ] then echo "Final Status: Passed" else echo "Final Status: FAILED" fi } do_uninstall() { # Do not allow the --install-dir to be used with uninstall. # This protects the removal process. install_dir="" if [ -f /etc/systemd/system/discoveryagent.service ] then install_dir=`grep discoveryagent /etc/systemd/system/discoveryagent.service | cut -d'=' -f2 | awk '{print substr($0, 0, length($0)-15)}'` fi if [ "X$install_dir" = "X" ] then echo "ERROR: Could not locate the install directory." exit fi if [ ! -d $install_dir ] then echo "ERROR: Install directory $install_dir does not exist." exit fi if [ ! -f $install_dir/discoveryagent ] then echo "ERROR: Install directory $install_dir does not appear to be valid install." exit fi if [ $force = "no" ] then echo "Are you sure you want to uninstall this Discovery Agent? Type YES to do so." read answer if [[ "0$answer" != "0YES" ]] then echo "Unistall was canceled." exit fi fi systemctl stop discoveryagent systemctl stop discoveryagentupdater sleep 3 systemctl disable discoveryagent systemctl disable discoveryagentupdater systemctl daemon-reload systemctl reset-failed rm -f /etc/systemd/system/discoveryagent.service rm -f /etc/systemd/system/discoveryagentupdater.service rm -rf $install_dir } download_bundle() { rm $pkg 2>/dev/null curl -m 900 $url -o $pkg chk=`ls -lrt $pkg | awk '{print \$5}'` if [ ! -e $pkg ] || [ $chk -eq 0 ] then echo "ERROR: Download failed!" exit fi } install_missing_pkgs () { if [ ${#missing_pkgs[@]} -ne 0 ] then for p in "${missing_pkgs[@]}" do # Make sure zypper chec is first to support openSUSE # since its apt does not work with our current function. if [[ $have_zypper = "yes" ]] then zypper -n install $p elif [[ $have_yum = "yes" ]] then yum -y install $p elif [[ $have_apt = "yes" ]] then apt install $p -y fi done fi # This should never be the case for now based on prereqs if [ $dotnet6_installed = "no" ] then # Make sure zypper chec is first to support openSUSE # since its apt does not work with our current function. if [[ $have_zypper = "yes" ]] then install_dotnet_zypper elif [[ $have_yum = "yes" ]] then install_dotnet_yum elif [[ $have_apt = "yes" ]] then install_dotnet_apt fi fi } do_install () { if [ "X$install_dir" = "X" ] then install_dir="/opt/discoveryagent" fi if [ -d $install_dir ] then echo "ERROR: Install Dir $install_dir already exists." exit fi if [ "X$bundle" = "X" ] then download_bundle else pkg=$bundle if [ ! -e $bundle ] then echo "ERROR: Specified bundle does not exist!" exit fi fi rm -rf /tmp/da-update 2>/dev/null mkdir /tmp/da-update 2>/dev/null unzip -o -d /tmp/da-update $pkg mkdir -p $install_dir 2>/dev/null unzip -o -d $install_dir /tmp/da-update/DiscoveryAgentLinux64Bins.zip if [[ $beta = "yes" ]] then touch ${install_dir}/usebeta elif [[ $alpha = "yes" ]] then touch ${install_dir}/usealpha elif [[ $internal = "yes" ]] then touch ${install_dir}/useinternal fi chmod 755 $install_dir/discoveryagent chmod 755 $install_dir/tmp/discoveryagentupdater mv $install_dir/tmp/discoveryagentupdater $install_dir chmod 755 $install_dir/collectors/dadc/dadc for i in `ls $install_dir/scripts` do chmod 755 $install_dir/scripts/$i sed -i -e 's/\r$//' $install_dir/scripts/$i done cp /tmp/da-update/discoveryagent.service.template /etc/systemd/system/discoveryagent.service cp /tmp/da-update/discoveryagentupdater.service.template /etc/systemd/system/discoveryagentupdater.service sed -i "s|REPLACE_INSTALL_DIR|$install_dir|g" /etc/systemd/system/discoveryagent.service sed -i "s|REPLACE_INSTALL_DIR|$install_dir|g" /etc/systemd/system/discoveryagentupdater.service systemctl enable --now discoveryagent.service systemctl enable --now discoveryagentupdater.service systemctl daemon-reload } print_usage() { echo " " echo "Syntax: $me [command] [options]" echo " " echo "commands:" echo " --version|-v" echo " --help|-h" echo " --check-prereqs|-c" echo " --install-missing-pkgs" echo " Note: --install will do this automatically. Only use this option to install the pkgs without doing the full install." echo " --download-bundle" echo " --url [url]" echo " Overrides the URL used for downloading the install bundle." echo " --install" echo " --install-dir [install dir]" echo " Defaults to /opt/discoveryagent" echo " --url [url]" echo " Overrides the URL used for downloading the install bundle." echo " --bundle [install bundle zip file]" echo " Use an install bundle already on the local machine." echo " --verify-install" echo " --uninstall" echo " options:" echo " --force" echo " Non-interactive mode. Does not prompt for confirmation." echo " " } check_prereqs () { critical_ary=() echo -n "Discovery Agent already installed..." chk=`which systemctl 2>/dev/null | wc -l` if [ -e /etc/systemd/system/discoveryagent.service ] then echo "Yes." critical_ary+=("Discovery Agent already appears to be installed. Exiting install.") prereq_passed="no" else echo "No." fi echo -n "SystemD in use..." chk=`which systemctl 2>/dev/null | wc -l` if [[ $chk -lt 1 ]] then echo "No." critical_ary+=("Missing required systemctl. Exiting install.") prereq_passed="no" else echo "Yes." fi echo -n "Checking for apt..." chk=`which apt 2>/dev/null | wc -l` if [[ $chk -gt 0 ]] then echo "Found." have_apt="yes" else echo "Not found." fi echo -n "Checking for yum..." chk=`which yum 2>/dev/null | wc -l` if [[ $chk -gt 0 ]] then echo "Found." have_yum="yes" else echo "Not found." fi echo -n "Checking for zypper..." chk=`which zypper 2>/dev/null | wc -l` if [[ $chk -gt 0 ]] then echo "Found." have_zypper="yes" else echo "Not found." fi if [ "$have_yum" = "no" ] && [ "$have_apt" = "no" ] && [ "$have_zypper" = "no" ] then critical_ary+=("Missing either YUM, APT, or ZYPPER.") prereq_passed="no" fi echo -n "Checking for unzip..." chk=`which unzip 2>/dev/null | wc -l` if [[ $chk -eq 0 ]] then echo "Not found." missing_pkgs+=("unzip") prereq_passed="no" else echo "Found." fi echo -n "Checking for curl..." chk=`which curl 2>/dev/null | wc -l` if [[ $chk -eq 0 ]] then echo "Not found." missing_pkgs+=("curl") prereq_passed="no" else echo "Found." fi echo -n "Checking for dotnet 6 runtime..." chk=`which dotnet 2>/dev/null | wc -l` if [[ $chk -eq 0 ]] then echo "Not found." prereq_passed="no" critical_ary+=("Missing dotnet6 runtime."); critical_ary+=("Visit https://dotnet.microsoft.com/en-us/download/dotnet/6.0 for install instrutions.") else chk=`dotnet --info | grep Ver | grep "6.0" | wc -l` if [[ $chk -eq 0 ]] then echo "Not found." prereq_passed="no" critical_ary+=("Missing dotnet6 runtime."); critical_ary+=("Visit https://dotnet.microsoft.com/en-us/download/dotnet/6.0 for install instrutions.") else echo "Found." dotnet6_installed="yes" fi fi if [ ${#critical_ary[@]} -eq 0 ] then if [ ${#missing_pkgs[@]} -gt 0 ] || [ $dotnet6_installed = "no" ] then echo "The following will be installed during the install process:" for p in "${missing_pkgs[@]}" do echo " $p" done if [ ${#missing_pkgs[@]} -gt 0 ] || [ $dotnet6_installed = "no" ] then echo " dotnet 6 runtime" fi fi echo "Final Reuslt: Installable." else echo "Failed Critical Pre-reqs:" for issue in "${critical_ary[@]}" do echo " $issue" done echo "Final Reuslt: FAILED!" exit fi } # Main Script DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" cd $DIR me=`basename "$0"` alpha="no" beta="no" internal="no" have_yum="no" have_apt="no" have_zypper="no" dotnet6_installed="no" prereq_passed="yes" missing_pkgs=() cid="" user="" pwd="" bundle="" install_dir="" for_install="no" for_uninstall="no" for_downlaod="no" force="no" pkg="DiscoveryAgentLinux64Update.zip" url="https://download.rapidfiretools.com/updates/inspector/$pkg" while test $# -gt 0; do case "$1" in -h|--help) print_usage exit ;; -v|--version) echo $version exit ;; --install) for_install="yes" ;; --uninstall) for_uninstall="yes" ;; -c|--check-prereqs) check_prereqs exit ;; --install-missing-pkgs) check_prereqs install_missing_pkgs exit ;; --verify-install) verify_install exit ;; --download-bundle) for_download="yes" ;; --force) force="yes" ;; --install-dir) shift install_dir=$1 ;; --url) shift url=$1 ;; --bundle) shift bundle=$1 ;; --alpha) alpha="yes" ;; --beta) beta="yes" ;; --internal) internal="yes" ;; *) break ;; esac shift done if [[ $beta = "yes" ]] then url="https://download.rapidfiretools.com/beta/$pkg" elif [[ $alpha = "yes" ]] then url="https://download.rapidfiretools.com/alpha/$pkg" elif [[ $internal = "yes" ]] then url="https://download.rapidfiretools.com/internal/$pkg" fi if [[ $for_download = "yes" ]] then check_prereqs download_bundle elif [[ $for_install = "yes" ]] then arch=`arch` if [ "$arch" != "x86_64" ] then echo "Your system architecture is not currenly supported. Please contact support for more information." echo "System Architecture: $arch" exit fi check_prereqs install_missing_pkgs do_install verify_install elif [[ $for_uninstall = "yes" ]] then do_uninstall else print_usage fi exit