-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 5eff131
Showing
12 changed files
with
693 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
name: Check | ||
|
||
on: | ||
push: | ||
|
||
jobs: | ||
check: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4 | ||
|
||
- name: Setup Go | ||
uses: actions/setup-go@v4 | ||
|
||
- name: Install dependencies | ||
run: go get | ||
|
||
- name: Build | ||
run: go build -v | ||
|
||
- name: Test | ||
run: go test -v |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
name: Release Please | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
secrets: | ||
#MY_RELEASE_PLEASE_TOKEN: | ||
# required: true | ||
DOCKERHUB_USERNAME: | ||
required: true | ||
DOCKERHUB_TOKEN: | ||
required: true | ||
|
||
permissions: | ||
contents: write | ||
pull-requests: write | ||
|
||
jobs: | ||
release-please: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: google-github-actions/release-please-action@v4 | ||
with: | ||
#token: ${{ secrets.MY_RELEASE_PLEASE_TOKEN }} | ||
release-type: simple | ||
|
||
- name: Checkout | ||
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4 | ||
if: ${{ steps.release.outputs.release_created }} | ||
|
||
- name: Setup Go | ||
uses: actions/setup-go@v4 | ||
if: ${{ steps.release.outputs.release_created }} | ||
|
||
- name: Setup ko | ||
uses: ko-build/setup-ko@v0.6 | ||
env: | ||
KO_DOCKER_REPO: ${{ secrets.DOCKERHUB_USERNAME }} | ||
#KO_DEFAULTPLATFORMS: linux/arm64,linux/amd64 | ||
KO_DEFAULTPLATFORMS: all | ||
if: ${{ steps.release.outputs.release_created }} | ||
|
||
- name: Publish | ||
run: > | ||
ko build | ||
--base-import-paths | ||
--tags latest,${{ steps.release.outputs.major }},${{ steps.release.outputs.major }}.${{ steps.release.outputs.minor }},${{ steps.release.outputs.major }}.${{ steps.release.outputs.minor }}.${{ steps.release.outputs.patch }} | ||
if: ${{ steps.release.outputs.release_created }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/mta-sts-exporter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Changelog |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2024 phi.ag | ||
|
||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# MTA-STS Exporter | ||
|
||
**WIP** Prometheus metrics not implemented yet! | ||
|
||
Configuration | ||
|
||
- `REPORTS_PORT` (default: 8080) | ||
- `METRICS_PORT` (default: 8081) | ||
- `METRICS_PATH` (default: /metrics) | ||
- `MAX_BODY_SIZE` (default: 1 MiB) | ||
- `MAX_JSON_SIZE` (default: 5 MiB) | ||
- `SAVE_REPORTS` (default: true) | ||
- `SAVE_REPORTS_PATH` (default: /tmp/reports) | ||
- `COLLECT_GO_STATS` (default: false) | ||
|
||
Example | ||
|
||
cat test/example.json | gzip | curl -X POST -v --data-binary @- localhost:8080 | ||
curl localhost:8081/metrics |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module github.com/phi-ag/mta-sts-exporter | ||
|
||
go 1.22.0 | ||
|
||
require github.com/prometheus/client_golang v1.18.0 | ||
|
||
require ( | ||
github.com/beorn7/perks v1.0.1 // indirect | ||
github.com/cespare/xxhash/v2 v2.2.0 // indirect | ||
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 // indirect | ||
github.com/prometheus/client_model v0.5.0 // indirect | ||
github.com/prometheus/common v0.45.0 // indirect | ||
github.com/prometheus/procfs v0.12.0 // indirect | ||
golang.org/x/sys v0.15.0 // indirect | ||
google.golang.org/protobuf v1.31.0 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= | ||
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= | ||
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= | ||
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= | ||
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= | ||
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= | ||
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= | ||
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 h1:jWpvCLoY8Z/e3VKvlsiIGKtc+UG6U5vzxaoagmhXfyg= | ||
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0/go.mod h1:QUyp042oQthUoa9bqDv0ER0wrtXnBruoNd7aNjkbP+k= | ||
github.com/prometheus/client_golang v1.18.0 h1:HzFfmkOzH5Q8L8G+kSJKUx5dtG87sewO+FoDDqP5Tbk= | ||
github.com/prometheus/client_golang v1.18.0/go.mod h1:T+GXkCk5wSJyOqMIzVgvvjFDlkOQntgjkJWKrN5txjA= | ||
github.com/prometheus/client_model v0.5.0 h1:VQw1hfvPvk3Uv6Qf29VrPF32JB6rtbgI6cYPYQjL0Qw= | ||
github.com/prometheus/client_model v0.5.0/go.mod h1:dTiFglRmd66nLR9Pv9f0mZi7B7fk5Pm3gvsjB5tr+kI= | ||
github.com/prometheus/common v0.45.0 h1:2BGz0eBc2hdMDLnO/8n0jeB3oPrt2D08CekT0lneoxM= | ||
github.com/prometheus/common v0.45.0/go.mod h1:YJmSTw9BoKxJplESWWxlbyttQR4uaEcGyv9MZjVOJsY= | ||
github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k6Bo= | ||
github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo= | ||
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc= | ||
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= | ||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= | ||
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= | ||
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8= | ||
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,227 @@ | ||
package main | ||
|
||
import ( | ||
"compress/gzip" | ||
"encoding/json" | ||
"io" | ||
"log" | ||
"net/http" | ||
"os" | ||
"path/filepath" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/collectors" | ||
"github.com/prometheus/client_golang/prometheus/promhttp" | ||
) | ||
|
||
type Ports struct { | ||
Reports string | ||
Metrics string | ||
} | ||
|
||
type Limits struct { | ||
MaxBodySize int64 | ||
MaxJsonSize int64 | ||
} | ||
|
||
type ReportConfig struct { | ||
Save bool | ||
SavePath string | ||
Limits Limits | ||
} | ||
|
||
type DateRange struct { | ||
StartDatetime time.Time `json:"start-datetime"` | ||
EndDatetime time.Time `json:"end-datetime"` | ||
} | ||
|
||
type Policy struct { | ||
PolicyType string `json:"policy-type"` | ||
PolicyString []string `json:"policy-string"` | ||
PolicyDomain string `json:"policy-domain"` | ||
MxHost string `json:"mx-host"` | ||
} | ||
|
||
type Summary struct { | ||
TotalSuccessfulSessionCount int64 `json:"total-successful-session-count"` | ||
TotalFailureSessionCount int64 `json:"total-failure-session-count"` | ||
} | ||
|
||
type FailureDetail struct { | ||
ResultType string `json:"result-type"` | ||
SendingMtaIp string `json:"sending-mta-ip"` | ||
ReceivingIp string `json:"receiving-ip"` | ||
ReceivingMxHostname string `json:"receiving-mx-hostname"` | ||
FailedSessionCount int64 `json:"failed-session-count"` | ||
FailureReasonCode string `json:"failure-reason-code"` | ||
AdditionalInformation string `json:"additional-information"` | ||
} | ||
|
||
type PolicyItem struct { | ||
Policy Policy `json:"policy"` | ||
Summary Summary `json:"summary"` | ||
FailureDetails []FailureDetail `json:"failure-details"` | ||
} | ||
|
||
type Report struct { | ||
OrganizationName string `json:"organization-name"` | ||
DateRange DateRange `json:"date-range"` | ||
ContactInfo string `json:"contact-info"` | ||
ReportId string `json:"report-id"` | ||
Policies []PolicyItem `json:"policies"` | ||
} | ||
|
||
func parseReport(reader io.Reader) (Report, error) { | ||
report := &Report{} | ||
|
||
err := json.NewDecoder(reader).Decode(report) | ||
return *report, err | ||
} | ||
|
||
func getEnv(key, fallback string) string { | ||
if value, ok := os.LookupEnv(key); ok { | ||
return value | ||
} | ||
|
||
return fallback | ||
} | ||
|
||
func getEnvBool(key string, fallback bool) bool { | ||
if value, ok := os.LookupEnv(key); ok { | ||
result, err := strconv.ParseBool(value) | ||
if err != nil { | ||
return fallback | ||
} | ||
|
||
return result | ||
} | ||
|
||
return fallback | ||
} | ||
|
||
func getEnvInt64(key string, fallback int64) int64 { | ||
if value, ok := os.LookupEnv(key); ok { | ||
result, err := strconv.ParseInt(value, 10, 64) | ||
if err != nil { | ||
return fallback | ||
} | ||
|
||
return result | ||
} | ||
|
||
return fallback | ||
} | ||
|
||
func createRegistry(collectGoStats bool) *prometheus.Registry { | ||
registry := prometheus.NewRegistry() | ||
|
||
/* | ||
counter := prometheus.NewCounterVec(prometheus.CounterOpts{ | ||
Name: "docker_events", | ||
Help: "Number of docker container events", | ||
}, []string{"type", "action", "scope", "from", "name", "namespace", "service_name", "node_id", "service_id"}) | ||
*/ | ||
|
||
if collectGoStats { | ||
registry.MustRegister( | ||
collectors.NewGoCollector(), | ||
collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}), | ||
) | ||
} | ||
|
||
return registry | ||
} | ||
|
||
func handleReport(config ReportConfig) http.HandlerFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
if r.Method != http.MethodPost { | ||
w.WriteHeader(http.StatusMethodNotAllowed) | ||
return | ||
} | ||
|
||
limitedBody := io.LimitReader(r.Body, config.Limits.MaxBodySize) | ||
defer r.Body.Close() | ||
|
||
gzipReader, err := gzip.NewReader(limitedBody) | ||
if err != nil { | ||
log.Println("Gzip error", r.RemoteAddr, err) | ||
w.WriteHeader(http.StatusBadRequest) | ||
return | ||
} | ||
|
||
limitedJson := io.LimitReader(gzipReader, config.Limits.MaxJsonSize) | ||
defer gzipReader.Close() | ||
|
||
if config.Save { | ||
err := os.MkdirAll(config.SavePath, os.ModePerm) | ||
if err != nil { | ||
log.Println("Failed to create directory", config.SavePath, err) | ||
return | ||
} | ||
|
||
filename := time.Now().Format(time.RFC3339Nano) + ".json" | ||
target := filepath.Join(config.SavePath, filename) | ||
log.Println("Saving report to", target) | ||
|
||
out, err := os.Create(target) | ||
if err != nil { | ||
log.Println("Failed to create file", target, err) | ||
return | ||
} | ||
defer out.Close() | ||
|
||
/// NOTE: It seems `TeeReader` writes complete json even when parsing fails later. | ||
limitedJson = io.TeeReader(limitedJson, out) | ||
} | ||
|
||
report, err := parseReport(limitedJson) | ||
if err != nil { | ||
if err == io.ErrUnexpectedEOF { | ||
log.Println("Request too large", r.RemoteAddr) | ||
w.WriteHeader(http.StatusRequestEntityTooLarge) | ||
return | ||
} | ||
|
||
log.Println("Report error", r.RemoteAddr, err) | ||
w.WriteHeader(http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
log.Println("DONE", report) | ||
} | ||
} | ||
|
||
func main() { | ||
ports := Ports{ | ||
Reports: getEnv("REPORTS_PORT", "8080"), | ||
Metrics: getEnv("METRICS_PORT", "8081"), | ||
} | ||
|
||
reportConfig := ReportConfig{ | ||
Save: getEnvBool("SAVE_REPORTS", true), | ||
SavePath: getEnv("SAVE_REPORTS_PATH", "/tmp/reports"), | ||
Limits: Limits{ | ||
MaxBodySize: getEnvInt64("MAX_BODY_SIZE", 1*1024*1024), | ||
MaxJsonSize: getEnvInt64("MAX_JSON_SIZE", 5*1024*1024), | ||
}, | ||
} | ||
|
||
collectGoStats := getEnvBool("COLLECT_GO_STATS", false) | ||
registry := createRegistry(collectGoStats) | ||
metricsHandler := promhttp.HandlerFor(registry, promhttp.HandlerOpts{Registry: registry}) | ||
|
||
metricsPath := getEnv("METRICS_PATH", "/metrics") | ||
metricsHttp := http.NewServeMux() | ||
metricsHttp.Handle(metricsPath, metricsHandler) | ||
|
||
go func() { | ||
log.Printf("Serving metrics on port %s and path '%s'", ports.Metrics, metricsPath) | ||
log.Fatal(http.ListenAndServe(":"+ports.Metrics, metricsHttp)) | ||
}() | ||
|
||
http.HandleFunc("/", handleReport(reportConfig)) | ||
log.Printf("Listening for reports on port %s", ports.Reports) | ||
log.Fatal(http.ListenAndServe(":"+ports.Reports, nil)) | ||
} |
Oops, something went wrong.