summaryrefslogtreecommitdiffstats
path: root/collectors/python.d.plugin/nginx_plus/nginx_plus.chart.py
blob: a6c035f6812ad33ce9f5746a368002f89543e4c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
# -*- coding: utf-8 -*-
# Description: nginx_plus netdata python.d module
# Author: Ilya Mashchenko (ilyam8)
# SPDX-License-Identifier: GPL-3.0-or-later

import re

from collections import defaultdict
from copy import deepcopy
from json import loads

try:
    from collections import OrderedDict
except ImportError:
    from third_party.ordereddict import OrderedDict

from bases.FrameworkServices.UrlService import UrlService

ORDER = [
    'requests_total',
    'requests_current',
    'connections_statistics',
    'connections_workers',
    'ssl_handshakes',
    'ssl_session_reuses',
    'ssl_memory_usage',
    'processes'
]

CHARTS = {
    'requests_total': {
        'options': [None, 'Requests Total', 'requests/s', 'requests', 'nginx_plus.requests_total', 'line'],
        'lines': [
            ['requests_total', 'total', 'incremental']
        ]
    },
    'requests_current': {
        'options': [None, 'Requests Current', 'requests', 'requests', 'nginx_plus.requests_current', 'line'],
        'lines': [
            ['requests_current', 'current']
        ]
    },
    'connections_statistics': {
        'options': [None, 'Connections Statistics', 'connections/s',
                    'connections', 'nginx_plus.connections_statistics', 'stacked'],
        'lines': [
            ['connections_accepted', 'accepted', 'incremental'],
            ['connections_dropped', 'dropped', 'incremental']
        ]
    },
    'connections_workers': {
        'options': [None, 'Workers Statistics', 'workers',
                    'connections', 'nginx_plus.connections_workers', 'stacked'],
        'lines': [
            ['connections_idle', 'idle'],
            ['connections_active', 'active']
        ]
    },
    'ssl_handshakes': {
        'options': [None, 'SSL Handshakes', 'handshakes/s', 'ssl', 'nginx_plus.ssl_handshakes', 'stacked'],
        'lines': [
            ['ssl_handshakes', 'successful', 'incremental'],
            ['ssl_handshakes_failed', 'failed', 'incremental']
        ]
    },
    'ssl_session_reuses': {
        'options': [None, 'Session Reuses', 'sessions/s', 'ssl', 'nginx_plus.ssl_session_reuses', 'line'],
        'lines': [
            ['ssl_session_reuses', 'reused', 'incremental']
        ]
    },
    'ssl_memory_usage': {
        'options': [None, 'Memory Usage', 'percentage', 'ssl', 'nginx_plus.ssl_memory_usage', 'area'],
        'lines': [
            ['ssl_memory_usage', 'usage', 'absolute', 1, 100]
        ]
    },
    'processes': {
        'options': [None, 'Processes', 'processes', 'processes', 'nginx_plus.processes', 'line'],
        'lines': [
            ['processes_respawned', 'respawned']
        ]
    }
}


def cache_charts(cache):
    family = 'cache {0}'.format(cache.real_name)
    charts = OrderedDict()

    charts['{0}_traffic'.format(cache.name)] = {
        'options': [None, 'Traffic', 'KiB', family, 'nginx_plus.cache_traffic', 'stacked'],
        'lines': [
            ['_'.join([cache.name, 'hit_bytes']), 'served', 'absolute', 1, 1024],
            ['_'.join([cache.name, 'miss_bytes_written']), 'written', 'absolute', 1, 1024],
            ['_'.join([cache.name, 'miss_bytes']), 'bypass', 'absolute', 1, 1024]
        ]
    }
    charts['{0}_memory_usage'.format(cache.name)] = {
        'options': [None, 'Memory Usage', 'percentage', family, 'nginx_plus.cache_memory_usage', 'area'],
        'lines': [
            ['_'.join([cache.name, 'memory_usage']), 'usage', 'absolute', 1, 100],
        ]
    }
    return charts


def web_zone_charts(wz):
    charts = OrderedDict()
    family = 'web zone {name}'.format(name=wz.real_name)

    # Processing
    charts['zone_{name}_processing'.format(name=wz.name)] = {
        'options': [None, 'Zone "{name}" Processing'.format(name=wz.name), 'requests', family,
                    'nginx_plus.web_zone_processing', 'line'],
        'lines': [
            ['_'.join([wz.name, 'processing']), 'processing']
        ]
    }
    # Requests
    charts['zone_{name}_requests'.format(name=wz.name)] = {
        'options': [None, 'Zone "{name}" Requests'.format(name=wz.name), 'requests/s', family,
                    'nginx_plus.web_zone_requests', 'line'],
        'lines': [
            ['_'.join([wz.name, 'requests']), 'requests', 'incremental']
        ]
    }
    # Response Codes
    charts['zone_{name}_responses'.format(name=wz.name)] = {
        'options': [None, 'Zone "{name}" Responses'.format(name=wz.name), 'requests/s', family,
                    'nginx_plus.web_zone_responses', 'stacked'],
        'lines': [
            ['_'.join([wz.name, 'responses_2xx']), '2xx', 'incremental'],
            ['_'.join([wz.name, 'responses_5xx']), '5xx', 'incremental'],
            ['_'.join([wz.name, 'responses_3xx']), '3xx', 'incremental'],
            ['_'.join([wz.name, 'responses_4xx']), '4xx', 'incremental'],
            ['_'.join([wz.name, 'responses_1xx']), '1xx', 'incremental']
        ]
    }
    # Traffic
    charts['zone_{name}_net'.format(name=wz.name)] = {
        'options': [None, 'Zone "{name}" Traffic'.format(name=wz.name), 'kilobits/s', family,
                    'nginx_plus.zone_net', 'area'],
        'lines': [
            ['_'.join([wz.name, 'received']), 'received', 'incremental', 1, 1000],
            ['_'.join([wz.name, 'sent']), 'sent', 'incremental', -1, 1000]
        ]
    }
    return charts


def web_upstream_charts(wu):
    def dimensions(value, a='absolute', m=1, d=1):
        dims = list()
        for p in wu:
            dims.append(['_'.join([wu.name, p.server, value]), p.real_server, a, m, d])
        return dims

    charts = OrderedDict()
    family = 'web upstream {name}'.format(name=wu.real_name)

    # Requests
    charts['web_upstream_{name}_requests'.format(name=wu.name)] = {
        'options': [None, 'Peers Requests', 'requests/s', family, 'nginx_plus.web_upstream_requests', 'line'],
        'lines': dimensions('requests', 'incremental')
    }
    # Responses Codes
    charts['web_upstream_{name}_all_responses'.format(name=wu.name)] = {
        'options': [None, 'All Peers Responses', 'responses/s', family,
                    'nginx_plus.web_upstream_all_responses', 'stacked'],
        'lines': [
            ['_'.join([wu.name, 'responses_2xx']), '2xx', 'incremental'],
            ['_'.join([wu.name, 'responses_5xx']), '5xx', 'incremental'],
            ['_'.join([wu.name, 'responses_3xx']), '3xx', 'incremental'],
            ['_'.join([wu.name, 'responses_4xx']), '4xx', 'incremental'],
            ['_'.join([wu.name, 'responses_1xx']), '1xx', 'incremental'],
        ]
    }
    for peer in wu:
        charts['web_upstream_{0}_{1}_responses'.format(wu.name, peer.server)] = {
            'options': [None, 'Peer "{0}" Responses'.format(peer.real_server), 'responses/s', family,
                        'nginx_plus.web_upstream_peer_responses', 'stacked'],
            'lines': [
                ['_'.join([wu.name, peer.server, 'responses_2xx']), '2xx', 'incremental'],
                ['_'.join([wu.name, peer.server, 'responses_5xx']), '5xx', 'incremental'],
                ['_'.join([wu.name, peer.server, 'responses_3xx']), '3xx', 'incremental'],
                ['_'.join([wu.name, peer.server, 'responses_4xx']), '4xx', 'incremental'],
                ['_'.join([wu.name, peer.server, 'responses_1xx']), '1xx', 'incremental']
            ]
        }
    # Connections
    charts['web_upstream_{name}_connections'.format(name=wu.name)] = {
        'options': [None, 'Peers Connections', 'active', family, 'nginx_plus.web_upstream_connections', 'line'],
        'lines': dimensions('active')
    }
    charts['web_upstream_{name}_connections_usage'.format(name=wu.name)] = {
        'options': [None, 'Peers Connections Usage', 'percentage', family,
                    'nginx_plus.web_upstream_connections_usage', 'line'],
        'lines': dimensions('connections_usage', d=100)
    }
    # Traffic
    charts['web_upstream_{0}_all_net'.format(wu.name)] = {
        'options': [None, 'All Peers Traffic', 'kilobits/s', family, 'nginx_plus.web_upstream_all_net', 'area'],
        'lines': [
            ['{0}_received'.format(wu.name), 'received', 'incremental', 1, 1000],
            ['{0}_sent'.format(wu.name), 'sent', 'incremental', -1, 1000]
        ]
    }
    for peer in wu:
        charts['web_upstream_{0}_{1}_net'.format(wu.name, peer.server)] = {
            'options': [None, 'Peer "{0}" Traffic'.format(peer.real_server), 'kilobits/s', family,
                        'nginx_plus.web_upstream_peer_traffic', 'area'],
            'lines': [
                ['{0}_{1}_received'.format(wu.name, peer.server), 'received', 'incremental', 1, 1000],
                ['{0}_{1}_sent'.format(wu.name, peer.server), 'sent', 'incremental', -1, 1000]
            ]
        }
    # Response Time
    for peer in wu:
        charts['web_upstream_{0}_{1}_timings'.format(wu.name, peer.server)] = {
            'options': [None, 'Peer "{0}" Timings'.format(peer.real_server), 'milliseconds', family,
                        'nginx_plus.web_upstream_peer_timings', 'line'],
            'lines': [
                ['_'.join([wu.name, peer.server, 'header_time']), 'header'],
                ['_'.join([wu.name, peer.server, 'response_time']), 'response']
            ]
        }
    # Memory Usage
    charts['web_upstream_{name}_memory_usage'.format(name=wu.name)] = {
        'options': [None, 'Memory Usage', 'percentage', family, 'nginx_plus.web_upstream_memory_usage', 'area'],
        'lines': [
            ['_'.join([wu.name, 'memory_usage']), 'usage', 'absolute', 1, 100]
        ]
    }
    # State
    charts['web_upstream_{name}_status'.format(name=wu.name)] = {
        'options': [None, 'Peers Status', 'state', family, 'nginx_plus.web_upstream_status', 'line'],
        'lines': dimensions('state')
    }
    # Downtime
    charts['web_upstream_{name}_downtime'.format(name=wu.name)] = {
        'options': [None, 'Peers Downtime', 'seconds', family, 'nginx_plus.web_upstream_peer_downtime', 'line'],
        'lines': dimensions('downtime', d=1000)
    }

    return charts


METRICS = {
    'SERVER': [
        'processes.respawned',
        'connections.accepted',
        'connections.dropped',
        'connections.active',
        'connections.idle',
        'ssl.handshakes',
        'ssl.handshakes_failed',
        'ssl.session_reuses',
        'requests.total',
        'requests.current',
        'slabs.SSL.pages.free',
        'slabs.SSL.pages.used'
    ],
    'WEB_ZONE': [
        'processing',
        'requests',
        'responses.1xx',
        'responses.2xx',
        'responses.3xx',
        'responses.4xx',
        'responses.5xx',
        'discarded',
        'received',
        'sent'
    ],
    'WEB_UPSTREAM_PEER': [
        'id',
        'server',
        'name',
        'state',
        'active',
        'max_conns',
        'requests',
        'header_time',  # alive only
        'response_time',  # alive only
        'responses.1xx',
        'responses.2xx',
        'responses.3xx',
        'responses.4xx',
        'responses.5xx',
        'sent',
        'received',
        'downtime'
    ],
    'WEB_UPSTREAM_SUMMARY': [
        'responses.1xx',
        'responses.2xx',
        'responses.3xx',
        'responses.4xx',
        'responses.5xx',
        'sent',
        'received'
    ],
    'CACHE': [
        'hit.bytes',  # served
        'miss.bytes_written',  # written
        'miss.bytes'  # bypass

    ]
}

BAD_SYMBOLS = re.compile(r'[:/.-]+')


class Cache:
    key = 'caches'
    charts = cache_charts

    def __init__(self, **kw):
        self.real_name = kw['name']
        self.name = BAD_SYMBOLS.sub('_', self.real_name)

    def memory_usage(self, data):
        used = data['slabs'][self.real_name]['pages']['used']
        free = data['slabs'][self.real_name]['pages']['free']
        return used / float(free + used) * 1e4

    def get_data(self, raw_data):
        zone_data = raw_data['caches'][self.real_name]
        data = parse_json(zone_data, METRICS['CACHE'])
        data['memory_usage'] = self.memory_usage(raw_data)
        return dict(('_'.join([self.name, k]), v) for k, v in data.items())


class WebZone:
    key = 'server_zones'
    charts = web_zone_charts

    def __init__(self, **kw):
        self.real_name = kw['name']
        self.name = BAD_SYMBOLS.sub('_', self.real_name)

    def get_data(self, raw_data):
        zone_data = raw_data['server_zones'][self.real_name]
        data = parse_json(zone_data, METRICS['WEB_ZONE'])
        return dict(('_'.join([self.name, k]), v) for k, v in data.items())


class WebUpstream:
    key = 'upstreams'
    charts = web_upstream_charts

    def __init__(self, **kw):
        self.real_name = kw['name']
        self.name = BAD_SYMBOLS.sub('_', self.real_name)
        self.peers = OrderedDict()

        peers = kw['response']['upstreams'][self.real_name]['peers']
        for peer in peers:
            self.add_peer(peer['id'], peer['server'])

    def __iter__(self):
        return iter(self.peers.values())

    def add_peer(self, idx, server):
        peer = WebUpstreamPeer(idx, server)
        self.peers[peer.real_server] = peer
        return peer

    def peers_stats(self, peers):
        peers = {int(peer['id']): peer for peer in peers}
        data = dict()
        for peer in self.peers.values():
            if not peer.active:
                continue
            try:
                data.update(peer.get_data(peers[peer.id]))
            except KeyError:
                peer.active = False
        return data

    def memory_usage(self, data):
        used = data['slabs'][self.real_name]['pages']['used']
        free = data['slabs'][self.real_name]['pages']['free']
        return used / float(free + used) * 1e4

    def summary_stats(self, data):
        rv = defaultdict(int)
        for metric in METRICS['WEB_UPSTREAM_SUMMARY']:
            for peer in self.peers.values():
                if peer.active:
                    metric = '_'.join(metric.split('.'))
                    rv[metric] += data['_'.join([peer.server, metric])]
        return rv

    def get_data(self, raw_data):
        data = dict()
        peers = raw_data['upstreams'][self.real_name]['peers']
        data.update(self.peers_stats(peers))
        data.update(self.summary_stats(data))
        data['memory_usage'] = self.memory_usage(raw_data)
        return dict(('_'.join([self.name, k]), v) for k, v in data.items())


class WebUpstreamPeer:
    def __init__(self, idx, server):
        self.id = idx
        self.real_server = server
        self.server = BAD_SYMBOLS.sub('_', self.real_server)
        self.active = True

    def get_data(self, raw):
        data = dict(header_time=0, response_time=0, max_conns=0)
        data.update(parse_json(raw, METRICS['WEB_UPSTREAM_PEER']))
        data['connections_usage'] = 0 if not data['max_conns'] else data['active'] / float(data['max_conns']) * 1e4
        data['state'] = int(data['state'] == 'up')
        return dict(('_'.join([self.server, k]), v) for k, v in data.items())


class Service(UrlService):
    def __init__(self, configuration=None, name=None):
        UrlService.__init__(self, configuration=configuration, name=name)
        self.order = list(ORDER)
        self.definitions = deepcopy(CHARTS)
        self.objects = dict()

    def check(self):
        if not self.url:
            self.error('URL is not defined')
            return None

        self._manager = self._build_manager()
        if not self._manager:
            return None

        raw_data = self._get_raw_data()
        if not raw_data:
            return None

        try:
            response = loads(raw_data)
        except ValueError:
            return None

        for obj_cls in [WebZone, WebUpstream, Cache]:
            for obj_name in response.get(obj_cls.key, list()):
                obj = obj_cls(name=obj_name, response=response)
                self.objects[obj.real_name] = obj
                charts = obj_cls.charts(obj)
                for chart in charts:
                    self.order.append(chart)
                    self.definitions[chart] = charts[chart]

        return bool(self.objects)

    def _get_data(self):
        """
        Format data received from http request
        :return: dict
        """
        raw_data = self._get_raw_data()
        if not raw_data:
            return None
        response = loads(raw_data)

        data = parse_json(response, METRICS['SERVER'])
        data['ssl_memory_usage'] = data['slabs_SSL_pages_used'] / float(data['slabs_SSL_pages_free']) * 1e4

        for obj in self.objects.values():
            if obj.real_name in response[obj.key]:
                data.update(obj.get_data(response))

        return data


def parse_json(raw_data, metrics):
    data = dict()
    for metric in metrics:
        value = raw_data
        metrics_list = metric.split('.')
        try:
            for m in metrics_list:
                value = value[m]
        except KeyError:
            continue
        data['_'.join(metrics_list)] = value
    return data