]> arthur.barton.de Git - netdata.git/blob - python.d/mdstat.chart.py
update regex
[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
23     def check(self):
24         raw_data = self._get_raw_data()
25         if not raw_data:
26             self.error('Cant read mdstat data from %s' % (self.proc_mdstat))
27             return False
28         else:
29             md_list = [md[0] for md in self.regex_disks.findall(raw_data)]
30             for md in md_list:
31                 self.order.append(md)
32                 self.definitions['agr_health']['lines'].append([''.join([md, '_health']), md, 'absolute'])
33                 self.definitions[md] = {'options':
34                                             [None, 'MD disks stats', 'disks', md, 'md.stats', 'stacked'],
35                                         'lines': [[''.join([md, '_total']), 'total', 'absolute'],
36                                                   [''.join([md, '_inuse']), 'inuse', 'absolute']]}
37             self.info('Plugin was started successfully. MDs to monitor %s' % (md_list))
38
39             return True
40
41     def _get_raw_data(self):
42         """
43         Read data from /proc/mdstat
44         :return: str
45         """
46         try:
47             with open(self.proc_mdstat, 'rt') as proc_mdstat:
48                 raw_result = proc_mdstat.read()
49         except Exception:
50             return None
51         else:
52             raw_result = ' '.join(raw_result.split())
53             return raw_result
54
55     def _get_data(self):
56         """
57         Parse data from _get_raw_data()
58         :return: dict
59         """
60         raw_mdstat = self._get_raw_data()
61         mdstat = self.regex_disks.findall(raw_mdstat)
62         to_netdata = {}
63
64         for md in mdstat:
65             to_netdata[''.join([md[0], '_total'])] = int(md[1])
66             to_netdata[''.join([md[0], '_inuse'])] = int(md[2])
67             to_netdata[''.join([md[0], '_health'])] = int(md[1]) - int(md[2])
68
69         return to_netdata