|
| 1 | +from .. import __version__ |
| 2 | +import sys |
| 3 | +import collectd |
| 4 | +from . import listener |
| 5 | +from . import receiver |
| 6 | +from . import aggregator |
| 7 | +from .. import protocol |
| 8 | + |
| 9 | +aggregator_ = None |
| 10 | + |
| 11 | + |
| 12 | +def _notice(msg): |
| 13 | + collectd.notice("[sqlalchemy-collectd] %s" % msg) |
| 14 | + |
| 15 | + |
| 16 | +def get_config(config): |
| 17 | + global aggregator_ |
| 18 | + |
| 19 | + _notice("sqlalchemy_collectd plugin version %s" % __version__) |
| 20 | + _notice("Python version: %s" % sys.version) |
| 21 | + start_plugin(config) |
| 22 | + |
| 23 | + |
| 24 | +def start_plugin(config): |
| 25 | + global aggregator_ |
| 26 | + |
| 27 | + config_dict = {elem.key: tuple(elem.values) for elem in config.children} |
| 28 | + host, port = config_dict.get("listen", ("localhost", 25827)) |
| 29 | + |
| 30 | + aggregator_ = aggregator.Aggregator() |
| 31 | + receiver_ = receiver.Receiver() |
| 32 | + connection = protocol.ServerConnection(host, int(port)) |
| 33 | + |
| 34 | + listener.listen(connection, aggregator_, receiver_) |
| 35 | + |
| 36 | + |
| 37 | +def _read_raw_struct_to_values(message): |
| 38 | + # dispatch( |
| 39 | + # [type][, values][, plugin_instance] |
| 40 | + # [, type_instance][, plugin][, host][, time][, interval]) -> None. |
| 41 | + |
| 42 | + return collectd.Values( |
| 43 | + type=message[protocol.TYPE_TYPE], |
| 44 | + values=message[protocol.TYPE_VALUES], |
| 45 | + plugin=message[protocol.TYPE_PLUGIN], |
| 46 | + host=message[protocol.TYPE_HOST], |
| 47 | + time=message[protocol.TYPE_TIME], |
| 48 | + type_instance=message[protocol.TYPE_TYPE_INSTANCE], |
| 49 | + plugin_instance=message[protocol.TYPE_PLUGIN_INSTANCE], |
| 50 | + interval=message[protocol.TYPE_INTERVAL] |
| 51 | + ) |
| 52 | + |
| 53 | +_read_struct_to_values = _read_raw_struct_to_values |
| 54 | + |
| 55 | + |
| 56 | +def read(data=None): |
| 57 | + for message in aggregator_.outgoing(): |
| 58 | + values = _read_struct_to_values(message) |
| 59 | + values.dispatch() |
| 60 | + |
| 61 | + |
| 62 | +collectd.register_config(get_config) |
| 63 | +collectd.register_read(read) |
0 commit comments