From a9f30d6e25642f175d3491fadf71f26a73ea7989 Mon Sep 17 00:00:00 2001 From: Marc Fournier Date: Thu, 2 Nov 2023 16:57:37 +0100 Subject: [PATCH] feat: count unread message in mailbox --- README.md | 7 +++++-- main.go | 30 +++++++++++++++++++++++------- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index e6a8325..81a29f0 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,10 @@ http://127.0.0.1:9101/probe?target=INBOX&hostname=imap.example.com&username=me@e ```txt # HELP probe_mailbox_count Displays the count of mails found in the mailbox # TYPE probe_mailbox_count gauge -probe_mailbox_count 0 +probe_mailbox_count 5 +# HELP probe_mailbox_unread_count Displays the count of unread mails found in the mailbox +# TYPE probe_mailbox_unread_count gauge +probe_mailbox_unread_count 2 ``` ### Configuration @@ -67,4 +70,4 @@ This project is licensed under the [MIT License](./LICENCE)
© 2022, jop-software Inh. Johannes Przymusinski -
\ No newline at end of file + diff --git a/main.go b/main.go index 407f8fd..9f934cd 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ import ( "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" @@ -14,26 +15,34 @@ import ( var cfg *config.Config -func countMailsInMailbox(server config.ConfigServer, account config.ConfigAcccount, mailbox string) (uint32, error) { +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, err + return 0, 0, err } defer c.Logout() // Login if err := c.Login(account.Username, account.Password); err != nil { - return 0, err + return 0, 0, err } // Select INBOX mbox, err := c.Select(mailbox, true) if err != nil { - return 0, err + return 0, 0, err } - return mbox.Messages, nil + // 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() { @@ -83,8 +92,14 @@ func main() { 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 { @@ -92,16 +107,17 @@ func main() { } // TODO: Proper error handling - count, err := countMailsInMailbox(*server, *account, mailbox) + 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", mailbox, username, hostname, count) + 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)