forked from jop-software/imap-mailbox-exporter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
129 lines (102 loc) · 3.27 KB
/
main.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
package main
import (
"fmt"
"log"
"net/http"
"github.com/emersion/go-imap"
"github.com/emersion/go-imap/client"
"github.com/joho/godotenv"
"github.com/jop-software/imap-mailbox-exporter/config"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var cfg *config.Config
func countMailsInMailbox(server config.ConfigServer, account config.ConfigAcccount, mailbox string) (uint32, int, error) {
c, err := client.DialTLS(server.HostPort(), nil)
if err != nil {
return 0, 0, err
}
defer c.Logout()
// Login
if err := c.Login(account.Username, account.Password); err != nil {
return 0, 0, err
}
// Select INBOX
mbox, err := c.Select(mailbox, true)
if err != nil {
return 0, 0, err
}
// Count unread emails
criteria := imap.NewSearchCriteria()
criteria.WithoutFlags = []string{imap.SeenFlag}
ids, err := c.Search(criteria)
if err != nil {
log.Fatal(err)
}
return mbox.Messages, len(ids), nil
}
func main() {
// We can ignore errors here, because the config might be set from environment variables
_ = godotenv.Load()
// Intialize Config
conf, err := config.NewConfig("./config.yaml")
if err != nil {
log.Fatalf("Could not load configuration: %v", err)
}
cfg = conf
http.HandleFunc("/-/health", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("Healthy"))
})
http.HandleFunc("/probe", func(w http.ResponseWriter, r *http.Request) {
// Maily use the target parameter, but support mailbox as a fallback.
target := r.URL.Query().Get("target")
if target == "" {
target = r.URL.Query().Get("mailbox")
if target == "" {
http.Error(w, "Mailbox parameter is missing", http.StatusBadRequest)
return
}
}
mailbox := target
hostname := r.URL.Query().Get("hostname")
if hostname == "" {
http.Error(w, "Hostname parameter is missing", http.StatusBadRequest)
return
}
username := r.URL.Query().Get("username")
if username == "" {
http.Error(w, "Username parameter is missing", http.StatusBadRequest)
return
}
probeCountGauge := prometheus.NewGauge(prometheus.GaugeOpts{
Name: "probe_mailbox_count",
Help: "Displays the count of mails found in the mailbox",
})
probeUnreadGauge := prometheus.NewGauge(prometheus.GaugeOpts{
Name: "probe_mailbox_unread_count",
Help: "Displays the count of unread mails found in the mailbox",
})
registry := prometheus.NewRegistry()
registry.MustRegister(probeCountGauge)
registry.MustRegister(probeUnreadGauge)
server, account, err := cfg.FindAccountInServer(hostname, username)
if err != nil {
log.Fatalf("Error: %v", err)
}
// TODO: Proper error handling
count, unread, err := countMailsInMailbox(*server, *account, mailbox)
if err != nil {
log.Printf("Cound not load mailbox data: %v", err)
http.Error(w, fmt.Sprintf("Cound not load mailbox data: %v", err), http.StatusInternalServerError)
return
}
log.Printf("Load mailbox count for %s of %s on %s: %d/%d", mailbox, username, hostname, count, unread)
probeCountGauge.Set(float64(count))
probeUnreadGauge.Set(float64(unread))
h := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
h.ServeHTTP(w, r)
})
http.Handle("/metrics", promhttp.Handler())
log.Fatal(http.ListenAndServe(":9101", nil))
}