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