telegraf

package module
v0.0.0-...-16a0e24 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 25, 2020 License: MIT Imports: 1 Imported by: 0

README

Telegraf二开版本

此工程主要为了能够方便的将telegraf嵌入其它golang工程之中,主要改造了telegraf的启动入口,以及配置文件加载方式

调用示例

package monitor

import (
	"fmt"
	"gitee.com/zhimiao/qiansi-telegraf/cmd/telegraf"
)
// 获取服务器远程配置(其实就是telegraf的配置文件内容)
func telegrafRemoteData() (result []byte, err error) {
	return
}
// 启动
func Start() error {
	// 获取服务器远程配置
	data, err := telegrafRemoteData()
	if err != nil {
		return err
	}
	telegraf.Start(data)
	return nil
}
// 重启
func Restart() error {
	// 获取服务器远程配置
	data, err := telegrafRemoteData()
	if err != nil {
		return err
	}
	telegraf.Restart(data)
	return nil
}

自定义插件注入方法

// 采集&输出插件重点在于init方法中的添加操作,因此无论放在哪里,只要能通过`outputs.Add` 或者 `inputs.Add` 在运行前添加到插件列表中就可以了
package monitor

import (
	"fmt"
	"gitee.com/zhimiao/qiansi-client/common"
	"gitee.com/zhimiao/qiansi-client/request"
	"gitee.com/zhimiao/qiansi-telegraf"
	"gitee.com/zhimiao/qiansi-telegraf/plugins/outputs"
	"gitee.com/zhimiao/qiansi-telegraf/plugins/serializers"
	"time"
)

type Qiansi struct {
	Ok bool `toml:"ok"`
}

func (s *Qiansi) Description() string {
	return "qiansi client output"
}

func (s *Qiansi) SampleConfig() string {
	return ` 
  ok = true
`
}

func (s *Qiansi) Init() error {
	return nil
}

func (s *Qiansi) Connect() error {
	// Make a connection to the URL here
	return nil
}

func (s *Qiansi) Close() error {
	// Close connection to the URL here
	return nil
}

func (s *Qiansi) Write(metrics []telegraf.Metric) error {
	ser, _ := serializers.NewJsonSerializer(time.Second)
	r, _ := ser.SerializeBatch(metrics)
	fmt.Println(string(r))
	if common.Config.ENV() == "dev" {
		fmt.Println(string(r))
	}
	// 推送到千丝服务端
	request.MetricPush(r)
	return nil
}

func init() {
	outputs.Add("qiansi", func() telegraf.Output { return &Qiansi{} })
}

Telegraf Circle CI Docker pulls

Telegraf is an agent for collecting, processing, aggregating, and writing metrics.

Design goals are to have a minimal memory footprint with a plugin system so that developers in the community can easily add support for collecting metrics.

Telegraf is plugin-driven and has the concept of 4 distinct plugin types:

  1. Input Plugins collect metrics from the system, services, or 3rd party APIs
  2. Processor Plugins transform, decorate, and/or filter metrics
  3. Aggregator Plugins create aggregate metrics (e.g. mean, min, max, quantiles, etc.)
  4. Output Plugins write metrics to various destinations

New plugins are designed to be easy to contribute, pull requests are welcomed and we work to incorporate as many pull requests as possible.

Try in Browser 🚀

You can try Telegraf right in your browser in the Telegraf playground.

Contributing

There are many ways to contribute:

Minimum Requirements

Telegraf shares the same minimum requirements as Go:

  • Linux kernel version 2.6.23 or later
  • Windows 7 or later
  • FreeBSD 11.2 or later
  • MacOS 10.11 El Capitan or later

Installation:

You can download the binaries directly from the downloads page or from the releases section.

Ansible Role:

Ansible role: https://github.com/rossmcdonald/telegraf

From Source:

Telegraf requires Go version 1.12 or newer, the Makefile requires GNU make.

  1. Install Go >=1.12 (1.13 recommended)
  2. Clone the Telegraf repository:
    cd ~/src
    git clone https://github.com/influxdata/telegraf.git
    
  3. Run make from the source directory
    cd ~/src/telegraf
    make
    
Changelog

View the changelog for the latest updates and changes by version.

Nightly Builds

These builds are generated from the master branch:

How to use it:

See usage with:

telegraf --help
Generate a telegraf config file:
telegraf config > telegraf.conf
Generate config with only cpu input & influxdb output plugins defined:
telegraf --section-filter agent:inputs:outputs --input-filter cpu --output-filter influxdb config
Run a single telegraf collection, outputting metrics to stdout:
telegraf --config telegraf.conf --test
Run telegraf with all plugins defined in config file:
telegraf --config telegraf.conf
Run telegraf, enabling the cpu & memory input, and influxdb output plugins:
telegraf --config telegraf.conf --input-filter cpu:mem --output-filter influxdb

Documentation

Latest Release Documentation.

For documentation on the latest development code see the documentation index.

Input Plugins

Parsers

Serializers

Processor Plugins

Aggregator Plugins

Output Plugins

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Accumulator

type Accumulator interface {
	// AddFields adds a metric to the accumulator with the given measurement
	// name, fields, and tags (and timestamp). If a timestamp is not provided,
	// then the accumulator sets it to "now".
	AddFields(measurement string,
		fields map[string]interface{},
		tags map[string]string,
		t ...time.Time)

	// AddGauge is the same as AddFields, but will add the metric as a "Gauge" type
	AddGauge(measurement string,
		fields map[string]interface{},
		tags map[string]string,
		t ...time.Time)

	// AddCounter is the same as AddFields, but will add the metric as a "Counter" type
	AddCounter(measurement string,
		fields map[string]interface{},
		tags map[string]string,
		t ...time.Time)

	// AddSummary is the same as AddFields, but will add the metric as a "Summary" type
	AddSummary(measurement string,
		fields map[string]interface{},
		tags map[string]string,
		t ...time.Time)

	// AddHistogram is the same as AddFields, but will add the metric as a "Histogram" type
	AddHistogram(measurement string,
		fields map[string]interface{},
		tags map[string]string,
		t ...time.Time)

	// AddMetric adds an metric to the accumulator.
	AddMetric(Metric)

	// SetPrecision sets the timestamp rounding precision.  All metrics addeds
	// added to the accumulator will have their timestamp rounded to the
	// nearest multiple of precision.
	SetPrecision(precision time.Duration)

	// Report an error.
	AddError(err error)

	// Upgrade to a TrackingAccumulator with space for maxTracked
	// metrics/batches.
	WithTracking(maxTracked int) TrackingAccumulator
}

Accumulator allows adding metrics to the processing flow.

type AggregatingOutput

type AggregatingOutput interface {
	Output

	// Add the metric to the aggregator
	Add(in Metric)
	// Push returns the aggregated metrics and is called every flush interval.
	Push() []Metric
	// Reset signals the the aggregator period is completed.
	Reset()
}

AggregatingOutput adds aggregating functionality to an Output. May be used if the Output only accepts a fixed set of aggregations over a time period. These functions may be called concurrently to the Write function.

type Aggregator

type Aggregator interface {
	// SampleConfig returns the default configuration of the Input.
	SampleConfig() string

	// Description returns a one-sentence description on the Input.
	Description() string

	// Add the metric to the aggregator.
	Add(in Metric)

	// Push pushes the current aggregates to the accumulator.
	Push(acc Accumulator)

	// Reset resets the aggregators caches and aggregates.
	Reset()
}

Aggregator is an interface for implementing an Aggregator plugin. the RunningAggregator wraps this interface and guarantees that Add, Push, and Reset can not be called concurrently, so locking is not required when implementing an Aggregator plugin.

type DeliveryInfo

type DeliveryInfo interface {
	// ID is the TrackingID
	ID() TrackingID

	// Delivered returns true if the metric was processed successfully.
	Delivered() bool
}

DeliveryInfo provides the results of a delivered metric group.

type Field

type Field struct {
	Key   string
	Value interface{}
}

Field represents a single field key and value.

type Initializer

type Initializer interface {
	// Init performs one time setup of the plugin and returns an error if the
	// configuration is invalid.
	Init() error
}

Initializer is an interface that all plugin types: Inputs, Outputs, Processors, and Aggregators can optionally implement to initialize the plugin.

type Input

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
}

type Logger

type Logger interface {
	// Errorf logs an error message, patterned after log.Printf.
	Errorf(format string, args ...interface{})
	// Error logs an error message, patterned after log.Print.
	Error(args ...interface{})
	// Debugf logs a debug message, patterned after log.Printf.
	Debugf(format string, args ...interface{})
	// Debug logs a debug message, patterned after log.Print.
	Debug(args ...interface{})
	// Warnf logs a warning message, patterned after log.Printf.
	Warnf(format string, args ...interface{})
	// Warn logs a warning message, patterned after log.Print.
	Warn(args ...interface{})
	// Infof logs an information message, patterned after log.Printf.
	Infof(format string, args ...interface{})
	// Info logs an information message, patterned after log.Print.
	Info(args ...interface{})
}

Logger defines an interface for logging.

type Metric

type Metric interface {
	// Name is the primary identifier for the Metric and corresponds to the
	// measurement in the InfluxDB data model.
	Name() string

	// Tags returns the tags as a map.  This method is deprecated, use TagList instead.
	Tags() map[string]string

	// TagList returns the tags as a slice ordered by the tag key in lexical
	// bytewise ascending order.  The returned value should not be modified,
	// use the AddTag or RemoveTag methods instead.
	TagList() []*Tag

	// Fields returns the fields as a map.  This method is deprecated, use FieldList instead.
	Fields() map[string]interface{}

	// FieldList returns the fields as a slice in an undefined order.  The
	// returned value should not be modified, use the AddField or RemoveField
	// methods instead.
	FieldList() []*Field

	// Time returns the timestamp of the metric.
	Time() time.Time

	// Type returns a general type for the entire metric that describes how you
	// might interpret, aggregate the values.
	//
	// This method may be removed in the future and its use is discouraged.
	Type() ValueType

	// SetName sets the metric name.
	SetName(name string)

	// AddPrefix adds a string to the front of the metric name.  It is
	// equivalent to m.SetName(prefix + m.Name()).
	//
	// This method is deprecated, use SetName instead.
	AddPrefix(prefix string)

	// AddSuffix appends a string to the back of the metric name.  It is
	// equivalent to m.SetName(m.Name() + suffix).
	//
	// This method is deprecated, use SetName instead.
	AddSuffix(suffix string)

	// GetTag returns the value of a tag and a boolean to indicate if it was set.
	GetTag(key string) (string, bool)

	// HasTag returns true if the tag is set on the Metric.
	HasTag(key string) bool

	// AddTag sets the tag on the Metric.  If the Metric already has the tag
	// set then the current value is replaced.
	AddTag(key, value string)

	// RemoveTag removes the tag if it is set.
	RemoveTag(key string)

	// GetField returns the value of a field and a boolean to indicate if it was set.
	GetField(key string) (interface{}, bool)

	// HasField returns true if the field is set on the Metric.
	HasField(key string) bool

	// AddField sets the field on the Metric.  If the Metric already has the field
	// set then the current value is replaced.
	AddField(key string, value interface{})

	// RemoveField removes the tag if it is set.
	RemoveField(key string)

	// SetTime sets the timestamp of the Metric.
	SetTime(t time.Time)

	// HashID returns an unique identifier for the series.
	HashID() uint64

	// Copy returns a deep copy of the Metric.
	Copy() Metric

	// Accept marks the metric as processed successfully and written to an
	// output.
	Accept()

	// Reject marks the metric as processed unsuccessfully.
	Reject()

	// Drop marks the metric as processed successfully without being written
	// to any output.
	Drop()

	// SetAggregate indicates the metric is an aggregated value.
	//
	// This method may be removed in the future and its use is discouraged.
	SetAggregate(bool)

	// IsAggregate returns true if the Metric is an aggregate.
	//
	// This method may be removed in the future and its use is discouraged.
	IsAggregate() bool
}

Metric is the type of data that is processed by Telegraf. Input plugins, and to a lesser degree, Processor and Aggregator plugins create new Metrics and Output plugins write them.

type Output

type Output interface {
	// Connect to the Output
	Connect() error
	// Close any connections to the Output
	Close() error
	// Description returns a one-sentence description on the Output
	Description() string
	// SampleConfig returns the default configuration of the Output
	SampleConfig() string
	// Write takes in group of points to be written to the Output
	Write(metrics []Metric) error
}

type Processor

type Processor interface {
	// SampleConfig returns the default configuration of the Input
	SampleConfig() string

	// Description returns a one-sentence description on the Input
	Description() string

	// Apply the filter to the given metric.
	Apply(in ...Metric) []Metric
}

type ServiceInput

type ServiceInput interface {
	Input

	// Start the ServiceInput.  The Accumulator may be retained and used until
	// Stop returns.
	Start(Accumulator) error

	// Stop stops the services and closes any necessary channels and connections
	Stop()
}

type Tag

type Tag struct {
	Key   string
	Value string
}

Tag represents a single tag key and value.

type TrackingAccumulator

type TrackingAccumulator interface {
	Accumulator

	// Add the Metric and arrange for tracking feedback after processing..
	AddTrackingMetric(m Metric) TrackingID

	// Add a group of Metrics and arrange for a signal when the group has been
	// processed.
	AddTrackingMetricGroup(group []Metric) TrackingID

	// Delivered returns a channel that will contain the tracking results.
	Delivered() <-chan DeliveryInfo
}

TrackingAccumulator is an Accumulator that provides a signal when the metric has been fully processed. Sending more metrics than the accumulator has been allocated for without reading status from the Accepted or Rejected channels is an error.

type TrackingID

type TrackingID uint64

TrackingID uniquely identifies a tracked metric group

type ValueType

type ValueType int

ValueType is an enumeration of metric types that represent a simple value.

const (
	Counter ValueType
	Gauge
	Untyped
	Summary
	Histogram
)

Possible values for the ValueType enum.

Directories

Path Synopsis
cmd
aws
choice
Package choice provides basic functions for working with plugin options that must be one of several values.
Package choice provides basic functions for working with plugin options that must be one of several values.
tls
plugins
inputs/docker
Helper functions copied from https://github.com/docker/cli/blob/master/cli/command/container/stats_helpers.go
Helper functions copied from https://github.com/docker/cli/blob/master/cli/command/container/stats_helpers.go
inputs/jti_openconfig_telemetry/auth
Package authentication is a generated protocol buffer package.
Package authentication is a generated protocol buffer package.
inputs/jti_openconfig_telemetry/oc
Package telemetry is a generated protocol buffer package.
Package telemetry is a generated protocol buffer package.
inputs/lustre2
Lustre 2.x telegraf plugin Lustre (http://lustre.org/) is an open-source, parallel file system for HPC environments.
Lustre 2.x telegraf plugin Lustre (http://lustre.org/) is an open-source, parallel file system for HPC environments.
inputs/minecraft/internal/rcon
Package rcon implements the communication protocol for communicating with RCON servers.
Package rcon implements the communication protocol for communicating with RCON servers.
inputs/neptune_apex
Package neptuneapex implements an input plugin for the Neptune Apex aquarium controller.
Package neptuneapex implements an input plugin for the Neptune Apex aquarium controller.
inputs/phpfpm
Package fcgi implements the FastCGI protocol.
Package fcgi implements the FastCGI protocol.
inputs/uwsgi
Package uwsgi implements a telegraf plugin for collecting uwsgi stats from the uwsgi stats server.
Package uwsgi implements a telegraf plugin for collecting uwsgi stats from the uwsgi stats server.
inputs/x509_cert
Package x509_cert reports metrics from an SSL certificate.
Package x509_cert reports metrics from an SSL certificate.
inputs/zipkin/cmd/thrift_serialize
A small cli utility meant to convert json to zipkin thrift binary format, and vice versa.
A small cli utility meant to convert json to zipkin thrift binary format, and vice versa.
outputs/application_insights/mocks
Code generated by mockery v1.0.0 Code generated by mockery v1.0.0
Code generated by mockery v1.0.0 Code generated by mockery v1.0.0
selfstat is a package for tracking and collecting internal statistics about telegraf.
selfstat is a package for tracking and collecting internal statistics about telegraf.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL