forked from DataDog/integrations-extras
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcheck.py
154 lines (125 loc) · 5.05 KB
/
check.py
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
# (C) Datadog, Inc. 2010-2016
# All rights reserved
# Licensed under Simplified BSD License (see LICENSE)
import requests
# stdlib
# project
from checks import AgentCheck
EVENT_TYPE = SOURCE_TYPE_NAME = 'gnatsd'
class GnatsdConfig:
def __init__(self, instance):
self.instance = instance
self.host = instance.get('host', '')
self.port = int(instance.get('port', 8222))
self.url = self.host + ':' + str(self.port)
self.server_name = instance.get('server_name', '')
self.tags = instance.get('tags', [])
class GnatsdCheckInvocation:
SERVICE_CHECK_NAME = 'gnatsd.can_connect'
METRICS = {
'varz': {
'connections': 'gauge',
'subscriptions': 'gauge',
'slow_consumers': 'count',
'remotes': 'gauge',
'routes': 'gauge',
'in_msgs': 'count',
'out_msgs': 'count',
'in_bytes': 'count',
'out_bytes': 'count',
'mem': 'gauge'
},
'connz': {
'num_connections': 'gauge',
'total': 'count',
'connections': {
'pending_bytes': 'gauge',
'in_msgs': 'count',
'out_msgs': 'count',
'subscriptions': 'gauge',
'in_bytes': 'count',
'out_bytes': 'count'
}
},
'routez': {
'num_routes': 'gauge',
'routes': {
'pending_size': 'gauge',
'in_msgs': 'count',
'out_msgs': 'count',
'subscriptions': 'gauge',
'in_bytes': 'count',
'out_bytes': 'count'
}
}
}
TAGS = {
'varz': ['server_id'],
'connz.connections': ['cid','ip','name','lang','version'],
'routez.routes': ['rid','remote_id','ip']
}
def __init__(self, instance, checker):
self.instance = instance
self.checker = checker
self.config = GnatsdConfig(instance)
self.tags = self.config.tags + ['server_name:%s' % self.config.server_name]
self.service_check_tags = self.tags + ['url:%s' % self.config.host]
def check(self):
# Confirm monitor endpoint is available
self._status_check()
# Gather NATS metrics
for endpoint, metrics in self.METRICS.items():
self._check_endpoint(endpoint, metrics)
def _status_check(self):
try:
response = requests.get(self.config.url)
if response.status_code == 200:
self.checker.service_check(self.SERVICE_CHECK_NAME, AgentCheck.OK, tags=self.service_check_tags)
else:
raise ValueError('Non 200 response from NATS monitor port')
except Exception as e:
msg = "Unable to fetch NATS stats: %s" % str(e)
self.checker.service_check(self.SERVICE_CHECK_NAME, AgentCheck.CRITICAL, message=msg, tags=self.service_check_tags)
raise e
def _check_endpoint(self, endpoint, metrics):
data = requests.get(self.config.url + '/' + endpoint).json()
self._track_metrics(endpoint, metrics, data)
def _track_metrics(self, namespace, metrics, data, tags={}):
if not tags:
tags = self._metric_tags(namespace, data)
for mname, mtype in metrics.items():
path = namespace + '.' + mname
if type(mtype) is dict:
for instance in data.get(mname, []):
if 'routez' in namespace:
# . is not a valid character in identifiers so replace it in IP addresses with _
title = str(instance.get('ip')).replace('.','_')
else:
title = str(instance.get('name') or 'unnamed')
self._track_metrics(path + '.' + title, mtype, instance, tags=self._metric_tags(path, instance))
else:
if mtype == 'count':
mid = str(data.get('cid') or data.get('rid') or '')
metric = self._count_delta(path + '.' + mid, data[mname])
else:
metric = data[mname]
# Send metric to Datadog
getattr(self.checker, mtype)('gnatsd.' + path, metric, tags=tags)
def _metric_tags(self, endpoint, data):
tags = []
if endpoint in self.TAGS:
for tag in self.TAGS[endpoint]:
if tag in data:
tags.append('gnatsd-' + tag + ':' + str(data[tag]))
return self.tags + tags
def _count_delta(self, count_id, current_value):
self.checker.counts.setdefault(count_id, 0)
delta = current_value - self.checker.counts[count_id]
self.checker.counts[count_id] = current_value
return delta
class GnatsdCheck(AgentCheck):
def __init__(self, name, init_config, agentConfig, instances=None):
AgentCheck.__init__(self, name, init_config, agentConfig, instances)
self.counts = {}
def check(self, instance):
GnatsdCheckInvocation(instance, self).check()