forked from commercetools/monit_exporter
-
Notifications
You must be signed in to change notification settings - Fork 2
/
monit_exporter.go
231 lines (202 loc) · 5.75 KB
/
monit_exporter.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package main
import (
"bytes"
"crypto/tls"
"encoding/xml"
"errors"
"flag"
"fmt"
"io/ioutil"
"net/http"
"sync"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"golang.org/x/net/html/charset"
)
const (
namespace = "monit" // Prefix for Prometheus metrics.
)
var configFile = flag.String("conf", "./config.toml", "Configuration file for exporter")
var serviceTypes = map[int]string{
0: "filesystem",
1: "directory",
2: "file",
3: "programPid",
4: "remoteHost",
5: "system",
6: "fifo",
7: "programPath",
8: "network",
}
type monitXML struct {
MonitServices []monitService `xml:"service"`
}
// Simplified structure of monit check.
type monitService struct {
Type int `xml:"type,attr"`
Name string `xml:"name"`
Status int `xml:"status"`
Monitored string `xml:"monitor"`
}
// Exporter collects monit stats from the given URI and exports them using
// the prometheus metrics package.
type Exporter struct {
config *Config
mutex sync.RWMutex
client *http.Client
up prometheus.Gauge
checkStatus *prometheus.GaugeVec
}
type Config struct {
listen_address string
metrics_path string
ignore_ssl bool
monit_scrape_uri string
monit_user string
monit_password string
}
func FetchMonitStatus(c *Config) ([]byte, error) {
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: c.ignore_ssl},
},
}
req, err := http.NewRequest("GET", c.monit_scrape_uri, nil)
if err != nil {
log.Errorf("Unable to create request: %v", err)
}
req.SetBasicAuth(c.monit_user, c.monit_password)
resp, err := client.Do(req)
if err != nil {
log.Error("Unable to fetch monit status")
return nil, err
}
switch resp.StatusCode {
case 200:
case 401:
return nil, errors.New("Authentication with monit failed")
default:
return nil, fmt.Errorf("Monit returned %s", resp.Status)
}
data, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
log.Fatal("Unable to read monit status")
return nil, err
}
return data, nil
}
func ParseMonitStatus(data []byte) (monitXML, error) {
var statusChunk monitXML
reader := bytes.NewReader(data)
decoder := xml.NewDecoder(reader)
// Parsing status results to structure
decoder.CharsetReader = charset.NewReaderLabel
err := decoder.Decode(&statusChunk)
return statusChunk, err
}
func ParseConfig() *Config {
flag.Parse()
v := viper.New()
v.SetDefault("listen_address", "localhost:9388")
v.SetDefault("metrics_path", "/metrics")
v.SetDefault("ignore_ssl", false)
v.SetDefault("monit_scrape_uri", "http://localhost:2812/_status?format=xml&level=full")
v.SetDefault("monit_user", "")
v.SetDefault("monit_password", "")
v.SetConfigFile(*configFile)
v.SetConfigType("toml")
err := v.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
log.Printf("Error reading config file: %s. Using defaults.", err)
}
return &Config{
listen_address: v.GetString("listen_address"),
metrics_path: v.GetString("metrics_path"),
ignore_ssl: v.GetBool("ignore_ssl"),
monit_scrape_uri: v.GetString("monit_scrape_uri"),
monit_user: v.GetString("monit_user"),
monit_password: v.GetString("monit_password"),
}
}
// Returns an initialized Exporter.
func NewExporter(c *Config) (*Exporter, error) {
return &Exporter{
config: c,
up: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Name: "exporter_up",
Help: "Monit status availability",
}),
checkStatus: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "exporter_service_check",
Help: "Monit service check info",
},
[]string{"check_name", "type", "monitored"},
),
}, nil
}
// Describe describes all the metrics ever exported by the monit exporter. It
// implements prometheus.Collector.
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
e.up.Describe(ch)
e.checkStatus.Describe(ch)
}
func (e *Exporter) scrape() error {
data, err := FetchMonitStatus(e.config)
if err != nil {
// set "monit_exporter_up" gauge to 0, remove previous metrics from e.checkStatus vector
e.up.Set(0)
e.checkStatus.Reset()
log.Errorf("Error getting monit status: %v", err)
return err
} else {
parsedData, err := ParseMonitStatus(data)
if err != nil {
e.up.Set(0)
e.checkStatus.Reset()
log.Errorf("Error parsing data from monit: %v\n%s", err, data)
} else {
e.up.Set(1)
// Constructing metrics
for _, service := range parsedData.MonitServices {
e.checkStatus.With(prometheus.Labels{"check_name": service.Name, "type": serviceTypes[service.Type], "monitored": service.Monitored}).Set(float64(service.Status))
}
}
return err
}
}
// Collect fetches the stats from configured monit location and delivers them
// as Prometheus metrics. It implements prometheus.Collector.
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
e.mutex.Lock() // Protect metrics from concurrent collects.
defer e.mutex.Unlock()
e.checkStatus.Reset()
e.scrape()
e.up.Collect(ch)
e.checkStatus.Collect(ch)
return
}
func main() {
config := ParseConfig()
exporter, err := NewExporter(config)
if err != nil {
log.Fatal(err)
}
prometheus.MustRegister(exporter)
log.Printf("Starting monit_exporter: %s", config.listen_address)
http.Handle(config.metrics_path, promhttp.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>Monit Exporter</title></head>
<body>
<h1>Monit Exporter</h1>
<p><a href="` + config.metrics_path + `">Metrics</a></p>
</body>
</html>`))
})
log.Fatal(http.ListenAndServe(config.listen_address, nil))
}