2015-09-26 08:36:40 -07:00
|
|
|
// Copyright 2015 The Prometheus Authors
|
|
|
|
// 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.
|
|
|
|
|
2014-11-24 14:15:13 -08:00
|
|
|
// +build !nomegacli
|
2014-07-08 07:24:29 -07:00
|
|
|
|
|
|
|
package collector
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2015-05-20 11:04:49 -07:00
|
|
|
"flag"
|
2014-07-08 07:24:29 -07:00
|
|
|
"io"
|
|
|
|
"os/exec"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
defaultMegaCli = "megacli"
|
|
|
|
adapterHeaderSep = "================"
|
|
|
|
)
|
|
|
|
|
2015-05-20 11:04:49 -07:00
|
|
|
var (
|
|
|
|
megacliCommand = flag.String("collector.megacli.command", defaultMegaCli, "Command to run megacli.")
|
|
|
|
)
|
|
|
|
|
2014-11-24 18:00:17 -08:00
|
|
|
type megaCliCollector struct {
|
2015-05-20 11:04:49 -07:00
|
|
|
cli string
|
2014-11-24 18:00:17 -08:00
|
|
|
|
|
|
|
driveTemperature *prometheus.GaugeVec
|
2017-01-06 03:32:12 -08:00
|
|
|
driveCounters *prometheus.GaugeVec
|
2014-11-24 18:00:17 -08:00
|
|
|
drivePresence *prometheus.GaugeVec
|
|
|
|
}
|
2014-07-08 07:24:29 -07:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
Factories["megacli"] = NewMegaCliCollector
|
|
|
|
}
|
|
|
|
|
2017-02-28 08:44:53 -08:00
|
|
|
// NewMegaCliCollector returns a new Collector exposing RAID status through
|
|
|
|
// megacli.
|
2015-05-20 11:04:49 -07:00
|
|
|
func NewMegaCliCollector() (Collector, error) {
|
2016-12-22 08:38:25 -08:00
|
|
|
warnDeprecated("megacli")
|
2014-11-24 18:00:17 -08:00
|
|
|
return &megaCliCollector{
|
2015-05-28 12:21:54 -07:00
|
|
|
cli: *megacliCommand,
|
2014-11-24 18:00:17 -08:00
|
|
|
driveTemperature: prometheus.NewGaugeVec(prometheus.GaugeOpts{
|
|
|
|
Namespace: Namespace,
|
|
|
|
Name: "megacli_drive_temperature_celsius",
|
|
|
|
Help: "megacli: drive temperature",
|
|
|
|
}, []string{"enclosure", "slot"}),
|
2017-01-06 03:32:12 -08:00
|
|
|
driveCounters: prometheus.NewGaugeVec(prometheus.GaugeOpts{
|
2014-11-24 18:00:17 -08:00
|
|
|
Namespace: Namespace,
|
|
|
|
Name: "megacli_drive_count",
|
|
|
|
Help: "megacli: drive error and event counters",
|
|
|
|
}, []string{"enclosure", "slot", "type"}),
|
|
|
|
drivePresence: prometheus.NewGaugeVec(prometheus.GaugeOpts{
|
|
|
|
Namespace: Namespace,
|
|
|
|
Name: "megacli_adapter_disk_presence",
|
|
|
|
Help: "megacli: disk presence per adapter",
|
|
|
|
}, []string{"type"}),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *megaCliCollector) Update(ch chan<- prometheus.Metric) (err error) {
|
|
|
|
err = c.updateAdapter()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = c.updateDisks()
|
|
|
|
c.driveTemperature.Collect(ch)
|
|
|
|
c.driveCounters.Collect(ch)
|
|
|
|
c.drivePresence.Collect(ch)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-11-24 15:30:07 -08:00
|
|
|
func parseMegaCliDisks(r io.Reader) (map[int]map[int]map[string]string, error) {
|
|
|
|
var (
|
|
|
|
stats = map[int]map[int]map[string]string{}
|
|
|
|
scanner = bufio.NewScanner(r)
|
|
|
|
curEnc = -1
|
|
|
|
curSlot = -1
|
|
|
|
)
|
2014-07-08 07:24:29 -07:00
|
|
|
|
|
|
|
for scanner.Scan() {
|
|
|
|
var err error
|
|
|
|
text := strings.TrimSpace(scanner.Text())
|
|
|
|
parts := strings.SplitN(text, ":", 2)
|
|
|
|
if len(parts) != 2 { // Adapter #X
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
key := strings.TrimSpace(parts[0])
|
|
|
|
value := strings.TrimSpace(parts[1])
|
|
|
|
switch {
|
|
|
|
case key == "Enclosure Device ID":
|
|
|
|
curEnc, err = strconv.Atoi(value)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
case key == "Slot Number":
|
|
|
|
curSlot, err = strconv.Atoi(value)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
case curSlot != -1 && curEnc != -1:
|
|
|
|
if _, ok := stats[curEnc]; !ok {
|
|
|
|
stats[curEnc] = map[int]map[string]string{}
|
|
|
|
}
|
|
|
|
if _, ok := stats[curEnc][curSlot]; !ok {
|
|
|
|
stats[curEnc][curSlot] = map[string]string{}
|
|
|
|
}
|
|
|
|
stats[curEnc][curSlot][key] = value
|
|
|
|
}
|
|
|
|
}
|
2014-11-24 15:30:07 -08:00
|
|
|
|
2014-07-08 07:24:29 -07:00
|
|
|
return stats, nil
|
|
|
|
}
|
|
|
|
|
2014-11-24 15:30:07 -08:00
|
|
|
func parseMegaCliAdapter(r io.Reader) (map[string]map[string]string, error) {
|
|
|
|
var (
|
|
|
|
raidStats = map[string]map[string]string{}
|
|
|
|
scanner = bufio.NewScanner(r)
|
|
|
|
header = ""
|
|
|
|
last = ""
|
|
|
|
)
|
|
|
|
|
2014-07-08 07:24:29 -07:00
|
|
|
for scanner.Scan() {
|
|
|
|
text := strings.TrimSpace(scanner.Text())
|
|
|
|
if text == adapterHeaderSep {
|
|
|
|
header = last
|
|
|
|
raidStats[header] = map[string]string{}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
last = text
|
|
|
|
if header == "" { // skip Adapter #X and separator
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
parts := strings.SplitN(text, ":", 2)
|
|
|
|
if len(parts) != 2 { // these section never include anything we are interested in
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
key := strings.TrimSpace(parts[0])
|
|
|
|
value := strings.TrimSpace(parts[1])
|
|
|
|
|
|
|
|
raidStats[header][key] = value
|
|
|
|
|
|
|
|
}
|
2014-11-24 15:30:07 -08:00
|
|
|
|
2014-07-08 07:24:29 -07:00
|
|
|
return raidStats, nil
|
|
|
|
}
|
|
|
|
|
2014-10-29 07:16:43 -07:00
|
|
|
func (c *megaCliCollector) updateAdapter() error {
|
2014-07-08 07:24:29 -07:00
|
|
|
cmd := exec.Command(c.cli, "-AdpAllInfo", "-aALL")
|
|
|
|
pipe, err := cmd.StdoutPipe()
|
|
|
|
if err != nil {
|
2014-10-29 07:16:43 -07:00
|
|
|
return err
|
2014-07-08 07:24:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := cmd.Start(); err != nil {
|
2014-10-29 07:16:43 -07:00
|
|
|
return err
|
2014-07-08 07:24:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
stats, err := parseMegaCliAdapter(pipe)
|
|
|
|
if err != nil {
|
2014-10-29 07:16:43 -07:00
|
|
|
return err
|
2014-07-08 07:24:29 -07:00
|
|
|
}
|
|
|
|
if err := cmd.Wait(); err != nil {
|
2014-10-29 07:16:43 -07:00
|
|
|
return err
|
2014-07-08 07:24:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range stats["Device Present"] {
|
|
|
|
value, err := strconv.ParseFloat(v, 64)
|
|
|
|
if err != nil {
|
2014-10-29 07:16:43 -07:00
|
|
|
return err
|
2014-07-08 07:24:29 -07:00
|
|
|
}
|
2014-11-24 18:00:17 -08:00
|
|
|
c.drivePresence.WithLabelValues(k).Set(value)
|
2014-07-08 07:24:29 -07:00
|
|
|
}
|
2014-10-29 07:16:43 -07:00
|
|
|
return nil
|
2014-07-08 07:24:29 -07:00
|
|
|
}
|
|
|
|
|
2014-10-29 07:16:43 -07:00
|
|
|
func (c *megaCliCollector) updateDisks() error {
|
2014-11-24 18:00:17 -08:00
|
|
|
var counters = []string{"Media Error Count", "Other Error Count", "Predictive Failure Count"}
|
|
|
|
|
2014-07-08 07:24:29 -07:00
|
|
|
cmd := exec.Command(c.cli, "-PDList", "-aALL")
|
|
|
|
pipe, err := cmd.StdoutPipe()
|
|
|
|
if err != nil {
|
2014-10-29 07:16:43 -07:00
|
|
|
return err
|
2014-07-08 07:24:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := cmd.Start(); err != nil {
|
2014-10-29 07:16:43 -07:00
|
|
|
return err
|
2014-07-08 07:24:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
stats, err := parseMegaCliDisks(pipe)
|
|
|
|
if err != nil {
|
2014-10-29 07:16:43 -07:00
|
|
|
return err
|
2014-07-08 07:24:29 -07:00
|
|
|
}
|
|
|
|
if err := cmd.Wait(); err != nil {
|
2014-10-29 07:16:43 -07:00
|
|
|
return err
|
2014-07-08 07:24:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for enc, encStats := range stats {
|
|
|
|
for slot, slotStats := range encStats {
|
|
|
|
encStr := strconv.Itoa(enc)
|
|
|
|
slotStr := strconv.Itoa(slot)
|
|
|
|
|
2015-07-25 07:45:45 -07:00
|
|
|
tStr := slotStats["Drive Temperature"]
|
|
|
|
if strings.Index(tStr, "C") > 0 {
|
|
|
|
tStr = tStr[:strings.Index(tStr, "C")]
|
|
|
|
t, err := strconv.ParseFloat(tStr, 64)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.driveTemperature.WithLabelValues(encStr, slotStr).Set(t)
|
|
|
|
}
|
2014-07-08 07:24:29 -07:00
|
|
|
|
2014-11-24 18:00:17 -08:00
|
|
|
for _, i := range counters {
|
|
|
|
counter, err := strconv.ParseFloat(slotStats[i], 64)
|
2014-07-08 07:24:29 -07:00
|
|
|
if err != nil {
|
2014-10-29 07:16:43 -07:00
|
|
|
return err
|
2014-07-08 07:24:29 -07:00
|
|
|
}
|
|
|
|
|
2014-11-24 18:00:17 -08:00
|
|
|
c.driveCounters.WithLabelValues(encStr, slotStr, i).Set(counter)
|
2014-07-08 07:24:29 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-10-29 07:16:43 -07:00
|
|
|
return nil
|
2014-07-08 07:24:29 -07:00
|
|
|
}
|