Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: count unread message in mailbox #9

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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