Skip to content

Commit 8eb4a5a

Browse files
vdombrovskiracciari
vdombrovski
authored andcommitted
Fix beanstalkd collector, normalize metric format
When beanstalkd instance could not be contacted, it previously raised an exception and terminated the collection; this has been fixed. A not implemented part has been removed from Zookeeper collector. Measurement names for beanstalk now contain only undescores. Linted the new collectors with flake8
1 parent b94ffa9 commit 8eb4a5a

File tree

3 files changed

+37
-46
lines changed

3 files changed

+37
-46
lines changed

collectors/openio/openiobeanstalkd.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,14 @@ class OpenioBeanstalkdCollector(diamond.collector.Collector):
2727

2828
def process_config(self):
2929
super(OpenioBeanstalkdCollector, self).process_config()
30-
self.instances = self.config['instances'] or 'OPENIO:127.0.0.1:6014'
30+
self.instances = self.config.get('instances', 'OPENIO:127.0.0.1:6014')
3131

3232
def get_default_config_help(self):
3333
config_help = super(OpenioBeanstalkdCollector,
3434
self).get_default_config_help()
3535
config_help.update({
36-
'instances': 'List of instances in the form NAMESPACE:host:port (comma separated)',
36+
'instances': 'List of instances in the form NAMESPACE:host:port\
37+
(comma separated)',
3738
})
3839
return config_help
3940

@@ -47,13 +48,13 @@ def get_default_config(self):
4748
})
4849
return config
4950

50-
def _get_stats(self,host,port):
51+
def _get_stats(self, host, port):
5152
stats = {}
5253
try:
53-
connection = beanstalkc.Connection(host,int(port))
54-
except beanstalkc.BeanstalkcException, e:
54+
connection = beanstalkc.Connection(host, int(port))
55+
except beanstalkc.BeanstalkcException as e:
5556
self.log.error("Couldn't connect to beanstalkd: %s", e)
56-
return {}
57+
return None
5758

5859
stats['instance'] = connection.stats()
5960
stats['tubes'] = []
@@ -74,23 +75,28 @@ def collect(self):
7475
instances = instances.split(',')
7576

7677
for instance in instances:
77-
namespace,host,port = instance.split(':')
78-
info = self._get_stats(host,port)
78+
namespace, host, port = instance.split(':')
79+
info = self._get_stats(host, port)
80+
if not info:
81+
continue
7982
metric_prefix = "%s.beanstalkd.%s:%s." % (namespace,
8083
host.replace('.', '_'),
8184
port)
8285

8386
for stat, value in info['instance'].items():
8487
if stat not in self.SKIP_LIST:
85-
self.publish(metric_prefix + stat, value,
86-
metric_type=self.get_metric_type(stat))
88+
stat = stat.replace('-', '_')
89+
self.publish("%s%s" % (metric_prefix, stat), value,
90+
metric_type=self.get_metric_type(stat))
8791

8892
for tube_stats in info['tubes']:
8993
tube = tube_stats['name']
9094
for stat, value in tube_stats.items():
9195
if stat != 'name':
92-
self.publish(metric_prefix + 'tubes.%s.%s' % (tube, stat), value,
93-
metric_type=self.get_metric_type(stat))
96+
_stat = stat.replace('-', '_')
97+
b_name = '%stubes.%s.%s' % (metric_prefix, tube, _stat)
98+
metric = self.get_metric_type(stat)
99+
self.publish(b_name, value, metric_type=metric)
94100

95101
def get_metric_type(self, stat):
96102
if self.COUNTERS_REGEX.match(stat):

collectors/openio/openioredisstat.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525

2626
import diamond.collector
2727
import time
28-
import os
2928

3029
try:
3130
import redis
@@ -89,13 +88,14 @@ def process_config(self):
8988
else:
9089
auth = None
9190

92-
nick = '%s:%s' % (host.replace('.','_'), port)
91+
nick = '%s:%s' % (host.replace('.', '_'), port)
9392
self.instances[nick] = (namespace, host, port, auth)
9493

9594
self.log.debug("Configured instances: %s" % self.instances.items())
9695

9796
def get_default_config_help(self):
98-
config_help = super(OpenioRedisCollector, self).get_default_config_help()
97+
config_help = super(OpenioRedisCollector, self)\
98+
.get_default_config_help()
9999
config_help.update({
100100
'timeout': 'Socket timeout',
101101
'db': '',
@@ -110,7 +110,7 @@ def get_default_config(self):
110110
"""
111111
Return default config
112112
113-
:rtype: dict
113+
:rtype: dict
114114
115115
"""
116116
config = super(OpenioRedisCollector, self).get_default_config()
@@ -125,11 +125,11 @@ def get_default_config(self):
125125
return config
126126

127127
def _client(self, host, port, auth):
128-
"""Return a redis client for the configuration.
128+
""" Return a redis client for the configuration.
129129
130-
:param str host: redis host
131-
:param int port: redis port
132-
:rtype: redis.Redis
130+
:param str host: redis host
131+
:param int port: redis port
132+
:rtype: redis.Redis
133133
134134
"""
135135
db = int(self.config['db'])
@@ -139,8 +139,9 @@ def _client(self, host, port, auth):
139139
db=db, socket_timeout=timeout, password=auth)
140140
cli.ping()
141141
return cli
142-
except Exception, ex:
143-
self.log.error("OpenioRedisCollector: failed to connect to %s:%i. %s.",
142+
except Exception as ex:
143+
self.log.error("OpenioRedisCollector:\
144+
failed to connect to %s:%i. %s.",
144145
host, port, ex)
145146

146147
def _precision(self, value):

collectors/openio/openiozookeeper.py

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626

2727
import diamond.collector
2828
import socket
29-
import re
3029

3130

3231
class OpenioZookeeperCollector(diamond.collector.Collector):
@@ -36,7 +35,8 @@ def process_config(self):
3635
self.instances = self.config['instances'] or 'OPENIO:localhost:6005'
3736

3837
def get_default_config_help(self):
39-
config_help = super(OpenioZookeeperCollector, self).get_default_config_help()
38+
config_help = super(OpenioZookeeperCollector, self)\
39+
.get_default_config_help()
4040
config_help.update({
4141
'publish':
4242
"Which rows of 'status' you would like to publish." +
@@ -62,7 +62,7 @@ def get_default_config(self):
6262
# 'publish': ''
6363

6464
# Connection settings
65-
#'instances': ['OPENIO:localhost:6005'],
65+
# 'instances': ['OPENIO:localhost:6005'],
6666
})
6767
return config
6868

@@ -88,60 +88,44 @@ def get_raw_stats(self, host, port):
8888
def _get_stats(self, host, port):
8989
# stuff that's always ignored, aren't 'stats'
9090
ignored = ('zk_version', 'zk_server_state')
91-
pid = None
9291

9392
stats = {}
9493
data = self.get_raw_stats(host, port)
9594

9695
# parse stats
9796
for line in data.splitlines():
98-
9997
pieces = line.split()
100-
10198
if pieces[0] in ignored:
10299
continue
103100
stats[pieces[0]] = pieces[1]
104101

105102
# get max connection limit
106-
self.log.debug('pid %s', pid)
107-
try:
108-
cmdline = "/proc/%s/cmdline" % pid
109-
f = open(cmdline, 'r')
110-
m = re.search("-c\x00(\d+)", f.readline())
111-
if m is not None:
112-
self.log.debug('limit connections %s', m.group(1))
113-
stats['limit_maxconn'] = m.group(1)
114-
f.close()
115-
except:
116-
self.log.debug("Cannot parse command line options for zookeeper")
117-
118103
return stats
119104

120105
def collect(self):
121106
instances = self.config.get('instances')
122107

123108
# Convert a string config value to be an array
124-
if isinstance(instances , basestring):
109+
if isinstance(instances, basestring):
125110
instances = instances.split(',')
126111

127112
for instance in instances:
128113
namespace, hostname, port = instance.split(':')
129114

130115
stats = self._get_stats(hostname, port)
131116

132-
metric_prefix = '%s.zookeeper.%s:%s.' % (namespace,
133-
hostname.replace('.', '_'),
134-
port)
117+
prefix = '%s.zookeeper.%s:%s.' % (namespace,
118+
hostname.replace('.', '_'),
119+
port)
135120

136121
# figure out what we're configured to get, defaulting to everything
137122
desired = self.config.get('publish', stats.keys())
138123

139124
# for everything we want
140125
for stat in desired:
141126
if stat in stats:
142-
self.publish(metric_prefix + stat, stats[stat])
127+
self.publish(prefix + stat, stats[stat])
143128
else:
144-
145129
# we don't, must be somehting configured in publish so we
146130
# should log an error about it
147131
self.log.error("No such key '%s' available, issue 'stats' "

0 commit comments

Comments
 (0)