SlideShare a Scribd company logo
Noah Crowley, Developer Advocate
Getting Started Series
Write your Own Telegraf
plugin
ยฉ 2017 InfluxData. All rights reserved.2 ยฉ 2017 InfluxData. All rights reserved.2
โœ“ Telegraf overview
โœ“ Examples of Telegraf plugins
โœ“ Telegraf plugin architecture
โœ“ How to a write a Telegraf plugin (demo)
What we will be covering
ยฉ 2017 InfluxData. All rights reserved.3 ยฉ 2017 InfluxData. All rights reserved.3
Telegraf Overview
ยฉ 2018 InfluxData. All rights reserved.4
InfluxData Open Source Projects: TELEGRAF!
ยฉ 2018 InfluxData. All rights reserved.5
InfluxData Open Source Projects: TELEGRAF!
ยฉ 2018 InfluxData. All rights reserved.6
Telegraf
Telegraf is an Open Source plugin-driven server agent for
collecting & reporting metrics: โ€œPushโ€ or โ€œPullโ€.
โ€ข Used to batch metrics from multiple sources together in order to reduce the number
of atomic write requests
โ€ข Large collection of Plugins:
โ€“ Inputs
โ€“ Outputs
โ€“ Aggregators
โ€“ Processors
โ€ข Can act as an:
โ€“ Agent
โ€“ Collector
โ€“ Ingest Pipeline
ยฉ 2018 InfluxData. All rights reserved.7
Telegraf continued
โ€ข Metrics ingestion from the host system (cpu, I/O, network)
โ€“ Common programs (Hadoop, Postgres, Redis, etc.)
โ€“ Third party APIs (Mailchimp, CloudWatch, Google Analytics, etc.)
โ€ข Output plugins to send metrics to a variety of other datastores,
services, and message queues, including
โ€“ InfluxDB, Graphite, OpenTSDB, Datadog, Librato, Kafka, MQTT,
NSQ, Prometheus, and many others
โ€ข Over 140 plugins!
ยฉ 2018 InfluxData. All rights reserved.8
Telegraf Benefits
โ€ข Although if we start using it then itโ€™s kind of the de-facto tool
anywayโ€ฆ
โ€ข Minimal memory footprint
โ€ข Tagging of metrics
โ€ข Easy contribution of functionality
โ€ข Strong Community contributions
ยฉ 2017 InfluxData. All rights reserved.9 ยฉ 2017 InfluxData. All rights reserved.9
Examples of Telegraf plugins
ยฉ 2017 InfluxData. All rights reserved.10
Some Telegraf plugins
statsd
Listens for data in statsd format and
outputs it to any configured output
apache
Collects data from the
/server-status?auto endpoint and
stores it
postgresql
Collects performance metrics about
postgres; uses data from the built in
pg_stat_database and
pg_stat_bgwriter views
win_perf_counters
Collects performance data from
Windows machines
ยฉ 2017 InfluxData. All rights reserved.11
Some Telegraf plugins
prometheus_client
Exposes all metrics on /metrics to be
polled by a Prometheus server
nginx
Read Nginx's basic status
(ngx_http_stub_status_module)
histogram
Creates histograms containing the
counts of field values within a range
kubernetes
Experimental; Talks to the kubelet
api using the /stats/summary
endpoint
ยฉ 2018 InfluxData. All rights reserved.12
How do I install it?
Download binaries at influxdata.com/downloads then:
Telegraf normally runs as a Linux service
$ telegraf -sample-config -input-filter haproxy -output-filter influxdb >
telegraf.conf
$ telegraf -config telegraf.conf
$ telegraf -sample-config -input-filter haproxy -output-filter influxdb >
/etc/telegraf/telegraf.conf
$ service telegraf start
ยฉ 2017 InfluxData. All rights reserved.13 ยฉ 2017 InfluxData. All rights reserved.13
Telegraf plugin architecture
ยฉ 2018 InfluxData. All rights reserved.14
The Open Source Collector Agent: Telegraf
ยฉ 2018 InfluxData. All rights reserved.15
Input interface
The plugin must conform to the telegraf.Input interface:
type Input interface {
// SampleConfig returns the default configuration of the Input
SampleConfig() string
// Description returns a one-sentence description on the Input
Description() string
// Gather takes in an accumulator and adds the metrics that the Input
// gathers. This is called every "interval"
Gather(Accumulator) error
}
ยฉ 2017 InfluxData. All rights reserved.16 ยฉ 2017 InfluxData. All rights reserved.16
Letโ€™s write a plugin
ยฉ 2017 InfluxData. All rights reserved.17
What do you need?
โ€ข Go 1.8+ installed and configured on your computer (1.9+
recommended)
โ€ข A desire to write a Telegraf plugin
โ€ข What is our plugin going to do?
โ€ข It will emit a trigonometric pattern called `trig`
ยฉ 2018 InfluxData. All rights reserved.18
Getting Started
This will create a new Telegraf binary at $GOPATH/bin/telegraf
$ go get github.com/influxdata/telegraf
$ cd $GOPATH/github.com/influxdata/telegraf
$ git checkout -b mySweetPlugin
$ make
ยฉ 2018 InfluxData. All rights reserved.19
Make your files and add boilerplate
$ cd plugins/inputs
$ mkdir trig
$ touch trig/trig.go
# Add boilerplate
$ cat trig/trig.go
ยฉ 2018 InfluxData. All rights reserved.20
Boilerplate
$ cat trig/trig.go
package trig
import (
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/inputs"
)
type Trig struct {
}
func (s *Trig) SampleConfig() string {
return ""
}
func (s *Trig) Description() string {
return ""
}
func (s *Trig) Gather(acc telegraf.Accumulator) error {
return nil
}
func init() {
inputs.Add("trig", func() telegraf.Input { return &Trig{} })
}
ยฉ 2018 InfluxData. All rights reserved.21
Import your plugin
In the file -> telegraf/plugins/inputs/all/all.go
Add:
This imports your plugin to the main telegraf package and ensures
that it can run
_ "github.com/influxdata/telegraf/plugins/inputs/trig"
ยฉ 2018 InfluxData. All rights reserved.22
Add properties to your struct
There must be a property on the struct to hold any configuration
variables
Any other variables should be unexported.
x will hold state of the plugin between collection intervals, being
updated each one.
type Trig struct {
x float64
Amplitude float64
}
ยฉ 2018 InfluxData. All rights reserved.23
Add any configuration variables you need
Telegraf dynamically constructs configuration files by aggregating
the configurations for all of itโ€™s plugins. This allows you to easily
generate configuration files with only certain plugins enabled by
just passing a couple of CLI flags.
var TrigConfig = `
## Set the amplitude
amplitude = 10.0
`
func (s *Trig) SampleConfig() string {
return TrigConfig
}
ยฉ 2018 InfluxData. All rights reserved.24
Add a simple description of your plugin
This description is included above the plugin configuration and
should briefly describe what your plugin does
func (s *Trig) Description() string {
return "Inserts sine and cosine waves for demonstration purposes"
}
ยฉ 2018 InfluxData. All rights reserved.25
Test it!
$ make
$ telegraf -sample-config -input-filter trig -output-filter influxdb
-debug
โ€ฆ
#
# INPUTS:
#
# Inserts sine and cosine waves for demonstration purposes
[[inputs.trig]]
## Set the amplitude
amplitude = 10.0
ยฉ 2017 InfluxData. All rights reserved.26
The Gather (telegraf.Accumulator) Function
โ€ข The Gather function is the heart of the plugin and is run every
time telegraf collects metrics.
โ€ข This number is configurable via the [agent]interval=10s
configuration option in the top level config in telegraf.
โ€ข Plugins can also be written to collect at different intervals.
โ€ข The important function is the
telegraf.Accumulator.AddFields(measurement,
tags, fields). This defines a new point in influxdb with
the timestamp being the collection time.
ยฉ 2018 InfluxData. All rights reserved.27
Write your Gather function!
func (s *Trig) Gather(acc telegraf.Accumulator) error {
sinner := math.Sin((s.x*math.Pi)/5.0) * s.Amplitude
cosinner := math.Cos((s.x*math.Pi)/5.0) * s.Amplitude
fields := make(map[string]interface{})
fields["sine"] = sinner
fields["cosine"] = cosinner
tags := make(map[string]string)
s.x += 1.0
acc.AddFields("trig", fields, tags)
return nil
}
ยฉ 2018 InfluxData. All rights reserved.28
Add starting state
Starting state can be defined in the struct you pass to the
input.Add() function. For our example we need to initialize x
This function packages up the whole plugin (all the methods are
defined on the Trig{} struct) and gives it to the Telegraf agent.
The agent iterates through all of the plugins that are enabled every
collection and calls their gather
func init() {
inputs.Add("trig", func() telegraf.Input { return &Trig{x: 0.0} })
}
ยฉ 2018 InfluxData. All rights reserved.29
Compile and test your plugin
$ make
$ telegraf -sample-config -input-filter trig -output-filter influxdb >>
telegraf.conf.test
$ telegraf -config telegraf.conf.test -debug
2016/03/24 11:50:26 Starting Telegraf (version 1.3.0-223-gf543dbb)
2016/03/24 11:50:26 Loaded outputs: influxdb
2016/03/24 11:50:26 Loaded inputs: trig
2016/03/24 11:50:26 Tags enabled: host=XXXXXX.local
2016/03/24 11:50:26 Agent Config: Interval:10s, Debug:false, Quiet:false,
Hostname:"XXXXXX.local", Flush Interval:10s
2016/03/24 11:50:30 Gathered metrics, (10s interval), from 1 inputs in
237.28ยตs
ยฉ 2018 InfluxData. All rights reserved.30
Graph it with Chronograf!
ยฉ 2018 InfluxData. All rights reserved.31
Write some tests...
package trig
import (
"math"
"testing"
"github.com/influxdata/telegraf/testutil"
)
func TestTrig(t *testing.T) {
s := &Trig{
Amplitude: 10.0,
}
for i := 0.0; i < 10.0; i++ {
var acc testutil.Accumulator
sine := math.Sin((i*math.Pi)/5.0) * s.Amplitude
cosine := math.Cos((i*math.Pi)/5.0) * s.Amplitude
s.Gather(&acc)
fields := make(map[string]interface{})
fields["sine"] = sine
fields["cosine"] = cosine
acc.AssertContainsFields(t, "trig", fields)
}
}
ยฉ 2018 InfluxData. All rights reserved.32
How to submit a plugin
Must include:
โ€ข a README.md file,
โ€ข a LICENSE file,
โ€ข and a sample of the output/input format
https://github.com/influxdata/telegraf/pulls
noah@influxdata.com @noahcrowley

More Related Content

PDF
Intro to Telegraf
PDF
Apache Bigtop3.2 (ไปฎ)๏ผˆOpen Source Conference 2022 Online/Hiroshima ็™บ่กจ่ณ‡ๆ–™๏ผ‰
PDF
Building a Telegraf Plugin by Noah Crowly | Developer Advocate | InfluxData
PDF
Terraform
PDF
InfluxDB + Telegraf Operator: Easy Kubernetes Monitoring
PPTX
Takalab ๅ‹‰ๅผทไผš#01 - Kali Linux ็’ฐๅขƒๆง‹็ฏ‰
PDF
Terraform
PPTX
Linux Network Stack
Intro to Telegraf
Apache Bigtop3.2 (ไปฎ)๏ผˆOpen Source Conference 2022 Online/Hiroshima ็™บ่กจ่ณ‡ๆ–™๏ผ‰
Building a Telegraf Plugin by Noah Crowly | Developer Advocate | InfluxData
Terraform
InfluxDB + Telegraf Operator: Easy Kubernetes Monitoring
Takalab ๅ‹‰ๅผทไผš#01 - Kali Linux ็’ฐๅขƒๆง‹็ฏ‰
Terraform
Linux Network Stack

What's hot (20)

PDF
BPF / XDP 8์›” ์„ธ๋ฏธ๋‚˜ KossLab
PDF
Terraform Best Practices - DevOps Unicorns 2019
PPTX
OpenStackใƒฆใƒผใ‚ตใ‚™ไผš่ณ‡ๆ–™ - Masakari
PDF
Terraform
PDF
BPF: Tracing and more
PPTX
How to Introduce Telemetry Streaming (gNMI) in Your Network with SNMP with Te...
PDF
Redis cluster
PDF
Kubernetes Deployment Strategies
PDF
Scouter์™€ influx db โ€“ grafana ์—ฐ๋™ ๊ฐ€์ด๋“œ
PDF
All about InfluxDB.
PDF
Apache kafka ๋ชจ๋‹ˆํ„ฐ๋ง์„ ์œ„ํ•œ Metrics ์ดํ•ด ๋ฐ ์ตœ์ ํ™” ๋ฐฉ์•ˆ
PPTX
Deep Dive into the Linux Kernel - ใƒกใƒขใƒช็ฎก็†ใซใŠใ‘ใ‚‹CompactionๆฉŸ่ƒฝใซใคใ„ใฆ
PPTX
PDF
Elasticsearch Tutorial | Getting Started with Elasticsearch | ELK Stack Train...
PDF
IoTๆ™‚ไปฃใซใŠใ‘ใ‚‹ใ‚นใƒˆใƒชใƒผใƒ ใƒ‡ใƒผใ‚ฟๅ‡ฆ็†ใจๆ€ฅๆˆ้•ทใฎ Apache Flink
PPTX
Tutorial: Using GoBGP as an IXP connecting router
PPTX
Building Data Pipelines for Solr with Apache NiFi
PPTX
Infrastructure-as-Code (IaC) using Terraform
PDF
Running Apache NiFi with Apache Spark : Integration Options
PDF
Elasticsearch in Netflix
BPF / XDP 8์›” ์„ธ๋ฏธ๋‚˜ KossLab
Terraform Best Practices - DevOps Unicorns 2019
OpenStackใƒฆใƒผใ‚ตใ‚™ไผš่ณ‡ๆ–™ - Masakari
Terraform
BPF: Tracing and more
How to Introduce Telemetry Streaming (gNMI) in Your Network with SNMP with Te...
Redis cluster
Kubernetes Deployment Strategies
Scouter์™€ influx db โ€“ grafana ์—ฐ๋™ ๊ฐ€์ด๋“œ
All about InfluxDB.
Apache kafka ๋ชจ๋‹ˆํ„ฐ๋ง์„ ์œ„ํ•œ Metrics ์ดํ•ด ๋ฐ ์ตœ์ ํ™” ๋ฐฉ์•ˆ
Deep Dive into the Linux Kernel - ใƒกใƒขใƒช็ฎก็†ใซใŠใ‘ใ‚‹CompactionๆฉŸ่ƒฝใซใคใ„ใฆ
Elasticsearch Tutorial | Getting Started with Elasticsearch | ELK Stack Train...
IoTๆ™‚ไปฃใซใŠใ‘ใ‚‹ใ‚นใƒˆใƒชใƒผใƒ ใƒ‡ใƒผใ‚ฟๅ‡ฆ็†ใจๆ€ฅๆˆ้•ทใฎ Apache Flink
Tutorial: Using GoBGP as an IXP connecting router
Building Data Pipelines for Solr with Apache NiFi
Infrastructure-as-Code (IaC) using Terraform
Running Apache NiFi with Apache Spark : Integration Options
Elasticsearch in Netflix
Ad

Similar to Write your own telegraf plugin (20)

PDF
How to Build a Telegraf Plugin by Noah Crowley
PDF
Virtual training Intro to InfluxDB & Telegraf
PDF
Getting Started: Intro to Telegraf - July 2021
PDF
OSMC 2024 | Telegraf โ€“ A data collection agent by Sven Rebhan.pdf
ย 
PPTX
How to Use Telegraf and Its Plugin Ecosystem
PDF
Nicolas Steinmetz [CรฉrรฉnIT] | Sustain Your Observability from Bare Metal TICK...
PDF
Virtual training Intro to Kapacitor
PDF
Taming the Tiger: Tips and Tricks for Using Telegraf
PDF
Advanced kapacitor
PPTX
Taming the Tiger: Tips and Tricks for Using Telegraf
PDF
Why Open Source Works for DevOps Monitoring
PDF
OSMC 2019 | The Telegraf Toolbelt: It Can Do That, Really? by David McKay
ย 
PDF
The Telegraf Toolbelt: It Can Do That, Really? | David McKay | InfluxData
PDF
The Telegraf Toolbelt | David McKay | InfluxData
PDF
OSMC 2018 | Distributed Tracing FAQ by Gianluca Arbezzano
ย 
PPTX
Anais Dotis-Georgiou & Faith Chikwekwe [InfluxData] | Top 10 Hurdles for Flux...
PDF
Overlay Technique | Pebble Developer Retreat 2014
PDF
Finding OOMS in Legacy Systems with the Syslog Telegraf Plugin
PDF
Sensor Data in InfluxDB by David Simmons, IoT Developer Evangelist | InfluxData
PDF
P4 Introduction
How to Build a Telegraf Plugin by Noah Crowley
Virtual training Intro to InfluxDB & Telegraf
Getting Started: Intro to Telegraf - July 2021
OSMC 2024 | Telegraf โ€“ A data collection agent by Sven Rebhan.pdf
ย 
How to Use Telegraf and Its Plugin Ecosystem
Nicolas Steinmetz [CรฉrรฉnIT] | Sustain Your Observability from Bare Metal TICK...
Virtual training Intro to Kapacitor
Taming the Tiger: Tips and Tricks for Using Telegraf
Advanced kapacitor
Taming the Tiger: Tips and Tricks for Using Telegraf
Why Open Source Works for DevOps Monitoring
OSMC 2019 | The Telegraf Toolbelt: It Can Do That, Really? by David McKay
ย 
The Telegraf Toolbelt: It Can Do That, Really? | David McKay | InfluxData
The Telegraf Toolbelt | David McKay | InfluxData
OSMC 2018 | Distributed Tracing FAQ by Gianluca Arbezzano
ย 
Anais Dotis-Georgiou & Faith Chikwekwe [InfluxData] | Top 10 Hurdles for Flux...
Overlay Technique | Pebble Developer Retreat 2014
Finding OOMS in Legacy Systems with the Syslog Telegraf Plugin
Sensor Data in InfluxDB by David Simmons, IoT Developer Evangelist | InfluxData
P4 Introduction
Ad

More from InfluxData (20)

PPTX
Announcing InfluxDB Clustered
PDF
Best Practices for Leveraging the Apache Arrow Ecosystem
PDF
How Bevi Uses InfluxDB and Grafana to Improve Predictive Maintenance and Redu...
PDF
Power Your Predictive Analytics with InfluxDB
PDF
How Terรฉga Replaces Legacy Data Historians with InfluxDB, AWS and IO-Base
PDF
Build an Edge-to-Cloud Solution with the MING Stack
PDF
Meet the Founders: An Open Discussion About Rewriting Using Rust
PDF
Introducing InfluxDB Cloud Dedicated
PDF
Gain Better Observability with OpenTelemetry and InfluxDB
PPTX
How a Heat Treating Plant Ensures Tight Process Control and Exceptional Quali...
PDF
How Delft University's Engineering Students Make Their EV Formula-Style Race ...
PPTX
Introducing InfluxDBโ€™s New Time Series Database Storage Engine
PDF
Start Automating InfluxDB Deployments at the Edge with balena
PDF
Understanding InfluxDBโ€™s New Storage Engine
PDF
Streamline and Scale Out Data Pipelines with Kubernetes, Telegraf, and InfluxDB
PPTX
Ward Bowman [PTC] | ThingWorx Long-Term Data Storage with InfluxDB | InfluxDa...
PDF
Scott Anderson [InfluxData] | New & Upcoming Flux Features | InfluxDays 2022
PDF
Steinkamp, Clifford [InfluxData] | Closing Thoughts | InfluxDays 2022
PDF
Steinkamp, Clifford [InfluxData] | Welcome to InfluxDays 2022 - Day 2 | Influ...
PDF
Steinkamp, Clifford [InfluxData] | Closing Thoughts Day 1 | InfluxDays 2022
Announcing InfluxDB Clustered
Best Practices for Leveraging the Apache Arrow Ecosystem
How Bevi Uses InfluxDB and Grafana to Improve Predictive Maintenance and Redu...
Power Your Predictive Analytics with InfluxDB
How Terรฉga Replaces Legacy Data Historians with InfluxDB, AWS and IO-Base
Build an Edge-to-Cloud Solution with the MING Stack
Meet the Founders: An Open Discussion About Rewriting Using Rust
Introducing InfluxDB Cloud Dedicated
Gain Better Observability with OpenTelemetry and InfluxDB
How a Heat Treating Plant Ensures Tight Process Control and Exceptional Quali...
How Delft University's Engineering Students Make Their EV Formula-Style Race ...
Introducing InfluxDBโ€™s New Time Series Database Storage Engine
Start Automating InfluxDB Deployments at the Edge with balena
Understanding InfluxDBโ€™s New Storage Engine
Streamline and Scale Out Data Pipelines with Kubernetes, Telegraf, and InfluxDB
Ward Bowman [PTC] | ThingWorx Long-Term Data Storage with InfluxDB | InfluxDa...
Scott Anderson [InfluxData] | New & Upcoming Flux Features | InfluxDays 2022
Steinkamp, Clifford [InfluxData] | Closing Thoughts | InfluxDays 2022
Steinkamp, Clifford [InfluxData] | Welcome to InfluxDays 2022 - Day 2 | Influ...
Steinkamp, Clifford [InfluxData] | Closing Thoughts Day 1 | InfluxDays 2022

Recently uploaded (20)

PPTX
Slides PPTX World Game (s) Eco Economic Epochs.pptx
PDF
Triggering QUIC, presented by Geoff Huston at IETF 123
ย 
PDF
๐Ÿ’ฐ ๐”๐Š๐“๐ˆ ๐Š๐„๐Œ๐„๐๐€๐๐†๐€๐ ๐Š๐ˆ๐๐„๐‘๐Ÿ’๐ƒ ๐‡๐€๐‘๐ˆ ๐ˆ๐๐ˆ ๐Ÿ๐ŸŽ๐Ÿ๐Ÿ“ ๐Ÿ’ฐ
ย 
PDF
RPKI Status Update, presented by Makito Lay at IDNOG 10
ย 
PPTX
Internet___Basics___Styled_ presentation
PPTX
June-4-Sermon-Powerpoint.pptx USE THIS FOR YOUR MOTIVATION
PDF
Vigrab.top โ€“ Online Tool for Downloading and Converting Social Media Videos a...
PPTX
PptxGenJS_Demo_Chart_20250317130215833.pptx
PPTX
Job_Card_System_Styled_lorem_ipsum_.pptx
PDF
Tenda Login Guide: Access Your Router in 5 Easy Steps
PPTX
SAP Ariba Sourcing PPT for learning material
PDF
Paper PDF World Game (s) Great Redesign.pdf
PPTX
artificial intelligence overview of it and more
PPTX
Module 1 - Cyber Law and Ethics 101.pptx
PPT
Design_with_Watersergyerge45hrbgre4top (1).ppt
PDF
The New Creative Director: How AI Tools for Social Media Content Creation Are...
PPTX
Introduction about ICD -10 and ICD11 on 5.8.25.pptx
PDF
Unit-1 introduction to cyber security discuss about how to secure a system
PDF
Decoding a Decade: 10 Years of Applied CTI Discipline
PPTX
INTERNET------BASICS-------UPDATED PPT PRESENTATION
Slides PPTX World Game (s) Eco Economic Epochs.pptx
Triggering QUIC, presented by Geoff Huston at IETF 123
ย 
๐Ÿ’ฐ ๐”๐Š๐“๐ˆ ๐Š๐„๐Œ๐„๐๐€๐๐†๐€๐ ๐Š๐ˆ๐๐„๐‘๐Ÿ’๐ƒ ๐‡๐€๐‘๐ˆ ๐ˆ๐๐ˆ ๐Ÿ๐ŸŽ๐Ÿ๐Ÿ“ ๐Ÿ’ฐ
ย 
RPKI Status Update, presented by Makito Lay at IDNOG 10
ย 
Internet___Basics___Styled_ presentation
June-4-Sermon-Powerpoint.pptx USE THIS FOR YOUR MOTIVATION
Vigrab.top โ€“ Online Tool for Downloading and Converting Social Media Videos a...
PptxGenJS_Demo_Chart_20250317130215833.pptx
Job_Card_System_Styled_lorem_ipsum_.pptx
Tenda Login Guide: Access Your Router in 5 Easy Steps
SAP Ariba Sourcing PPT for learning material
Paper PDF World Game (s) Great Redesign.pdf
artificial intelligence overview of it and more
Module 1 - Cyber Law and Ethics 101.pptx
Design_with_Watersergyerge45hrbgre4top (1).ppt
The New Creative Director: How AI Tools for Social Media Content Creation Are...
Introduction about ICD -10 and ICD11 on 5.8.25.pptx
Unit-1 introduction to cyber security discuss about how to secure a system
Decoding a Decade: 10 Years of Applied CTI Discipline
INTERNET------BASICS-------UPDATED PPT PRESENTATION

Write your own telegraf plugin

  • 1. Noah Crowley, Developer Advocate Getting Started Series Write your Own Telegraf plugin
  • 2. ยฉ 2017 InfluxData. All rights reserved.2 ยฉ 2017 InfluxData. All rights reserved.2 โœ“ Telegraf overview โœ“ Examples of Telegraf plugins โœ“ Telegraf plugin architecture โœ“ How to a write a Telegraf plugin (demo) What we will be covering
  • 3. ยฉ 2017 InfluxData. All rights reserved.3 ยฉ 2017 InfluxData. All rights reserved.3 Telegraf Overview
  • 4. ยฉ 2018 InfluxData. All rights reserved.4 InfluxData Open Source Projects: TELEGRAF!
  • 5. ยฉ 2018 InfluxData. All rights reserved.5 InfluxData Open Source Projects: TELEGRAF!
  • 6. ยฉ 2018 InfluxData. All rights reserved.6 Telegraf Telegraf is an Open Source plugin-driven server agent for collecting & reporting metrics: โ€œPushโ€ or โ€œPullโ€. โ€ข Used to batch metrics from multiple sources together in order to reduce the number of atomic write requests โ€ข Large collection of Plugins: โ€“ Inputs โ€“ Outputs โ€“ Aggregators โ€“ Processors โ€ข Can act as an: โ€“ Agent โ€“ Collector โ€“ Ingest Pipeline
  • 7. ยฉ 2018 InfluxData. All rights reserved.7 Telegraf continued โ€ข Metrics ingestion from the host system (cpu, I/O, network) โ€“ Common programs (Hadoop, Postgres, Redis, etc.) โ€“ Third party APIs (Mailchimp, CloudWatch, Google Analytics, etc.) โ€ข Output plugins to send metrics to a variety of other datastores, services, and message queues, including โ€“ InfluxDB, Graphite, OpenTSDB, Datadog, Librato, Kafka, MQTT, NSQ, Prometheus, and many others โ€ข Over 140 plugins!
  • 8. ยฉ 2018 InfluxData. All rights reserved.8 Telegraf Benefits โ€ข Although if we start using it then itโ€™s kind of the de-facto tool anywayโ€ฆ โ€ข Minimal memory footprint โ€ข Tagging of metrics โ€ข Easy contribution of functionality โ€ข Strong Community contributions
  • 9. ยฉ 2017 InfluxData. All rights reserved.9 ยฉ 2017 InfluxData. All rights reserved.9 Examples of Telegraf plugins
  • 10. ยฉ 2017 InfluxData. All rights reserved.10 Some Telegraf plugins statsd Listens for data in statsd format and outputs it to any configured output apache Collects data from the /server-status?auto endpoint and stores it postgresql Collects performance metrics about postgres; uses data from the built in pg_stat_database and pg_stat_bgwriter views win_perf_counters Collects performance data from Windows machines
  • 11. ยฉ 2017 InfluxData. All rights reserved.11 Some Telegraf plugins prometheus_client Exposes all metrics on /metrics to be polled by a Prometheus server nginx Read Nginx's basic status (ngx_http_stub_status_module) histogram Creates histograms containing the counts of field values within a range kubernetes Experimental; Talks to the kubelet api using the /stats/summary endpoint
  • 12. ยฉ 2018 InfluxData. All rights reserved.12 How do I install it? Download binaries at influxdata.com/downloads then: Telegraf normally runs as a Linux service $ telegraf -sample-config -input-filter haproxy -output-filter influxdb > telegraf.conf $ telegraf -config telegraf.conf $ telegraf -sample-config -input-filter haproxy -output-filter influxdb > /etc/telegraf/telegraf.conf $ service telegraf start
  • 13. ยฉ 2017 InfluxData. All rights reserved.13 ยฉ 2017 InfluxData. All rights reserved.13 Telegraf plugin architecture
  • 14. ยฉ 2018 InfluxData. All rights reserved.14 The Open Source Collector Agent: Telegraf
  • 15. ยฉ 2018 InfluxData. All rights reserved.15 Input interface The plugin must conform to the telegraf.Input interface: type Input interface { // SampleConfig returns the default configuration of the Input SampleConfig() string // Description returns a one-sentence description on the Input Description() string // Gather takes in an accumulator and adds the metrics that the Input // gathers. This is called every "interval" Gather(Accumulator) error }
  • 16. ยฉ 2017 InfluxData. All rights reserved.16 ยฉ 2017 InfluxData. All rights reserved.16 Letโ€™s write a plugin
  • 17. ยฉ 2017 InfluxData. All rights reserved.17 What do you need? โ€ข Go 1.8+ installed and configured on your computer (1.9+ recommended) โ€ข A desire to write a Telegraf plugin โ€ข What is our plugin going to do? โ€ข It will emit a trigonometric pattern called `trig`
  • 18. ยฉ 2018 InfluxData. All rights reserved.18 Getting Started This will create a new Telegraf binary at $GOPATH/bin/telegraf $ go get github.com/influxdata/telegraf $ cd $GOPATH/github.com/influxdata/telegraf $ git checkout -b mySweetPlugin $ make
  • 19. ยฉ 2018 InfluxData. All rights reserved.19 Make your files and add boilerplate $ cd plugins/inputs $ mkdir trig $ touch trig/trig.go # Add boilerplate $ cat trig/trig.go
  • 20. ยฉ 2018 InfluxData. All rights reserved.20 Boilerplate $ cat trig/trig.go package trig import ( "github.com/influxdata/telegraf" "github.com/influxdata/telegraf/plugins/inputs" ) type Trig struct { } func (s *Trig) SampleConfig() string { return "" } func (s *Trig) Description() string { return "" } func (s *Trig) Gather(acc telegraf.Accumulator) error { return nil } func init() { inputs.Add("trig", func() telegraf.Input { return &Trig{} }) }
  • 21. ยฉ 2018 InfluxData. All rights reserved.21 Import your plugin In the file -> telegraf/plugins/inputs/all/all.go Add: This imports your plugin to the main telegraf package and ensures that it can run _ "github.com/influxdata/telegraf/plugins/inputs/trig"
  • 22. ยฉ 2018 InfluxData. All rights reserved.22 Add properties to your struct There must be a property on the struct to hold any configuration variables Any other variables should be unexported. x will hold state of the plugin between collection intervals, being updated each one. type Trig struct { x float64 Amplitude float64 }
  • 23. ยฉ 2018 InfluxData. All rights reserved.23 Add any configuration variables you need Telegraf dynamically constructs configuration files by aggregating the configurations for all of itโ€™s plugins. This allows you to easily generate configuration files with only certain plugins enabled by just passing a couple of CLI flags. var TrigConfig = ` ## Set the amplitude amplitude = 10.0 ` func (s *Trig) SampleConfig() string { return TrigConfig }
  • 24. ยฉ 2018 InfluxData. All rights reserved.24 Add a simple description of your plugin This description is included above the plugin configuration and should briefly describe what your plugin does func (s *Trig) Description() string { return "Inserts sine and cosine waves for demonstration purposes" }
  • 25. ยฉ 2018 InfluxData. All rights reserved.25 Test it! $ make $ telegraf -sample-config -input-filter trig -output-filter influxdb -debug โ€ฆ # # INPUTS: # # Inserts sine and cosine waves for demonstration purposes [[inputs.trig]] ## Set the amplitude amplitude = 10.0
  • 26. ยฉ 2017 InfluxData. All rights reserved.26 The Gather (telegraf.Accumulator) Function โ€ข The Gather function is the heart of the plugin and is run every time telegraf collects metrics. โ€ข This number is configurable via the [agent]interval=10s configuration option in the top level config in telegraf. โ€ข Plugins can also be written to collect at different intervals. โ€ข The important function is the telegraf.Accumulator.AddFields(measurement, tags, fields). This defines a new point in influxdb with the timestamp being the collection time.
  • 27. ยฉ 2018 InfluxData. All rights reserved.27 Write your Gather function! func (s *Trig) Gather(acc telegraf.Accumulator) error { sinner := math.Sin((s.x*math.Pi)/5.0) * s.Amplitude cosinner := math.Cos((s.x*math.Pi)/5.0) * s.Amplitude fields := make(map[string]interface{}) fields["sine"] = sinner fields["cosine"] = cosinner tags := make(map[string]string) s.x += 1.0 acc.AddFields("trig", fields, tags) return nil }
  • 28. ยฉ 2018 InfluxData. All rights reserved.28 Add starting state Starting state can be defined in the struct you pass to the input.Add() function. For our example we need to initialize x This function packages up the whole plugin (all the methods are defined on the Trig{} struct) and gives it to the Telegraf agent. The agent iterates through all of the plugins that are enabled every collection and calls their gather func init() { inputs.Add("trig", func() telegraf.Input { return &Trig{x: 0.0} }) }
  • 29. ยฉ 2018 InfluxData. All rights reserved.29 Compile and test your plugin $ make $ telegraf -sample-config -input-filter trig -output-filter influxdb >> telegraf.conf.test $ telegraf -config telegraf.conf.test -debug 2016/03/24 11:50:26 Starting Telegraf (version 1.3.0-223-gf543dbb) 2016/03/24 11:50:26 Loaded outputs: influxdb 2016/03/24 11:50:26 Loaded inputs: trig 2016/03/24 11:50:26 Tags enabled: host=XXXXXX.local 2016/03/24 11:50:26 Agent Config: Interval:10s, Debug:false, Quiet:false, Hostname:"XXXXXX.local", Flush Interval:10s 2016/03/24 11:50:30 Gathered metrics, (10s interval), from 1 inputs in 237.28ยตs
  • 30. ยฉ 2018 InfluxData. All rights reserved.30 Graph it with Chronograf!
  • 31. ยฉ 2018 InfluxData. All rights reserved.31 Write some tests... package trig import ( "math" "testing" "github.com/influxdata/telegraf/testutil" ) func TestTrig(t *testing.T) { s := &Trig{ Amplitude: 10.0, } for i := 0.0; i < 10.0; i++ { var acc testutil.Accumulator sine := math.Sin((i*math.Pi)/5.0) * s.Amplitude cosine := math.Cos((i*math.Pi)/5.0) * s.Amplitude s.Gather(&acc) fields := make(map[string]interface{}) fields["sine"] = sine fields["cosine"] = cosine acc.AssertContainsFields(t, "trig", fields) } }
  • 32. ยฉ 2018 InfluxData. All rights reserved.32 How to submit a plugin Must include: โ€ข a README.md file, โ€ข a LICENSE file, โ€ข and a sample of the output/input format https://github.com/influxdata/telegraf/pulls