]> arthur.barton.de Git - netdata.git/blob - python.d/tomcat.chart.py
154e4e4ba69ca8ea766c861eabc9a1491ce3a025
[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 from base import UrlService
6 from re import compile
7
8 try:
9     from urlparse import urlparse
10 except ImportError:
11     from urllib.parse import urlparse
12
13 # default module values (can be overridden per job in `config`)
14 # update_every = 2
15 priority = 60000
16 retries = 60
17
18 # charts order (can be overridden if you want less charts, or different order)
19 ORDER = ['accesses', 'volume', 'threads', 'jvm']
20
21 CHARTS = {
22     'accesses': {
23         'options': [None, "Requests", "requests/s", "statistics", "tomcat.accesses", "area"],
24         'lines': [
25             ["requestCount", 'accesses', 'incremental']
26         ]},
27     'volume': {
28         'options': [None, "Volume", "KB/s", "volume", "tomcat.volume", "area"],
29         'lines': [
30             ["bytesSent", 'volume', 'incremental', 1, 1024]
31         ]},
32     'threads': {
33         'options': [None, "Threads", "current threads", "statistics", "tomcat.threads", "line"],
34         'lines': [
35             ["currentThreadCount", 'current', "absolute"],
36             ["currentThreadsBusy", 'busy', "absolute"]
37         ]},
38     'jvm': {
39         'options': [None, "JVM Free Memory", "MB", "statistics", "tomcat.jvm", "area"],
40         'lines': [
41             ["free", None, "absolute", 1, 1048576]
42         ]}
43 }
44
45
46 class Service(UrlService):
47     def __init__(self, configuration=None, name=None):
48         UrlService.__init__(self, configuration=configuration, name=name)
49         self.url = self.configuration.get('url', "http://127.0.0.1:8080/manager/status?XML=true")
50         self.order = ORDER
51         self.definitions = CHARTS
52
53     def check(self):
54         if not self.url.endswith('manager/status?XML=true'):
55             self.error('Bad url(%s). Must be http://<ip.address>:<port>/manager/status?XML=true' % self.url)
56             return False
57
58         netloc = urlparse(self.url).netloc.rpartition(':')
59         if netloc[1] == ':': port = netloc[2]
60         else: port = 80
61         
62         self.regex_jvm = compile(r'<jvm>.*?</jvm>')
63         self.regex_connector = compile(r'[a-z-]+%s.*?/connector' % port)
64         self.regex = compile(r'([\w]+)=\\?[\'\"](\d+)\\?[\'\"]')
65         
66         return UrlService.check(self)
67
68     def _get_data(self):
69         """
70         Format data received from http request
71         :return: dict
72         """
73         data = self._get_raw_data()
74         if data:
75             jvm = self.regex_jvm.findall(data) or ['']
76             connector = self.regex_connector.findall(data) or ['']
77             data = dict(self.regex.findall(''.join([jvm[0], connector[0]])))
78         
79         return data or None
80