This repository was archived by the owner on Oct 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathhpraid_exporter.go
More file actions
211 lines (188 loc) · 5.99 KB
/
hpraid_exporter.go
File metadata and controls
211 lines (188 loc) · 5.99 KB
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
// Copyright 2018 Prodrive Technologies, https://prodrive-technologies.com/
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"archive/zip"
"encoding/xml"
"errors"
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"os/exec"
"path/filepath"
"sync"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
)
var (
hpraidScrapeSuccessDesc = prometheus.NewDesc(
prometheus.BuildFQName("hpraid", "", "scrape_success"),
"Whether scraping the HP RAID controller stats was successful.",
nil, nil)
hpraidErrorsDesc = prometheus.NewDesc(
prometheus.BuildFQName("hpraid", "", "errors"),
"Errors in the diagnostic report reported by the RAID controller.",
[]string{"device", "message", "severity"}, nil)
)
// ADUReport is the top level structure contained in the XML report file
// generated by ssacli.
type ADUReport struct {
XMLName xml.Name `xml:"ADUReport"`
Devices []Device `xml:"Device"`
}
// Collect data from the XML report and convert it to Prometheus metrics.
func (r *ADUReport) Collect(ch chan<- prometheus.Metric) {
for _, device := range r.Devices {
device.Collect("", ch)
}
}
// Device contains per-device information in the XML report file
// generated by ssacli.
type Device struct {
DeviceType string `xml:"deviceType,attr"`
MarketingName string `xml:"marketingName,attr"`
Errors []Message `xml:"Errors>Message"`
Devices []Device `xml:"Device"`
}
// Collect data from the per-device object and convert it to Prometheus metrics.
func (d *Device) Collect(devicePrefix string, ch chan<- prometheus.Metric) {
if len(devicePrefix) > 0 {
devicePrefix += "/"
}
devicePrefix += d.DeviceType
devicePrefix += "="
devicePrefix += d.MarketingName
for _, message := range d.Errors {
message.Collect(devicePrefix, ch)
}
for _, device := range d.Devices {
device.Collect(devicePrefix, ch)
}
}
// Message contains an error message part of the XML report file
// generated by ssacli.
type Message struct {
Message string `xml:"message,attr"`
Severity string `xml:"severity,attr"`
}
// Collect data from an error message and convert it to a Prometheus metric.
func (m *Message) Collect(devicePrefix string, ch chan<- prometheus.Metric) {
ch <- prometheus.MustNewConstMetric(
hpraidErrorsDesc, prometheus.GaugeValue, 1.0,
devicePrefix, m.Message, m.Severity)
}
// HpraidExporter is a Prometheus exporter implementation that calls
// into the "ssacli" utility to generate a diagnostic report for HP
// RAID hardware. It then converts the diagnostic report into Prometheus
// metrics.
type HpraidExporter struct {
collectLock sync.Mutex
utilityPath string
}
// Describe metrics provided by the HP RAID exporter.
func (e *HpraidExporter) Describe(ch chan<- *prometheus.Desc) {
ch <- hpraidScrapeSuccessDesc
ch <- hpraidErrorsDesc
}
func collectFromUtility(utilityPath string, ch chan<- prometheus.Metric) error {
tempDir, err := ioutil.TempDir("", "hpraid")
if err != nil {
return fmt.Errorf("failed to create temporary zip path: %s", err)
}
log.Debug("Using %s as temporary zip directory", tempDir)
defer os.RemoveAll(tempDir)
temporaryZipPath := filepath.Join(tempDir, "hpraid_exporter.zip")
// Invoke diagnostic utility in such a way that it writes into a zip file.
cmd := exec.Command(utilityPath, "ctrl", "all", "diag", "file="+temporaryZipPath)
if err := cmd.Run(); err != nil {
return err
}
// Look for the XML file stored in the zip file.
z, err := zip.OpenReader(temporaryZipPath)
if err != nil {
return err
}
var xmlFile *zip.File
defer z.Close()
for _, f := range z.File {
if f.Name == "ADUReport.xml" {
xmlFile = f
}
}
if xmlFile == nil {
return errors.New("Zip file does not contain ADUReport.xml")
}
// Parse the XML data.
reader, err := xmlFile.Open()
if err != nil {
return err
}
defer reader.Close()
xmlDecoder := xml.NewDecoder(reader)
var report ADUReport
err = xmlDecoder.Decode(&report)
if err != nil {
return err
}
// Extract Prometheus metrics from the report.
report.Collect(ch)
return nil
}
// Collect metrics from HP RAID hardware.
func (e *HpraidExporter) Collect(ch chan<- prometheus.Metric) {
e.collectLock.Lock()
err := collectFromUtility(e.utilityPath, ch)
e.collectLock.Unlock()
if err == nil {
ch <- prometheus.MustNewConstMetric(
hpraidScrapeSuccessDesc, prometheus.GaugeValue, 1.0)
} else {
log.Error("Failed to gather stats: ", err)
ch <- prometheus.MustNewConstMetric(
hpraidScrapeSuccessDesc, prometheus.GaugeValue, 0.0)
}
}
func newHpraidExporter(utilityPath string) (*HpraidExporter, error) {
return &HpraidExporter{
utilityPath: utilityPath,
}, nil
}
func main() {
var (
listenAddress = flag.String("web.listen-address", ":9423", "Address to listen on for web interface and telemetry.")
metricsPath = flag.String("web.telemetry-path", "/metrics", "Path under which to expose metrics.")
utilityPath = flag.String("hpraid.utility-path", "ssacli", "Path of the ssacli utility.")
)
flag.Parse()
log.Info("Starting hpraid_exporter")
exporter, err := newHpraidExporter(*utilityPath)
if err != nil {
panic(err)
}
prometheus.MustRegister(exporter)
http.Handle(*metricsPath, prometheus.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`
<html>
<head><title>Hpraid Exporter</title></head>
<body>
<h1>Hpraid Exporter</h1>
<p><a href='` + *metricsPath + `'>Metrics</a></p>
</body>
</html>`))
})
log.Info("Listening on address:port => ", *listenAddress)
log.Fatal(http.ListenAndServe(*listenAddress, nil))
}