summaryrefslogtreecommitdiffstats
path: root/collectors/python.d.plugin/monit/monit.chart.py
blob: 3ac0032c569117d6fc34c9b7685dd846920d9f73 (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
# -*- coding: utf-8 -*-
# Description: monit netdata python.d module
# Author: Evgeniy K. (n0guest)
# SPDX-License-Identifier: GPL-3.0-or-later

import xml.etree.ElementTree as ET
from bases.FrameworkServices.UrlService import UrlService


# see enum State_Type from monit.h (https://bitbucket.org/tildeslash/monit/src/master/src/monit.h)
MONIT_SERVICE_NAMES = [
    'Filesystem',
    'Directory',
    'File',
    'Process',
    'Host',
    'System',
    'Fifo',
    'Program',
    'Net',
]

DEFAULT_SERVICES_IDS = [0, 1, 2, 3, 4, 6, 7, 8]

# charts order (can be overridden if you want less charts, or different order)
ORDER = [
    'filesystem',
    'directory',
    'file',
    'process',
    'process_uptime',
    'process_threads',
    'process_children',
    'host',
    'host_latency',
    'system',
    'fifo',
    'program',
    'net'
]
CHARTS = {
    'filesystem': {
        'options': ['filesystems', 'Filesystems', 'filesystems', 'filesystem', 'monit.filesystems', 'line'],
        'lines': []
    },
    'directory': {
        'options': ['directories', 'Directories', 'directories', 'filesystem', 'monit.directories', 'line'],
        'lines': []
    },
    'file': {
        'options': ['files', 'Files', 'files', 'filesystem', 'monit.files', 'line'],
        'lines': []
    },
    'fifo': {
        'options': ['fifos', 'Pipes (fifo)', 'pipes', 'filesystem', 'monit.fifos', 'line'],
        'lines': []
    },
    'program': {
        'options': ['programs', 'Programs statuses', 'programs', 'applications', 'monit.programs', 'line'],
        'lines': []
    },
    'process': {
        'options': ['processes', 'Processes statuses', 'processes', 'applications', 'monit.services', 'line'],
        'lines': []
    },
    'process_uptime': {
        'options': ['processes uptime', 'Processes uptime', 'seconds', 'applications',
                    'monit.process_uptime', 'line', 'hidden'],
        'lines': []
    },
    'process_threads': {
        'options': ['processes threads', 'Processes threads', 'threads', 'applications',
                    'monit.process_threads', 'line'],
        'lines': []
    },
    'process_children': {
        'options': ['processes childrens', 'Child processes', 'childrens', 'applications',
                    'monit.process_childrens', 'line'],
        'lines': []
    },
    'host': {
        'options': ['hosts', 'Hosts', 'hosts', 'network', 'monit.hosts', 'line'],
        'lines': []
    },
    'host_latency': {
        'options': ['hosts latency', 'Hosts latency', 'milliseconds/s', 'network', 'monit.host_latency', 'line'],
        'lines': []
    },
    'net': {
        'options': ['interfaces', 'Network interfaces and addresses', 'interfaces', 'network',
                    'monit.networks', 'line'],
        'lines': []
    },
}


class Service(UrlService):
    def __init__(self, configuration=None, name=None):
        UrlService.__init__(self, configuration=configuration, name=name)
        self.order = ORDER
        self.definitions = CHARTS
        base_url = self.configuration.get('url', 'http://localhost:2812')
        self.url = '{0}/_status?format=xml&level=full'.format(base_url)

    def parse(self, data):
        try:
            xml = ET.fromstring(data)
        except ET.ParseError:
            self.error("URL {0} didn't return a vaild XML page. Please check your settings.".format(self.url))
            return None
        return xml

    def check(self):
        self._manager = self._build_manager()

        raw_data = self._get_raw_data()
        if not raw_data:
            return None

        return bool(self.parse(raw_data))

    def _get_data(self):
        raw_data = self._get_raw_data()

        if not raw_data:
            return None

        xml = self.parse(raw_data)
        if not xml:
            return None

        data = {}
        for service_id in DEFAULT_SERVICES_IDS:
            service_category = MONIT_SERVICE_NAMES[service_id].lower()

            if service_category == 'system':
                self.debug("Skipping service from 'System' category, because it's useless in graphs")
                continue

            xpath_query = "./service[@type='{0}']".format(service_id)
            self.debug('Searching for {0} as {1}'.format(service_category, xpath_query))
            for service_node in xml.findall(xpath_query):

                service_name = service_node.find('name').text
                service_status = service_node.find('status').text
                service_monitoring = service_node.find('monitor').text
                self.debug('=> found {0} with type={1}, status={2}, monitoring={3}'.format(service_name,
                           service_id, service_status, service_monitoring))

                dimension_key = service_category + '_' + service_name
                if dimension_key not in self.charts[service_category]:
                    self.charts[service_category].add_dimension([dimension_key, service_name, 'absolute'])
                data[dimension_key] = 1 if service_status == '0' and service_monitoring == '1' else 0

                if service_category == 'process':
                    for subnode in ('uptime', 'threads', 'children'):
                        subnode_value = service_node.find(subnode)
                        if subnode_value is None:
                            continue
                        if subnode == 'uptime' and int(subnode_value.text) < 0:
                            self.debug('Skipping bugged metrics with negative uptime (monit before v5.16')
                            continue
                        dimension_key = 'process_{0}_{1}'.format(subnode, service_name)
                        if dimension_key not in self.charts['process_' + subnode]:
                            self.charts['process_' + subnode].add_dimension([dimension_key, service_name, 'absolute'])
                        data[dimension_key] = int(subnode_value.text)

                if service_category == 'host':
                    subnode_value = service_node.find('./icmp/responsetime')
                    if subnode_value is None:
                        continue
                    dimension_key = 'host_latency_{0}'.format(service_name)
                    if dimension_key not in self.charts['host_latency']:
                        self.charts['host_latency'].add_dimension([dimension_key, service_name,
                                                                   'absolute', 1000, 1000000])
                    data[dimension_key] = float(subnode_value.text) * 1000000

        return data or None