2015-09-13 15:29:22 +02:00
|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
# EvoMaintenance script
|
2016-12-08 16:51:55 +01:00
|
|
|
# Dependencies (all OS): git postgresql-client
|
|
|
|
# Dependencies (Debian): sudo
|
2015-09-13 15:29:22 +02:00
|
|
|
|
2018-09-05 00:04:22 +02:00
|
|
|
# version 0.3
|
|
|
|
# Copyright 2007-2018 Gregory Colpart <reg@evolix.fr>, Jérémy Lecour <jlecour@evolix.fr>, Evolix <info@evolix.fr>
|
2016-12-08 16:32:35 +01:00
|
|
|
|
2015-09-13 15:29:22 +02:00
|
|
|
test -f /etc/evomaintenance.cf && . /etc/evomaintenance.cf
|
|
|
|
|
2018-09-05 18:44:16 +02:00
|
|
|
[ -n "${HOSTNAME}" ] || HOSTNAME=$(hostname)
|
2018-09-04 23:08:42 +02:00
|
|
|
[ -n "${EVOMAINTMAIL}" ] || EVOMAINTMAIL=evomaintenance-$(echo "${HOSTNAME}" | cut -d- -f1)@${REALM}
|
2018-09-05 18:44:16 +02:00
|
|
|
[ -n "${LOGFILE}" ] || LOGFILE=/var/log/evomaintenance.log
|
2015-09-13 15:29:22 +02:00
|
|
|
|
2018-09-05 18:44:53 +02:00
|
|
|
PATH=${PATH}:/usr/sbin
|
2015-09-13 15:29:22 +02:00
|
|
|
|
2018-09-05 18:44:53 +02:00
|
|
|
WHO=$(LC_ALL=C who -m)
|
|
|
|
USER=$(echo ${WHO} | cut -d" " -f1)
|
2018-09-05 00:03:16 +02:00
|
|
|
IP=$(echo ${WHO} | cut -d" " -f6 | sed -e "s/^(// ; s/)$//")
|
2018-09-06 10:12:06 +02:00
|
|
|
BEGIN_DATE="$(date "+%Y") $(echo ${WHO} | cut -d" " -f3,4,5)"
|
|
|
|
END_DATE=$(date +"%Y %b %d %H:%M")
|
|
|
|
# we can't use "date --iso8601" because this options is not available everywhere
|
|
|
|
NOW_ISO=$(date +"%Y-%m-%dT%H:%M:%S%z")
|
2015-09-13 15:29:22 +02:00
|
|
|
|
|
|
|
# details
|
|
|
|
echo "Please, enter details about your maintenance"
|
|
|
|
read TEXTE
|
|
|
|
|
2018-09-04 23:08:42 +02:00
|
|
|
if [ "${TEXTE}" = "" ]; then
|
2015-09-13 15:29:22 +02:00
|
|
|
echo "no value..."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# recapitulatif
|
2018-09-05 18:45:46 +02:00
|
|
|
BLOB=$(cat <<END
|
2018-09-06 10:44:13 +02:00
|
|
|
Host : $HOSTNAME
|
|
|
|
User : $USER
|
|
|
|
IP : $IP
|
|
|
|
Begin : $BEGIN_DATE
|
|
|
|
End : $END_DATE
|
|
|
|
Message : $TEXTE
|
2018-09-05 18:45:46 +02:00
|
|
|
END
|
|
|
|
)
|
|
|
|
|
|
|
|
echo "${BLOB}"
|
2015-09-13 15:29:22 +02:00
|
|
|
echo "To cancel, [Ctrl+C] else press [Enter]"
|
|
|
|
read enter
|
|
|
|
|
2018-09-04 22:50:25 +02:00
|
|
|
# write log
|
2018-09-06 10:12:06 +02:00
|
|
|
echo "---------- ${NOW_ISO} ----------" >> "${LOGFILE}"
|
|
|
|
echo "${BLOB}" >> "${LOGFILE}"
|
2018-09-04 22:50:25 +02:00
|
|
|
|
|
|
|
# git commit
|
2018-09-04 23:10:27 +02:00
|
|
|
GIT_BIN=$(command -v git)
|
|
|
|
|
|
|
|
if test -x "${GIT_BIN}"; then
|
2018-09-06 10:46:50 +02:00
|
|
|
# loop on possible directories managed by GIT
|
|
|
|
for dir in /etc /etc/bind; do
|
|
|
|
# tell Git where to find the repository and the work tree (no need to `cd …` there)
|
|
|
|
export GIT_DIR="${dir}/.git" GIT_WORK_TREE="${dir}"
|
|
|
|
# If the repository and the work tree exist, try to commit changes
|
|
|
|
if test -d "${GIT_DIR}" && test -d "${GIT_WORK_TREE}"; then
|
|
|
|
${GIT_BIN} add --all
|
|
|
|
${GIT_BIN} commit --message "${TEXTE}" --author="${USER} <${USER}@evolix.net>" --quiet
|
2018-09-06 10:12:26 +02:00
|
|
|
# Add the SHA to the log file if something has been committed
|
|
|
|
if [ "$?" = "0" ]; then
|
2018-09-06 10:46:50 +02:00
|
|
|
SHA=$(git rev-parse HEAD)
|
|
|
|
echo "${dir} : ${SHA}" >> "${LOGFILE}"
|
2018-09-06 10:12:26 +02:00
|
|
|
fi
|
|
|
|
fi
|
2018-09-06 10:46:50 +02:00
|
|
|
# unset environment variables to prevent accidental influence on other git commands
|
|
|
|
unset GIT_DIR GIT_WORK_TREE
|
2018-09-06 10:12:26 +02:00
|
|
|
done
|
2015-12-02 16:20:42 +01:00
|
|
|
fi
|
|
|
|
|
2018-09-04 22:50:25 +02:00
|
|
|
# insert into PG
|
2018-09-04 23:08:42 +02:00
|
|
|
SQL_TEXTE=`echo "${TEXTE}" | sed "s/'/\\\\\\'/g ; s@/@\\\\\/@g ; s@\\&@et@g"`
|
2018-09-04 22:50:25 +02:00
|
|
|
|
2015-09-13 15:29:22 +02:00
|
|
|
echo "
|
|
|
|
INSERT INTO evomaint(hostname,userid,ipaddress,begin_date,end_date,details)
|
2018-09-06 10:12:06 +02:00
|
|
|
VALUES ('${HOSTNAME}','${USER}','${IP}','${BEGIN_DATE}',now(),'${SQL_TEXTE}') " | \
|
2018-09-04 23:08:42 +02:00
|
|
|
psql ${PGDB} ${PGTABLE} -h ${PGHOST}
|
2015-09-13 15:29:22 +02:00
|
|
|
|
2018-09-04 22:50:25 +02:00
|
|
|
# send mail
|
2018-09-04 23:08:42 +02:00
|
|
|
SENDMAIL=$(command -v sendmail)
|
|
|
|
|
2015-09-13 15:29:22 +02:00
|
|
|
cat /usr/share/scripts/evomaintenance.tpl | \
|
2018-09-06 10:12:06 +02:00
|
|
|
sed -e "s/__TO__/${EVOMAINTMAIL}/ ; s/__HOSTNAME__/${HOSTNAME}/ ; s/__USER__/${USER}/ ; s/__BEGIN_DATE__/${BEGIN_DATE}/ ; s/__TEXTE__/${TEXTE}/ ; s/__IP__/${IP}/ ; s/__FULLFROM__/${FULLFROM}/ ; s/__FROM__/${FROM}/ ; s/__URGENCYFROM__/${URGENCYFROM}/ ; s/__URGENCYTEL__/${URGENCYTEL}/" | \
|
2018-09-04 23:08:42 +02:00
|
|
|
${SENDMAIL} -oi -t -f ${FROM}
|