#!/usr/local/bin/bash
#
# Munin Plugin
# to count logins to your dovecot mailserver
#
# Created by Dominik Schulz <lkml@ds.gauner.org>
# http://developer.gauner.org/munin/
# Contributions by:
# - Stephane Enten <tuf@delyth.net>
# - Steve Schnepp <steve.schnepp@pwkf.org>
# - pcy <pcy@ulyssis.org> (make 'Connected Users' DERIVE, check existence of logfile in autoconf)
# - Stephan Kleber <stephan@kleber.space>: journald support inspired by the sshd_log plugin
#
# This plugin requires read permission for the logfile or journald.
#
# Parameters understood:
#
#	config		(required)
#	autoconf 	(optional - used by munin-config)
#
# Config variables:
#
#   logfile        - Where to find the syslog file or "journald" to use journald.
#   journalctlargs - space separated list of arguments to pass to
#                    journalctl to get the sshd logs.
#                    default: "-u dovecot"
#
# Add the following line to a file in /etc/munin/plugin-conf.d:
# 	env.logfile /var/log/your/logfile.log
#
# Magic markers (optional - used by munin-config and installation scripts):
#
#%# family=auto
#%# capabilities=autoconf

######################
# Configuration
######################
LOGFILE=${logfile:-/var/log/mail.log}
JOURNALCTL_ARGS=${journalctlargs:--u dovecot}
TYPE=${type:-GAUGE}
######################

if [ "$LOG" = "journald" -a "$TYPE" = "DERIVE" ]; then
  TYPE=ABSOLUTE
fi

if [ "$1" = "autoconf" ]; then
  if [ "$LOGFILE" = "journald" ]; then
    # shellcheck disable=SC2086,SC2034
    if journalctl --no-pager --quiet --lines=1 $JOURNALCTL_ARGS | read -r DUMMY; then
      echo "yes"
    else
      echo "no (journald empty log for '$JOURNALCTL_ARGS' not found)"
    fi
  else
    if [ -r "$LOGFILE" ]; then
      echo "yes"
    else
      echo "no (logfile '$LOGFILE' not readable)"
    fi
  fi
  exit 0
fi

if [ "$1" = "config" ]; then
  echo 'graph_title Dovecot Logins'
  echo 'graph_category mail'
  echo 'graph_args --base 1000 -l 0'
  echo 'graph_vlabel Login Counters'

  for t in Total TLS SSL IMAP POP3
  do
    field=$(echo $t | tr '[:upper:]' '[:lower:]')
    echo "login_$field.label $t Logins"
    echo "login_$field.type DERIVE"
    echo "login_$field.min 0"
  done

  echo 'connected.label Connected Users'
  echo "connected.type ABSOLUTE"

  exit 0
fi

if [ "$LOGFILE" = "journald" -a "$TYPE" = "ABSOLUTE" ]; then
  CURSOR_FILE="$MUNIN_STATEFILE"
  # read cursor
  # format: "journald-cursor <cursor>"
  CURSOR=
  if [ -f "$CURSOR_FILE" ]; then
    CURSOR=$(awk '/^journald-cursor / {print $2}' "$CURSOR_FILE")
  fi
else
  CURSOR_FILE=
fi

if [ "$LOGFILE" = "journald" ]; then
  # shellcheck disable=SC2086
  if [ "$TYPE" = "ABSOLUTE" ]; then
    journalctl --no-pager --quiet --show-cursor ${CURSOR:+"--after-cursor=$CURSOR"} $JOURNALCTL_ARGS
  else
    journalctl --no-pager --quiet --since=$(date -dlast-sunday +%Y-%m-%d) $JOURNALCTL_ARGS
  fi
else
        cat "$LOGFILE"
fi | \
  awk -v cursor_file="$CURSOR_FILE" -v connected="$(doveadm who -1 | expr $(wc -l) - 1)" 'BEGIN{c["login_total"]=0;c["connected"]=connected;c["login_tls"]=0;c["login_ssl"]=0;c["login_imap"]=0;c["login_pop3"]=0; }
     /dovecot\[.*Login/{c["login_total"]++}
     /dovecot\[.*Login.*TLS/{c["login_tls"]++}
     /dovecot\[.*Login.*SSL/{c["login_ssl"]++}
     /dovecot\[.*imap.*Login/{c["login_imap"]++}
     /dovecot\[.*pop3.*Login/{c["login_pop3"]++}a
     END{if (cursor_file != "") { print "journald-cursor " $3 > cursor_file }; 
     for(i in c){print i".value " c[i]} }'

######################
# Total Logins
# use doveadm instead of     
#   /dovecot\[.*Disconnected/{c["connected"]++}
# and
#   {c["connected"]=c["login_total"]-c["connected"]};  {if (c["connected"] < 0) {c["connected"] = 0}};
# Connected Users
#   VALUE=$(( CONNECTS - DISCONNECTS ))
# TLS Logins
# SSL Logins
# IMAP Logins
# POP3 Logins
######################
