]> arthur.barton.de Git - netdata.git/commitdiff
rewrite `apache`, `nginx` and `phpfpm` plugins to use wrappers on chart creation
authorpaulfantom <paulfantom@gmail.com>
Fri, 24 Jun 2016 09:22:44 +0000 (11:22 +0200)
committerpaulfantom <paulfantom@gmail.com>
Fri, 24 Jun 2016 09:22:44 +0000 (11:22 +0200)
python.d/apache.chart.py
python.d/nginx.chart.py
python.d/phpfpm.chart.py
python.d/python_modules/base.py

index f61242ecce16c47a811ff5c1b52981d995a8096f..e40d388ed83ce2c37f314744d6c3749b76094dc2 100644 (file)
@@ -22,58 +22,47 @@ ORDER = ['requests', 'connections', 'conns_async', 'net', 'workers', 'reqpersec'
 
 CHARTS = {
     'bytesperreq': {
-        'options': "'' 'apache Lifetime Avg. Response Size' 'bytes/request' statistics apache.bytesperreq area",
+        'options': [None, 'apache Lifetime Avg. Response Size', 'bytes/request', 'statistics', 'apache.bytesperreq', 'area'],
         'lines': [
-            {"name": "size_req",
-             "options": "'' absolute 1 1"}
+            ["size_req"]
         ]},
     'workers': {
-        'options': "'' 'apache Workers' 'workers' workers apache.workers stacked",
+        'options': [None, 'apache Workers', 'workers', 'workers', 'apache.workers', 'stacked'],
         'lines': [
-            {"name": "idle",
-             "options": "'' absolute 1 1"},
-            {"name": "busy",
-             "options": "'' absolute 1 1"}
+            ["idle"],
+            ["busy"]
         ]},
     'reqpersec': {
-        'options': "'' 'apache Lifetime Avg. Requests/s' 'requests/s' statistics apache.reqpersec area",
+        'options': [None, 'apache Lifetime Avg. Requests/s', 'requests/s', 'statistics', 'apache.reqpersec', 'area'],
         'lines': [
-            {"name": "requests_sec",
-             "options": "'' absolute 1 1"}
+            ["requests_sec"]
         ]},
     'bytespersec': {
-        'options': "'' 'apache Lifetime Avg. Bandwidth/s' 'kilobytes/s' statistics apache.bytesperreq area",
+        'options': [None, 'apache Lifetime Avg. Bandwidth/s', 'kilobytes/s', 'statistics', 'apache.bytesperreq', 'area'],
         'lines': [
-            {"name": "size_sec",
-             "options": "'' absolute 1 1000"}
+            ["size_sec", None, 'absolute', 1, 1000]
         ]},
     'requests': {
-        'options': "'' 'apache Requests' 'requests/s' requests apache.requests line",
+        'options': [None, 'apache Requests', 'requests/s', 'requests', 'apache.requests', 'line'],
         'lines': [
-            {"name": "requests",
-             "options": "'' incremental 1 1"}
+            ["requests", None, 'incremental']
         ]},
     'net': {
-        'options': "'' 'apache Bandwidth' 'kilobytes/s' bandwidth apache.net area",
+        'options': [None, 'apache Bandwidth', 'kilobytes/s', 'bandwidth', 'apache.net', 'area'],
         'lines': [
-            {"name": "sent",
-             "options": "'' incremental 1 1"}
+            ["sent", None, 'incremental']
         ]},
     'connections': {
-        'options': "'' 'apache Connections' 'connections' connections apache.connections line",
+        'options': [None, 'apache Connections', 'connections', 'connections', 'apache.connections', 'line'],
         'lines': [
-            {"name": "connections",
-             "options": "'' absolute 1 1"}
+            ["connections"]
         ]},
     'conns_async': {
-        'options': "'' 'apache Async Connections' 'connections' connections apache.conns_async stacked",
+        'options': [None, 'apache Async Connections', 'connections', 'connections', 'apache.conns_async', 'stacked'],
         'lines': [
-            {"name": "keepalive",
-             "options": "'' absolute 1 1"},
-            {"name": "closing",
-             "options": "'' absolute 1 1"},
-            {"name": "writing",
-             "options": "'' absolute 1 1"}
+            ["keepalive"],
+            ["closing"],
+            ["writing"]
         ]}
 }
 
@@ -82,9 +71,9 @@ class Service(UrlService):
     def __init__(self, configuration=None, name=None):
         UrlService.__init__(self, configuration=configuration, name=name)
         if len(self.url) == 0:
-            self.url = "http://localhost/server-status?auto"
+            self.url = "http://www.apache.org/server-status?auto"
         self.order = ORDER
-        self.charts = CHARTS
+        self.definitions = CHARTS
         self.assignment = {"BytesPerReq": 'size_req',
                            "IdleWorkers": 'idle',
                            "BusyWorkers": 'busy',
index 89752f565d6b508bb004fe0437fd36b055c6c30b..94d78d5ade6579ee0924166cdb845dd581e9b364 100644 (file)
@@ -22,34 +22,27 @@ ORDER = ['connections', 'requests', 'connection_status', 'connect_rate']
 
 CHARTS = {
     'connections': {
-        'options': "'' 'nginx Active Connections' 'connections' nginx nginx.connections line",
+        'options': [None, 'nginx Active Connections', 'connections', 'nginx', 'nginx.connections', 'line'],
         'lines': [
-            {"name": "active",
-             "options": "'' absolute 1 1"}
+            ["active"]
         ]},
     'requests': {
-        'options': "'' 'nginx Requests' 'requests/s' nginx nginx.requests line",
+        'options': [None, 'nginx Requests', 'requests/s', 'nginx', 'nginx.requests', 'line'],
         'lines': [
-            {"name": "requests",
-             "options": "'' incremental 1 1"}
+            ["requests", None, 'incremental']
         ]},
     'connection_status': {
-        'options': "'' 'nginx Active Connections by Status' 'connections' nginx nginx.connection.status line",
+        'options': [None, 'nginx Active Connections by Status', 'connections', 'nginx', 'nginx.connection.status', 'line'],
         'lines': [
-            {"name": "reading",
-             "options": "'' absolute 1 1"},
-            {"name": "writing",
-             "options": "'' absolute 1 1"},
-            {"name": "waiting",
-             "options": "idle absolute 1 1"}
+            ["reading"],
+            ["writing"],
+            ["waiting", "idle"]
         ]},
     'connect_rate': {
-        'options': "'' 'nginx Connections Rate' 'connections/s' nginx nginx.performance line",
+        'options': [None, 'nginx Connections Rate', 'connections/s', 'nginx', 'nginx.performance', 'line'],
         'lines': [
-            {"name": "accepts",
-             "options": "accepted incremental 1 1"},
-            {"name": "handled",
-             "options": "'' incremental 1 1"}
+            ["accepts", "accepted", "incremental"],
+            ["handled", None, "incremental"]
         ]}
 }
 
@@ -60,7 +53,7 @@ class Service(UrlService):
         if len(self.url) == 0:
             self.url = "http://localhost/stub_status"
         self.order = ORDER
-        self.charts = CHARTS
+        self.definitions = CHARTS
 
     def _formatted_data(self):
         """
index 8fadf2beefa2ce8b5ed8b8abce19bef4b587f3de..00f6336175a0fff2e53fbef77bec1a2a558f1102 100755 (executable)
@@ -22,29 +22,22 @@ ORDER = ['connections', 'requests', 'performance']
 
 CHARTS = {
     'connections': {
-        'options': "'' 'PHP-FPM Active Connections' 'connections' phpfpm phpfpm.connections line",
+        'options': [None, 'PHP-FPM Active Connections', 'connections', 'phpfpm', 'phpfpm.connections', 'line'],
         'lines': [
-            {"name": "active",
-             "options": "'' absolute 1 1"},
-            {"name": "maxActive",
-             "options": "'max active' absolute 1 1"},
-            {"name": "idle",
-             "options": "'' absolute 1 1"}
+            ["active"],
+            ["maxActive", 'max active'],
+            ["idle"]
         ]},
     'requests': {
-        'options': "'' 'PHP-FPM Requests' 'requests/s' phpfpm phpfpm.requests line",
+        'options': [None, 'PHP-FPM Requests', 'requests/s', 'phpfpm', 'phpfpm.requests', 'line'],
         'lines': [
-            {"name": "requests",
-             "options": "'' incremental 1 1"}
+            ["requests", None, "incremental"]
         ]},
     'performance': {
-
-        'options': "'' 'PHP-FPM Performance' 'status' phpfpm phpfpm.performance line",
+        'options': [None, 'PHP-FPM Performance', 'status', 'phpfpm', 'phpfpm.performance', 'line'],
         'lines': [
-            {"name": "reached",
-             "options": "'max children reached' absolute 1 1"},
-            {"name": "slow",
-             "options": "'slow requests' absolute 1 1"}
+            ["reached", 'max children reached'],
+            ["slow", 'slow requests']
         ]}
 }
 
@@ -55,7 +48,7 @@ class Service(UrlService):
         if len(self.url) == 0:
             self.url = "http://localhost/status"
         self.order = ORDER
-        self.charts = CHARTS
+        self.definitions = CHARTS
         self.assignment = {"active processes": 'active',
                            "max active processes": 'maxActive',
                            "idle processes": 'idle',
index 1183c42886ed1a85cceeaf99decaa4cc106831e4..ab9b3febf287a94db698f0427f52d395d6aefd4a 100644 (file)
@@ -25,7 +25,7 @@ class BaseService(threading.Thread):
         :param name: str
         """
         threading.Thread.__init__(self)
-        self.data_stream = ""
+        self._data_stream = ""
         self.daemon = True
         self.retries = 0
         self.retries_left = 0
@@ -34,8 +34,8 @@ class BaseService(threading.Thread):
         self.name = name
         self.override_name = None
         self.chart_name = ""
-        self.dimensions = []
-        self.charts = []
+        self._dimensions = []
+        self._charts = []
         if configuration is None:
             self.error("BaseService: no configuration parameters supplied. Cannot create Service.")
             raise RuntimeError
@@ -142,15 +142,18 @@ class BaseService(threading.Thread):
         Converts *params to string and joins them with one space between every one.
         :param params: str/int/float
         """
-        self.data_stream += instruction
+        self._data_stream += instruction
         for p in params:
-            p = str(p)
+            if p is None:
+                p = ""
+            else:
+                p = str(p)
             if len(p) == 0:
                 p = "''"
             if ' ' in p:
                 p = "'" + p + "'"
-            self.data_stream += " " + p
-        self.data_stream += "\n"
+            self._data_stream += " " + p
+        self._data_stream += "\n"
 
     def chart(self, type_id, name="", title="", units="", family="",
               category="", charttype="line", priority="", update_every=""):
@@ -166,8 +169,7 @@ class BaseService(threading.Thread):
         :param priority: int/str
         :param update_every: int/str
         """
-        self.charts.append(type_id)
-
+        self._charts.append(type_id)
         self._line("CHART", type_id, name, title, units, family, category, charttype, priority, update_every)
 
     def dimension(self, id, name=None, algorithm="absolute", multiplier=1, divisor=1, hidden=False):
@@ -196,7 +198,7 @@ class BaseService(threading.Thread):
         if algorithm not in ("absolute", "incremental", "percentage-of-absolute-row", "percentage-of-incremental-row"):
             algorithm = "absolute"
 
-        self.dimensions.append(id)
+        self._dimensions.append(id)
         if hidden:
             self._line("DIMENSION", id, name, algorithm, multiplier, divisor, "hidden")
         else:
@@ -209,7 +211,7 @@ class BaseService(threading.Thread):
         :param microseconds: int
         :return: boolean
         """
-        if type_id not in self.charts:
+        if type_id not in self._charts:
             self.error("wrong chart type_id:", type_id)
             return False
         try:
@@ -228,7 +230,7 @@ class BaseService(threading.Thread):
         :param value: int/float
         :return: boolean
         """
-        if id not in self.dimensions:
+        if id not in self._dimensions:
             self.error("wrong dimension id:", id)
             return False
         try:
@@ -246,8 +248,8 @@ class BaseService(threading.Thread):
         """
         Upload new data to netdata
         """
-        print(self.data_stream)
-        self.data_stream = ""
+        print(self._data_stream)
+        self._data_stream = ""
 
     def error(self, *params):
         """
@@ -364,40 +366,21 @@ class UrlService(BaseService):
         Create charts
         :return: boolean
         """
-        for name in self.order:
-            if name not in self.charts:
-                continue
-            self.definitions[name] = []
-            for line in self.charts[name]['lines']:
-                self.definitions[name].append(line['name'])
-
-        idx = 0
         data = self._formatted_data()
         if data is None:
             return False
-        data_stream = ""
+
+        idx = 0
         for name in self.order:
-            header = "CHART " + \
-                     self.__module__ + "_" + \
-                     self.name + "." + \
-                     name + " " + \
-                     self.charts[name]['options'] + " " + \
-                     str(self.priority + idx) + " " + \
-                     str(self.update_every)
-            content = ""
+            options = self.definitions[name]['options'] + [self.priority + idx, self.update_every]
+            self.chart(self.__module__ + "_" + self.name + "." + name, *options)
             # check if server has this datapoint
-            for line in self.charts[name]['lines']:
-                if line['name'] in data:
-                    content += "\nDIMENSION " + line['name'] + " " + line['options']
-
-            if len(content) > 0:
-                data_stream += header + content + "\n"
-                idx += 1
-
-        print(data_stream)
+            for line in self.definitions[name]['lines']:
+                if line[0] in data:
+                    self.dimension(*line)
+            idx += 1
 
-        if idx == 0:
-            return False
+        self.commit()
         return True
 
     def update(self, interval):
@@ -410,17 +393,17 @@ class UrlService(BaseService):
         if data is None:
             return False
 
-        data_stream = ""
-        for chart, dimensions in self.definitions.items():
-            header = "BEGIN " + self.__module__ + "_" + str(self.name) + "." + chart + " " + str(interval)
-            c = ""
-            for dim in dimensions:
-                try:
-                    c += "\nSET " + dim + " = " + str(data[dim])
-                except KeyError:
-                    pass
-            if len(c) != 0:
-                data_stream += header + c + "\nEND\n"
-        print(data_stream)
+        updated = False
+        for chart in self.order:
+            if self.begin(self.__module__ + "_" + str(self.name) + "." + chart, interval):
+                updated = True
+                for dim in self.definitions[chart]['lines']:
+                    try:
+                        self.set(dim[0], data[dim[0]])
+                    except KeyError:
+                        pass
+                self.end()
 
-        return True
+        self.commit()
+
+        return updated