!/bin/bash

SENDWITH0=0
URL=$(echo "http://$(hostname --fqdn)/zabbix/")
MAILTO="root@$(hostname -d)"
MAILFROM=$(echo "zabbix@$(hostname -d)")

SQLHOST=$(sed -e '/^DBHost/!d; s/.*=\(.*\)/\1/' /etc/zabbix/zabbix_server.conf)
SQLUSER=$(sed -e '/^DBUser/!d; s/.*=\(.*\)/\1/' /etc/zabbix/zabbix_server.conf)
SQLPASS=$(sed -e '/^DBPassword/!d; s/.*=\(.*\)/\1/' /etc/zabbix/zabbix_server.conf)

SQL='SELECT
        hosts.host AS hostname,
        items.description AS item,
        items.key_ AS "key"
FROM
        items
RIGHT JOIN
        hosts ON items.hostid=hosts.hostid
WHERE
        items.status = 3;'

echo ${SQL}|mysql -h ${SQLHOST} -u ${SQLUSER} -p${SQLPASS} zabbix -t > /dev/shm/$(basename $0)

body="This is an automatically generated mail message sent by a script running on $(hostname --fqdn)."
subject="Zabbix / Unsupported Items"
num=0

if [ -s /dev/shm/$(basename $0) ]; then

num=$(( $(cat /dev/shm/$(basename $0)|wc -l) -4))
body="${body}

The following items are currently unsupported:

$(cat /dev/shm/$(basename $0))"

else

body="${body}

All quiet on the western front!"

fi

body="${body}

Log in to Zabbix at ${URL}

Regards,
$(hostname -s) (Zabbix Server)"

subject="${subject} / $num"

if [ ${num} -eq 0 ] && [ ${SENDWITH0} -eq 0 ]; then
        exit 0;
else
        echo -e "From: ${MAILFROM}\nTo: ${MAILTO}\nSubject: ${subject}\n\n${body}"|mail -t
fi
