Tóm tắt: Bật gRPC/gNMI trên router, khai báo [[inputs.gnmi]] với một [[inputs.gnmi.subscription]] có name = "interfaces" (tên này thành measurement), rồi xuất ra outputs.influxdb_v2 (bucket telemetry) và/hoặc outputs.prometheus_client. Telegraf là bên dial-in: nó chủ động kết nối tới router.
Luồng hoạt động
flowchart LR A["Router IOS XR (gRPC 57400)"] -->|gNMI Subscribe STREAM| T[Telegraf inputs.gnmi] B["Router Junos (gRPC 32767)"] -->|gNMI Subscribe STREAM| T T --> P[processors.strings] P --> I["InfluxDB 2.x bucket telemetry"] P --> M["prometheus_client :9273"] M --> S[Prometheus scrape]
Cấu hình
Bật gNMI trên thiết bị
IOS XR 7.x (lab, không TLS):
grpc
port 57400
no-tls
address-family ipv4
!Junos (lab, clear-text):
set system services extension-service request-response grpc clear-text port 32767
set system services extension-service request-response grpc skip-authenticationno-tls / clear-text chỉ dùng cho lab. Production nên bật TLS và đặt enable_tls = true + tls_ca ở Telegraf. skip-authentication trên Junos bỏ qua xác thực user, đừng để trên thiết bị thật.
Kiểm tra path trước bằng gnmic (gnmic cheatsheet), cú pháp path xem gNMI và YANG path.
Input gnmi
[[inputs.gnmi]]
addresses = ["198.51.100.11:57400", "198.51.100.12:57400"]
username = "${NET_USER}"
password = "${NET_PASS}"
encoding = "proto" # proto | json | json_ietf | bytes
redial = "10s"
# enable_tls = true
# tls_ca = "/etc/telegraf/ca.pem"
[[inputs.gnmi.subscription]]
name = "interfaces" # = measurement
origin = "openconfig"
path = "/interfaces/interface/state/counters"
subscription_mode = "sample" # sample | on_change | target_defined
sample_interval = "10s"
[[inputs.gnmi.subscription]]
name = "interfaces"
origin = "openconfig"
path = "/interfaces/interface/state/oper-status"
subscription_mode = "on_change"Junos dùng một block [[inputs.gnmi]] riêng với addresses = ["198.51.100.21:32767"]. Bản Telegraf mới có thêm vendor_specific = ["juniper_header"] để giải mã header JTI của Junos.
Telegraf tự gắn tag source (địa chỉ thiết bị) và biến key trong path interface[name=...] thành tag name, khớp schema trong InfluxDB Flux.
Chuẩn hoá tên field
Leaf OpenConfig dùng dấu - (in-octets). Đổi sang _ để query Flux/PromQL dễ hơn:
[[processors.strings]]
namepass = ["interfaces"]
[[processors.strings.replace]]
field_key = "*"
old = "-"
new = "_"Output
[[outputs.influxdb_v2]]
urls = ["http://influxdb.example.net:8086"]
token = "${INFLUX_TOKEN}"
organization = "netops"
bucket = "telemetry"
[[outputs.prometheus_client]]
listen = ":9273"
metric_version = 2
namepass = ["interfaces"]Với prometheus_client, metric có tên <measurement>_<field>, ví dụ interfaces_in_octets{source="198.51.100.11",name="HundredGigE0/0/0/0"}. Query xem PromQL cheatsheet.
Chạy và kiểm tra
export NET_USER=telemetry NET_PASS='***' INFLUX_TOKEN='***'
telegraf --config /etc/telegraf/telegraf.conf --config-directory /etc/telegraf/telegraf.d \
--input-filter gnmi --test --test-wait 30 # in line protocol ra stdout, không ghi output
systemctl restart telegraf && journalctl -u telegraf -f
curl -s localhost:9273/metrics | grep '^interfaces_in_octets' | headLỗi thường gặp
| Triệu chứng | Nguyên nhân | Cách xử lý |
|---|---|---|
rpc error: code = Unavailable |
Sai port, gRPC chưa bật, ACL chặn, hoặc lệch TLS (router TLS, Telegraf không) | nc -vz 198.51.100.11 57400, so no-tls với enable_tls |
code = Unauthenticated |
Sai user/pass hoặc user thiếu quyền | Thử cùng credential bằng gnmic capabilities |
code = InvalidArgument / NotFound |
Path hoặc origin thiết bị không hỗ trợ |
Kiểm tra bằng gnmic get; bỏ origin nếu thiết bị không nhận |
code = Unimplemented khi subscribe |
Encoding không hỗ trợ | Đổi encoding (proto / json_ietf) |
Không có tag name |
Path subscribe không đi qua list interface[name] |
Subscribe từ /interfaces/interface/... |
| Measurement lạ (path dài) | Thiếu name trong subscription |
Đặt name = "interfaces" |
Field in-octets thay vì in_octets |
Leaf YANG có dấu - |
processors.strings như trên |
| Cardinality tăng | Tag path / description được gắn tag |
tagexclude = ["path"] trong input |