-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
73 lines (62 loc) · 1.75 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"log"
"net/http"
"os"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/cli"
)
type HelmCollector struct {
releaseStatus *prometheus.Desc
}
func NewHelmCollector() *HelmCollector {
return &HelmCollector{
releaseStatus: prometheus.NewDesc(
"helm_release_status",
"Status of Helm releases (1 for deployed, 0 otherwise)",
[]string{"release_name", "namespace", "chart", "app_version", "chart_version"},
nil,
),
}
}
func (collector *HelmCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- collector.releaseStatus
}
func (collector *HelmCollector) Collect(ch chan<- prometheus.Metric) {
settings := cli.New()
actionConfig := new(action.Configuration)
if err := actionConfig.Init(settings.RESTClientGetter(), settings.Namespace(), os.Getenv("HELM_DRIVER"), log.Printf); err != nil {
log.Fatalf("Error initializing Helm action configuration: %v", err)
}
client := action.NewList(actionConfig)
client.AllNamespaces = true
releases, err := client.Run()
if err != nil {
log.Fatalf("Error fetching Helm releases: %v", err)
}
for _, release := range releases {
status := float64(0)
if release.Info.Status == "deployed" {
status = 1
}
ch <- prometheus.MustNewConstMetric(
collector.releaseStatus,
prometheus.GaugeValue,
status,
release.Name,
release.Namespace,
release.Chart.Metadata.Name,
release.Chart.Metadata.AppVersion,
release.Chart.Metadata.Version,
)
}
}
func main() {
collector := NewHelmCollector()
prometheus.MustRegister(collector)
http.Handle("/metrics", promhttp.Handler())
log.Println("Starting server on :2112")
log.Fatal(http.ListenAndServe(":2112", nil))
}