]> arthur.barton.de Git - netdata.git/blob - python.d/tomcat.chart.py
Merge pull request #757 from ktsaou/master
[netdata.git] / python.d / tomcat.chart.py
1 # -*- coding: utf-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
7 from base import UrlService
8 import xml.etree.ElementTree as ET  # phone home...
9 #from xml.parsers.expat import errors
10
11 # default module values (can be overridden per job in `config`)
12 # update_every = 2
13 priority = 60000
14 retries = 60
15
16 # charts order (can be overridden if you want less charts, or different order)
17 ORDER = ['accesses', 'volume', 'threads', 'jvm']
18
19 CHARTS = {
20     'accesses': {
21         'options': [None, "tomcat requests", "requests/s", "statistics", "tomcat.accesses", "area"],
22         'lines': [
23             ["accesses"]
24         ]},
25     'volume': {
26         'options': [None, "tomcat volume", "KB/s", "volume", "tomcat.volume", "area"],
27         'lines': [
28             ["volume", None, 'incremental']
29         ]},
30     'threads': {
31         'options': [None, "tomcat threads", "current threads", "statistics", "tomcat.threads", "line"],
32         'lines': [
33             ["current", None, "absolute"],
34             ["busy", None, "absolute"]
35         ]},
36     'jvm': {
37         'options': [None, "JVM Free Memory", "MB", "statistics", "tomcat.jvm", "area"],
38         'lines': [
39             ["jvm", None, "absolute"]
40         ]}
41 }
42
43
44 class Service(UrlService):
45     def __init__(self, configuration=None, name=None):
46         UrlService.__init__(self, configuration=configuration, name=name)
47         if len(self.url) == 0:
48             self.url = "http://localhost:8080/manager/status?XML=true"
49         self.order = ORDER
50         self.definitions = CHARTS
51         self.port = 8080
52
53     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         if self._get_data() is None or len(self._get_data()) == 0:
70             return False
71         else:
72             return True
73
74     def _get_data(self):
75         """
76         Format data received from http request
77         :return: dict
78         """
79         try:
80             raw = self._get_raw_data()
81             try:
82                 data = ET.fromstring(raw)
83             except ET.ParseError as e:
84                 #if e.code == errors.codes[errors.XML_ERROR_JUNK_AFTER_DOC_ELEMENT]:
85                 if e.code == 9:
86                     # cut rest of invalid string
87                     pos = 0
88                     for i in range(e.position[0] - 1):
89                         pos += raw.find('\n', pos)
90                     raw = raw[:47604 + pos + e.position[0] - 1]
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))