#!/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:    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; }

section_header "Supportconfig Plugin for SUSE Manager, v${SVER}"

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

plugin_command "hostname --fqdn"

RPMLIST="
susemanager-tools
susemanager
susemanager-schema
SUSE-Manager-Server-release
sle-module-suse-manager-server-release
salt-minion
salt
"
DAEMONLIST="susemanager tomcat osa-dispatcher postgresql"

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

for THISRPM in $RPMLIST
do
   validate_rpm $THISRPM
done

for i in $DAEMONLIST
do
    plugin_command "systemctl status $i"
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 "find /srv/tomcat/webapps/rhn/WEB-INF/lib/ | xargs file | grep broken"
plugin_command "find /usr/share/spacewalk/taskomatic/ | xargs file | grep broken"
plugin_command "find /usr/share/rhn/search/lib/ | xargs file | grep broken"

section_header "SSL Configuration"

pconf_files $(spacewalk-cfg-get documentroot)/pub/RHN-ORG-TRUSTED-SSL-CERT \
            /etc/apache2/ssl.crt/server.crt
plugin_command "/bin/ls -l --time-style=long-iso /etc/ssl/certs/"

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


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

plugin_command "echo \"select username scc_org from susecredentials sc join susecredentialstype sct on sct.id = sc.type_id where sct.label = 'scc';\" | spacewalk-sql --select-mode-direct -"

plugin_command "echo \"Number of elements in table susesaltevent. If the next number is too high, please verify the large scale tuning guide property 'java.message_queue_thread_pool_size'.\""
plugin_command "echo \"select count(*) from susesaltevent;\" | spacewalk-sql --select-mode-direct -"

plugin_command "/sbin/supportconfig-sumalog $LOG"
plugin_command "cp /var/log/zypper.log $LOG"
plugin_command "grep \"Client bootstrap script\" /srv/www/htdocs/pub/bootstrap/*.sh"

