-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpostfix_exporter.go
177 lines (155 loc) · 5.86 KB
/
postfix_exporter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package main
import (
"flag"
"net/http"
"os/exec"
"sync"
"strings"
"strconv"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/log"
)
const (
namespace = "postfix" // For Prometheus metrics.
)
var (
listenAddress = flag.String("telemetry.address", ":9115", "Address on which to expose metrics.")
metricsPath = flag.String("telemetry.endpoint", "/metrics", "Path under which to expose metrics.")
)
// Exporter collects postfix stats from machine of a specified user and exports them using
// the prometheus metrics package.
type Exporter struct {
mutex sync.RWMutex
totalQ prometheus.Gauge
incomingQ prometheus.Gauge
activeQ prometheus.Gauge
maildropQ prometheus.Gauge
deferredQ prometheus.Gauge
holdQ prometheus.Gauge
bounceQ prometheus.Gauge
}
// NewPostfixExporter returns an initialized Exporter.
func NewPostfixExporter() *Exporter {
return &Exporter{
totalQ: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Name: "total_queue_length",
Help: "length of mail queue",
}),
incomingQ: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Name: "incoming_queue_length",
Help: "length of incoming mail queue",
}),
activeQ: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Name: "active_queue_length",
Help: "length of active mail queue",
}),
maildropQ: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Name: "maildrop_queue_length",
Help: "length of maildrop queue",
}),
deferredQ: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Name: "deferred_queue_length",
Help: "length of deferred mail queue",
}),
holdQ: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Name: "hold_queue_length",
Help: "length of hold mail queue",
}),
bounceQ: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Name: "bounce_queue_length",
Help: "length of bounce mail queue",
}),
}
}
// Describe describes all the metrics ever exported by the postfix exporter. It
// implements prometheus.Collector.
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
e.totalQ.Describe(ch)
e.incomingQ.Describe(ch)
e.activeQ.Describe(ch)
e.maildropQ.Describe(ch)
e.deferredQ.Describe(ch)
e.holdQ.Describe(ch)
e.bounceQ.Describe(ch)
}
func getPostfixQueueLength() string {
cmd := "/usr/sbin/postqueue -p | tail -n1 | awk '{print $5}'"
out, _ := exec.Command("bash", "-c", cmd).Output()
len := string(out)
len = strings.TrimSpace(len)
return len
}
func getQueueDir() string{
cmd := "postconf -h queue_directory"
out,_ := exec.Command("bash", "-c", cmd).Output()
dir := string(out)
dir = strings.TrimSpace(dir)
return dir
}
func getQueueLength(qname string, qdir string) string{
cmd := "sudo find "+qdir+"/"+qname+" -type f -print | wc -l | awk '{print $1}'"
out,_ := exec.Command("bash", "-c", cmd).Output()
qlen := string(out)
qlen = strings.TrimSpace(qlen)
return qlen
}
func (e *Exporter) scrape(ch chan<- prometheus.Metric) error {
queue_length, _ := strconv.ParseFloat(getPostfixQueueLength(), 64)
e.totalQ.Set(float64(queue_length))
queue_dir := getQueueDir()
incoming_queue, _ := strconv.ParseFloat(getQueueLength("incoming", queue_dir), 64)
e.incomingQ.Set(float64(incoming_queue))
active_queue, _ := strconv.ParseFloat(getQueueLength("active", queue_dir), 64)
e.activeQ.Set(float64(active_queue))
maildrop_queue, _ := strconv.ParseFloat(getQueueLength("maildrop", queue_dir), 64)
e.maildropQ.Set(float64(maildrop_queue))
deferred_queue, _ := strconv.ParseFloat(getQueueLength("deferred", queue_dir), 64)
e.deferredQ.Set(float64(deferred_queue))
hold_queue, _ := strconv.ParseFloat(getQueueLength("hold", queue_dir), 64)
e.holdQ.Set(float64(hold_queue))
bounce_queue, _ := strconv.ParseFloat(getQueueLength("bounce", queue_dir), 64)
e.bounceQ.Set(float64(bounce_queue))
return nil
}
// Collect fetches the stats of a user and delivers them
// as Prometheus metrics. It implements prometheus.Collector.
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
e.mutex.Lock() // To protect metrics from concurrent collects.
defer e.mutex.Unlock()
if err := e.scrape(ch); err != nil {
log.Printf("Error scraping postfix: %s", err)
}
e.totalQ.Collect(ch)
e.incomingQ.Collect(ch)
e.activeQ.Collect(ch)
e.maildropQ.Collect(ch)
e.deferredQ.Collect(ch)
e.holdQ.Collect(ch)
e.bounceQ.Collect(ch)
return
}
func main() {
flag.Parse()
exporter := NewPostfixExporter()
prometheus.MustRegister(exporter)
http.Handle(*metricsPath, prometheus.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>Postfix exporter</title></head>
<body>
<h1>Postfix exporter</h1>
<p><a href='` + *metricsPath + `'>Metrics</a></p>
</body>
</html>
`))
})
log.Infof("Starting Server: %s", *listenAddress)
log.Fatal(http.ListenAndServe(*listenAddress, nil))
}