@cryptotaxi247 / netdata-1 / commits / 80b0db674

tomcat plugin: replace xml parse with regex

Ilya committed Jan 23, 2017 at 17:40 UTC 80b0db6744f5155c309f59741919f83ec003f832
1 file changed +16 -63
python.d/tomcat.chart.py
+16 -63
@@ -2,11 +2,8 @@
2 # Description: tomcat netdata python.d module
3 # Author: Pawel Krupa (paulfantom)
4
5 -# Python version higher than 2.7 is needed to run this module.
6 -
5 from base import UrlService
8 -import xml.etree.ElementTree as ET # phone home...
9 -#from xml.parsers.expat import errors
6 +from re import compile
7
8 # default module values (can be overridden per job in `config`)
9 # update_every = 2
@@ -20,23 +17,23 @@ CHARTS = {
17 'accesses': {
18 'options': [None, "Requests", "requests/s", "statistics", "tomcat.accesses", "area"],
19 'lines': [
23 - ["accesses", None, 'incremental']
20 + ["requestCount", 'accesses', 'incremental']
21 ]},
22 'volume': {
23 'options': [None, "Volume", "KB/s", "volume", "tomcat.volume", "area"],
24 'lines': [
28 - ["volume", None, 'incremental', 1, 1024]
25 + ["bytesSent", 'volume', 'incremental', 1, 1024]
26 ]},
27 'threads': {
28 'options': [None, "Threads", "current threads", "statistics", "tomcat.threads", "line"],
29 'lines': [
33 - ["current", None, "absolute"],
34 - ["busy", None, "absolute"]
30 + ["currentThreadCount", 'current', "absolute"],
31 + ["currentThreadsBusy", 'busy', "absolute"]
32 ]},
33 'jvm': {
34 'options': [None, "JVM Free Memory", "MB", "statistics", "tomcat.jvm", "area"],
35 'lines': [
39 - ["jvm", None, "absolute", 1, 1048576]
36 + ["free", None, "absolute", 1, 1048576]
37 ]}
38 }
39
@@ -44,68 +41,24 @@ CHARTS = {
41 class Service(UrlService):
42 def __init__(self, configuration=None, name=None):
43 UrlService.__init__(self, configuration=configuration, name=name)
47 - if len(self.url) == 0:
48 - self.url = "http://localhost:8080/manager/status?XML=true"
44 + self.url = self.configuration.get('url', "http://127.0.0.1:8080/manager/status?XML=true")
45 self.order = ORDER
46 self.definitions = CHARTS
51 - self.port = 8080
47 + self.regex = compile(r'([\w]+)=\\?[\'\"](\d+)\\?[\'\"]')
48
49 def check(self):
54 - if UrlService.check(self):
55 - return True
56 -
57 - # get port from url
58 - self.port = 0
59 - for i in self.url.split('/'):
60 - try:
61 - int(i[-1])
62 - self.port = i.split(':')[-1]
63 - break
64 - except:
65 - pass
66 - if self.port == 0:
67 - self.port = 80
68 -
69 - test = self._get_data()
70 - if test is None or len(test) == 0:
50 + if not self.url.endswith('manager/status?XML=true'):
51 + self.error('Bad url(%s). Must be http://<ip.address>:<port>/manager/status?XML=true' % self.url)
52 return False
72 - else:
73 - return True
53 +
54 + return UrlService.check(self)
55
56 def _get_data(self):
57 """
58 Format data received from http request
59 :return: dict
60 """
80 - try:
81 - raw = self._get_raw_data()
82 - try:
83 - data = ET.fromstring(raw)
84 - except ET.ParseError as e:
85 - # if e.code == errors.codes[errors.XML_ERROR_JUNK_AFTER_DOC_ELEMENT]:
86 - if e.code == 9:
87 - end = raw.find('</status>')
88 - end += 9
89 - raw = raw[:end]
90 - self.debug(raw)
91 - data = ET.fromstring(raw)
92 - else:
93 - raise Exception(e)
94 -
95 - memory = data.find('./jvm/memory')
96 - threads = data.find("./connector[@name='\"http-bio-" + str(self.port) + "\"']/threadInfo")
97 - requests = data.find("./connector[@name='\"http-bio-" + str(self.port) + "\"']/requestInfo")
98 -
99 - return {'accesses': requests.attrib['requestCount'],
100 - 'volume': requests.attrib['bytesSent'],
101 - 'current': threads.attrib['currentThreadCount'],
102 - 'busy': threads.attrib['currentThreadsBusy'],
103 - 'jvm': memory.attrib['free']}
104 - except (ValueError, AttributeError) as e:
105 - self.debug(str(e))
106 - return None
107 - except SyntaxError as e:
108 - self.error("Tomcat module needs python 2.7 at least. Stopping")
109 - self.debug(str(e))
110 - except Exception as e:
111 - self.debug(str(e))
61 + data = self._get_raw_data()
62 + if data: data = dict(self.regex.findall(data))
63 +
64 + return data or None