在 Telegraf 中实现网络拨测(网络质量监测)主要通过 inputs.ping
和 inputs.http_response
插件实现。以下是详细配置指南:
一、使用 inputs.ping
插件进行 ICMP 拨测
监测目标主机的可达性、延迟和丢包率。
配置示例 (/etc/telegraf/telegraf.conf
):
[[inputs.ping]]
## 目标主机列表 (支持域名/IP)
urls = ["8.8.8.8", "github.com", "baidu.com"]
## 测试参数
count = 4 # 每次发送的包数
ping_interval = 1.0 # 发包间隔(秒)
timeout = 2.0 # 超时时间(秒)
deadline = 10 # 最大执行时间(秒)
## 高级选项
method = "native" # 使用系统ping命令(native)或Go实现(exec)
interface = "eth0" # 指定出接口
## 标签增强
[inputs.ping.tags]
region = "cn-east"
network = "prod"
生成的指标:
-
ping_packets_transmitted
发送包数 -
ping_packets_received
接收包数 -
ping_percent_packet_loss
丢包率 -
ping_minimum_response_ms
最小延迟 -
ping_average_response_ms
平均延迟 -
ping_maximum_response_ms
最大延迟
二、使用 inputs.http_response
插件进行 HTTP 拨测
监测 Web 服务的可用性、响应时间和状态码。
配置示例:
[[inputs.http_response]]
## 测试目标URL
urls = [
"http://example.com",
"https://api.service.com/health"
]
## 请求设置
method = "GET" # HTTP方法
timeout = "5s" # 超时时间
follow_redirects = true # 是否跟随重定向
## 结果验证
response_string_match = "OK" # 响应体需包含的字符串
expected_response_codes = [200, 301] # 期望的HTTP状态码
## 请求头定制
[inputs.http_response.headers]
User-Agent = "Telegraf-NetProbe/1.0"
Authorization = "Bearer xxxxx"
## 标签增强
[inputs.http_response.tags]
service = "web-frontend"
env = "production"
生成的指标:
-
http_response_code
HTTP 状态码 -
response_time
响应时间(秒) -
result_type
结果类型 (success/timeout/connection_failed) -
result_code
详细结果码 (0=成功, 1=字符串不匹配, 2=状态码不匹配等)
三、TCP 端口拨测
使用 inputs.net_response
插件检测 TCP 服务的可用性。
[[inputs.net_response]]
## 检测目标
protocol = "tcp" # tcp/udp
address = ":80" # 检测地址
targets = [
"192.168.1.1:3306", # MySQL
"10.0.0.100:9090" # Prometheus
]
## 测试参数
timeout = "3s" # 连接超时
send = "PING\r\n" # 发送的测试数据
expect = "PONG" # 期望的响应
## 标签
[inputs.net_response.tags]
role = "database"
四、DNS 解析拨测
通过 inputs.dns_query
监测 DNS 解析质量。
[[inputs.dns_query]]
## 目标域名
domains = ["example.com", "google.com"]
## DNS服务器
servers = ["8.8.8.8", "1.1.1.1"]
## 查询类型
record_type = "A" # A/AAAA/CNAME/MX
## 标签
[inputs.dns_query.tags]
network = "public-dns"
五、数据输出配置
将拨测结果发送到后端存储:
[[outputs.influxdb_v2]]
urls = ["http://influxdb:8086"]
token = "YOUR_API_TOKEN"
organization = "my-org"
bucket = "network-probes"
## 或输出到Prometheus
[[outputs.prometheus_client]]
listen = ":9273"
六、Grafana 仪表板示例
创建网络拨测监控面板:
-
ICMP 质量:折线图显示延迟变化,状态图显示丢包率
-
HTTP 可用性:状态码分布饼图,响应时间趋势图
-
TCP 端口状态:服务可用性状态图
-
报警规则:丢包率 >5% 或响应时间 >1s 时触发告警
七、高级技巧
-
动态目标列表:
[[inputs.file]] files = ["/etc/telegraf/targets.txt"] data_format = "influx" [[inputs.ping]] urls = [] urls_from_tag = "ping_target" # 从文件输入获取目标
-
结果过滤:
[[processors.starlark]] source = ''' def apply(metric): if metric.fields.get("result_code") != 0: return metric return None '''
-
拨测频率控制:
[agent] interval = "60s" # 全局频率 flush_interval = "60s" [[inputs.ping]] interval = "5m" # 单个插件频率 urls = ["core-router"]
八、验证与调试
-
测试配置文件:
telegraf --test --config /etc/telegraf/telegraf.conf
-
查看实时指标:
telegraf --config /etc/telegraf/telegraf.conf --input-filter ping --test
-
日志分析:
journalctl -u telegraf -f -n 100
通过以上配置,Telegraf 可实现全面的网络拨测功能,帮助企业监控网络服务质量,快速定位网络故障。