]> arthur.barton.de Git - netdata.git/blob - python.d/mdstat.chart.py
add resync/check/recovery progress charts
[netdata.git] / python.d / mdstat.chart.py
1 # -*- coding: utf-8 -*-
2 # Description: mdstat netdata python.d module
3 # Author: l2isbad
4
5 from base import SimpleService
6 from re import compile
7 priority = 60000
8 retries = 60
9 update_every = 1
10
11
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':
17                                 {'options':
18                                      [None, 'Faulty devices in MD', 'failed disks', 'health', 'md.health', 'line'],
19                                  'lines': []}}
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.]+(?=%))')
23
24     def check(self):
25         raw_data = self._get_raw_data()
26         if not raw_data:
27             self.error('Cant read mdstat data from %s' % (self.proc_mdstat))
28             return False
29         else:
30             md_list = [md[0] for md in self.regex_disks.findall(raw_data)]
31             for md in md_list:
32                 self.order.append(md)
33                 self.order.append(''.join([md, '_status']))
34                 self.definitions['agr_health']['lines'].append([''.join([md, '_health']), md, 'absolute'])
35                 self.definitions[md] = {'options':
36                                             [None, 'MD disks stats', 'disks', md, 'md.stats', 'stacked'],
37                                         'lines': [[''.join([md, '_total']), 'total', 'absolute'],
38                                                   [''.join([md, '_inuse']), 'inuse', 'absolute']]}
39                 self.definitions[''.join([md, '_status'])] = {'options':
40                                             [None, 'MD current status', 'percent', md, 'md.status', 'line'],
41                                         'lines': [[''.join([md, '_resync']), 'resync', 'absolute'],
42                                                   [''.join([md, '_recovery']), 'recovery', 'absolute'],
43                                                   [''.join([md, '_check']), 'check', 'absolute']]}
44             self.info('Plugin was started successfully. MDs to monitor %s' % (md_list))
45
46             return True
47
48     def _get_raw_data(self):
49         """
50         Read data from /proc/mdstat
51         :return: str
52         """
53         try:
54             with open(self.proc_mdstat, 'rt') as proc_mdstat:
55                 raw_result = proc_mdstat.read()
56         except Exception:
57             return None
58         else:
59             raw_result = ' '.join(raw_result.split())
60             return raw_result
61
62     def _get_data(self):
63         """
64         Parse data from _get_raw_data()
65         :return: dict
66         """
67         raw_mdstat = self._get_raw_data()
68         mdstat_disks = self.regex_disks.findall(raw_mdstat)
69         mdstat_status = self.regex_status.findall(raw_mdstat)
70         to_netdata = {}
71
72         for md in mdstat_disks:
73             to_netdata[''.join([md[0], '_total'])] = int(md[1])
74             to_netdata[''.join([md[0], '_inuse'])] = int(md[2])
75             to_netdata[''.join([md[0], '_health'])] = int(md[1]) - int(md[2])
76             to_netdata[''.join([md[0], '_check'])] = 0
77             to_netdata[''.join([md[0], '_resync'])] = 0
78             to_netdata[''.join([md[0], '_recovery'])] = 0
79         
80         for md in mdstat_status:
81             to_netdata[''.join([md[0], '_' + md[2]])] = round(float(md[3]))
82
83         return to_netdata