Subversion Repositories sysadmin_scripts

Rev

Rev 14 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
14 rodolico 1
#!/usr/bin/env bash
2
 
3
# see
4
# http://lifeisabug.com/postfix-statistical-graphs-zabbix-using-passive-checks/
5
 
6
MAILLOG=/var/log/mail.log
7
PFOFFSETFILE=/tmp/zabbix-postfix-offset.dat
8
PFSTATSFILE=/tmp/postfix_statsfile.dat
9
TEMPFILE=$(mktemp)
10
PFLOGSUMM=/usr/sbin/pflogsumm
11
LOGTAIL=/usr/sbin/logtail
12
 
13
PFVALS=( 'received' 'delivered' 'forwarded' 'deferred' 'bounced' 'rejected' 'held' 'discarded' 'reject_warnings' 'bytes_received' 'bytes_delivered' )
14
 
15
[ ! -e "${PFSTATSFILE}" ] && touch "${PFSTATSFILE}" && chown zabbix:zabbix "${PFSTATSFILE}"
16
 
17
printvalues() {
18
  key=$1
19
  pfkey=$(echo "$1" | tr '_' ' ')
20
  value=$(grep -m 1 "${pfkey}" $TEMPFILE | awk '{print $1}' | awk '/k|m/{p = /k/?1:2}{printf "%d\n", int($1) * 1024 ^ p}')
21
  old_value=$(grep -e "^${key};" "${PFSTATSFILE}" | cut -d ";" -f2)
22
  if [ -n "${old_value}" ]; then
23
    sed -i -e "s/^${key};${old_value}/${key};$((${old_value}+${value}))/" "${PFSTATSFILE}"
24
  else
25
    echo "${key};${value}" >> "${PFSTATSFILE}"
26
  fi
27
}
28
 
29
if [ -n "$1" ]; then 
30
  key=$(echo ${PFVALS[@]} | grep -wo $1)
31
  if [ -n "${key}" ]; then
32
    value=$(grep -e "^${key};" "${PFSTATSFILE}" | cut -d ";" -f2)
33
    echo "${value}"
34
  else
35
    rm "${TEMPFILE}"
36
    exit 2
37
  fi
38
else
39
  "${LOGTAIL}" -f"${MAILLOG}" -o"${PFOFFSETFILE}" | "${PFLOGSUMM}" -h 0 -u 0 --bounce_detail=0 --deferral_detail=0 --reject_detail=0 --no_no_msg_size --smtpd_warning_detail=0 > "${TEMPFILE}"
40
  for i in "${PFVALS[@]}"; do
41
    printvalues "$i"
42
  done
43
fi
44
 
45
rm "${TEMPFILE}"