Skip to content

Commit

Permalink
feat: count unread message in mailbox
Browse files Browse the repository at this point in the history
  • Loading branch information
mfournier authored and yann-soubeyrand committed Jul 18, 2024
1 parent ce17c3e commit a9f30d6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -67,4 +70,4 @@ This project is licensed under the [MIT License](./LICENCE)

<div align="center">
<span>&copy; 2022, jop-software Inh. Johannes Przymusinski</span>
</div>
</div>
30 changes: 23 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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() {
Expand Down Expand Up @@ -83,25 +92,32 @@ 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 {
log.Fatalf("Error: %v", err)
}

// 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)
Expand Down

0 comments on commit a9f30d6

Please # to comment.