small changes requested at PR#4027
Evgeniy Kostenko committed
Aug 2, 2018 at 07:50 UTC
5101bf0531f1b5e3c74b2a2141e6d6ba7edf797d
1 file changed
+51
-51
python.d/monit.chart.py
+51
-51
@@ -11,6 +11,7 @@ from bases.FrameworkServices.UrlService import UrlService
11
priority = 60000
12
retries = 60
13
14
+# see enum State_Type from monit.h (https://bitbucket.org/tildeslash/monit/src/master/src/monit.h)
15
MONIT_SERVICE_NAMES = [ 'Filesystem', 'Directory', 'File', 'Process', 'Host', 'System', 'Fifo', 'Program', 'Net' ]
16
DEFAULT_SERVICES_IDS = [ 0, 1, 2, 3, 4, 6, 7, 8 ]
17
@@ -82,65 +83,64 @@ class Service(UrlService):
83
def check(self):
84
self._manager = self._build_manager()
85
raw_data = self._get_raw_data()
85
- if raw_data:
86
- try:
87
- xml = ET.fromstring(raw_data)
88
- except ET.ParseError:
89
- self.debug('%s is not a vaild XML page. Please add "_status?format=xml&level=full" to monit URL.' % self.url)
90
- return None
86
+ if not raw_data:
87
+ return None
88
+ try:
89
+ xml = ET.fromstring(raw_data)
90
return True
92
- return None
91
+ except ET.ParseError:
92
+ self.debug('%s is not a vaild XML page. Please add "_status?format=xml&level=full" to monit URL.' % self.url)
93
+ return None
94
95
def _get_data(self):
95
- data = None
96
raw_data = self._get_raw_data()
97
- if raw_data:
98
- try:
99
- xml = ET.fromstring(raw_data)
100
- except ET.ParseError:
101
- self.debug('%s is not a vaild XML page. Please add "_status?format=xml&level=full" to monit URL.' % self.url)
102
- return None
103
- data = {}
97
+ if not raw_data:
98
+ return None
99
105
- for svc_id in DEFAULT_SERVICES_IDS:
106
- if svc_id not in DEFAULT_SERVICES_IDS:
107
- continue
100
+ try:
101
+ xml = ET.fromstring(raw_data)
102
+ except ET.ParseError:
103
+ self.debug('%s is not a vaild XML page. Please add "_status?format=xml&level=full" to monit URL.' % self.url)
104
+ return None
105
109
- svc_category = MONIT_SERVICE_NAMES[svc_id].lower()
110
- if svc_category == 'system':
111
- self.debug("Skipping service from 'System' category, because it's useless in graphs")
112
- continue
106
+ data = {}
107
+ for svc_id in DEFAULT_SERVICES_IDS:
108
+ svc_category = MONIT_SERVICE_NAMES[svc_id].lower()
109
+ if svc_category == 'system':
110
+ self.debug("Skipping service from 'System' category, because it's useless in graphs")
111
+ continue
112
114
- xpath_query = "./service[@type='%d']" % (svc_id)
115
- self.debug("Searching for %s as %s" % (svc_category, xpath_query))
116
- for svc in xml.findall(xpath_query):
117
- svc_name = svc.find('name').text
118
- svc_status = svc.find('status').text
119
- svc_monitor = svc.find('monitor').text
120
- self.debug('=> found %s with type=%s, status=%s, monitoring=%s' % (svc_name, svc_id, svc_status, svc_monitor))
113
+ xpath_query = "./service[@type='%d']" % (svc_id)
114
+ self.debug("Searching for %s as %s" % (svc_category, xpath_query))
115
+ for svc in xml.findall(xpath_query):
116
+ svc_name = svc.find('name').text
117
+ svc_status = svc.find('status').text
118
+ svc_monitor = svc.find('monitor').text
119
+ self.debug('=> found %s with type=%s, status=%s, monitoring=%s' % (svc_name, svc_id, svc_status, svc_monitor))
120
122
- dimension_key = svc_category + '_' + svc_name
123
- if dimension_key not in self.charts[svc_category]:
124
- self.charts[svc_category].add_dimension([dimension_key, svc_name, 'absolute'])
125
- data[dimension_key] = 1 if svc_status == "0" and svc_monitor == "1" else 0
121
+ dimension_key = svc_category + '_' + svc_name
122
+ if dimension_key not in self.charts[svc_category]:
123
+ self.charts[svc_category].add_dimension([dimension_key, svc_name, 'absolute'])
124
+ data[dimension_key] = 1 if svc_status == "0" and svc_monitor == "1" else 0
125
127
- if svc_category == 'process':
128
- for node in ('uptime', 'threads', 'children'):
129
- node_value = svc.find(node)
130
- if node_value != None:
131
- if node == 'uptime' and int(node_value.text) < 0:
132
- self.debug('Skipping bugged metrics with negative uptime')
133
- continue
134
- dimension_key = 'process_%s_%s' % (node, svc_name)
135
- if dimension_key not in self.charts['process_' + node]:
136
- self.charts['process_' + node].add_dimension([dimension_key, svc_name, 'absolute'])
137
- data[dimension_key] = int(node_value.text)
138
-
139
- if svc_category == 'host':
140
- node_value = svc.find('./icmp/responsetime')
126
+ if svc_category == 'process':
127
+ for node in ('uptime', 'threads', 'children'):
128
+ node_value = svc.find(node)
129
if node_value != None:
142
- dimension_key = 'host_latency_%s' % (svc_name)
143
- if dimension_key not in self.charts['host_latency']:
144
- self.charts['host_latency'].add_dimension([dimension_key, svc_name, 'absolute', 1000, 1000000])
145
- data[dimension_key] = float(node_value.text) * 1000000
130
+ if node == 'uptime' and int(node_value.text) < 0:
131
+ self.debug('Skipping bugged metrics with negative uptime')
132
+ continue
133
+ dimension_key = 'process_%s_%s' % (node, svc_name)
134
+ if dimension_key not in self.charts['process_' + node]:
135
+ self.charts['process_' + node].add_dimension([dimension_key, svc_name, 'absolute'])
136
+ data[dimension_key] = int(node_value.text)
137
+
138
+ if svc_category == 'host':
139
+ node_value = svc.find('./icmp/responsetime')
140
+ if node_value != None:
141
+ dimension_key = 'host_latency_%s' % (svc_name)
142
+ if dimension_key not in self.charts['host_latency']:
143
+ self.charts['host_latency'].add_dimension([dimension_key, svc_name, 'absolute', 1000, 1000000])
144
+ data[dimension_key] = float(node_value.text) * 1000000
145
+
146
return data or None