91 lines
2.8 KiB
Bash
91 lines
2.8 KiB
Bash
#!/bin/sh
|
|
|
|
# EvoMaintenance script
|
|
# Dependencies (all OS): git postgresql-client
|
|
# Dependencies (Debian): sudo
|
|
|
|
# version 0.3
|
|
# Copyright 2007-2018 Gregory Colpart <reg@evolix.fr>, Jérémy Lecour <jlecour@evolix.fr>, Evolix <info@evolix.fr>
|
|
|
|
test -f /etc/evomaintenance.cf && . /etc/evomaintenance.cf
|
|
|
|
[ -n "${HOSTNAME}" ] || HOSTNAME=$(hostname)
|
|
[ -n "${EVOMAINTMAIL}" ] || EVOMAINTMAIL=evomaintenance-$(echo "${HOSTNAME}" | cut -d- -f1)@${REALM}
|
|
[ -n "${LOGFILE}" ] || LOGFILE=/var/log/evomaintenance.log
|
|
|
|
PATH=${PATH}:/usr/sbin
|
|
|
|
WHO=$(LC_ALL=C who -m)
|
|
USER=$(echo ${WHO} | cut -d" " -f1)
|
|
IP=$(echo ${WHO} | cut -d" " -f6 | sed -e "s/^(// ; s/)$//")
|
|
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")
|
|
|
|
# details
|
|
echo "Please, enter details about your maintenance"
|
|
read TEXTE
|
|
|
|
if [ "${TEXTE}" = "" ]; then
|
|
echo "no value..."
|
|
exit 1
|
|
fi
|
|
|
|
# recapitulatif
|
|
BLOB=$(cat <<END
|
|
Host : $HOSTNAME
|
|
User : $USER
|
|
IP : $IP
|
|
Begin : $BEGIN_DATE
|
|
End : $END_DATE
|
|
Message : $TEXTE
|
|
END
|
|
)
|
|
|
|
echo "${BLOB}"
|
|
echo "To cancel, [Ctrl+C] else press [Enter]"
|
|
read enter
|
|
|
|
# write log
|
|
echo "---------- ${NOW_ISO} ----------" >> "${LOGFILE}"
|
|
echo "${BLOB}" >> "${LOGFILE}"
|
|
|
|
# git commit
|
|
GIT_BIN=$(command -v git)
|
|
|
|
if test -x "${GIT_BIN}"; then
|
|
# loop on possible GIT repositories
|
|
for dir in /etc/.git /etc/bind/.git; do
|
|
# tell Git where to find the repository (no need to `cd …` there)
|
|
export GIT_DIR="${dir}"
|
|
# If the repository exists, try to commit changes
|
|
if test -d "${GIT_DIR}"; then
|
|
${GIT_BIN} add .
|
|
${GIT_BIN} commit --all --message "${TEXTE}" --author="${USER} <${USER}@evolix.net>" --quiet
|
|
# Add the SHA to the log file if something has been committed
|
|
if [ "$?" = "0" ]; then
|
|
SHA=$(git rev-parse --short HEAD)
|
|
echo "${GIT_DIR} : ${SHA}" >> "${LOGFILE}"
|
|
fi
|
|
fi
|
|
|
|
unset GIT_DIR
|
|
done
|
|
fi
|
|
|
|
# insert into PG
|
|
SQL_TEXTE=`echo "${TEXTE}" | sed "s/'/\\\\\\'/g ; s@/@\\\\\/@g ; s@\\&@et@g"`
|
|
|
|
echo "
|
|
INSERT INTO evomaint(hostname,userid,ipaddress,begin_date,end_date,details)
|
|
VALUES ('${HOSTNAME}','${USER}','${IP}','${BEGIN_DATE}',now(),'${SQL_TEXTE}') " | \
|
|
psql ${PGDB} ${PGTABLE} -h ${PGHOST}
|
|
|
|
# send mail
|
|
SENDMAIL=$(command -v sendmail)
|
|
|
|
cat /usr/share/scripts/evomaintenance.tpl | \
|
|
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}/" | \
|
|
${SENDMAIL} -oi -t -f ${FROM}
|