Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
cngJo committed Aug 2, 2022
0 parents commit 6897f9d
Show file tree
Hide file tree
Showing 8 changed files with 690 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
IMAP_SERVER=
IMAP_USERNAME=
IMAP_PASSWORD=
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/.vscode
/.idea

.env
imap-mailbox-exporter
21 changes: 21 additions & 0 deletions LICENCE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 jop-software Inh. Johannes Przymusinski

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
16 changes: 16 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
BINARY=imap-mailbox-exporter

build:
go build -o $(BINARY) ./...

run: build
./$(BINARY)

test:
go test ./...

test-coverage:
go test ./... -cover

test-coverage-html:
go test ./... -coverprofile=coverage.out && go tool cover -html=coverage.out
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Imap Mailbox Exporter

> Export the amount of mails in a mailbox for use in prometheus.
## Usage

### Configuration

```dotenv
IMAP_SERVER=""
IMAP_USERNAME=""
IMAP_PASSWORD=""
```

### Probe

```txt
http://127.0.0.1:9101/probe?mailbox=INBOX
```

### Provided metrics

```txt
# HELP probe_mailbox_count Displays the count of mails found in the mailbox
# TYPE probe_mailbox_count gauge
probe_mailbox_count 0
```

## License

This project is licensed under the [MIT License](./LICENCE)

<div align="center">
<span>&copy; 2022, jop-software Inh. Johannes Przymusinski</span>
</div>
23 changes: 23 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module github.com/jop-software/imap-mailbox-exporter

go 1.18

require (
github.com/emersion/go-imap v1.2.1
github.com/joho/godotenv v1.4.0
github.com/prometheus/client_golang v1.12.2
)

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/emersion/go-sasl v0.0.0-20200509203442-7bfe0ed36a21 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.32.1 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9 // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/protobuf v1.26.0 // indirect
)
473 changes: 473 additions & 0 deletions go.sum

Large diffs are not rendered by default.

114 changes: 114 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package main

import (
"errors"
"fmt"
"log"
"net/http"
"os"

"github.com/emersion/go-imap/client"
"github.com/joho/godotenv"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)

// Config holds the base IMAP credentialsf
type Config struct {
ImapUsername string
ImapPassword string
ImapServer string
}

func (c *Config) validate() bool {
return c.ImapUsername != "" && c.ImapServer != "" && c.ImapPassword != ""
}

// NewConfig creates and validated a new Config struct from environment variables
func NewConfig() (*Config, error) {
config := &Config{
ImapServer: os.Getenv("IMAP_SERVER"),
ImapUsername: os.Getenv("IMAP_USERNAME"),
ImapPassword: os.Getenv("IMAP_PASSWORD"),
}

if !config.validate() {
return nil, errors.New("not all needed configuration flags could be found")
}

return config, nil
}

var config *Config

func countMailsInMailbox(mailbox string) (uint32, error) {
c, err := client.DialTLS(config.ImapServer, nil)
if err != nil {
return 0, err
}

defer c.Logout()

// Login
if err := c.Login(config.ImapUsername, config.ImapPassword); err != nil {
return 0, err
}

// Select INBOX
mbox, err := c.Select(mailbox, true)
if err != nil {
return 0, err
}

return mbox.Messages, nil
}

func main() {
// We can ignore errors here, because the config might be set from environment variables
_ = godotenv.Load()

// Intialize Config
conf, err := NewConfig()
if err != nil {
log.Fatalf("Could not load configuration: %v", err)
}

config = 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) {
mailbox := r.URL.Query().Get("mailbox")
if mailbox == "" {
http.Error(w, "Mailbox 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",
})

registry := prometheus.NewRegistry()
registry.MustRegister(probeCountGauge)

// TODO: Proper error handling
count, err := countMailsInMailbox(mailbox)
if err != nil {
http.Error(w, fmt.Sprintf("Cound not load mailbox data: %v", err), http.StatusInternalServerError)
return
}

probeCountGauge.Set(float64(count))

h := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
h.ServeHTTP(w, r)
})

http.Handle("/metrics", promhttp.Handler())

log.Fatal(http.ListenAndServe(":9101", nil))
}

0 comments on commit 6897f9d

Please sign in to comment.