summaryrefslogtreecommitdiffstats
path: root/src/go/collectors/go.d.plugin/modules/megacli/collect_bbu.go
blob: 1ba827185f4e185b171e440243d34762e664f5b3 (plain)
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
// SPDX-License-Identifier: GPL-3.0-or-later

package megacli

import (
	"bufio"
	"bytes"
	"fmt"
	"strings"
)

type megaBBU struct {
	adapterNumber         string
	batteryType           string
	temperature           string
	relativeStateOfCharge string
	absoluteStateOfCharge string // apparently can be 0 while relative > 0 (e.g. relative 91%, absolute 0%)
	cycleCount            string
}

func (m *MegaCli) collectBBU(mx map[string]int64) error {
	bs, err := m.exec.bbuInfo()
	if err != nil {
		return err
	}

	bbus, err := parseBBUInfo(bs)
	if err != nil {
		return err
	}

	for _, bbu := range bbus {
		if !m.bbu[bbu.adapterNumber] {
			m.bbu[bbu.adapterNumber] = true
			m.addBBUCharts(bbu)
		}

		px := fmt.Sprintf("bbu_adapter_%s_", bbu.adapterNumber)

		writeInt(mx, px+"temperature", bbu.temperature)
		writeInt(mx, px+"relative_state_of_charge", bbu.relativeStateOfCharge)
		writeInt(mx, px+"absolute_state_of_charge", bbu.absoluteStateOfCharge)
		writeInt(mx, px+"cycle_count", bbu.cycleCount)
	}

	return nil
}

func parseBBUInfo(bs []byte) (map[string]*megaBBU, error) {
	bbus := make(map[string]*megaBBU)

	var section string
	var bbu *megaBBU

	sc := bufio.NewScanner(bytes.NewReader(bs))

	for sc.Scan() {
		line := strings.TrimSpace(sc.Text())

		switch {
		case strings.HasPrefix(line, "BBU status for Adapter"):
			section = "status"
			ad := getColonSepValue(line)
			if _, ok := bbus[ad]; !ok {
				bbu = &megaBBU{adapterNumber: ad}
				bbus[ad] = bbu
			}
			continue
		case strings.HasPrefix(line, "BBU Capacity Info for Adapter"):
			section = "capacity"
			continue
		case strings.HasPrefix(line, "BBU Firmware Status"),
			strings.HasPrefix(line, "BBU GasGauge Status"),
			strings.HasPrefix(line, "BBU Design Info for Adapter"),
			strings.HasPrefix(line, "BBU Properties for Adapter"):
			section = ""
			continue
		}

		if bbu == nil {
			continue
		}

		switch section {
		case "status":
			switch {
			case strings.HasPrefix(line, "BatteryType:"):
				bbu.batteryType = getColonSepValue(line)
			case strings.HasPrefix(line, "Temperature:"):
				bbu.temperature = getColonSepNumValue(line)
			}
		case "capacity":
			switch {
			case strings.HasPrefix(line, "Relative State of Charge:"):
				bbu.relativeStateOfCharge = getColonSepNumValue(line)
			case strings.HasPrefix(line, "Absolute State of charge:"):
				bbu.absoluteStateOfCharge = getColonSepNumValue(line)
			case strings.HasPrefix(line, "Cycle Count:"):
				bbu.cycleCount = getColonSepNumValue(line)
			}
		}
	}

	return bbus, nil
}