ScoDoc-Lille/tools/diagnostic.sh

251 lines
6.1 KiB
Bash
Raw 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
#
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)
DEPTS_TO_SAVE=""
SAVE_USERS=0
SEND_BY_MAIL=1
SCODOC_DIR="/opt/scodoc"
SCODOC_VAR_DIR="/opt/scodoc-data"
2020-09-26 16:19:37 +02:00
# -------------------------------------
# Arguments
# -------------------------------------
function join_by { local IFS="$1"; shift; echo "$*"; }
while getopts ":d:aunh" opt; do
case $opt in
a)
# Liste des noms des departements, a partir des bases SQL SCO*
DEPTS_TO_SAVE=$( (su postgres -c "psql -l") | grep SCO | grep -v SCOUSERS | awk '{ gsub("SCO", "", $1); print $1}' )
SAVE_USERS=1
;;
u)
SAVE_USERS=1
;;
n)
SEND_BY_MAIL=0
;;
d)
2020-12-19 19:22:22 +01:00
DEPTS_TO_SAVE=$( join_by ' ' "$DEPTS_TO_SAVE" "$OPTARG" )
2020-09-26 16:19:37 +02:00
;;
h)
echo "Diagnostic installation ScoDoc"
echo "Rassemble informations sur le systeme et l'installation ScoDoc"
echo "Usage: $0 [-h] [-n] [-a] [-u] [-d dept]"
echo " -h cette aide"
echo " -n pas d'envoi par mail"
echo " -a enregistre aussi toutes les bases de donnees"
echo " -u enregistre la base utilisateurs"
echo " -d dept enregistre la base du departement dept"
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/apache2 $SCODOC_DIR/VERSION"
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
(cd "$SCODOC_HOME"; git status > "$TMP"/git.status)
(cd "$SCODOC_HOME"; git diff > "$TMP"/git.diff)
2020-09-26 16:19:37 +02:00
(cd "$SCODOC_HOME"; git log -n 5 > "$TMP"/git.log)
ls -laR "$SCODOC_HOME" > "$TMP"/ls-laR
2020-09-26 16:19:37 +02:00
# -------------------------------------
# Databases configurations
# -------------------------------------
(su postgres -c "psql -l") > "${TMP}/psql-l.out"
for dept in "${SCODOC_VAR_DIR}"/config/depts/*.cfg
2020-09-26 16:19:37 +02:00
do
2020-12-19 19:22:22 +01:00
cnx=$(cat "$dept")
2020-09-26 16:19:37 +02:00
(su postgres -c "echo '\dt' | psql -d $cnx") > "${TMP}/psql-$(basename ${dept%%.*}).out"
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
# -------------------------------------
# Optionally save dept(s) database(s)
# -------------------------------------
2020-12-19 19:22:22 +01:00
DEPTS_TO_SAVE=$(echo "${DEPTS_TO_SAVE}" | tr ' ' '\n' | sort | uniq)
2020-09-26 16:19:37 +02:00
# Dump database of a dept (eg "RT")
function dump_dept_db {
dept=$1
DB=$2
echo "Dumping database ${DB}..."
mkdir -p "${TMP}/depts/${dept}"
chmod -R a+wr "${TMP}/depts/"
(su postgres -c "pg_dump --create ${DB}") | gzip > "${TMP}/depts/${dept}/${DB}.dump.gz"
# may add archives ? (but probably too big)
}
for dept in ${DEPTS_TO_SAVE}
do
dump_dept_db "${dept}" "SCO${dept}"
done
# -------------------------------------
# Optionally saveUSERS db
# -------------------------------------
if [ "${SAVE_USERS}" = "1" ]
then
dump_dept_db "USERS" "SCOUSERS"
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";
SUBJECT="ScoDoc diagnostic - $YYYYMMDD";
MSGBODY="ScoDoc diagnostic sent by diagnostic.sh";
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