Skip to content

Commit 216fc5e

Browse files
committed
- do some logging
1 parent 35af70d commit 216fc5e

File tree

4 files changed

+93
-8
lines changed

4 files changed

+93
-8
lines changed

collectdconsole.conf

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55

66
LoadPlugin logfile
77
<Plugin logfile>
8-
LogLevel info
8+
# note "debug" will usually not work
9+
LogLevel "info"
910
File STDOUT
1011
Timestamp true
11-
PrintSeverity false
12+
PrintSeverity true
1213
</Plugin>
1314

1415
LoadPlugin write_log
@@ -29,6 +30,11 @@ LoadPlugin python
2930
<Module "sqlalchemy_collectd.server.plugin">
3031
listen "localhost" 25827
3132

33+
# the plugin sends logging into the collectd
34+
# notify system.
35+
# "info" shows essentially a startup message; set to "debug" to
36+
# show messages received
37+
loglevel "info"
3238
</Module>
3339
</Plugin>
3440

setup.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,9 @@
4141
setup_requires=['pytest-runner'],
4242
zip_safe=False,
4343
install_requires=requires,
44+
entry_points={
45+
'sqlalchemy.plugins': [
46+
'collectd = sqlalchemy_collectd.client.plugin.Plugin'
47+
]
48+
}
4449
)

sqlalchemy_collectd/protocol.py

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
connect straight to the network plugin.
33
44
"""
5+
import collections
56
import struct
67
import socket
78
import logging
@@ -149,8 +150,59 @@ def send(self, connection, timestamp, *values):
149150

150151
payload = self.type._encode_values(*values)
151152

153+
log.debug("send: %s", _SendMsg(self, values))
152154
connection.send(header_ + payload)
153155

156+
def __str__(self):
157+
return (
158+
"(host=%r, plugin=%r, plugin_instance=%r, "
159+
"type=%r, type_instance=%r, interval=%r)" % (
160+
self.host, self.plugin, self.plugin_instance,
161+
self.type.name, self.type_instance, self.interval
162+
)
163+
)
164+
165+
166+
class _SendMsg(collections.namedtuple('sendmsg', ['sender', 'values'])):
167+
def __str__(self):
168+
sender = self.sender
169+
type_ = sender.type
170+
return (
171+
"(host=%r, plugin=%r, plugin_instance=%r, "
172+
"type=%r, type_instance=%r, interval=%r, values=%s)" % (
173+
sender.host, sender.plugin, sender.plugin_instance,
174+
type_.name, sender.type_instance, sender.interval,
175+
", ".join(
176+
"%s=%s" % (field_name, value)
177+
for field_name, value in zip(type_.names, self.values)
178+
)
179+
)
180+
)
181+
182+
183+
class _RecvMsg(collections.namedtuple("receivemsg", ['result', 'type'])):
184+
185+
def __str__(self):
186+
if self.type:
187+
type_names = self.type.names
188+
else:
189+
type_names = ["(unknown)" for value in self.result[TYPE_VALUES]]
190+
191+
return (
192+
"(host=%r, plugin=%r, plugin_instance=%r, "
193+
"type=%r, type_instance=%r, interval=%r, values=%s)" % (
194+
self.result[TYPE_HOST], self.result[TYPE_PLUGIN],
195+
self.result[TYPE_PLUGIN_INSTANCE],
196+
self.result[TYPE_TYPE], self.result[TYPE_TYPE_INSTANCE],
197+
self.result[TYPE_INTERVAL],
198+
", ".join(
199+
"%s=%s" % (field_name, value)
200+
for field_name, value
201+
in zip(type_names, self.result[TYPE_VALUES])
202+
)
203+
)
204+
)
205+
154206

155207
class MessageReceiver(object):
156208
def __init__(self, *types):
@@ -171,6 +223,7 @@ def __init__(self, *types):
171223
def receive(self, buf):
172224
result = self._unpack_packet(buf)
173225
type_name = result[TYPE_TYPE]
226+
type_ = None
174227
try:
175228
type_ = self._types[type_name]
176229
except KeyError:
@@ -183,6 +236,8 @@ def receive(self, buf):
183236
for name, value in zip(type_._field_names, result[TYPE_VALUES])
184237
}
185238
return result
239+
finally:
240+
log.debug("receive: %s", _RecvMsg(result, type_))
186241

187242
def _unpack_packet(self, buf):
188243
pos = 0
@@ -257,7 +312,6 @@ def send(self, message):
257312
self._mutex.acquire()
258313
try:
259314
self._check_connect()
260-
log.debug("sending: %r", message)
261315
self.socket.sendto(message, (self.host, self.port))
262316
except IOError:
263317
log.error("Error in socket.sendto", exc_info=True)

sqlalchemy_collectd/server/plugin.py

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,24 @@
1212
receiver_ = None
1313

1414

15-
# TODO: merge collectd.notice w/ python logging?
16-
def _notice(msg):
17-
collectd.notice("[sqlalchemy-collectd] %s" % msg)
15+
class CollectdHandler(logging.Handler):
16+
levels = {
17+
logging.INFO: collectd.info,
18+
logging.WARN: collectd.warning,
19+
logging.ERROR: collectd.error,
20+
logging.DEBUG: collectd.info,
21+
logging.CRITICAL: collectd.error,
22+
23+
}
24+
25+
def emit(self, record):
26+
fn = self.levels[record.levelno]
27+
fn(record.msg % record.args)
1828

1929

2030
def get_config(config):
2131
global aggregator_
2232

23-
_notice("sqlalchemy_collectd plugin version %s" % __version__)
24-
_notice("Python version: %s" % sys.version)
2533
start_plugin(config)
2634

2735

@@ -31,6 +39,18 @@ def start_plugin(config):
3139
config_dict = {elem.key: tuple(elem.values) for elem in config.children}
3240
host, port = config_dict.get("listen", ("localhost", 25827))
3341

42+
logging.getLogger().addHandler(CollectdHandler())
43+
44+
loglevel = {
45+
"warn": logging.WARN, "error": logging.ERROR,
46+
"debug": logging.DEBUG, "info": logging.INFO
47+
}[config_dict.get("loglevel", ("info", ))[0]]
48+
49+
logging.getLogger().setLevel(loglevel)
50+
51+
log.info("sqlalchemy_collectd plugin version %s", __version__)
52+
log.info("Python version: %s", sys.version)
53+
3454
receiver_ = receiver.Receiver()
3555
connection = protocol.ServerConnection(host, int(port))
3656

0 commit comments

Comments
 (0)