From a280185a04c2c10fa7327266974680c67baf0e9c Mon Sep 17 00:00:00 2001 From: Jeremy Lecour Date: Tue, 25 Sep 2018 09:48:29 +0200 Subject: [PATCH] =?UTF-8?q?Extraction=20de=20methode=20pour=20la=20r=C3=A9?= =?UTF-8?q?cup=C3=A9ration=20d'infos?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Selon qu'il y a un TTY ou pas, les infos sont plus ou moins fiables (voire absentes). --- evomaintenance.sh | 77 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 66 insertions(+), 11 deletions(-) diff --git a/evomaintenance.sh b/evomaintenance.sh index 04afac6..c35565e 100644 --- a/evomaintenance.sh +++ b/evomaintenance.sh @@ -7,9 +7,65 @@ # version 0.3 # Copyright 2007-2018 Gregory Colpart , Jérémy Lecour , Evolix +get_system() { + uname -s +} + +get_fqdn() { + if [ "$(get_system)" = "Linux" ]; then + hostname --fqdn + elif [ "$(get_system)" = "OpenBSD" ]; then + hostname + else + echo "OS not detected!" + exit 1 + fi +} + +get_tty() { + if [ "$(get_system)" = "Linux" ]; then + ps -o tty= | tail -1 + elif [ "$(get_system)" = "OpenBSD" ]; then + env | grep SSH_TTY | cut -d"/" -f3 + else + echo "OS not detected!" + exit 1 + fi +} + +get_who() { + who=$(LC_ALL=C who -m) + + if [ -n "${who}" ]; then + echo "${who}" + else + LC_ALL=C who | grep $(get_tty) | tr -s ' ' + fi +} + +get_begin_date() { + echo "$(date "+%Y") $(echo $(get_who) | cut -d" " -f3,4,5)" +} + +get_ip() { + ip=$(echo $(get_who) | cut -d" " -f6 | sed -e "s/^(// ; s/)$//") + [ -z "${ip}" ] && ip="unknown (no tty)" + [ "${ip}" = ":0" ] && ip="localhost" + + echo "${ip}" +} + +get_end_date() { + date +"%Y %b %d %H:%M" +} + +get_now() { + date +"%Y-%m-%dT%H:%M:%S%z" +} + test -f /etc/evomaintenance.cf && . /etc/evomaintenance.cf -[ -n "${HOSTNAME}" ] || HOSTNAME=$(hostname --fqdn) +[ -n "${HOSTNAME}" ] || HOSTNAME=$(get_fqdn) [ -n "${EVOMAINTMAIL}" ] || EVOMAINTMAIL=evomaintenance-$(echo "${HOSTNAME}" | cut -d- -f1)@${REALM} [ -n "${LOGFILE}" ] || LOGFILE=/var/log/evomaintenance.log @@ -17,13 +73,20 @@ test -f /etc/evomaintenance.cf && . /etc/evomaintenance.cf # Only after this line, because some config variables might be missing. set -u -REAL_HOSTNAME=$(hostname --fqdn) +REAL_HOSTNAME=$(get_fqdn) if [ "${HOSTNAME}" = "${REAL_HOSTNAME}" ]; then HOSTNAME_TEXT="${HOSTNAME}" else HOSTNAME_TEXT="${HOSTNAME} (${REAL_HOSTNAME})" fi +# TTY=$(get_tty) +# WHO=$(get_who) +IP=$(get_ip) +BEGIN_DATE=$(get_begin_date) +END_DATE=$(get_end_date) +USER=$(logname) + PATH=${PATH}:/usr/sbin SENDMAIL_BIN=$(command -v sendmail) @@ -31,14 +94,6 @@ GIT_BIN=$(command -v git) GIT_REPOSITORIES="/etc /etc/bind" -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") - # git statuses GIT_STATUSES="" @@ -93,7 +148,7 @@ echo "> Press to submit, or to cancel." read enter # write log -echo "----------- ${NOW_ISO} ---------------" >> "${LOGFILE}" +echo "----------- $(get_now) ---------------" >> "${LOGFILE}" echo "${BLOB}" >> "${LOGFILE}" # git commit