ARTICLE DETAIL

资讯详情

深耕网站建设与运营推广的一线实战洞察。

Telegraf Reverse DNS 处理器详解:IP 反向解析、缓存并发与实战配置

Telegraf Reverse DNS 处理器详解:IP 反向解析、缓存并发与实战配置 Telegraf Reverse DNS 处理器详解IP 反向解析、缓存并发与实战配置【免费下载链接】telegrafAgent for collecting, processing, aggregating, and writing metrics, logs, and other arbitrary data.项目地址: https://gitcode.com/GitHub_Trending/te/telegrafTelegraf 的reverse_dns处理器插件用于对指标中的 IP 地址无论是 tag 还是 field执行反向 DNS 查询并把解析出的域名写入新的 tag 或 field从而把裸 IP增强为可读的域名信息。本文以该插件的官方文档为主体结合 reverse_dns.go、rdnscache.go 及其测试代码系统讲解配置参数、底层缓存与并发模型、行为细节以及完整可用的配置示例帮助你安全、高效地把它接入自己的数据管道。插件简介与适用范围reverse_dns是一个标准处理器Processor插件自Telegraf v1.15.0起可用类型标注为annotation为指标补充注解信息可运行于所有平台all。它的核心能力是读取指标中携带 IP 的 tag 或 field进行反向 DNS 查询PTR 记录再将解析到的 DNS 名称写入指定的目标 tag 或 field原始指标内容不做其他改动。典型应用场景包括将网络监控指标中的源/目的 IP 解析为域名便于告警与报表阅读将 Web 访问日志经tail等输入插件采集中的客户端 IP 补充为主机名结合ping、net_response等输入插件的探测结果输出更易理解的目标域名。插件在 plugins/processors/all/reverse_dns.go 中被注册进默认构建并通过processors.AddStreaming(reverse_dns, ...)注册为流式处理器见 reverse_dns.go意味着它按流式方式逐条处理指标无需缓冲整个批次。完整配置与参数说明以下为插件官方 sample.conf 中的完整配置模板# ReverseDNS does a reverse lookup on IP addresses to retrieve the DNS name [[processors.reverse_dns]] ## For optimal performance, you may want to limit which metrics are passed to this ## processor. eg: ## namepass [my_metric_*] ## cache_ttl is how long the dns entries should stay cached for. ## generally longer is better, but if you expect a large number of diverse lookups ## youll want to consider memory use. cache_ttl 24h ## lookup_timeout is how long should you wait for a single dns request to respond. ## this is also the maximum acceptable latency for a metric travelling through ## the reverse_dns processor. After lookup_timeout is exceeded, a metric will ## be passed on unaltered. ## multiple simultaneous resolution requests for the same IP will only make a ## single rDNS request, and they will all wait for the answer for this long. lookup_timeout 3s ## max_parallel_lookups is the maximum number of dns requests to be in flight ## at the same time. Requesting hitting cached values do not count against this ## total, and neither do mulptiple requests for the same IP. ## Its probably best to keep this number fairly low. max_parallel_lookups 10 ## ordered controls whether or not the metrics need to stay in the same order ## this plugin received them in. If false, this plugin will change the order ## with requests hitting cached results moving through immediately and not ## waiting on slower lookups. This may cause issues for you if you are ## depending on the order of metrics staying the same. If so, set this to true. ## keeping the metrics ordered may be slightly slower. ordered false [[processors.reverse_dns.lookup]] ## get the ip from the field source_ip, and put the result in the field source_name field source_ip dest source_name [[processors.reverse_dns.lookup]] ## get the ip from the tag destination_ip, and put the result in the tag ## destination_name. tag destination_ip dest destination_name ## If you would prefer destination_name to be a field instead, you can use a ## processors.converter after this one, specifying the order attribute.顶层参数参数类型文档示例值源码默认值说明cache_ttlduration24h24h反向解析结果在缓存中的存活时间。DNS 记录一般变动不频繁设置较长 TTL 可显著减少查询次数若预期有大量不同的 IP 被查询需权衡内存占用lookup_timeoutduration3s1m单个 DNS 请求的最大等待时间同时也是指标流经本处理器的最大可接受延迟。超时后指标会原样放行不添加解析结果max_parallel_lookupsint1010同时进行的最大 DNS 查询并发数。命中缓存或同一 IP 的合并请求不计入该额度官方建议保持较小值orderedboolfalsefalse是否保持指标进入时的顺序。为false时命中缓存的指标会立即通过不等待慢查询可能导致指标顺序变化lookup 子表[[processors.reverse_dns.lookup]]声明一次取值-回写映射可重复出现多个每个条目支持以下键字段定义见 reverse_dns.go 中的lookupEntry结构体tag从指定 tag 读取 IP 字符串field从指定 field 读取 IP 字符串源码要求该 field 值必须是 string 类型见 reverse_dns.godest解析结果的回写目标。若本次读取来自field则结果写入同名的 field若来自tag则结果写入同名的 tag。每个 lookup 条目中tag与field二选一即可若两者都配置源码会先处理 field 再处理 tag。若目标 IP 不存在于指标中或解析失败如超时、无 PTR 记录该条 lookup 会被静默跳过指标保持原样。另外需要注意官方文档提示如果希望把 tag 形式的结果转成 field例如将destination_name作为字段输出可以在reverse_dns之后串联processors.converter并利用处理器顺序order控制执行次序。处理器顺序与全局配置与所有 Telegraf 处理器一致reverse_dns支持在插件表之外配置namepass、namedrop、tagexclude、order等全局选项。官方文档特别建议出于性能考虑尽量用namepass之类的过滤把进入该插件的指标限制在必要的范围内避免对所有指标做无谓的 DNS 查询。详细的全局配置说明可参阅 docs/CONFIGURATION.md#plugins原文档链接../../../docs/CONFIGURATION.md#plugins已转换为仓库根目录相对路径。工作流程与底层实现原理从指标到域名的完整调用链整个插件的处理流程如下对应 reverse_dns.go 中的Start/Add/asyncAddStart阶段根据配置创建reverseDNSCache实例并依据ordered开关选择并行执行器ordered true时使用parallel.NewOrdered(acc, r.asyncAdd, 10000, r.MaxParallelLookups)ordered false时使用parallel.NewUnordered(acc, r.asyncAdd, r.MaxParallelLookups)。 并行执行器接口定义见 plugins/common/parallel/parallel.go有序实现见 plugins/common/parallel/ordered.go其通过 future channel 保证输出顺序与入队顺序一致。每条指标经Add入队由 worker 调用asyncAdd遍历所有lookup条目从对应 tag/field 取出 IP 字符串调用reverseDNSCache.lookup(ip)。解析成功后若来源是 field 则metric.AddField(dest, result[0])若来源是 tag 则metric.AddTag(dest, result[0])仅取返回的第一个域名写入。处理完成后的指标继续沿管道传给后续处理器或输出插件。缓存与并发控制的核心设计reverseDNSCacherdnscache.go是性能的关键其设计要点包括并发安全的缓存以map[string]*dnslookup存放每个 IP 的查询状态通过sync.RWMutex保护读写可安全地被多个 goroutine 同时访问。请求合并singleflight 语义当多个 goroutine 同时请求同一个 IP 时只有第一个请求真正触发 DNS 查询其余请求注册为回调callback channel并等待同一份结果从而避免重复查询。这一行为在 rdnscache_test.go 的TestParallelReverseDNSLookup中得到验证——两个并发请求同一 IP 时只产生 1 次 cacheMiss。信号量限流semaphore.NewWeighted(maxWorkers)控制同时在途的 DNS 查询数max_parallel_lookups即对应此值。若 worker 池被占满新请求会在lookup_timeout内等待信号量超时未获取到信号量则请求被标记为放弃abandon。TTL 与过期清理每个查询结果带有expiresAt同时维护一个按时间有序的expireList后台清理协程每 10 秒运行一次cleanup()从头弹出已过期条目并删除对应缓存无需遍历整个 map见 rdnscache.go。超时兜底lookup内部通过time.NewTimer(lookupTimeout)与结果 channel 做 select 竞争即使 worker 池饥饿或 DNS 服务器无响应调用方也不会无限阻塞而是在超时后返回errTimeout。插件还维护了cacheHit、cacheMiss、cacheExpire、requestsAbandoned、requestsFilled等原子计数器见 rdnscache.go可用于性能观测与调试。错误处理语义超时或解析错误lookup返回错误时asyncAdd记录lookup error日志并跳过该条 lookup指标原样继续不会被丢弃。NXDOMAIN无 PTR 记录源码对net.DNSError且IsNotFound的情况做了专门处理——将结果视为无名称names nil而不是当作查询失败从而避免对无法反向解析的 IP 反复报错见 rdnscache.go。空值IP 字符串为空时直接返回空结果不发起查询。实战示例基础用法将 tag 中的 IP 解析为域名沿用官方文档的示例在配置文件telegraf.conf中加入[[processors.reverse_dns]] [[processors.reverse_dns.lookup]] tag ip dest domain指标进入前后的对比以ping输入采集8.8.8.8为例- ping,ip8.8.8.8 elapsed300i 1502489900000000000 ping,ip8.8.8.8,domaindns.google. elapsed300i 1502489900000000000注意结果域名为dns.google.带末尾点号——这正是 PTR 记录的标准表示形式。该行为与集成测试 reverse_dns_test.go 中TestSimpleReverseLookupIntegration的断言一致期望值one.one.one.one.。同时解析 field 与 tag[[processors.reverse_dns]] cache_ttl 24h lookup_timeout 3s max_parallel_lookups 10 ordered false # 从 field 读取结果写回 field [[processors.reverse_dns.lookup]] field source_ip dest source_name # 从 tag 读取结果写回 tag [[processors.reverse_dns.lookup]] tag destination_ip dest destination_name结果类型转换tag 转 field若希望destination_name以 field 形式输出可在reverse_dns之后追加converter处理器并利用order明确先后关系[[processors.reverse_dns]] order 1 [[processors.reverse_dns.lookup]] tag destination_ip dest destination_name [[processors.converter]] order 2 [processors.converter.tag] destination_name string性能调优建议控制进入插件的指标量优先使用namepass等过滤器只放行需要解析的指标这是官方文档强调的最有效优化手段。合理设置cache_ttl域名通常变化缓慢24h是较为稳妥的默认值内存敏感且 IP 基数大时可适当缩短。保持较低的max_parallel_lookups并发过高会放大对本地 DNS 解析器的压力官方建议保持较小数值如默认的 10。区分ordered的取舍默认ordered false能最大化吞吐命中缓存的指标立即放行若下游依赖指标顺序如按序写入文件或聚合则应设为true代价是可能略微变慢。验证与测试仓库内置了完整的单元测试与集成测试可帮助你理解插件行为并自行验证reverse_dns_test.goTestSimpleReverseLookupIntegration验证 field 与 tag 的双路解析并断言结果带末尾点号TestTracking验证指标追踪tracking metric语义下处理结果与投递回执的正确性。rdnscache_test.goTestSimpleReverseDNSLookup/TestParallelReverseDNSLookup验证缓存命中、同 IP 请求合并TestUnavailableDNSServerRespectsTimeout/TestLookupTimeout验证 DNS 服务器不可用或超时时按预期返回并释放资源TestCleanupHappens验证 TTL 过期后的缓存清理。运行测试集成测试在-short模式下会跳过可参考go test ./plugins/processors/reverse_dns/... -run TestSimpleReverseLookupIntegration小结reverse_dns处理器以极低的侵入性为 Telegraf 指标补充反向 DNS 注解通过cache_ttl、lookup_timeout、max_parallel_lookups、ordered四个顶层参数与可重复的lookup子表即可精细控制缓存、并发、超时与顺序语义。其底层基于并发安全的 TTL 缓存、请求合并与信号量限流保证了解析失败超时、无记录时指标仍原样放行从而不影响主数据链路。配合namepass过滤和converter处理器它可以在真实生产管道中稳定地完成IP 到域名的增强工作。【免费下载链接】telegrafAgent for collecting, processing, aggregating, and writing metrics, logs, and other arbitrary data.项目地址: https://gitcode.com/GitHub_Trending/te/telegraf创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表