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

package megacli

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

type megaBBU struct {
	adapterNumber string
	batteryType   string
	temperature   string
	rsoc          string
	asoc          string // apparently can be 0 while relative > 0 (e.g. relative 91%, absolute 0%)
	cycleCount    string
	fullChargeCap string
	designCap     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
	}

	if len(bbus) == 0 {
		m.Debugf("no BBUs found")
		return nil
	}

	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.rsoc)
		writeInt(mx, px+"absolute_state_of_charge", bbu.asoc)
		writeInt(mx, px+"cycle_count", bbu.cycleCount)
		if v, ok := calcCapDegradationPerc(bbu); ok {
			mx[px+"capacity_degradation_perc"] = v
		}
	}

	m.Debugf("found %d BBUs", len(m.bbu))

	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 Design Info for Adapter"):
			section = "design"
			continue
		case strings.HasPrefix(line, "BBU Firmware Status"),
			strings.HasPrefix(line, "BBU GasGauge Status"),
			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.rsoc = getColonSepNumValue(line)
			case strings.HasPrefix(line, "Absolute State of charge:"):
				bbu.asoc = getColonSepNumValue(line)
			case strings.HasPrefix(line, "Full Charge Capacity:"):
				bbu.fullChargeCap = getColonSepNumValue(line)
			case strings.HasPrefix(line, "Cycle Count:"):
				bbu.cycleCount = getColonSepNumValue(line)
			}
		case "design":
			if strings.HasPrefix(line, "Design Capacity:") {
				bbu.designCap = getColonSepNumValue(line)
			}
		}
	}

	return bbus, nil
}

func calcCapDegradationPerc(bbu *megaBBU) (int64, bool) {
	full, err := strconv.ParseInt(bbu.fullChargeCap, 10, 64)
	if err != nil || full == 0 {
		return 0, false
	}
	design, err := strconv.ParseInt(bbu.designCap, 10, 64)
	if err != nil || design == 0 {
		return 0, false
	}

	v := 100 - float64(full)/float64(design)*100

	return int64(v), true
}