#!/usr/bin/env bash

# Monitoring/management script for ZFS pools with e-mail notification
# 
# Changelog
# 2013-07-18 23:21	Added online and offline commands for ease of use.
# 2014-01-12 04:04	Adapted script to new pool names and disks; rewrote online and offline case constructs.
#            04:17	Simplified online and offline case constructs.
# 2014-01-26 00:29	Further automation on the pool bash array. Added language check.
#			Put variables in separate config file in /etc/default/
# 2014-01-27 16:46	Used associative arrays for the mirror disks. Requires Bash 4.0 or higher.
# 2014-02-22 23:58	Fixed base error in week function.

# Todo
# - add logging functions (to syslog?) for more verbosity



#############
# Variables #
#############

# Sourced from default configuration file. See the file for possible settings.
if [ -f /etc/default/zpm.conf ]
then
	. /etc/default/zpm.conf
else
	echo "Please provide a configuration."
	exit 1
fi

# Language setting falls back to English
language=${language:-en}



#############
# Functions #
#############

# Make sure it only runs on even weeks; set skipweek to 0 to skip even numbered weeks,
# to 1 to skip odd numbered weeks.
skipweek=1
week=$(date +%V)

check_week() {
    [ $((10#$week % 2)) -eq $skipweek ] && exit 0
    }

# Language check. Mostly for script readability, not really needed.
if_nl() {
    [ "$language" = "nl" ]
    }

if_en() {
    [ "$language" = "en" ]
    }



########
# Code #
########

case $1 in
	scrub)
		check_week
		for zfspool in "${zfspools[@]}"
		do
			/sbin/zpool scrub "$zfspool"
		done
		;;

	report)
		check_week
		for zfspool in "${zfspools[@]}"
		do
			if_en && echo -e "* Report created at $(date '+%Y-%m-%d %H:%M %Z') * \n" > /tmp/"$zfspool"
			if_nl && echo -e "* Rapport aangemaakt om $(date '+%Y-%m-%d %H:%M %Z') * \n" > /tmp/"$zfspool"
		
			if
				# -x assures only 'zpool status' will only display pools with actual errors.
				# This also includes degraded pools without any data errors.
				/sbin/zpool status -x "$zfspool" | grep -m 2 errors >> /tmp/"$zfspool"
			then
				if_en && {
				  echo -e "Errors have been found in \"$zfspool\". Below you will see the errors reported by the system. \n \n" && \
				  cat /tmp/"$zfspool" ; 
				  } | /usr/bin/mailx -s "Status update for ZFS pool \"$zfspool\"" "$email"
				
				if_nl && {
				  echo -e "Er werden fouten vastgesteld op \"$zfspool\". Hieronder de meldingen die het systeem doorgaf. \n \n" && \
				  cat /tmp/"$zfspool" ; 
				  } | /usr/bin/mailx -s "Statusupdate voor ZFS-pool \"$zfspool\"" "$email"
			fi
				
			# Clean up
			rm /tmp/"$zfspool"
		done
		;;

	online)
                # Check whether the specified pool is part of the array.
		if
			echo "${zfspools[@]}" | grep "$2" > /dev/null 
		then
			if [ -e /dev/disk/by-id/"${zfsmirrors[$2]}" ]
			then
				zpool online "$2" "${zsfmirrors[$2]}"
			else
				if_en && echo "Second disk of pool "$2" is not present. Please check whether you connected the right disk."
				if_nl && echo "Tweede schijf van pool "$2" niet aanwezig. Controleer of u de juiste schijf hebt aangesloten."
			fi
		else
			if_en && echo "Pool "$2" does not exist. Operation aborted."
			if_nl && echo "Pool "$2" bestaat niet. Handeling afgebroken."
		fi
		;;

	offline)
		if
			echo "${zfspools[@]}" | grep "$2" > /dev/null
		then
			if [ -e /dev/disk/by-id/"${zfsmirrors[$2]}" ]
			then
				zpool offline "$2" "${zfsmirrors[$2]}"
			else
				if_en && echo "Second disk of pool "$2" is not present. Please check whether you connected the right disk."
                                if_nl && echo "Tweede schijf van pool "$2" niet aanwezig. Controleer of u de juiste schijf hebt aangesloten."
			fi
		else
			if_en && echo "Pool "$2" does not exist. Operation aborted."
			if_nl && echo "Pool "$2" bestaat niet. Handeling afgebroken."
		fi
		;;
	*)
		echo "\
Possible arguments:
 * scrub		Start a ZFS scrub on all pools defined in the array
 * report		Check pool status and send an e-mail report to a predefined address
 * online \$pool 	Bring the mirror disk of said pool online
 * offline \$pool	Take the mirror disk of said pool offline
Configuration file is /etc/default/zpm.conf."
		;;
esac
