summaryrefslogtreecommitdiffstats
path: root/collectors/python.d.plugin/megacli/megacli.chart.py
blob: 8222092a800094c3c7ece9262feca3f85a168182 (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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# -*- coding: utf-8 -*-
# Description: megacli netdata python.d module
# Author: Ilya Mashchenko (ilyam8)
# SPDX-License-Identifier: GPL-3.0-or-later


import re

from bases.FrameworkServices.ExecutableService import ExecutableService
from bases.collection import find_binary

disabled_by_default = True

update_every = 5


def adapter_charts(ads):
    order = [
        'adapter_degraded',
    ]

    def dims(ad):
        return [['adapter_{0}_degraded'.format(a.id), 'adapter {0}'.format(a.id)] for a in ad]

    charts = {
        'adapter_degraded': {
            'options': [None, 'Adapter State', 'is degraded', 'adapter', 'megacli.adapter_degraded', 'line'],
            'lines': dims(ads)
        },
    }

    return order, charts


def pd_charts(pds):
    order = [
        'pd_media_error',
        'pd_predictive_failure',
    ]

    def dims(k, pd):
        return [['slot_{0}_{1}'.format(p.id, k), 'slot {0}'.format(p.id), 'incremental'] for p in pd]

    charts = {
        'pd_media_error': {
            'options': [None, 'Physical Drives Media Errors', 'errors/s', 'pd', 'megacli.pd_media_error', 'line'],
            'lines': dims('media_error', pds)
        },
        'pd_predictive_failure': {
            'options': [None, 'Physical Drives Predictive Failures', 'failures/s', 'pd',
                        'megacli.pd_predictive_failure', 'line'],
            'lines': dims('predictive_failure', pds)
        }
    }

    return order, charts


def battery_charts(bats):
    order = list()
    charts = dict()

    for b in bats:
        order.append('bbu_{0}_relative_charge'.format(b.id))
        charts.update(
            {
                'bbu_{0}_relative_charge'.format(b.id): {
                    'options': [None, 'Relative State of Charge', 'percentage', 'battery',
                                'megacli.bbu_relative_charge', 'line'],
                    'lines': [
                        ['bbu_{0}_relative_charge'.format(b.id), 'adapter {0}'.format(b.id)],
                    ]
                }
            }
        )

    for b in bats:
        order.append('bbu_{0}_cycle_count'.format(b.id))
        charts.update(
            {
                'bbu_{0}_cycle_count'.format(b.id): {
                    'options': [None, 'Cycle Count', 'cycle count', 'battery', 'megacli.bbu_cycle_count', 'line'],
                    'lines': [
                        ['bbu_{0}_cycle_count'.format(b.id), 'adapter {0}'.format(b.id)],
                    ]
                }
            }
        )

    return order, charts


RE_ADAPTER = re.compile(
    r'Adapter #([0-9]+) State(?:\s+)?: ([a-zA-Z ]+)'
)

RE_VD = re.compile(
    r'Slot Number: ([0-9]+) Media Error Count: ([0-9]+) Predictive Failure Count: ([0-9]+)'
)

RE_BATTERY = re.compile(
    r'BBU Capacity Info for Adapter: ([0-9]+) Relative State of Charge: ([0-9]+) % Cycle Count: ([0-9]+)'
)


def find_adapters(d):
    keys = ('Adapter #', 'State')
    d = ' '.join(v.strip() for v in d if v.startswith(keys))
    return [Adapter(*v) for v in RE_ADAPTER.findall(d)]


def find_pds(d):
    keys = ('Slot Number', 'Media Error Count', 'Predictive Failure Count')
    d = ' '.join(v.strip() for v in d if v.startswith(keys))
    return [PD(*v) for v in RE_VD.findall(d)]


def find_batteries(d):
    keys = ('BBU Capacity Info for Adapter', 'Relative State of Charge', 'Cycle Count')
    d = ' '.join(v.strip() for v in d if v.strip().startswith(keys))
    return [Battery(*v) for v in RE_BATTERY.findall(d)]


class Adapter:
    def __init__(self, n, state):
        self.id = n
        # TODO: Rewrite all of this
        self.state = int(state in ("Partially Degraded", "Degraded", "Failed"))

    def data(self):
        return {
            'adapter_{0}_degraded'.format(self.id): self.state,
        }

class PD:
    def __init__(self, n, media_err, predict_fail):
        self.id = n
        self.media_err = media_err
        self.predict_fail = predict_fail

    def data(self):
        return {
            'slot_{0}_media_error'.format(self.id): self.media_err,
            'slot_{0}_predictive_failure'.format(self.id): self.predict_fail,
        }


class Battery:
    def __init__(self, adapt_id, rel_charge, cycle_count):
        self.id = adapt_id
        self.rel_charge = rel_charge
        self.cycle_count = cycle_count

    def data(self):
        return {
            'bbu_{0}_relative_charge'.format(self.id): self.rel_charge,
            'bbu_{0}_cycle_count'.format(self.id): self.cycle_count,
        }


# TODO: hardcoded sudo...
class Megacli:
    def __init__(self):
        self.s = find_binary('sudo')
        self.m = find_binary('megacli') or find_binary('MegaCli')  # Binary on FreeBSD is MegaCli
        self.sudo_check = [self.s, '-n', '-l']
        self.disk_info = [self.s, '-n', self.m, '-LDPDInfo', '-aAll', '-NoLog']
        self.battery_info = [self.s, '-n', self.m, '-AdpBbuCmd', '-a0', '-NoLog']

    def __bool__(self):
        return bool(self.s and self.m)

    def __nonzero__(self):
        return self.__bool__()


class Service(ExecutableService):
    def __init__(self, configuration=None, name=None):
        ExecutableService.__init__(self, configuration=configuration, name=name)
        self.order = list()
        self.definitions = dict()
        self.do_battery = self.configuration.get('do_battery')
        self.megacli = Megacli()

    def check_sudo(self):
        err = self._get_raw_data(command=self.megacli.sudo_check, stderr=True)
        if err:
            self.error(''.join(err))
            return False
        return True

    def check_disk_info(self):
        d = self._get_raw_data(command=self.megacli.disk_info)
        if not d:
            return False

        ads = find_adapters(d)
        pds = find_pds(d)

        if not (ads and pds):
            self.error('failed to parse "{0}" output'.format(' '.join(self.megacli.disk_info)))
            return False

        o, c = adapter_charts(ads)
        self.order.extend(o)
        self.definitions.update(c)

        o, c = pd_charts(pds)
        self.order.extend(o)
        self.definitions.update(c)

        return True

    def check_battery(self):
        d = self._get_raw_data(command=self.megacli.battery_info)
        if not d:
            return False

        bats = find_batteries(d)

        if not bats:
            self.error('failed to parse "{0}" output'.format(' '.join(self.megacli.battery_info)))
            return False

        o, c = battery_charts(bats)
        self.order.extend(o)
        self.definitions.update(c)
        return True

    def check(self):
        if not self.megacli:
            self.error('can\'t locate "sudo" or "megacli" binary')
            return None

        if not (self.check_sudo() and self.check_disk_info()):
            return False

        if self.do_battery:
            self.do_battery = self.check_battery()

        return True

    def get_data(self):
        data = dict()

        data.update(self.get_adapter_pd_data())

        if self.do_battery:
            data.update(self.get_battery_data())

        return data or None

    def get_adapter_pd_data(self):
        raw = self._get_raw_data(command=self.megacli.disk_info)
        data = dict()

        if not raw:
            return data

        for a in find_adapters(raw):
            data.update(a.data())

        for p in find_pds(raw):
            data.update(p.data())

        return data

    def get_battery_data(self):
        raw = self._get_raw_data(command=self.megacli.battery_info)
        data = dict()

        if not raw:
            return data

        for b in find_batteries(raw):
            data.update(b.data())

        return data