summaryrefslogtreecommitdiffstats
path: root/collectors/python.d.plugin/adaptec_raid/adaptec_raid.chart.py
blob: 564c2ce87edddf77e676ea50bb0ba6c19f7e83ca (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
# -*- coding: utf-8 -*-
# Description: adaptec_raid netdata python.d module
# Author: Ilya Mashchenko (ilyam8)
# SPDX-License-Identifier: GPL-3.0-or-later


import re
from copy import deepcopy

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

disabled_by_default = True

update_every = 5

ORDER = [
    'ld_status',
    'pd_state',
    'pd_smart_warnings',
    'pd_temperature',
]

CHARTS = {
    'ld_status': {
        'options': [None, 'Status Is Not OK', 'bool', 'logical devices', 'adapter_raid.ld_status', 'line'],
        'lines': []
    },
    'pd_state': {
        'options': [None, 'State Is Not OK', 'bool', 'physical devices', 'adapter_raid.pd_state', 'line'],
        'lines': []
    },
    'pd_smart_warnings': {
        'options': [None, 'S.M.A.R.T warnings', 'count', 'physical devices',
                    'adapter_raid.smart_warnings', 'line'],
        'lines': []
    },
    'pd_temperature': {
        'options': [None, 'Temperature', 'celsius', 'physical devices', 'adapter_raid.temperature', 'line'],
        'lines': []
    },
}

SUDO = 'sudo'
ARCCONF = 'arcconf'

BAD_LD_STATUS = (
    'Degraded',
    'Failed',
)

GOOD_PD_STATUS = (
    'Online',
)

RE_LD = re.compile(
    r'Logical [dD]evice number\s+([0-9]+).*?'
    r'Status of [lL]ogical [dD]evice\s+: ([a-zA-Z]+)'
)


def find_lds(d):
    d = ' '.join(v.strip() for v in d)
    return [LD(*v) for v in RE_LD.findall(d)]


def find_pds(d):
    pds = list()
    pd = PD()

    for row in d:
        row = row.strip()
        if row.startswith('Device #'):
            pd = PD()
            pd.id = row.split('#')[-1]
        elif not pd.id:
            continue

        if row.startswith('State'):
            v = row.split()[-1]
            pd.state = v
        elif row.startswith('S.M.A.R.T. warnings'):
            v = row.split()[-1]
            pd.smart_warnings = v
        elif row.startswith('Temperature'):
            v = row.split(':')[-1].split()[0]
            pd.temperature = v
        elif row.startswith('NCQ status'):
            if pd.id and pd.state and pd.smart_warnings:
                pds.append(pd)
            pd = PD()

    return pds


class LD:
    def __init__(self, ld_id, status):
        self.id = ld_id
        self.status = status

    def data(self):
        return {
            'ld_{0}_status'.format(self.id): int(self.status in BAD_LD_STATUS)
        }


class PD:
    def __init__(self):
        self.id = None
        self.state = None
        self.smart_warnings = None
        self.temperature = None

    def data(self):
        data = {
            'pd_{0}_state'.format(self.id): int(self.state not in GOOD_PD_STATUS),
            'pd_{0}_smart_warnings'.format(self.id): self.smart_warnings,
        }
        if self.temperature and self.temperature.isdigit():
            data['pd_{0}_temperature'.format(self.id)] = self.temperature

        return data


class Arcconf:
    def __init__(self, arcconf):
        self.arcconf = arcconf

    def ld_info(self):
        return [self.arcconf, 'GETCONFIG', '1', 'LD']

    def pd_info(self):
        return [self.arcconf, 'GETCONFIG', '1', 'PD']


# TODO: hardcoded sudo...
class SudoArcconf:
    def __init__(self, arcconf, sudo):
        self.arcconf = Arcconf(arcconf)
        self.sudo = sudo

    def ld_info(self):
        return [self.sudo, '-n'] + self.arcconf.ld_info()

    def pd_info(self):
        return [self.sudo, '-n'] + self.arcconf.pd_info()


class Service(ExecutableService):
    def __init__(self, configuration=None, name=None):
        ExecutableService.__init__(self, configuration=configuration, name=name)
        self.order = ORDER
        self.definitions = deepcopy(CHARTS)
        self.use_sudo = self.configuration.get('use_sudo', True)
        self.arcconf = None

    def execute(self, command, stderr=False):
        return self._get_raw_data(command=command, stderr=stderr)

    def check(self):
        arcconf = find_binary(ARCCONF)
        if not arcconf:
            self.error('can\'t locate "{0}" binary'.format(ARCCONF))
            return False

        sudo = find_binary(SUDO)
        if self.use_sudo:
            if not sudo:
                self.error('can\'t locate "{0}" binary'.format(SUDO))
                return False
            err = self.execute([sudo, '-n', '-v'], True)
            if err:
                self.error(' '.join(err))
                return False

        if self.use_sudo:
            self.arcconf = SudoArcconf(arcconf, sudo)
        else:
            self.arcconf = Arcconf(arcconf)

        lds = self.get_lds()
        if not lds:
            return False

        self.debug('discovered logical devices ids: {0}'.format([ld.id for ld in lds]))

        pds = self.get_pds()
        if not pds:
            return False

        self.debug('discovered physical devices ids: {0}'.format([pd.id for pd in pds]))

        self.update_charts(lds, pds)
        return True

    def get_data(self):
        data = dict()

        for ld in self.get_lds():
            data.update(ld.data())

        for pd in self.get_pds():
            data.update(pd.data())

        return data

    def get_lds(self):
        raw_lds = self.execute(self.arcconf.ld_info())
        if not raw_lds:
            return None

        lds = find_lds(raw_lds)
        if not lds:
            self.error('failed to parse "{0}" output'.format(' '.join(self.arcconf.ld_info())))
            self.debug('output: {0}'.format(raw_lds))
            return None
        return lds

    def get_pds(self):
        raw_pds = self.execute(self.arcconf.pd_info())
        if not raw_pds:
            return None

        pds = find_pds(raw_pds)
        if not pds:
            self.error('failed to parse "{0}" output'.format(' '.join(self.arcconf.pd_info())))
            self.debug('output: {0}'.format(raw_pds))
            return None
        return pds

    def update_charts(self, lds, pds):
        charts = self.definitions
        for ld in lds:
            dim = ['ld_{0}_status'.format(ld.id), 'ld {0}'.format(ld.id)]
            charts['ld_status']['lines'].append(dim)

        for pd in pds:
            dim = ['pd_{0}_state'.format(pd.id), 'pd {0}'.format(pd.id)]
            charts['pd_state']['lines'].append(dim)

            dim = ['pd_{0}_smart_warnings'.format(pd.id), 'pd {0}'.format(pd.id)]
            charts['pd_smart_warnings']['lines'].append(dim)

            dim = ['pd_{0}_temperature'.format(pd.id), 'pd {0}'.format(pd.id)]
            charts['pd_temperature']['lines'].append(dim)