1 # -*- coding: utf-8 -*-
2 # Description: mdstat netdata python.d module
5 from base import SimpleService
12 class Service(SimpleService):
13 def __init__(self, configuration=None, name=None):
14 SimpleService.__init__(self, configuration=configuration, name=name)
15 self.order = ['agr_health']
16 self.definitions = {'agr_health':
18 [None, 'Faulty devices in MD', 'failed disks', 'health', 'md.health', 'line'],
20 self.proc_mdstat = '/proc/mdstat'
21 self.regex_disks = compile(r'((?<=\ )[a-zA-Z_0-9]+(?= : active)).*?((?<= \[)[0-9]+)/([0-9]+(?=\] ))')
22 self.regex_status = compile(r'([a-zA-Z_0-9]+)( : active)[^:]*?([a-z]+) = ([0-9.]+(?=%)).*?((?<=finish=)[0-9.]+)min speed=([0-9]+)')
25 raw_data = self._get_raw_data()
27 self.error('Cant read mdstat data from %s' % (self.proc_mdstat))
30 md_list = [md[0] for md in self.regex_disks.findall(raw_data)]
33 self.order.append(''.join([md, '_status']))
34 self.order.append(''.join([md, '_rate']))
35 self.definitions['agr_health']['lines'].append([''.join([md, '_health']), md, 'absolute'])
36 self.definitions[md] = {'options':
37 [None, 'MD disks stats', 'disks', md, 'md.disks', 'stacked'],
38 'lines': [[''.join([md, '_total']), 'total', 'absolute'],
39 [''.join([md, '_inuse']), 'inuse', 'absolute']]}
40 self.definitions[''.join([md, '_status'])] = {'options':
41 [None, 'MD current status', 'percent', md, 'md.status', 'line'],
42 'lines': [[''.join([md, '_resync']), 'resync', 'absolute', 1, 100],
43 [''.join([md, '_recovery']), 'recovery', 'absolute', 1, 100],
44 [''.join([md, '_check']), 'check', 'absolute', 1, 100]]}
45 self.definitions[''.join([md, '_rate'])] = {'options':
46 [None, 'MD operation status', 'rate', md, 'md.rate', 'line'],
47 'lines': [[''.join([md, '_finishin']), 'finish min', 'absolute', 1, 100],
48 [''.join([md, '_rate']), 'megabyte/s', 'absolute', -1, 100]]}
49 self.info('Plugin was started successfully. MDs to monitor %s' % (md_list))
54 def _get_raw_data(self):
56 Read data from /proc/mdstat
60 with open(self.proc_mdstat, 'rt') as proc_mdstat:
61 raw_result = proc_mdstat.read()
65 raw_result = ' '.join(raw_result.split())
70 Parse data from _get_raw_data()
73 raw_mdstat = self._get_raw_data()
74 mdstat_disks = self.regex_disks.findall(raw_mdstat)
75 mdstat_status = self.regex_status.findall(raw_mdstat)
78 for md in mdstat_disks:
79 to_netdata[''.join([md[0], '_total'])] = int(md[1])
80 to_netdata[''.join([md[0], '_inuse'])] = int(md[2])
81 to_netdata[''.join([md[0], '_health'])] = int(md[1]) - int(md[2])
82 to_netdata[''.join([md[0], '_check'])] = 0
83 to_netdata[''.join([md[0], '_resync'])] = 0
84 to_netdata[''.join([md[0], '_recovery'])] = 0
85 to_netdata[''.join([md[0], '_finishin'])] = 0
86 to_netdata[''.join([md[0], '_rate'])] = 0
88 for md in mdstat_status:
89 to_netdata[''.join([md[0], '_' + md[2]])] = round(float(md[3]) * 100)
90 to_netdata[''.join([md[0], '_finishin'])] = round(float(md[4]) * 100)
91 to_netdata[''.join([md[0], '_rate'])] = round(float(md[5]) / 1000 * 100)