#!/bin/bash
#############################################################
# Name:        Supportconfig Plugin for SUSE Manager Proxy
# Description: Gathers important troubleshooting information
#              about SUSE Manager Proxy
# License:     GPLv2
# Author:      Michael Calmer <mc@suse.de>
# Modified:    2020 November 09
#############################################################

SVER=4.2.0
RCFILE="/usr/lib/supportconfig/resources/scplugin.rc"
LOG_LINES=500  # 0 means include the entire file

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

plugin_command "grep web.version /usr/share/rhn/config-defaults/rhn_web.conf"

plugin_command "hostname --fqdn"

validate_rpm_if_installed() {
        THISRPM=$1
        echo "#==[ Validating RPM ]=================================#"
        if rpm -q $THISRPM >/dev/null 2>&1; then
                echo "# rpm -V $THISRPM"

                if rpm -V $THISRPM; then
                        echo "Status: Passed"
                else
                        echo "Status: WARNING"
                fi
        else
                echo "package $THISRPM is not installed"
                echo "Status: Skipped"
        fi
        echo
}

#############################################################
section_header "Supportconfig Plugin for SUSE Manager Proxy, v${SVER}"
RPMLIST="
spacewalk-backend
spacewalk-backend-libs
spacewalk-base-minimal
spacewalk-certs-tools
spacewalk-proxy-broker
spacewalk-proxy-common
spacewalk-proxy-docs
spacewalk-proxy-html
spacewalk-proxy-installer
spacewalk-proxy-management
spacewalk-proxy-package-manager
spacewalk-proxy-redirect
spacewalk-remote-utils
spacewalk-ssl-cert-check
SUSE-Manager-Proxy-release
"

for THISRPM in $RPMLIST; do
        validate_rpm_if_installed $THISRPM
done

find_and_plog_files () {
        [ -d "$1" ] || return 0
        FILES=$(find "$@" ! -name \*.gz ! -name \*.bz2 ! -name \*.xz)
        if [ -n "$FILES" ]; then
                plog_files $LOG_LINES $FILES
        fi
}

section_header "SUSE Manager Proxy Config Files"

pconf_files \
    /etc/rhn/rhn.conf \
    /etc/squid/squid.conf

section_header "SUSE Manager Proxy Log Files"

find_and_plog_files /var/log/rhn -type f
find_and_plog_files /var/log/squid -type f
find_and_plog_files /var/log/apache2 -type f
find_and_plog_files /var/log/salt -type f

section_header "SSL Configuration"

pconf_files $(spacewalk-cfg-get documentroot)/pub/RHN-ORG-TRUSTED-SSL-CERT \
            /etc/apache2/ssl.crt/server.crt


plugin_command "zypper --no-refresh ls"
plugin_command "zypper --no-refresh lr -u"

