forked from kumina/dovecot_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdovecot_exporter.go
160 lines (144 loc) · 4.09 KB
/
dovecot_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
// Copyright 2016 Kumina, https://kumina.nl/
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"bufio"
"flag"
"fmt"
"io"
"log"
"net"
"net/http"
"os"
"strconv"
"strings"
"github.com/prometheus/client_golang/prometheus"
)
var (
dovecotUpDesc = prometheus.NewDesc(
prometheus.BuildFQName("dovecot", "", "up"),
"Whether scraping Dovecot's metrics was successful.",
[]string{"scope"},
nil)
dovecotScopes = [...]string{"user"}
)
// Converts the output of Dovecot's EXPORT command to metrics.
func CollectFromReader(file io.Reader, ch chan<- prometheus.Metric) error {
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
// Read first line of input, containing the aggregation and column names.
if !scanner.Scan() {
return fmt.Errorf("Failed to extract columns from input")
}
columnNames := strings.Fields(scanner.Text())
if len(columnNames) < 2 {
return fmt.Errorf("Input does not provide any columns")
}
columns := []*prometheus.Desc{}
for _, columnName := range columnNames[1:] {
columns = append(columns, prometheus.NewDesc(
prometheus.BuildFQName("dovecot", columnNames[0], columnName),
"Help text not provided by this exporter.",
[]string{columnNames[0]},
nil))
}
// Read successive lines, containing the values.
for scanner.Scan() {
values := strings.Fields(scanner.Text())
if len(values) != len(columns) + 1 {
break
}
for i, value := range values[1:] {
f, err := strconv.ParseFloat(value, 64)
if err != nil {
return err
}
ch <- prometheus.MustNewConstMetric(
columns[i],
prometheus.UntypedValue,
f,
values[0])
}
}
return scanner.Err()
}
func CollectFromFile(path string, ch chan<- prometheus.Metric) error {
conn, err := os.Open(path)
if err != nil {
return err
}
return CollectFromReader(conn, ch)
}
func CollectFromSocket(path string, scope string, ch chan<- prometheus.Metric) error {
conn, err := net.Dial("unix", path)
if err != nil {
return err
}
_, err = conn.Write([]byte("EXPORT\t" + scope + "\n"))
if err != nil {
return err
}
return CollectFromReader(conn, ch)
}
type DovecotExporter struct {
socketPath string
}
func NewDovecotExporter(socketPath string) *DovecotExporter {
return &DovecotExporter{
socketPath: socketPath,
}
}
func (e *DovecotExporter) Describe(ch chan<- *prometheus.Desc) {
ch <- dovecotUpDesc
}
func (e *DovecotExporter) Collect(ch chan<- prometheus.Metric) {
for _, scope := range dovecotScopes {
err := CollectFromSocket(e.socketPath, scope, ch)
if err == nil {
ch <- prometheus.MustNewConstMetric(
dovecotUpDesc,
prometheus.GaugeValue,
1.0,
scope)
} else {
log.Printf("Failed to scrape socket: %s", err)
ch <- prometheus.MustNewConstMetric(
dovecotUpDesc,
prometheus.GaugeValue,
0.0,
scope)
}
}
}
func main() {
var (
listenAddress = flag.String("web.listen-address", ":9166", "Address to listen on for web interface and telemetry.")
metricsPath = flag.String("web.telemetry-path", "/metrics", "Path under which to expose metrics.")
socketPath = flag.String("dovecot.socket-path", "/var/run/dovecot/stats", "Path under which to expose metrics.")
)
flag.Parse()
exporter := NewDovecotExporter(*socketPath)
prometheus.MustRegister(exporter)
http.Handle(*metricsPath, prometheus.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`
<html>
<head><title>Dovecot Exporter</title></head>
<body>
<h1>Dovecot Exporter</h1>
<p><a href='` + *metricsPath + `'>Metrics</a></p>
</body>
</html>`))
})
log.Fatal(http.ListenAndServe(*listenAddress, nil))
}