From e880f004983810702b5732a88c2801201a818299 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Mon, 6 Jan 2020 15:24:52 +0100 Subject: [PATCH] Added Graphite integration - closes #96 --- docs/source/conf.py | 5 ++- docs/source/platypush/plugins/graphite.rst | 5 +++ docs/source/plugins.rst | 2 + platypush/plugins/graphite.py | 51 ++++++++++++++++++++++ requirements.txt | 3 ++ setup.py | 2 + 6 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 docs/source/platypush/plugins/graphite.rst create mode 100644 platypush/plugins/graphite.py diff --git a/docs/source/conf.py b/docs/source/conf.py index 16836ea3c7..7128a74982 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -233,8 +233,9 @@ autodoc_mock_imports = ['googlesamples.assistant.grpc.audio_helpers', 'trello', 'telegram', 'telegram.ext', - 'pyfirmata2' - 'cups' + 'pyfirmata2', + 'cups', + 'graphyte', ] sys.path.insert(0, os.path.abspath('../..')) diff --git a/docs/source/platypush/plugins/graphite.rst b/docs/source/platypush/plugins/graphite.rst new file mode 100644 index 0000000000..9248a46488 --- /dev/null +++ b/docs/source/platypush/plugins/graphite.rst @@ -0,0 +1,5 @@ +``platypush.plugins.graphite`` +============================== + +.. automodule:: platypush.plugins.graphite + :members: diff --git a/docs/source/plugins.rst b/docs/source/plugins.rst index 183c024b45..e49b2320e1 100644 --- a/docs/source/plugins.rst +++ b/docs/source/plugins.rst @@ -44,6 +44,7 @@ Plugins platypush/plugins/gpio.sensor.mcp3008.rst platypush/plugins/gpio.sensor.motion.pwm3901.rst platypush/plugins/gpio.zeroborg.rst + platypush/plugins/graphite.rst platypush/plugins/homeseer.rst platypush/plugins/http.request.rst platypush/plugins/http.request.ota.booking.rst @@ -84,6 +85,7 @@ Plugins platypush/plugins/sound.rst platypush/plugins/switch.rst platypush/plugins/switch.switchbot.rst + platypush/plugins/switch.tplink.rst platypush/plugins/switch.wemo.rst platypush/plugins/tcp.rst platypush/plugins/todoist.rst diff --git a/platypush/plugins/graphite.py b/platypush/plugins/graphite.py new file mode 100644 index 0000000000..5a82e1407d --- /dev/null +++ b/platypush/plugins/graphite.py @@ -0,0 +1,51 @@ +from typing import Optional, Dict + +from platypush.plugins import Plugin, action + + +class GraphitePlugin(Plugin): + """ + Plugin for sending data to a Graphite instance. + """ + + def __init__(self, host: str = 'localhost', port: int = 2003, **kwargs): + """ + :param host: Default Graphite host (default: 'localhost'). + :param port: Default Graphite port (default: 2003). + """ + super().__init__(**kwargs) + self.host = host + self.port = port + + @action + def send(self, + metric: str, + value, + host: Optional[str] = None, + port: Optional[int] = None, + tags: Optional[Dict[str, str]] = None, + prefix: str = '', + protocol: str = 'tcp'): + """ + Send data to a Graphite instance. + + :param metric: Metric name. + :param value: Value to be sent. + :param host: Graphite host (default: default configured ``host``). + :param port: Graphite port (default: default configured ``port``). + :param tags: Map of tags for the metric. + :param prefix: Metric prefix name (default: empty string). + :param protocol: Communication protocol - possible values: 'tcp', 'udp' (default: 'tcp'). + """ + import graphyte + + host = host or self.host + port = port or self.port + tags = tags or {} + protocol = protocol.lower() + + graphyte.init(host, port=port, prefix=prefix, protocol=protocol) + graphyte.send(metric, value, tags=tags) + + +# vim:sw=4:ts=4:et: diff --git a/requirements.txt b/requirements.txt index fbb19b1cd5..0b20bad6b2 100644 --- a/requirements.txt +++ b/requirements.txt @@ -214,3 +214,6 @@ croniter # Support for CUPS printers management # pycups +# Support for Graphite integration +# graphyte + diff --git a/setup.py b/setup.py index 2413935947..25d755a103 100755 --- a/setup.py +++ b/setup.py @@ -271,5 +271,7 @@ setup( 'arduino': ['pyserial', 'pyfirmata2'], # Support for CUPS printers management 'cups': ['pycups'], + # Support for Graphite integration + 'graphite': ['graphyte'], }, )