夜莺-Nightingale
夜莺V6
项目介绍 架构介绍
快速开始
黄埔营
安装部署
升级
采集器
使用手册
API
数据库表结构
FAQ
开源生态
Prometheus
版权声明
第1章:天降奇兵
第2章:探索PromQL
第3章:Prometheus告警处理
第4章:Exporter详解
第5章:数据与可视化
第6章:集群与高可用
第7章:Prometheus服务发现
第8章:监控Kubernetes
第9章:Prometheus Operator
参考资料

Telegraf调研笔记5:本地端口监控、远程TCP探测

端口监控功能,核心原理就是给目标端口发个tcp包,如果能连通就是ok的。遍历了一下Telegraf的input plugin列表,看起来可以用net_response这个plugin来实现。该plugin配置如下:

# # Collect response time of a TCP or UDP connection
# [[inputs.net_response]]
#   ## Protocol, must be "tcp" or "udp"
#   ## NOTE: because the "udp" protocol does not respond to requests, it requires
#   ## a send/expect string pair (see below).
#   protocol = "tcp"
#   ## Server address (default localhost)
#   address = "localhost:80"
#
#   ## Set timeout
#   # timeout = "1s"
#
#   ## Set read timeout (only used if expecting a response)
#   # read_timeout = "1s"
#
#   ## The following options are required for UDP checks. For TCP, they are
#   ## optional. The plugin will send the given string to the server and then
#   ## expect to receive the given 'expect' string back.
#   ## string sent to the server
#   # send = "ssh"
#   ## expected string in answer
#   # expect = "ssh"
#
#   ## Uncomment to remove deprecated fields
#   # fielddrop = ["result_type", "string_found"]

这个注释写的很明白,可以进行tcp和udp探测,udp探测的话,需要配置发送字符串和期望返回的字符串,tcp的话比较简单,就配置一些连接地址和超时时间即可。ok,我们来配置一条tcp探测规则:

[[inputs.net_response]]
protocol = "tcp"
address = "116.85.46.86:10090"
timeout = "5s"

返回的内容如下:

ulric@bogon:~/gopath/src/telegraf$ ./telegraf --config telegraf.conf --test --input-filter net_response
2021-11-07T01:39:15Z I! Starting Telegraf
> net_response,host=192.168.0.103,port=10090,protocol=tcp,result=success,server=116.85.46.86 response_time=0.043524127,result_code=0i,result_type="success" 1636249156000000000

咱们故意把这个10090端口宕掉,再次探测,看看返回内容有何变化:

ulric@bogon:~/gopath/src/telegraf$ ./telegraf --config telegraf.conf --test --input-filter net_response
2021-11-07T01:45:39Z I! Starting Telegraf
> net_response,host=192.168.0.103,port=10090,protocol=tcp,result=connection_failed,server=116.85.46.86 result_code=2i,result_type="connection_failed" 1636249540000000000

这就有点坑了(针对常见的时序库而言),这个result标签在成功和失败的时候值不同,也就是前文讲到的标签不是稳态的,从时序角度来看,会当做两条监控数据来对待,非常不友好。另外,field部分有个result_type,是字符串类型,很多时序库也不支持,这个field要被干掉,field这个,配置中有这么两行,大家应该看到了:

#   ## Uncomment to remove deprecated fields
#   # fielddrop = ["result_type", "string_found"]

看来是之前老版本有result_type和string_found这俩字符串,建议是drop掉,所以tag部分和field部分,都要做一下过滤,最终配置如下:

[[inputs.net_response]]
protocol = "tcp"
address = "116.85.46.86:10090"
timeout = "5s"
fielddrop = ["result_type", "string_found"]
tagexclude = ["result"]

这样配置之后,我们重新测试一下,看看输出的监控数据长什么样?

ulric@bogon:~/gopath/src/telegraf$ ./telegraf --config telegraf.conf --test --input-filter net_response
2021-11-07T01:48:06Z I! Starting Telegraf
> net_response,host=192.168.0.103,port=10090,protocol=tcp,server=116.85.46.86 result_code=2i 1636249686000000000

嗯,nice~现在来看标签是稳态的了,只有一个field是result_code,只要这个值是0就是正常的,非0就是异常的,搞定!

net_response这个plugin,既可以探测本地端口,也可以探测远程端口(作为一个探针使用),本地端口的话,大家可以把address设置为localhost:${port},或者127.0.0.1:${port},在云原生的场景下,比如我们使用公有云的虚机,一般都是用小规格的虚机,一台虚机只部署一个应用程序,这个虚机上面的监控客户端,比如Telegraf,就是专门给这个应用程序独享使用的,可以在部署升级应用程序的时候,顺便通过自动化手段来修改Telegraf的配置,把应用程序的端口监控配置加进去,比较方便。

也可以找一台专门的机器做为中心探针,去探测多个远端地址,类似Prom生态的Blackbox_exporter,那如果我这里要探测两个tcp地址,应该如何配置呢?简单,如下:

[[inputs.net_response]]
protocol = "tcp"
address = "116.85.46.86:10090"
timeout = "5s"
fielddrop = ["result_type", "string_found"]
tagexclude = ["result"]
tags = {bu="cloud", region="bj"}

[[inputs.net_response]]
protocol = "tcp"
address = "localhost:18000"
timeout = "1s"
fielddrop = ["result_type", "string_found"]
tagexclude = ["result"]
tags = {bu="cloud", region="local"}

用两个section来描述即可,[[]]这种格式的部分,表示数组,都可以写多个,另外上例中也同时演示了如何附加额外的标签,标签一般要放在section的最后,受制于toml格式配置文件的解析规则,如果要附加额外的标签,一定要每个探测目标都有对应的标签,保持标签key的稳定性。如上配置,运行之后输出如下:

ulric@bogon:~/gopath/src/telegraf$ ./telegraf --config telegraf.conf --test --input-filter net_response
2021-11-07T02:10:32Z I! Starting Telegraf
> net_response,bu=cloud,host=192.168.0.103,port=18000,protocol=tcp,region=local,server=localhost result_code=2i 1636251032000000000
> net_response,bu=cloud,host=192.168.0.103,port=10090,protocol=tcp,region=bj,server=116.85.46.86 result_code=2i 1636251032000000000

附加的标签已经出现在输出的监控数据中,nice~ UDP的场景这里没有测试,大家如果有需求自行尝试一下。

开源版
Flashcat
Flashduty