-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
161 lines (112 loc) · 4.08 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package main
import (
"context"
"encoding/base64"
"encoding/json"
"io/ioutil"
"net/http"
"os"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/docker/docker/pkg/archive"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
logs "github.com/sirupsen/logrus"
)
type Exporter struct {
Logger zerolog.Logger
RegistryMetric *prometheus.Desc
}
//Define the metrics we wish to expose
func main() {
exporter := NewExporter()
prometheus.MustRegister(exporter)
http.Handle("/metrics", promhttp.Handler())
logs.Fatal(http.ListenAndServe(":9300", nil))
}
func NewExporter() *Exporter {
fs, _ := os.Create("exporter.log")
log.Logger = log.With().Caller().Logger().Output(fs)
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
log.Info().Msg("starting exporter")
var registryMetric = prometheus.NewDesc("registry_health_metric", "registry health", []string{"registry_instance", "app"}, nil)
return &Exporter{Logger: log.Logger, RegistryMetric: registryMetric}
}
func (collector *Exporter) Describe(ch chan<- *prometheus.Desc) {
//Update this section with the each metric you create for a given collector
ch <- collector.RegistryMetric
}
//Collect implements required collect function for all promehteus collectors
func (collector *Exporter) Collect(ch chan<- prometheus.Metric) {
ctx := context.Background()
//Write latest value for each metric in the prometheus metric channel.
//Note that you can pass CounterValue, GaugeValue, or UntypedValue types here.
ch <- prometheus.MustNewConstMetric(collector.RegistryMetric, prometheus.GaugeValue, collector.CheckRegistry(ctx, os.Getenv("REGISTRY_HOST")), os.Getenv("REGISTRY_HOST"), "registry")
}
func (exporter *Exporter) CheckRegistry(ctx context.Context, registryURL string) float64 {
exporter.Logger.Info().Msgf("checking docker registry %s", registryURL)
//os.RemoveAll("images")
err := os.MkdirAll("images", 0777)
if err != nil {
exporter.Logger.Err(err).Msg("error creating images directory")
return 0.0
}
fs, err := os.Create("images/Dockerfile")
if err != nil {
exporter.Logger.Err(err).Msg("error creating Dockerfile")
return 0.0
}
defer fs.Close()
bigBuff := make([]byte, 10000000)
ioutil.WriteFile("images/dummy.test", bigBuff, 0666)
fs.WriteString("FROM scratch \n")
fs.WriteString("MAINTAINER sumesh \n")
fs.WriteString("COPY dummy.test /dummy.test \n")
tar, err := archive.TarWithOptions("images/", &archive.TarOptions{})
if err != nil {
exporter.Logger.Err(err).Msg("error tar directory")
return 0.0
}
defer tar.Close()
dockerClient, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
exporter.Logger.Info().Msgf("docker client version is %s", dockerClient.ClientVersion())
if err != nil {
exporter.Logger.Err(err).Msg("error connecting to docker daemon")
return 0.0
}
defer dockerClient.Close()
authConfig := types.AuthConfig{}
authConfig.Username = os.Getenv("REGISTRY_USERNAME")
authConfig.Password = os.Getenv("REGISTRY_PASSWORF")
authConfig.ServerAddress = os.Getenv("REGISTRY_HOST")
authConfigBytes, _ := json.Marshal(authConfig)
authConfigEncoded := base64.URLEncoding.EncodeToString(authConfigBytes)
_, err = dockerClient.RegistryLogin(ctx, authConfig)
if err != nil {
exporter.Logger.Err(err).Msg("error logging in to to docker registry")
return 0.0
}
opts := types.ImageBuildOptions{
Dockerfile: "Dockerfile",
Tags: []string{registryURL + "/devops/monitoring"},
Remove: true,
}
resp, err := dockerClient.ImageBuild(ctx, tar, opts)
if err != nil {
if err != nil {
exporter.Logger.Err(err).Msg("error building docker images")
return 0.0
}
}
defer resp.Body.Close()
dPush, err := dockerClient.ImagePush(ctx, registryURL+"/devops/monitoring", types.ImagePushOptions{RegistryAuth: authConfigEncoded})
if err != nil {
exporter.Logger.Err(err).Msg("error pushing docker images")
return 0.0
}
defer dPush.Close()
exporter.Logger.Info().Msg("checking registry completed")
return 1.0
}