From ffe420b0e4d6833ac74994b86536a77baa76d82b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20S=C3=89RIE?= Date: Tue, 13 Aug 2019 17:30:49 +0200 Subject: [PATCH] etc-git: add etc-git-status.sh script for #44 The main idea is to send "non commited /etc" message to all last sysadmins connected in the last 7 days. --- etc-git/files/etc-git-status.sh | 58 +++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 etc-git/files/etc-git-status.sh diff --git a/etc-git/files/etc-git-status.sh b/etc-git/files/etc-git-status.sh new file mode 100644 index 00000000..f1fce51c --- /dev/null +++ b/etc-git/files/etc-git-status.sh @@ -0,0 +1,58 @@ +#!/bin/bash +set -e +export TERM=screen +export LC_ALL=C + +hostname=$(grep HOSTNAME /etc/evomaintenance.cf | cut -d'=' -f2) +hostname=${hostname%%.evolix.net} +template=$(mktemp --tmpdir=/tmp etc-git-status.XXX) +body=$(mktemp --tmpdir=/tmp etc-git-status.XXX) +lastLogOutput=$(mktemp --tmpdir=/tmp etc-git-status.XXX) +gitOutput=$(mktemp --tmpdir=/tmp etc-git-status.XXX) +lastTime=7 +uidRange="2000-2099" + +# Remove temporary files on exit +trap "rm $lastLogOutput" EXIT + +# Get last admins connected +lastlog -t $lastTime -u $uidRange > $lastLogOutput + +# Add these admins to an array if there are +lastLogOutputCount=$(wc -l $lastLogOutput | awk '{ print $1 }') +if [ $lastLogOutputCount -gt 1 ]; then + while read line; do + user=$(awk '{ print $1 }' <<< $line) + if [ $user != "Username" ]; then + lastAdmins="$lastAdmins${user}@evolix.fr, " + fi + done < $lastLogOutput +else + lastAdmins="$lastAdmins" +fi + +# Send the mail +git --git-dir=/etc/.git --work-tree=/etc status --short > $gitOutput +if [ $gitOuput -n ]; then + cat << EOT > $template +Content-Type: text/plain; charset="utf-8" +Reply-To: Équipe Evolix +From: Équipe Evolix +To: $lastAdmins +Subject: Non commited /etc for server $hostname +EOT + cat << EOT > $body +Dear ${lastAdmins} + +As you were connected on $hostname in the last 7 days, please commit modifications on /etc. +You should use evomaintenance for that. + +git status: + +$(<$gitOutput) + +-- +etc-git-status.sh +EOT + mutt -x -e 'set send_charset="utf-8"' -H $template < $body +fi