Git Product home page Git Product logo

telegraf_pyplug's Introduction

Build Status PyPI PyPI - License Python 3.6 Python 3.7 Python 3.8

Telegraf_PyPlug

Problem

Telegaf is a plugin-driven agent for collecting, processing, aggregating, and writing metrics. Custom input plugins collect metrics from the system, services, or 3rd party APIs and outputs them in the InfluxDB line protocol format.

  • Printing metrics in the InfluxDB line protocol format is a bit complicated, and it's easy to make mistakes.
  • There is no standard way to develop Telegraf plugins in Python. Maintaining a lot of plugins designed in different ways becomes hell.

Solution

Telegraf_pyplug is a free and open-source software library to simplify and standardize the development of python input plugins for the Telegraf.

Usage

One field:

from telegraf_pyplug.main import print_influxdb_format


METRIC_NAME: str = 'jumping_sheep'
METRIC_COUNT: int = 321


def main() -> None:
    print_influxdb_format(measurement=METRIC_NAME, fields={'count': METRIC_COUNT})


if __name__ == '__main__':
    main()

Outputs:

jumping_sheep count=321

add_timestamp argument:

from telegraf_pyplug.main import print_influxdb_format


METRIC_NAME: str = 'jumping_sheep'
METRIC_COUNT: int = 321


def main() -> None:
    print_influxdb_format(measurement=METRIC_NAME, fields={'count': METRIC_COUNT}, add_timestamp=True)


if __name__ == '__main__':
    main()

Outputs:

jumping_sheep count=321 1599846911207090944

One field, One tag:

from telegraf_pyplug.main import print_influxdb_format


METRIC_NAME: str = 'jumping_sheep'
METRIC_COUNT: int = 321
METRIC_COLOR: str = 'white'


def main() -> None:
    print_influxdb_format(measurement=METRIC_NAME, tags={'color': METRIC_COLOR}, fields={'count': METRIC_COUNT})


if __name__ == '__main__':
    main()

Outputs:

jumping_sheep,color=white count=321

Multiple fields and tags, nano_timestamp argument:

from datetime import datetime
from typing import Dict

from telegraf_pyplug.main import print_influxdb_format, datetime_tzinfo_to_nano_unix_timestamp


METRIC_NAME: str = 'jumping_sheep'
METRIC_FIELDS: Dict[str, int] = {'count': 321, 'height_m': 1.5}
METRIC_TAGS: Dict[str, str] = {'color': 'white', 'name': 'sweater'}
METRIC_DATE: str = '01.01.2020 03:00:00+0300'


def main() -> None:
    date: datetime = datetime.strptime(METRIC_DATE, '%d.%m.%Y %H:%M:%S%z')

    print_influxdb_format(
        measurement=METRIC_NAME,
        tags=METRIC_TAGS,
        fields=METRIC_FIELDS,
        nano_timestamp=datetime_tzinfo_to_nano_unix_timestamp(date)
    )


if __name__ == '__main__':
    main()

Outputs:

jumping_sheep,color=white,name=sweater count=321,height_m=1.5 1577836800000000000

More advanced examples can be found in the examples_dir.

Installation

Telegraf_PyPlug can easily be installed with pip.

Mac/Linux

pip install --upgrade telegraf_pyplug

Windows

python -m pip install --upgrade telegraf_pyplug

License

Telegraf_PyPlug is under MIT license. See the LICENSE file for the full license text.

telegraf_pyplug's People

Contributors

daddy-shark avatar

Stargazers

 avatar Concept avatar  avatar Andreas Motl avatar Yuriy Golikov avatar

Watchers

Benjamin Petterle avatar James Cloos avatar  avatar

telegraf_pyplug's Issues

Why include db.py

I've been looking at this package as a convenient way to output measurements to telegraf. It looks like the intent of this package is to write a python script that is called by telegraf's exec plugin, so it just needs to handle printing the telegraf format.

However, there also seems to be a db.py that has two seemingly unrelated utilities for extracting data from mysql and postgresql datasets. Not a big deal in itself, but this does add dependencies on extra database-related python packages, but also e.g. the libpg package, making telegraf_pyplug a lot less suitable for a quick standalone python script.

I can imagine it is convenient to have these database helpers in this package that you're already including anyway, but maybe it would make sense to put these helpers in their own package and leaving this package for just the telegraf-related code?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.