#!/bin/bash
#############################################################
# Name:        Supportconfig Plugin for SUSE Manager
# Description: Gathers important troubleshooting information
#              about SUSE Manager
# License:     GPLv2
# Author:      Stefan Bogner <sbogner@suse.com>
#              Michael Calmer <mc@suse.com>
# Modified:    2024 February 03
#############################################################

SVER=5.0.0
RCFILE="/usr/lib/supportconfig/resources/supportconfig.rc"
OF='plugin-susemanager.txt'

[ -s $RCFILE ] && . $RCFILE || { echo "ERROR: Initializing resource file: $RCFILE"; exit 1; }

log_write $OF
log_entry $OF note "Supportconfig Plugin for SUSE Manager, v${SVER}"
addHeaderFile $OF

log_cmd $OF "grep web.version /usr/share/rhn/config-defaults/rhn_web.conf"

log_cmd $OF "hostname --fqdn"

RPMLIST="
susemanager-tools
susemanager
susemanager-schema
SUSE-Manager-Server-release
sle-module-suse-manager-server-release
salt-minion
salt
"
DAEMONLIST="tomcat taskomatic postgresql rhn-search cobblerd apache2 salt-api salt-master"

if ! rpm -q susemanager &>/dev/null; then
  echo "ERROR: SUSE Manager package(s) not installed"
  echo
  exit 111
fi

for THISRPM in $RPMLIST
do
   rpm_verify $OF $THISRPM
done

for i in $DAEMONLIST
do
    log_cmd $OF "systemctl status $i"
done

log_cmd $OF "/bin/ls -l --time-style=long-iso /usr/local/lib"
log_cmd $OF "/bin/ls -l --time-style=long-iso /usr/local/lib64"

log_cmd $OF "find /usr/share/susemanager/www/tomcat/webapps/rhn/WEB-INF/lib/ | xargs file | grep broken"
log_cmd $OF "find /usr/share/spacewalk/taskomatic/ | xargs file | grep broken"
log_cmd $OF "find /usr/share/rhn/search/lib/ | xargs file | grep broken"

log_entry $OF note "SSL Configuration"

conf_files $OF $(spacewalk-cfg-get documentroot)/pub/RHN-ORG-TRUSTED-SSL-CERT \
            /etc/pki/tls/certs/spacewalk.crt
log_cmd $OF "/bin/ls -l --time-style=long-iso /etc/ssl/certs/"

log_cmd $OF "/bin/ls -l --time-style=long-iso $(spacewalk-cfg-get documentroot)/pub/ | grep -i trusted"

log_entry $OF note "Crypto Policy"

if [ -f /etc/crypto-policies/config ]; then
	log_cmd $OF "cat /etc/crypto-policies/config"
elif [ $(cat /proc/sys/crypto/fips_enabled) -ne 0 ]; then
	log_write $OF "FIPS"
else
	log_cmd $OF "grep -v '#' /usr/share/crypto-policies/default-config"
fi


log_cmd $OF "psql --version"
log_cmd $OF "cat /var/lib/pgsql/data/PG_VERSION"
log_cmd $OF "zypper --no-refresh ls"
log_cmd $OF "zypper --no-refresh lr -u"
log_cmd $OF "/usr/lib/susemanager/bin/susemanager-connection-check"
log_cmd $OF "salt-master --versions-report"

log_cmd $OF "spacewalk-sql --select-mode-direct - <<< \"select username scc_org from susecredentials sc where sc.type = 'scc';\""

log_write $OF "# Number of elements in table susesaltevent. If the next number is too high, please verify the large scale tuning guide property 'java.salt_event_thread_pool_size'."
log_cmd $OF "spacewalk-sql --select-mode-direct - <<< \"select count(*) from susesaltevent;\""
log_cmd $OF "grep \"Client bootstrap script\" /srv/www/htdocs/pub/bootstrap/*.sh"

log_entry $OF note "Cloud / PAYG"
if [ -x /usr/bin/instance-flavor-check ]; then
        log_cmd $OF "/usr/bin/instance-flavor-check"
        if [ $(/usr/bin/instance-flavor-check) == "PAYG" ]; then
                validate_rpm "python-instance-billing-flavor-check"
                validate_rpm "billing-data-service"
                validate_rpm "csp-billing-adapter-service"
                validate_rpm "python3-csp-billing-adapter"
                validate_rpm "python3-csp-billing-adapter-local"
                validate_rpm "python3-csp-billing-adapter-amazon"
                validate_rpm "suma-amazon-adapter-config-llc"
                validate_rpm "suma-amazon-adapter-config-ltd"
                validate_rpm "python3-csp-billing-adapter-azure"
                validate_rpm "suma-azure-adapter-config-llc"
                validate_rpm "suma-azure-adapter-config-ltd"
        else
                log_write $OF "Not a PAYG Instance"
        fi
else
        log_write $OF "Not a Cloud Instance"
fi

log_cmd $OF "/sbin/supportconfig-sumalog $LOG"
log_cmd $OF "cp /var/log/zypper.log $LOG"

