-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
189 lines (163 loc) · 5.07 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package main
import (
"context"
"fmt"
"net/http"
_ "net/http/pprof"
"os"
"os/signal"
"syscall"
"github.com/alecthomas/kingpin/v2"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/mackerelio/mackerel-agent/checks"
"github.com/mackerelio/mackerel-agent/config"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/promlog"
"github.com/prometheus/common/promlog/flag"
common_version "github.com/prometheus/common/version"
"github.com/pyama86/mackerel-check-plugin-exporter/mackerel"
)
const (
namespace = "mackerel_checks_exporter"
)
var (
mackerelChecks = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "check_status"),
"Status of health checks associated with mackerel checks.",
[]string{"check_name", "status", "message"}, nil,
)
mackerelPlugins = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "plugin_value"),
"Value of mackerel plugins.",
[]string{"name"}, nil,
)
)
type promHTTPLogger struct {
logger log.Logger
}
func (l promHTTPLogger) Println(v ...interface{}) {
level.Error(l.logger).Log("msg", fmt.Sprint(v...))
}
type Exporter struct {
logger log.Logger
conf *config.Config
}
func NewExporter(logger log.Logger, conf *config.Config) (*Exporter, error) {
return &Exporter{
logger: logger,
conf: conf,
}, nil
}
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
ch <- mackerelChecks
ch <- mackerelPlugins
}
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
for n, _ := range e.conf.CheckPlugins {
v, ok := mackerel.CheckResult.Load(n)
vm, _ := mackerel.CheckResultMessage.Load(n)
if ok {
var up float64
switch v.(checks.Status) {
case checks.StatusOK:
up = 1.0
case checks.StatusWarning:
up = 2.0
case checks.StatusCritical:
up = 3.0
case checks.StatusUnknown:
up = 0.0
}
ch <- prometheus.MustNewConstMetric(
mackerelChecks, prometheus.GaugeValue, up, n, string(v.(checks.Status)), vm.(string),
)
}
}
mackerel.PluginResult.Range(func(key, value interface{}) bool {
ch <- prometheus.MustNewConstMetric(
mackerelPlugins, prometheus.GaugeValue, value.(float64), key.(string),
)
return true
})
}
var (
version = "dev"
goversion = ""
commit = "none"
date = "unknown"
builtBy = "unknown"
)
func main() {
var (
listenAddress = kingpin.Flag("web.listen-address", "Address to listen on for web interface and telemetry.").Default(":9111").String()
metricsPath = kingpin.Flag("web.telemetry-path", "Path under which to expose metrics.").Default("/metrics").String()
mackerelConfigPath = kingpin.Flag("mackerel.config-path", "Mackerel Config Path.").Default("/etc/mackerel-agent/mackerel-agent.conf").String()
)
promlogConfig := &promlog.Config{}
flag.AddFlags(kingpin.CommandLine, promlogConfig)
kingpin.HelpFlag.Short('h')
v := fmt.Sprintf("mackerel-checks-exporter version: %s (%s)\n", version, commit)
v = v + fmt.Sprintf("build at %s (with %s)\n", date, goversion)
kingpin.Version(v)
kingpin.Parse()
logger := promlog.New(promlogConfig)
level.Info(logger).Log("msg", "Starting mackerel_checks_exporter", "version", common_version.Info())
level.Info(logger).Log("build_context", common_version.BuildContext())
mackerelConf, err := config.LoadConfig(*mackerelConfigPath)
if err != nil {
level.Error(logger).Log("msg", "Error cant open mackerl.conf", "err", err)
os.Exit(1)
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
checkers := mackerel.CreateCheckers(mackerelConf)
plugins := mackerel.PluginGenerators(mackerelConf)
go mackerel.Loop(checkers, plugins, ctx)
exporter, err := NewExporter(logger, mackerelConf)
if err != nil {
level.Error(logger).Log("msg", "Error creating the exporter", "err", err)
os.Exit(1)
}
prometheus.MustRegister(exporter)
http.Handle(*metricsPath,
promhttp.InstrumentMetricHandler(
prometheus.DefaultRegisterer,
promhttp.HandlerFor(
prometheus.DefaultGatherer,
promhttp.HandlerOpts{
ErrorLog: &promHTTPLogger{
logger: logger,
},
},
),
),
)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>Mackerel Checks Exporter</title></head>
<body>
<h1>Mackerel Checks Exporter</h1>
<p><a href='` + *metricsPath + `'>Metrics</a></p>
<h2>Build</h2>
<pre>` + common_version.Info() + ` ` + common_version.BuildContext() + `</pre>
</body> </html>`))
})
level.Info(logger).Log("msg", "Listening on address", "address", *listenAddress)
srv := &http.Server{Addr: *listenAddress}
go func() {
if err := srv.ListenAndServe(); err != nil {
level.Error(logger).Log("msg", "Error starting HTTP server", "err", err)
os.Exit(1)
}
}()
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGHUP)
<-sigCh
cancel()
if err := srv.Shutdown(ctx); err != nil {
level.Error(logger).Log("msg", "Shutdown HTTP server", "err", err)
}
os.Exit(0)
}