]> arthur.barton.de Git - netdata.git/blob - python.d/tomcat.chart.py
Merge remote-tracking branch 'upstream/master' into health
[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, "Requests", "requests/s", "statistics", "tomcat.accesses", "area"],
22         'lines': [
23             ["accesses", None, 'incremental']
24         ]},
25     'volume': {
26         'options': [None, "Volume", "KB/s", "volume", "tomcat.volume", "area"],
27         'lines': [
28             ["volume", None, 'incremental', 1, 1024]
29         ]},
30     'threads': {
31         'options': [None, "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", 1, 1048576]
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         test = self._get_data()
70         if test is None or len(test) == 0:
71             return False
72         else:
73             return True
74
75     def _get_data(self):
76         """
77         Format data received from http request
78         :return: dict
79         """
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))