Postfix exporter with docker and prometheus.
in previous article we setup mailcow server with mailcow dockerized
Now we configure monitoring for this server(postfix) with promethes exporter,prometheus,grafana.
Setup prometheus exporter
mkdir -p /etc/prometheus/exporters/postfix
Create docker-compose file(docker-compose.yml)
version: '3.5'
services:
exporter_postfix:
image: "devsadds/exporter_postfix:2.0.0"
volumes:
- "/var/lib/docker/volumes/YOU_DOCKER_VOLUME_FOR_POSTFIX//_data/public/showq:/var/spool/postfix/public/showq:ro"
- "/var/log/dockers/postfix/docker-postfix.log:/var/log/maillog:ro"
ports:
- "10.48.0.109:9154:9154"
stdin_open: true
tty: true
restart: always
Set Postfix logs to rsyslog in docker-compose yml.
logging:
driver: syslog
options:
syslog-address: "udp://127.0.0.1:515"
tag: "docker-postfix-{{.Name}}"
Restart postfix container with docker-compose.
Configure rsyslog for docker logs(postfix)
Install rsyslog from repo(example for ubuntu 20.04 ).
echo 'deb http://download.opensuse.org/repositories/home:/rgerhards/xUbuntu_20.04/ /' | sudo tee /etc/apt/sources.list.d/home:rgerhards.list
curl -fsSL https://download.opensuse.org/repositories/home:rgerhards/xUbuntu_20.04/Release.key | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/home:rgerhards.gpg > /dev/null
sudo apt update
sudo apt install rsyslog rsyslog-mmjsonparse rsyslog-mmnormalize
Change rsyslog configs
cat << OEF > /etc/rsyslog.d/docker_postfix.conf
module(load="imudp")
input(type="imudp" port=["515","20515"] device="lo" rcvbufSize="3m")
\$template t_docker_postfix, "%msg%"
if ( \$syslogtag startswith "docker-postfix") then {action(type="omfile" file="/var/log/dockers/postfix/docker-postfix.log" template="t_docker_postfix" ) stop }
OEF
Create dir
mkdir -p /var/log/dockers/postfix/
Edit /etc/rsyslog.conf
##MODULES
module(load="imuxsock") # provides support for local system logging
module(load="imklog") # provides kernel logging support
#module(load="imfile")
module(load="mmnormalize")
module(load="mmjsonparse")
#### GLOBAL DIRECTIVES
$ActionFileDefaultTemplate RSYSLOG_TraditionalFileFormat
#
# Set the default permissions for all log files.
#
$FileOwner root
$FileGroup adm
$FileCreateMode 0640
$DirCreateMode 0755
$Umask 0022
#
# Where to place spool and state files
#
$WorkDirectory /var/spool/rsyslog
#
# Include all config files in /etc/rsyslog.d/
#
$IncludeConfig /etc/rsyslog.d/*.conf
#### RULES
#
# First some standard log files. Log by facility.
#
auth,authpriv.* /var/log/auth.log
*.*;auth,authpriv.none -/var/log/syslog
#cron.* /var/log/cron.log
daemon.* -/var/log/daemon.log
kern.* -/var/log/kern.log
lpr.* -/var/log/lpr.log
mail.* -/var/log/mail.log
user.* -/var/log/user.log
#
# Logging for the mail system. Split it up so that
# it is easy to write scripts to parse these files.
#
mail.info -/var/log/mail.info
mail.warn -/var/log/mail.warn
mail.err /var/log/mail.err
#
# Some "catch-all" log files.
#
*.=debug;\
auth,authpriv.none;\
news.none;mail.none -/var/log/debug
*.=info;*.=notice;*.=warn;\
auth,authpriv.none;\
cron,daemon.none;\
mail,news.none -/var/log/messages
#
# Emergencies are sent to everybody logged in.
#
*.emerg :omusrmsg:*
Restart rsyslog
systemctl restart rsyslog && systemctl status rsyslog
See logs in dir
tail -f /var/log/dockers/postfix/*.log
Start exporter
cd /etc/prometheus/exporters/postfix
docker-composeu up -d
Visit page in browser
http://10.48.0.109:9154/metrics
Create Section in prometheus and add exporter
- job_name: "exporter_postfix"
scrape_interval: 10s
file_sd_configs:
- files:
- '/etc/prometheus/file_sd_configs/exporter_postfix/*.yml'
Create dir for exporter inventory file
mkdir -p /etc/prometheus/file_sd_configs/exporter_postfix/
Create Inventory file
/etc/prometheus/file_sd_configs/exporter_postfix/prod.yml
- targets:
- "10.48.0.109:9154"
labels:
job: exporter_postfix
env: prod
Reload prometheus(i use docker)
reload_prometheus() {
nginx_container=$(docker ps | grep 'mon.*nginx' | awk '{print $NF}')
docker exec -i -u 0 ${nginx_container} sh -c "curl -s -X POST http://prometheus:9090/prometheus/-/reload"
}
Import dashboard in grafana with id 10013
Profit