ScoDoc/tools/diagnostic.sh

222 lines
5.3 KiB
Bash
Raw Permalink Normal View History

2020-09-26 16:19:37 +02:00
#!/bin/bash
# Rassemble informations sur le systeme et l'installation ScoDoc pour
# faciliter le support a distance.
#
# Avec option:
# -a : sauve aussi les bases de données
#
2021-08-15 16:43:53 +02:00
set -euo pipefail
# Le répertoire de ce script:
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
source "$SCRIPT_DIR"/config.sh || die "config.sh not found, exiting"
2020-12-19 19:22:22 +01:00
DEST_ADDRESS=emmanuel.viennet@gmail.com
2020-09-26 16:19:37 +02:00
TMP=/tmp/scodoc-$(date +%F-%s)
2021-08-15 16:43:53 +02:00
SAVE_DB=0
2020-09-26 16:19:37 +02:00
SEND_BY_MAIL=1
# -------------------------------------
# Arguments
# -------------------------------------
function join_by { local IFS="$1"; shift; echo "$*"; }
2021-08-15 16:43:53 +02:00
while getopts "anh" opt; do
case "$opt" in
2020-09-26 16:19:37 +02:00
a)
2021-08-15 16:43:53 +02:00
SAVE_DB=1
2020-09-26 16:19:37 +02:00
;;
n)
SEND_BY_MAIL=0
;;
h)
echo "Diagnostic installation ScoDoc"
echo "Rassemble informations sur le systeme et l'installation ScoDoc"
echo "Usage: $0 [-h] [-n] [-a] [-u]"
2020-09-26 16:19:37 +02:00
echo " -h cette aide"
echo " -n pas d'envoi par mail"
2021-08-15 16:43:53 +02:00
echo " -a enregistre la bases de donnees (prod)"
2020-09-26 16:19:37 +02:00
exit 0
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
# -------------------------------------
# Configuration
# -------------------------------------
# needed for uuencode
if [ ! -e /usr/bin/uuencode ]
then
apt-get install sharutils
fi
2020-12-19 19:22:22 +01:00
mkdir "$TMP"
2020-09-26 16:19:37 +02:00
# Files to copy:
FILES="/etc/hosts /etc/debian_version /etc/apt /etc/nginx /etc/postfix $SCODOC_DIR/sco_version.py $SCODOC_VAR_DIR/config"
2020-09-26 16:19:37 +02:00
echo "ScoDoc diagnostic: informations about your system will be "
if [ "${SEND_BY_MAIL}" = "1" ]
then
echo "sent to ${DEST_ADDRESS}"
echo -n "and "
fi
echo "left in ${TMP}"
# -------------------------------------
# Logs
# -------------------------------------
2020-12-19 19:22:22 +01:00
mkdir "$TMP"/scodoc_logs/
cp "$SCODOC_VAR_DIR"/log/*.log "$TMP"/scodoc_logs/
2020-09-26 16:19:37 +02:00
# -------------------------------------
# Linux System Configuration
# -------------------------------------
2020-12-19 19:22:22 +01:00
iptables -L > "$TMP"/iptables.out
ip a > "$TMP"/ip-a.out
2020-12-19 19:22:22 +01:00
ps auxww > "$TMP"/ps.out
df -h > "$TMP"/df.out
dpkg -l > "$TMP"/dpkg.lst
2020-09-26 16:19:37 +02:00
2021-08-02 10:34:28 +02:00
ls -laR "$SCODOC_DIR" > "$TMP"/ls-laR
2020-09-26 16:19:37 +02:00
# -------------------------------------
# Databases configurations
# -------------------------------------
(su postgres -c "psql -l") > "${TMP}/psql-l.out"
2021-08-15 16:43:53 +02:00
for db in "$SCODOC_DB_PROD" "$SCODOC_DB_DEV"
2020-09-26 16:19:37 +02:00
do
(su postgres -c "echo '\dt' | psql $db") > "${TMP}/psql-$db.out"
2020-09-26 16:19:37 +02:00
done
# -------------------------------------
# Other system configuration files
# -------------------------------------
# copy files:
for f in $FILES
do
if [ -e "$f" ]
then
cp -R "$f" "$TMP"
fi
2020-09-26 16:19:37 +02:00
done
# -------------------------------------
2021-08-15 16:43:53 +02:00
# Optionally save database
2020-09-26 16:19:37 +02:00
# -------------------------------------
2021-08-15 16:43:53 +02:00
if [ "${SAVE_DB}" = "1" ]
2020-09-26 16:19:37 +02:00
then
2021-08-15 16:43:53 +02:00
for db in "$SCODOC_DB_PROD" "$SCODOC_DB_DEV"
do
echo "Dumping database ${db}..."
su -c "pg_dump --create ${db}" postgres | gzip > "${TMP}/${db}.dump.gz"
2021-08-15 16:43:53 +02:00
# may add archives ? (no, probably too big)
done
2020-09-26 16:19:37 +02:00
fi
# -------------------------------------
# Archive all stuff to /tmp
# -------------------------------------
2020-12-19 19:22:22 +01:00
tar cfz "$TMP".tgz "$TMP"
2020-09-26 16:19:37 +02:00
echo
2020-12-19 19:22:22 +01:00
echo "Fichier de diagnostic: "$TMP".tgz"
2020-09-26 16:19:37 +02:00
echo
# If no mail, stop here
if [ "${SEND_BY_MAIL}" = "0" ]
then
exit 0
fi
# -------------------------------------
# Send by e-mail
# -------------------------------------
# Code below found on http://www.zedwood.com/article/103/bash-send-mail-with-an-attachment
#requires: basename,date,md5sum,sed,sendmail,uuencode
function fappend {
2020-12-19 19:22:22 +01:00
echo "$2">>"$1";
2020-09-26 16:19:37 +02:00
}
2020-12-19 19:22:22 +01:00
YYYYMMDD=$(date +%Y%m%d)
2020-09-26 16:19:37 +02:00
# CHANGE THESE
TOEMAIL=$DEST_ADDRESS
FREMAIL="scodoc-diagnostic@none.org";
2021-08-15 16:43:53 +02:00
SUBJECT="ScoDoc 9 diagnostic - $YYYYMMDD";
MSGBODY="ScoDoc 9 diagnostic sent by diagnostic.sh";
2020-09-26 16:19:37 +02:00
ATTACHMENT="$TMP.tgz"
MIMETYPE="application/gnutar" #if not sure, use http://www.webmaster-toolkit.com/mime-types.shtml
# DON'T CHANGE ANYTHING BELOW
TMP="/tmp/tmpfil_123"$RANDOM;
2020-12-19 19:22:22 +01:00
BOUNDARY=$(date +%s|md5sum)
2020-09-26 16:19:37 +02:00
BOUNDARY=${BOUNDARY:0:32}
2020-12-19 19:22:22 +01:00
FILENAME=$(basename "$ATTACHMENT")
rm -rf "$TMP"
uuencode --base64 "$FILENAME" < "$ATTACHMENT" >"$TMP"
sed -i -e '1,1d' -e '$d' "$TMP"; #removes first & last lines from "$TMP"
DATA=$(cat "$TMP")
rm -rf "$TMP";
fappend "$TMP" "From: $FREMAIL";
fappend "$TMP" "To: $TOEMAIL";
fappend "$TMP" "Reply-To: $FREMAIL";
fappend "$TMP" "Subject: $SUBJECT";
fappend "$TMP" "Content-Type: multipart/mixed; boundary=\""$BOUNDARY"\"";
fappend "$TMP" "";
fappend "$TMP" "This is a MIME formatted message. If you see this text it means that your";
fappend "$TMP" "email software does not support MIME formatted messages.";
fappend "$TMP" "";
fappend "$TMP" "--$BOUNDARY";
fappend "$TMP" "Content-Type: text/plain; charset=ISO-8859-1; format=flowed";
fappend "$TMP" "Content-Transfer-Encoding: 7bit";
fappend "$TMP" "Content-Disposition: inline";
fappend "$TMP" "";
fappend "$TMP" "$MSGBODY";
fappend "$TMP" "";
fappend "$TMP" "";
fappend "$TMP" "--$BOUNDARY";
fappend "$TMP" "Content-Type: $MIMETYPE; name=\"$FILENAME\"";
fappend "$TMP" "Content-Transfer-Encoding: base64";
fappend "$TMP" "Content-Disposition: attachment; filename=\"$FILENAME\";";
fappend "$TMP" "";
fappend "$TMP" "$DATA";
fappend "$TMP" "";
fappend "$TMP" "";
fappend "$TMP" "--$BOUNDARY--";
fappend "$TMP" "";
fappend "$TMP" "";
#cat "$TMP">out.txt
cat "$TMP"|sendmail -t -f none@example.com;
rm "$TMP";
2020-09-26 16:19:37 +02:00