22736c
#!/bin/sh
d91308
tmp=$(mktemp -p /tmp -d memberaliases.XXXXXXXXXX)
22736c
cd "$tmp" || exit 1
22736c
d91308
nsca_client_name='{{grains.id}}'
22736c
nsca_client_service='openSUSE virtual_users'
22736c
nsca_server_ip='192.168.47.7'
22736c
nsca_config='/etc/send_nsca.cfg'
22736c
#status_code=3
22736c
#status_message='unknown'
22736c
22736c
send_nsca_message(){
22736c
	local status_code="$1"
22736c
	local status_message="$2"
22736c
	echo -e "$nsca_client_name\t$nsca_client_service\t$status_code\t$status_message" | send_nsca -H $nsca_server_ip -c "$nsca_config"
22736c
}
22736c
d91308
mysql -h proxy.infra.opensuse.org -P3307 -Dmembers -NB -e 'select addr, email_target from email_aliases' >member.aliases.new
22736c
22736c
# need to make sure it is ordered
22736c
LC_ALL=C sort -d -k1 member.aliases.new >member.aliases.ordered
22736c
d91308
added=$(diff -B -b /etc/postfix/virtual-opensuse-users member.aliases.ordered | grep ^\> | wc -l)
d91308
removed=$(diff -B -b /etc/postfix/virtual-opensuse-users member.aliases.ordered | grep ^\< | wc -l)
22736c
total=$(wc -l 
22736c
22736c
# if too many would be removed, something's probably wrong.
22736c
if test $removed -gt 20
22736c
then
22736c
        printf "New member-aliases list (%u entries) would remove %u aliases ?\n" $total $removed
22736c
	send_nsca_message "1" "New member-aliases list ($total entries)  would remove $removed aliases | total=$total; added=$added; removed=$removed;"
22736c
	# tidy up
22736c
	rm -Rf "$tmp"
22736c
        exit 1
22736c
fi
22736c
22736c
# if we had any changes, update our alias list
22736c
if test $added -gt 0 -o $removed -gt 0
22736c
then
22736c
        printf "virtual-opensuse-users updated: %u additions, %u removals, now %u entries\n" $added $removed $total
22736c
        send_nsca_message "0" "virtual-opensuse-users updated: $added additions, $removed removals, now $total entries | total=$total; added=$added; removed=$removed;"
22736c
	# debug
d91308
	diff -B -b /etc/postfix/virtual-opensuse-users member.aliases.ordered
22736c
22736c
	mv member.aliases.ordered /etc/postfix/virtual-opensuse-users
22736c
        postmap /etc/postfix/virtual-opensuse-users
22736c
        postfix reload
22736c
else
22736c
#        printf "virtual-opensuse-users not updated, no changes found.\n"
22736c
        send_nsca_message "0" "virtual-opensuse-users not updated, no changes found | total=$total; added=$added; removed=$removed;" >/dev/null
22736c
fi
22736c
22736c
# tidy up
22736c
rm -Rf "$tmp"