2019-02-20 23:03:41 +00:00
package main
import (
2020-02-19 17:10:45 +00:00
"context"
2019-02-20 23:03:41 +00:00
"log"
"net/http"
"os"
"github.com/alecthomas/kingpin"
"github.com/prometheus/client_golang/prometheus"
2020-02-21 11:11:58 +00:00
"github.com/prometheus/client_golang/prometheus/promhttp"
2019-02-20 23:03:41 +00:00
)
func main ( ) {
var (
app = kingpin . New ( "postfix_exporter" , "Prometheus metrics exporter for postfix" )
listenAddress = app . Flag ( "web.listen-address" , "Address to listen on for web interface and telemetry." ) . Default ( ":9154" ) . String ( )
metricsPath = app . Flag ( "web.telemetry-path" , "Path under which to expose metrics." ) . Default ( "/metrics" ) . String ( )
postfixShowqPath = app . Flag ( "postfix.showq_path" , "Path at which Postfix places its showq socket." ) . Default ( "/var/spool/postfix/public/showq" ) . String ( )
postfixLogfilePath = app . Flag ( "postfix.logfile_path" , "Path where Postfix writes log entries. This file will be truncated by this exporter." ) . Default ( "/var/log/postfix_exporter_input.log" ) . String ( )
2020-02-19 17:10:45 +00:00
logUnsupportedLines = app . Flag ( "log.unsupported" , "Log all unsupported lines." ) . Bool ( )
2019-02-20 23:03:41 +00:00
systemdEnable bool
systemdUnit , systemdSlice , systemdJournalPath string
)
systemdFlags ( & systemdEnable , & systemdUnit , & systemdSlice , & systemdJournalPath , app )
kingpin . MustParse ( app . Parse ( os . Args [ 1 : ] ) )
var journal * Journal
if systemdEnable {
var err error
journal , err = NewJournal ( systemdUnit , systemdSlice , systemdJournalPath )
if err != nil {
log . Fatalf ( "Error opening systemd journal: %s" , err )
}
defer journal . Close ( )
2020-02-19 17:10:45 +00:00
log . Println ( "Reading log events from systemd" )
} else {
log . Printf ( "Reading log events from %v" , * postfixLogfilePath )
2019-02-20 23:03:41 +00:00
}
exporter , err := NewPostfixExporter (
* postfixShowqPath ,
* postfixLogfilePath ,
journal ,
2020-02-19 17:10:45 +00:00
* logUnsupportedLines ,
2019-02-20 23:03:41 +00:00
)
if err != nil {
log . Fatalf ( "Failed to create PostfixExporter: %s" , err )
}
prometheus . MustRegister ( exporter )
2020-02-19 17:10:45 +00:00
http . Handle ( * metricsPath , promhttp . Handler ( ) )
2019-02-20 23:03:41 +00:00
http . HandleFunc ( "/" , func ( w http . ResponseWriter , r * http . Request ) {
_ , err = w . Write ( [ ] byte ( `
< html >
< head > < title > Postfix Exporter < / title > < / head >
< body >
< h1 > Postfix Exporter < / h1 >
< p > < a href = ' ` + *metricsPath + ` ' > Metrics < / a > < / p >
< / body >
< / html > ` ) )
if err != nil {
panic ( err )
}
} )
2020-02-19 17:10:45 +00:00
ctx , cancelFunc := context . WithCancel ( context . Background ( ) )
defer cancelFunc ( )
go exporter . StartMetricCollection ( ctx )
2019-02-20 23:03:41 +00:00
log . Print ( "Listening on " , * listenAddress )
log . Fatal ( http . ListenAndServe ( * listenAddress , nil ) )
}