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

SVER=4.2.0
RCFILE="/usr/lib/supportconfig/resources/scplugin.rc"

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

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 Client, v${SVER}"
RPMLIST="
spacewalk-client-tools
spacewalk-check
spacewalk-client-setup
rhnlib
osad
zypp-plugin-spacewalk
salt-minion
salt
"

for THISRPM in $RPMLIST; do
    validate_rpm_if_installed $THISRPM
done

plugin_command "/bin/ls -l --time-style=long-iso /usr/local/lib"
plugin_command "/bin/ls -l --time-style=long-iso /usr/local/lib64"
plugin_command "/bin/ls -l --time-style=long-iso /etc/ssl/certs/"

section_header "SUSE Manager Client Config Files"

pconf_files \
    /etc/sysconfig/rhn/up2date \
    /etc/sysconfig/rhn/osad.conf \
    /etc/sysconfig/rhn/rhncfg-client.conf \
    /etc/sysconfig/rhn/rhncfg-manager.conf \
    /etc/sysconfig/rhn/image.cfg \
    /etc/sysconfig/rhn/rhnpushrc \
    /etc/sysconfig/rhn/rhnsd \
    /etc/sysconfig/rhn/systemid \
    /etc/salt/minion \
    /etc/salt/minion.d/susemanager.conf \
    /etc/salt/minion.d/_schedule.conf


section_header "SUSE Manager Client Capabilities"

find /etc/sysconfig/rhn/clientCaps.d/

section_header "SUSE Manager Client allowed Actions"

find /etc/sysconfig/rhn/allowed-actions/

section_header "SUSE Manager Client Log Files"

plog_files 1000 /var/log/up2date
plog_files 100 /var/log/osad
plog_files 1000 /var/log/salt/minion

plugin_command "zypper --no-refresh ls"
plugin_command "zypper --no-refresh lr -u"
plugin_command "salt-minion --versions-report"

plugin_command "cp /var/log/zypper.log $LOG"
