监控-telegraf

telegraf

参考

netstream 华为官网协议解析
netflow、netstream、是flow对比
go简单读取netstream
goflow2
netstream v9版本解析
Netflow/IPFIX 流量收集与分析-goflow命令解析协议
b站学习视频
ensp华为模拟器
netstream python github

telegraf 官方文档
telegraf github

运维事件中心 robitmq hbase mysql 规则引擎
瑞祥云
一文搞懂telegraf知乎

监控思科

netflow

Netflow
Plugin ID: inputs.netflow
Telegraf 1.25.0+

The Netflow input plugin gathers metrics from Netflow v5, Netflow v9 and IPFIX collectors.

Netflow输入插件
netflow插件充当Netflowv5、Netflowv9和IPFIX流信息的收集器。第4层协议编号是从官方IANA分配中收集的。Netflow v5字段的内部字段映射根据Cisco的Netflow v6文档定义,Netflow v9字段根据Cisco的Netflow v9文档和ASA扩展定义。IPFIX的定义根据IANA分配文件。

全局配置选项
除了特定于插件的配置设置外,插件还支持其他全局和插件配置设置。这些设置用于修改度量、标记和字段,或创建别名和配置排序等。有关详细信息,请参阅CONFIGURATION.md。

开发

代码示例

1.26版本

git clone -b v1.26.0 https://github.com/influxdata/telegraf.git

make all 下载包
go mod tidy 依赖包管理

go proxy
GOPROXY=https://goproxy.cn,direct
exporter 伊克斯剖特
plugin 普拉根
toml 涛某
gather 该z鹅

介绍

  • 目录
  • cmd 命令
  • config 配置
  • plugins 插件

简单插件开发

测试
go test -v /Users/haogeoyes/go/pkg/mod/github.com/netsampler/goflow2@v1.1.1/decoders/netflow/netflow_test.go
包含字节数据byte

在这里插入图片描述

make all
go mod download -x
env -u GOOS -u GOARCH -u GOARM -- go build -o ./tools/custom_builder/custom_builder ./tools/custom_builder
env -u GOOS -u GOARCH -u GOARM -- go build -o ./tools/license_checker/license_checker ./tools/license_checker
env -u GOOS -u GOARCH -u GOARM -- go build -o ./tools/readme_config_includer/generator ./tools/readme_config_includer/generator.go
GOOS=linux go generate -run="readme_config_includer/generator$" ./plugins/inputs/...
2023/03/16 11:17:11 Cannot get file permissions: lstat README.md: no such file or directory
plugins/inputs/netstream/netstream.go:3: running "../../../tools/readme_config_includer/generator": exit status 1
make: *** [embed_readme_inputs] Error 1

创建 README.md

  • 构建
go build -tags "" -ldflags " -X github.com/influxdata/telegraf/internal.Commit=67c08c51 -X github.com/influxdata/telegraf/internal.Branch=HEAD -X github.com/influxdata/telegraf/internal.Version=1.25.2" ./cmd/telegraf

*开发插件 netstream.go

// 帮助生成文档 README.md Configuration toml @sample.conf
//
//go:generate ../../../tools/readme_config_includer/generator
package nets

import (
	_ "embed"
	"github.com/influxdata/telegraf"
	"github.com/influxdata/telegraf/plugins/inputs"
)

// 必须,在编译过程中会将sample.conf文件的内容赋值
//
//go:embed sample.conf
var sampleConfig string

// toml风格标签,表示映射哪一个键
type Simple struct {
	size int             `toml:"size"`
	Log  telegraf.Logger `toml:"-"`
}

// 方法调用时返回配置文件
func (*Simple) SampleConfig() string {
	return sampleConfig
}

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

func (s *Simple) Start(acc telegraf.Accumulator) error {
	return nil
}

func (s *Simple) Stop() error {
	return nil
}

func (s *Simple) Gather(_ telegraf.Accumulator) error {
	return nil
}

// Register the plugin
func init() {
	//完成simple空结构体的构造,框架完成解析的操作,结构体为配置项
	inputs.Add("netstream", func() telegraf.Input {
		return &Simple{}
	})
}

*sample.conf

[[inputs.netstream]]
  size = 5

  • 插件注册
在 all 包位置新建文件netstream.go
//go:build !custom || inputs || inputs.netstream

package all
//导入模块利用副作用,执行init函数
import _ "github.com/influxdata/telegraf/plugins/inputs/netstream" // register plugin

// 帮助生成文档 README.md Configuration toml @sample.conf
//
//go:generate ../../../tools/readme_config_includer/generator
package nets

import (
	_ "embed"
	"errors"
	"github.com/influxdata/telegraf"
	"github.com/influxdata/telegraf/plugins/inputs"
	"math/rand"
)

// 必须,在编译过程中会将sample.conf文件的内容赋值
//
//go:embed sample.conf
var sampleConfig string

// 配置项被外部成员访问必须首字母大写
type Range struct {
	Min int `toml:"min"`
	Max int `toml:"max"`
}

// toml风格标签,表示映射哪一个键
type Simple struct {
	Size  int             `toml:"size"`
	Range *Range          `toml:"range"`
	Log   telegraf.Logger `toml:"-"`
}

// 方法调用时返回配置文件
func (*Simple) SampleConfig() string {
	return sampleConfig
}

func (s *Simple) Init() error {
	//初始化校验是否合规
	if s.Range.Min > s.Range.Max {
		return errors.New("最大值要比最小值大")
	}

	return nil
}

func (s *Simple) Start(acc telegraf.Accumulator) error {
	return nil
}

func (s *Simple) Stop() error {
	return nil
}

func (s *Simple) Gather(acc telegraf.Accumulator) error {
	//数据生成发送
	//生成size个随机数
	for i := 0; i < s.Size; i++ {
		//标签名,传值{初始化}
		acc.AddFields("netstream", map[string]interface{}{"num": s.Range.Min + rand.Intn(s.Range.Max-s.Range.Min)}, nil)
	}
	return nil
}

// Register the plugin
func init() {
	// 想全局注册表表中注册插件
	//完成simple空结构体的构造,框架完成解析的操作,结构体为配置项
	inputs.Add("netstream", func() telegraf.Input {
		return &Simple{}
	})
}

make all 编译后根目录 会多出telegraf文件
检查包
./telegraf --input-list | grep netream

./telegraf --input-filter netstream config

  • 配置文件
cat examplenetstream.conf 
[agent]
  interval = "3s"
  flush_interval = "5s"

[[inputs.netstream]]
  size = 1
  [inputs.netstream.range]
    min = 50
    max = 30

[[outputs.file]]
  files = ["stdout"]

cd go/src/telegraf
./telegraf --config examplenetstream.conf
2023-03-16T07:52:02Z E! error loading config file examplenetstream.conf: error parsing data: line 7: table `inputs.netstream’ is in conflict with array table in line 5

  • min max互换一下
  • 2023-03-16T07:54:07Z E! error loading config file examplenetstream.conf: plugin inputs.netstream: line 5: configuration specified the fields [“size”], but they weren’t used
./telegraf --config examplenetstream.conf
2023-03-16T08:23:10Z I! Starting Telegraf 1.25.2
2023-03-16T08:23:10Z I! Available plugins: 214 inputs, 9 aggregators, 26 processors, 21 parsers, 57 outputs, 2 secret-stores
2023-03-16T08:23:10Z I! Loaded inputs: netstream
2023-03-16T08:23:10Z I! Loaded aggregators:
2023-03-16T08:23:10Z I! Loaded processors:
2023-03-16T08:23:10Z I! Loaded secretstores:
2023-03-16T08:23:10Z I! Loaded outputs: file
2023-03-16T08:23:10Z I! Tags enabled: host=B-V1WTQ05P-0318.local
2023-03-16T08:23:10Z I! [agent] Config: Interval:3s, Quiet:false, Hostname:"B-V1WTQ05P-0318.local", Flush Interval:5s
2023-03-16T08:23:10Z E! [telegraf] Error running agent: could not initialize input inputs.netstream: 最大值要比最小值大
telegraf % ./telegraf --config examplenetstream.conf
2023-03-16T07:59:06Z I! Starting Telegraf 1.25.2
2023-03-16T07:59:06Z I! Available plugins: 214 inputs, 9 aggregators, 26 processors, 21 parsers, 57 outputs, 2 secret-stores
2023-03-16T07:59:06Z I! Loaded inputs: netstream
2023-03-16T07:59:06Z I! Loaded aggregators:
2023-03-16T07:59:06Z I! Loaded processors:
2023-03-16T07:59:06Z I! Loaded secretstores:
2023-03-16T07:59:06Z I! Loaded outputs: file
2023-03-16T07:59:06Z I! Tags enabled: host=B-V1WTQ05P-0318.local
2023-03-16T07:59:06Z I! [agent] Config: Interval:3s, Quiet:false, Hostname:"B-V1WTQ05P-0318.local", Flush Interval:5s
netstream,host=B-V1WTQ05P-0318.local num=33i 1678953549000000000
netstream,host=B-V1WTQ05P-0318.local num=31i 1678953552000000000

mac 安装telegraf

brew install telegraf
fatal: not in a git directory
Warning: No remote 'origin' in /opt/homebrew/Library/Taps/homebrew/homebrew-services, skipping update!
==> Downloading https://mirrors.ustc.edu.cn/homebrew-bottles/telegraf-1.23.3.arm64_monterey.bottle.tar.gz
curl: (22) The requested URL returned error: 404

Warning: Bottle missing, falling back to the default domain...
==> Downloading https://ghcr.io/v2/homebrew/core/telegraf/manifests/1.23.3
Already downloaded: /Users/haogeoyes/Library/Caches/Homebrew/downloads/a97d7162557501433d472593f70f0f3035e65a60c1c2ebf1d41001ea5ce20831--telegraf-1.23.3.bottle_manifest.json
==> Downloading https://ghcr.io/v2/homebrew/core/telegraf/blobs/sha256:a41890b558fc99569d282d6c6b6bf1e358160549d5fb6c5c22226d1dea8a86e4
==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sha256:a41890b558fc99569d282d6c6b6bf1e358160549d5fb6c5c22226d1dea8a86e4?se=2023-03-15T05%3A55%3A00Z&si
######################################################################## 100.0%
==> Pouring telegraf--1.23.3.arm64_monterey.bottle.tar.gz
==> Caveats
To restart telegraf after an upgrade:
  brew services restart telegraf
Or, if you don't want/need a background service you can just run:
  /opt/homebrew/opt/telegraf/bin/telegraf -config /opt/homebrew/etc/telegraf.conf -config-directory /opt/homebrew/etc/telegraf.d
==> Summary
🍺  /opt/homebrew/Cellar/telegraf/1.23.3: 9 files, 185MB
==> `brew cleanup` has not been run in the last 30 days, running now...
  • 启动 brew services restart telegraf
    ==> Successfully started telegraf (label: homebrew.mxcl.telegraf)

监控

  • 简单监控cpu
  • /opt/homebrew/opt/telegraf/bin/telegraf -config example01.conf
/opt/homebrew/opt/telegraf/bin/telegraf -config example01.conf
2023-03-16T02:23:45Z I! Starting Telegraf 1.23.3
2023-03-16T02:23:45Z I! Loaded inputs: cpu
2023-03-16T02:23:45Z I! Loaded aggregators:
2023-03-16T02:23:45Z I! Loaded processors:
2023-03-16T02:23:45Z I! Loaded outputs: file
2023-03-16T02:23:45Z I! Tags enabled: host=B-V1WTQ05P-0318.local
2023-03-16T02:23:45Z I! [agent] Config: Interval:3s, Quiet:false, Hostname:"B-V1WTQ05P-0318.local", Flush Interval:10s
cpu,cpu=cpu0,host=B-V1WTQ05P-0318.local usage_iowait=0,usage_irq=0,usage_guest=0,usage_guest_nice=0,usage_user=16.666666666666668,usage_idle=71.33333333331393,usage_nice=0,usage_system=12.000000000019403,usage_softirq=0,usage_steal=0 1678933431000000000
cpu,cpu=cpu1,host=B-V1WTQ05P-0318.local usage_system=11.627906976772294,usage_guest=0,usage_irq=0,usage_softirq=0,usage_steal=0,usage_guest_nice=0,usage_user=15.282392026709978,usage_idle=73.08970099706161,usage_nice=0,usage_iowait=0 1678933431000000000
....
cpu,cpu=cpu-total,host=B-V1WTQ05P-0318.local usage_iowait=0,usage_guest=0,usage_guest_nice=0,usage_user=7.91336942943771,usage_system=5.081216159936239,usage_idle=87.00541441068665,usage_nice=0,usage_irq=0,usage_softirq=0,usage_steal=0 1678933437000000000
2023-03-16T02:23:57Z I! [agent] Stopping running outputs
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值