]> arthur.barton.de Git - netdata.git/blob - python.d/tomcat.chart.py
python 2.7 notification in tomcat
[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
10 # default module values (can be overridden per job in `config`)
11 # update_every = 2
12 priority = 60000
13 retries = 60
14
15 # charts order (can be overridden if you want less charts, or different order)
16 ORDER = ['accesses', 'volume', 'threads', 'jvm']
17
18 CHARTS = {
19     'accesses': {
20         'options': [None, "tomcat requests", "requests/s", "statistics", "tomcat.accesses", "area"],
21         'lines': [
22             ["accesses"]
23         ]},
24     'volume': {
25         'options': [None, "tomcat volume", "KB/s", "volume", "tomcat.volume", "area"],
26         'lines': [
27             ["volume", None, 'incremental']
28         ]},
29     'threads': {
30         'options': [None, "tomcat threads", "current threads", "statistics", "tomcat.threads", "line"],
31         'lines': [
32             ["current", None, "absolute"],
33             ["busy", None, "absolute"]
34         ]},
35     'jvm': {
36         'options': [None, "JVM Free Memory", "MB", "statistics", "tomcat.jvm", "area"],
37         'lines': [
38             ["jvm", None, "absolute"]
39         ]}
40 }
41
42
43 class Service(UrlService):
44     def __init__(self, configuration=None, name=None):
45         UrlService.__init__(self, configuration=configuration, name=name)
46         if len(self.url) == 0:
47             self.url = "http://localhost:8080/manager/status?XML=true"
48         self.order = ORDER
49         self.definitions = CHARTS
50         # get port from url
51         self.port = 0
52         for i in self.url.split('/'):
53             try:
54                 int(i[-1])
55                 self.port = i.split(':')[-1]
56                 break
57             except:
58                 pass
59         if self.port == 0:
60             self.port = 80
61
62     def _get_data(self):
63         """
64         Format data received from http request
65         :return: dict
66         """
67         try:
68             raw = self._get_raw_data()
69             data = ET.fromstring(raw)
70             memory = data.find('./jvm/memory')
71             threads = data.find("./connector[@name='\"http-bio-" + str(self.port) + "\"']/threadInfo")
72             requests = data.find("./connector[@name='\"http-bio-" + str(self.port) + "\"']/requestInfo")
73
74             return {'accesses': requests.attrib['requestCount'],
75                     'volume': requests.attrib['bytesSent'],
76                     'current': threads.attrib['currentThreadCount'],
77                     'busy': threads.attrib['currentThreadsBusy'],
78                     'jvm': memory.attrib['free']}
79         except (ValueError, AttributeError):
80             return None
81         except SyntaxError as e:
82             self.error("Tomcat module needs python 2.7 at least. Stopping")
83             self.debug(str(e))
84         except Exception as e:
85             self.debug(str(e))