V
V
VS2017-09-18 22:11:47
linux
VS, 2017-09-18 22:11:47

How to set up postfix monitoring on zabbix?

Salute to all.
There was a task to monitor operability of a mail rayleigh.
On this issue, I came across this article.
https://serveradmin.ru/monitoring-postfix-v-zabbix/
There is a script

spoiler
#!/bin/bash
export LC_ALL=""
export LANG="en_US.UTF-8"
#
if ; then
exit 1
fi
##### PARAMETERS #####
MAILQ=$(which mailq)
PFLOGSUMM=$(which pflogsumm)
MAILLOG="/var/log/maillog"
METRIC="$1"
CACHE_TTL="1740"
CACHE_FILE="/tmp/zabbix.postfix.cache"
EXEC_TIMEOUT="4"
NOW_TIME=`date '+%s'`
##### RUN #####
if [ "$METRIC" = "queue" ]; then
TEMP_DATA=`${MAILQ} 2>&1 | tail -n1`
if echo "${TEMP_DATA}" | grep -iq "Mail queue is empty"; then
echo 0
elif echo "${TEMP_DATA}" | grep -iPq "in\s+\d+\s+request"; then
echo "${TEMP_DATA}" | sed -e 's/.*in\s\+\([0-9]\+\)\s\+request.*/\1/gI' 2> /dev/null | head -n1
else
# Error
echo 65535
fi
exit 0
else
if [ -s "${CACHE_FILE}" ]; then
CACHE_TIME=`stat -c"%Y" "${CACHE_FILE}"`
DELTA_TIME=$((${NOW_TIME} - ${CACHE_TIME}))
if [ ${DELTA_TIME} -lt ${EXEC_TIMEOUT} ]; then
sleep $((${EXEC_TIMEOUT} - ${DELTA_TIME}))
elif [ ${DELTA_TIME} -gt ${CACHE_TTL} ]; then
echo "" >> "${CACHE_FILE}" # !!!
DATE_TO=`date +%d\ %H:%M:%S`
DATE_FROM=`date -d @${CACHE_TIME} +%d\ %H:%M:%S`
DATA_CACHE=`sudo cat ${MAILLOG} | sed -e 's/^\([a-zA-Z]\{3\}\s\)\s\([0-9]\s\)/\10\2/g' | awk '$2" "$3>=from && $2" "$3<=to' from="${DATE_FROM}" to="${DATE_TO}" | ${PFLOGSUMM} -h 0 -u 0 --bounce_detail=0 --deferral_detail=0 --reject_detail=0 --smtpd_warning_detail=0 --no_no_msg_size 2>&1`
echo "${DATA_CACHE}" > ${CACHE_FILE} # !!!
chmod 640 ${CACHE_FILE}
fi
else
echo "" > ${CACHE_FILE} # !!!
exit 0
fi
awk "BEGIN{IGNORECASE=1} /${METRIC}/ {print \$1}" ${CACHE_FILE} | awk '{print $1}' | awk '/k|m/{p = /k/?1:2}{printf "%d\n", int($1) * 1024 ^ p}' | head -n1
fi
exit 0

I don't understand his work.
one)
if echo "${TEMP_DATA}" | grep -iq "Mail queue is empty"; then

It turns out that if we enter the word "queue" when executing the script and mailq gives out "Mail queue is empty", then it returns 0 - everything is OK, otherwise 65535
2)

elif echo "${TEMP_DATA}" | grep -iPq "in\s+\d+\s+request"; then
echo "${TEMP_DATA}" | sed -e 's/.*in\s\+\([0-9]\+\)\s\+request.*/\1/gI' 2> /dev/null | head-n1

here it is not clear.
3) Next big block

if [ -s "${CACHE_FILE}" ]; then
CACHE_TIME=`stat -c"%Y" "${CACHE_FILE}"`
DELTA_TIME=$((${NOW_TIME} - ${CACHE_TIME}))
if [ ${DELTA_TIME} -lt ${EXEC_TIMEOUT} ]; then
sleep $((${EXEC_TIMEOUT} - ${DELTA_TIME}))
elif [ ${DELTA_TIME} -gt ${CACHE_TTL} ]; then
echo "" >> "${CACHE_FILE}" # !!!
DATE_TO=`date +%d\ %H:%M:%S`
DATE_FROM=`date -d @${CACHE_TIME} +%d\ %H:%M:%S`
DATA_CACHE=`sudo cat ${MAILLOG} | sed -e 's/^\([a-zA-Z]\{3\}\s\)\s\([0-9]\s\)/\10\2/g' | awk '$2" "$3>=from && $2" "$3<=to' from="${DATE_FROM}" to="${DATE_TO}" | ${PFLOGSUMM} -h 0 -u 0 --bounce_detail=0 --deferral_detail=0 --reject_detail=0 --smtpd_warning_detail=0 --no_no_msg_size 2>&1`
echo "${DATA_CACHE}" > ${CACHE_FILE} # !!!
chmod 640 ${CACHE_FILE}
fi

Here we take the current date and date of the cache file and calculate how much time has passed since it was created. If the requirements are suitable, then we parse the postfix statistics and write them to the cache file.
4) It is also not clear what is happening here

awk "BEGIN{IGNORECASE=1} /${METRIC}/ {print \$1}" ${CACHE_FILE} | awk '{print $1}' | awk '/k|m/{p = /k/?1:2}{printf "%d\n", int($1) * 1024 ^ p}' | head-n1

So, in general, I didn’t understand from this that we check this script except for the queue in posfix? In fact, you need to check the sending and receiving of mail so that there are no queues and gaps in incoming messages. Does it fulfill this function? Help me to understand.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Saboteur, 2017-09-19
@saboteur_kiev

if evaluates to 0 (true) and not zero (false), so the bottom line is that if
if echo "${TEMP_DATA}" | grep -iq "Mail queue is empty" ; then
returns 0 (that is, grep found the phrase "Mail queue is empty" at least once), then the condition is met.
Otherwise, it tries another condition:
elif echo "${TEMP_DATA}" | grep -iPq "in\s+\d+\s+request"; then
In this condition, we are looking for the phrase "in numbers request", if we find it, we execute it.
echo "${TEMP_DATA}" | sed -e 's/.*in\s\+\([0-9]\+\)\s\+request.*/\1/gI' 2> /dev/null | head -n1
Thus, three conditions
If the queue is empty - print 0, if not empty - print the number of request-s, otherwise we print an error.
Then the question is how does zabbix process it - it just takes these numbers and builds a graph, and reacts to an error, or something else.
In general, it is better, of course, to learn the basics of bash and regular expressions. Such scripts are hints for those who are already writing. They are not end-user solutions that you can just take and put on your server without understanding what they do. That is, there is always a risk that they do not do everything you want.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question