|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
|
|
3
|
|
|
__author__ = 'Kenny Freeman' |
|
4
|
|
|
__email__ = '[email protected]' |
|
5
|
|
|
__license__ = "ISCL" |
|
6
|
|
|
__docformat__ = 'reStructuredText' |
|
7
|
|
|
|
|
8
|
|
|
import sys |
|
9
|
|
|
|
|
10
|
|
|
PY3 = sys.version_info > (3,) |
|
11
|
|
|
|
|
12
|
|
|
from collections import deque |
|
13
|
|
|
|
|
14
|
|
|
import plumd |
|
15
|
|
|
import plumd.plugins |
|
16
|
|
|
from plumd.calc import Differential |
|
17
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
# -*- coding: utf-8 -*- |
|
20
|
|
|
|
|
21
|
|
|
__author__ = 'Kenny Freeman' |
|
22
|
|
|
__email__ = '[email protected]' |
|
23
|
|
|
__license__ = "ISCL" |
|
24
|
|
|
__docformat__ = 'reStructuredText' |
|
25
|
|
|
|
|
26
|
|
|
import sys |
|
27
|
|
|
|
|
28
|
|
|
PY3 = sys.version_info > (3,) |
|
29
|
|
|
|
|
30
|
|
|
if PY3: |
|
31
|
|
|
import urllib.request as urllib |
|
32
|
|
|
else: |
|
33
|
|
|
import urllib2 as urllib |
|
34
|
|
|
from collections import deque |
|
35
|
|
|
|
|
36
|
|
|
import plumd |
|
37
|
|
|
import plumd.plugins |
|
38
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
class Nginx(plumd.plugins.Reader): |
|
41
|
|
|
"""Plugin to record nginx stub_status metrics.""" |
|
42
|
|
|
|
|
43
|
|
|
# default config values |
|
44
|
|
|
defaults = { |
|
45
|
|
|
'poll.interval': 10, |
|
46
|
|
|
'url': "/status", # url nginx stub_status is configured for |
|
47
|
|
|
'proto': 'http', # http/https |
|
48
|
|
|
'host': '127.0.0.1', # Nginx server hostname/ip |
|
49
|
|
|
'port': 80, # Nginx server port |
|
50
|
|
|
'timeout': 10 |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
def __init__(self, log, config): |
|
54
|
|
|
"""Plugin to record nginx stub_status metrics. |
|
55
|
|
|
|
|
56
|
|
|
:param log: A logger |
|
57
|
|
|
:type log: logging.RootLogger |
|
58
|
|
|
:param config: a plumd.config.Conf configuration helper instance. |
|
59
|
|
|
:type config: plumd.config.Conf |
|
60
|
|
|
""" |
|
61
|
|
|
super(Nginx, self).__init__(log, config) |
|
62
|
|
|
self.config.defaults(Nginx.defaults) |
|
63
|
|
|
|
|
64
|
|
|
# Nginx connection |
|
65
|
|
|
proto = self.config.get('proto') |
|
66
|
|
|
host = self.config.get('host') |
|
67
|
|
|
port = self.config.get('port') |
|
68
|
|
|
url = self.config.get('url') |
|
69
|
|
|
self.url = "{0}://{1}:{2}{3}".format(proto, host, port, url) |
|
70
|
|
|
self.timeout = config.get('timeout') |
|
71
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
def poll(self): |
|
74
|
|
|
"""Query Nginx for metrics. |
|
75
|
|
|
|
|
76
|
|
|
:rtype: ResultSet |
|
77
|
|
|
""" |
|
78
|
|
|
result = plumd.Result("nginx") |
|
79
|
|
|
name = self.name |
|
80
|
|
|
try: |
|
81
|
|
|
ret = urllib.urlopen(self.url, timeout=self.timeout).read() |
|
82
|
|
|
dat_str = None |
|
83
|
|
|
if PY3: |
|
84
|
|
|
dat = deque(ret.decode('utf8').split("\n")) |
|
85
|
|
|
else: |
|
86
|
|
|
dat = deque(ret.split("\n")) |
|
87
|
|
|
ncons = dat.popleft().split()[-1] |
|
88
|
|
|
dat.popleft() |
|
89
|
|
|
hnames = ["accepts", "handled", "requests"] |
|
90
|
|
|
hmetrics = dict(zip(hnames, dat.popleft().split())) |
|
91
|
|
|
rwnames = ["reading", "writing", "waiting"] |
|
92
|
|
|
rwmetrics = dict(zip(rwnames, dat.popleft().split()[1::2])) |
|
93
|
|
|
mfmt = "{0}.{1}" |
|
94
|
|
|
result.add(plumd.Int(mfmt.format(name, "connections"), ncons)) |
|
95
|
|
|
for key, val in hmetrics.items(): |
|
96
|
|
|
result.add(plumd.Int(mfmt.format(name, key), val)) |
|
97
|
|
|
for key, val in rwmetrics.items(): |
|
98
|
|
|
result.add(plumd.Int(mfmt.format(name, key), val)) |
|
99
|
|
|
except Exception as e: |
|
100
|
|
|
self.log.error("Nginx: check failed: {0} : {1}".format(self.url, e)) |
|
101
|
|
|
return plumd.ResultSet([result]) |
|
102
|
|
|
|