telegraf + influxdb + grafana で YAMAHA RTX830 を可視化する (telegraf編 1)

さて。

[最初から、自宅のルータは YAMAHA にしておけば良かったんだ](https://note.com/sho7650/n/n0d6df61bad5d) ということで、YAMAHA RTX830 に変更して安定運用を続けています。将来にわたっての安定運用のためには、監視が必要です。不可欠です。

わが家では、しれっと [telegraf + influx + grafana で可視化](https://note.com/sho7650/n/nc60176d4ace2) していましたから、RTX830 用に設定をせっせと探しました。が、しかし、Edgerouter ほど尖っていないにもかかわらず、サンプル設定ファイルは入手できず。仕方ないので、泣きながら telegraf の設定ファイルを作成しました。

yamaha Private MIBで詳細情報とれますが、ネットワーク利用状況を可視化するための基本情報は、ほとんど標準MIBで取得できます。Private MIBが必要なのは、CPU/MEM/uptimeなどあたりですね。ネットワークの情報だけならPrivate MIBなくとも設定可能です。

そんなわけで、使用状況を最大限に取得する設定ファイルができましたので、ここで公開します。

## 前提条件

- telegraf 導入・influxdb向け設定済 (動くこと)
- influxdb 導入・設定済
- grafana などの可視化ツールを入れておきましょう
- 今回の設定は ubuntu 20.04 Server で実施

## tunnel または PP インタフェイスを利用している場合

なお、tunnelやPPのインタフェイスを利用している場合には、それぞれに応じて RTX830 側で次の設定をして該当のSNMPを取得できるようにします。わが家では tunnelを利用していますので、そちらのみ on にしています。

```lua:tunnel利用の場合
snmp yriftunneldisplayatmib2 on
```
[yamaha: トンネルインタフェースの情報を MIB2 の範囲で表示するか否かの設定](http://www.rtpro.yamaha.co.jp/RT/manual/rt-common/snmp/snmp_yriftunneldisplayatmib2.html)

```lua:PP利用の場合
snmp yrifppdisplayatmib2 on
```
[yamaha: PP インタフェースの情報を MIB2 の範囲で表示するか否かの設定](http://www.rtpro.yamaha.co.jp/RT/manual/rt-common/snmp/snmp_yrifppdisplayatmib2.html)

## 拡張MIBの設定

YAMAHA の拡張MIBを取得して配置しておきます。

### yamaha private MIB の配置

```bash
sudo tar -xvzf ~/yamaha-private-mib.tar.gz -C /usr/share/snmp/mibs/
```

なお、debian系だとPrivate MIB入れた後にエラーが出ます。そのままだと snmpwalk も telegraf の snmpも取得に失敗します。

```bash:debian/ubuntuの場合(だけ？)
Bad operator (INTEGER): At line 73 in /usr/share/snmp/mibs/ietf/SNMPv2-PDU
```

ので、修正されたファイルを入れておきましょう。

```bash
sudo wget http://pastebin.com/raw.php?i=p3QyuXzZ -O /usr/share/snmp/mibs/ietf/SNMPv2-PDU
```

#### 参考情報

[How to fix net-snmp / snmpwalk errors in Debian 7](https://docs.linuxconsulting.mn.it/notes/net-snmp-errors)
[SNMP MIBs on Ubuntu - error in MIBs](https://serverfault.com/questions/936119/snmp-mibs-on-ubuntu-error-in-mibs/936145)

## telegraf 用構成ファイル

ということで、これが完成した telegraf 用の定義ファイルです。

`/etc/telegraf/telegraf.d/` へ配置して `sudo systemctl ^Cstart telegraf.service` すれば取得されます。

気をつけるポイントとしては、取得に時間がかかりますので、`telegraf.conf` の次の設定をしておくことを推奨します。interval 10s だとたくさんワーニングはきます。

```config:telegraf.conf
[agent]
  interval = "30s"
  metric_batch_size = 5000
  flush_jitter = "5s"
```

#### 参考情報

[took longer to collect than collection interval (10s)](https://github.com/influxdata/telegraf/issues/4083)

snmp でどうにかできるのはこれでほぼすべてですが、どうしても NAT セッション数は取得できません。これは luaをうまく使って連携する必要がありますので、これはまた次回。

```config:rtx830.conf
##
## SNMP Input Plugin
##
## YAMAHA RTX830
##
[[inputs.snmp]]
   # List of agents to poll
   agents = [ "<hostname|ip addr>[:port]" ]
   # Polling interval
   interval = "60s"
   # Timeout for each SNMP query.
   timeout = "30s"
   # Number of retries to attempt within timeout.
   retries = 3
   # SNMP version, UAP only supports v1
   version = 1
   # SNMP community string.
   community = "public"
   # The GETBULK max-repetitions parameter
   max_repetitions = 10
   # Measurement name
   name = "rtx830"

  ##
  ## Product Information
  ##
  [[inputs.snmp.field]]
    name = "productName"
    oid = "YAMAHA-SMI::yamahaRT.6.5.0"
  [[inputs.snmp.field]]
    name = "firmware"
    oid = "YAMAHA-SMI::yamahaRT.6.6.0"
  [[inputs.snmp.field]]
    name = "revision"
    oid = "YAMAHA-RT-FIRMWARE::yrfRevision.0"

  ##
  ## System details
  ##
  #  System name (hostname)
  [[inputs.snmp.field]]
    name = "sysName"
    oid = "SNMPv2-MIB::sysName.0"
    is_tag = true
  #  System vendor OID
  [[inputs.snmp.field]]
    name = "sysObjectID"
    oid = "SNMPv2-MIB::sysObjectID.0"
  #  System description
  [[inputs.snmp.field]]
    name = "sysDescr"
    oid = "SNMPv2-MIB::sysDescr.0"

  ##
  ## System Details & Metrics
  ##
  # System uptime
  [[inputs.snmp.field]]
    name = "sysUpTime"
    oid = "YAMAHA-RT-FIRMWARE::yrfUpTime.0"
    
  ##
  ## CPU inventory
  ##
  # CPU Table
  [[inputs.snmp.table]]
    name = "cpuTable"
    inherit_tags = [ "sysName" ]
    oid = "YAMAHA-RT-HARDWARE::yrhMultiCpuTable"

    [[inputs.snmp.table.field]]
      name = "cpuName"
      oid = "YAMAHA-RT-HARDWARE::yrhMultiCpuName"
      is_tag = true

  ##
  ## System Memory
  ##
  # real/physical memory size
  [[inputs.snmp.field]]
    name = "memSize"
    oid = "YAMAHA-RT-HARDWARE::yrhMemorySize.0"
  # memory utilization
  [[inputs.snmp.field]]
    name = "memUtil"
    oid = "YAMAHA-RT-HARDWARE::yrhMemoryUtil.0"

  ##
  ## Interfaces metrics
  ##
  # Interface entity information table
  [[inputs.snmp.table]]
    name = "ifTable"
    oid = "IF-MIB::ifTable"
    inherit_tags = [ "sysName" ]

    [[inputs.snmp.table.field]]
      name = "ifName"
      oid = "IF-MIB::ifName"
      is_tag = true
    [[inputs.snmp.table.field]]
      name = "ifDescr"
      oid = "IF-MIB::ifDescr"
      is_tag = true

  [[inputs.snmp.table]]
    name = "ifXTable"
    oid = "IF-MIB::ifXTable"
    inherit_tags = [ "sysName" ]

    [[inputs.snmp.table.field]]
      name = "ifName"
      oid = "IF-MIB::ifName"
      is_tag = true
    [[inputs.snmp.table.field]]
      name = "ifDescr"
      oid = "IF-MIB::ifDescr"
      is_tag = true

  ##
  ## ICMP statictics
  ##
  # The total number of ICMP messages which the entity received.
  [[inputs.snmp.field]]
    name = "icmpInMsgs"
    oid = "RFC1213-MIB::icmpInMsgs.0"
  # The number of ICMP messages which the entity received.
  [[inputs.snmp.field]]
    name = "icmpInErrors"
    oid = "RFC1213-MIB::icmpInErrors.0"
  # The number of ICMP Destination Unreachable messages received.
  [[inputs.snmp.field]]
    name = "icmpInDestUnreachs"
    oid = "RFC1213-MIB::icmpInDestUnreachs.0"
  # The number of ICMP Time Exceeded messages received.
  [[inputs.snmp.field]]
    name = "icmpInTimeExcds"
    oid = "RFC1213-MIB::icmpInTimeExcds.0"
  # The number of ICMP Parameter Problem messages received.
  [[inputs.snmp.field]]
    name = "icmpInParmProbs"
    oid = "RFC1213-MIB::icmpInParmProbs.0"
  # The number of ICMP Source Quench messages received.received
  [[inputs.snmp.field]]
    name = "icmpInSrcQuenchs"
    oid = "RFC1213-MIB::icmpInSrcQuenchs.0"
  # The number of ICMP Redirect messages received.
  [[inputs.snmp.field]]
    name = "icmpInRedirects"
    oid = "RFC1213-MIB::icmpInRedirects.0"
  # The number of ICMP Echo (request) messages received.
  [[inputs.snmp.field]]
    name = "icmpInEchos"
    oid = "RFC1213-MIB::icmpInEchos.0"
  # The number of ICMP Echo Reply messages received.
  [[inputs.snmp.field]]
    name = "icmpInEchoReps"
    oid = "RFC1213-MIB::icmpInEchoReps.0"
  # The number of ICMP Timestamp (request) messages received.
  [[inputs.snmp.field]]
    name = "icmpInTimestamps"
    oid = "RFC1213-MIB::icmpInTimestamps.0"
  # The number of ICMP Timestamp Reply messages received.
  [[inputs.snmp.field]]
    name = "icmpInTimestampReps"
    oid = "RFC1213-MIB::icmpInTimestampReps.0"
  # The number of ICMP Address Mask Request messages received.
  [[inputs.snmp.field]]
    name = "icmpInAddrMasks"
    oid = "RFC1213-MIB::icmpInAddrMasks.0"
  # The number of ICMP Address Mask Reply messages received.
  [[inputs.snmp.field]]
    name = "icmpInAddrMaskReps"
    oid = "RFC1213-MIB::icmpInAddrMaskReps.0"
  # The total number of ICMP messages which this entity attempted to send.
  [[inputs.snmp.field]]
    name = "icmpOutMsgs"
    oid = "RFC1213-MIB::icmpOutMsgs.0"
  # The number of ICMP messages which this entity did not send due to problems discovered within ICMP such as a lack of buffers
  [[inputs.snmp.field]]
    name = "icmpOutErrors"
    oid = "RFC1213-MIB::icmpOutErrors.0"
  # The number of ICMP Destination Unreachable messages sent
  [[inputs.snmp.field]]
    name = "icmpOutDestUnreachs"
    oid = "RFC1213-MIB::icmpOutDestUnreachs.0"
  # The number of ICMP Time Exceeded messages sent
  [[inputs.snmp.field]]
    name = "icmpOutTimeExcds"
    oid = "RFC1213-MIB::icmpOutTimeExcds.0"
  # The number of ICMP Parameter Problem messages sent
  [[inputs.snmp.field]]
    name = "icmpOutParmProbs"
    oid = "RFC1213-MIB::icmpOutParmProbs.0"
  # The number of ICMP Source Quench messages sent
  [[inputs.snmp.field]]
    name = "icmpOutSrcQuenchs"
    oid = "RFC1213-MIB::icmpOutSrcQuenchs.0"
  # The number of ICMP Redirect messages sent
  [[inputs.snmp.field]]
    name = "icmpOutRedirects"
    oid = "RFC1213-MIB::icmpOutRedirects.0"
  # The number of ICMP Echo (request) messages sent
  [[inputs.snmp.field]]
    name = "icmpOutEchos"
    oid = "RFC1213-MIB::icmpOutEchos.0"
  # The number of ICMP Echo Reply messages sent
  [[inputs.snmp.field]]
    name = "icmpOutEchoReps"
    oid = "RFC1213-MIB::icmpOutEchoReps.0"
  # The number of ICMP Timestamp (request) messages sent
  [[inputs.snmp.field]]
    name = "icmpOutTimestamps"
    oid = "RFC1213-MIB::icmpOutTimestamps.0"
  # The number of ICMP Timestamp Reply messages sent
  [[inputs.snmp.field]]
    name = "icmpOutTimestampReps"
    oid = "RFC1213-MIB::icmpOutTimestampReps.0"
  # The number of ICMP Address Mask Request messages sent
  [[inputs.snmp.field]]
    name = "icmpOutAddrMasks"
    oid = "RFC1213-MIB::icmpOutAddrMasks.0"
  # The number of ICMP Address Mask Reply messages sent
  [[inputs.snmp.field]]
    name = "icmpOutAddrMaskReps"
    oid = "RFC1213-MIB::icmpOutAddrMaskReps.0"
 
  ##
  ## UDP statistics
  ##
  #  Datagrams delivered to app
  [[inputs.snmp.field]]
    name = "udpInDatagrams"
    oid = "UDP-MIB::udpInDatagrams.0"
  #  Datagrams received with no app
  [[inputs.snmp.field]]
    name = "udpNoPorts"
    oid = "UDP-MIB::udpNoPorts.0"
  #  Datagrams received with error
  [[inputs.snmp.field]]
    name = "udpInErrors"
    oid = "UDP-MIB::udpInErrors.0"
  #  Datagrams sent
  [[inputs.snmp.field]]
    name = "udpOutDatagrams"
    oid = "UDP-MIB::udpOutDatagrams.0"
  ##
  ## TCP statistics
  ##
  #  Number of CLOSED -> SYN-SENT transitions
  [[inputs.snmp.field]]
    name = "tcpActiveOpens"
    oid = "TCP-MIB::tcpActiveOpens.0"
  #  Number of SYN-RCVD -> LISTEN transitions
  [[inputs.snmp.field]]
    name = "tcpPassiveOpens"
    oid = "TCP-MIB::tcpPassiveOpens.0"
  #  Number of SYN-SENT/RCVD -> CLOSED transitions
  [[inputs.snmp.field]]
    name = "tcpAttemptFails"
    oid = "TCP-MIB::tcpAttemptFails.0"
  #  Number of ESTABLISHED/CLOSE-WAIT -> CLOSED transitions
  [[inputs.snmp.field]]
    name = "tcpEstabResets"
    oid = "TCP-MIB::tcpEstabResets.0"
  #  Number of ESTABLISHED or CLOSE-WAIT
  [[inputs.snmp.field]]
    name = "tcpCurrEstab"
    oid = "TCP-MIB::tcpCurrEstab.0"
  #  Number of segments received
  [[inputs.snmp.field]]
    name = "tcpInSegs"
    oid = "TCP-MIB::tcpInSegs.0"
  #  Number of segments sent
  [[inputs.snmp.field]]
    name = "tcpOutSegs"
    oid = "TCP-MIB::tcpOutSegs.0"
  #  Number of segments retransmitted
  [[inputs.snmp.field]]
    name = "tcpRetransSegs"
    oid = "TCP-MIB::tcpRetransSegs.0"
  #  Number of segments received with error
  [[inputs.snmp.field]]
    name = "tcpInErrs"
    oid = "TCP-MIB::tcpInErrs.0"
  #  Number of segments sent w/RST
  [[inputs.snmp.field]]
    name = "tcpOutRsts"
    oid = "TCP-MIB::tcpOutRsts.0"
  ##
  ## IP routing statistics
  ##
  #  Number of valid routing entries
  [[inputs.snmp.field]]
    name = "inetCidrRouteNumber"
    oid = "IP-FORWARD-MIB::inetCidrRouteNumber.0"
  #  Number of valid entries discarded
  [[inputs.snmp.field]]
    name = "inetCidrRouteDiscards"
    oid = "IP-FORWARD-MIB::inetCidrRouteDiscards.0"
  #  Number of valid forwarding entries
  [[inputs.snmp.field]]
   name = "ipForwardNumber"
    oid = "IP-FORWARD-MIB::ipForwardNumber.0"
  # Number of valid routes discarded
  [[inputs.snmp.field]]
    name = "ipRoutingDiscards"
    oid = "RFC1213-MIB::ipRoutingDiscards.0"
  # The indication of whether this entity is acting as an IP gateway in respect to the forwarding of datagrams received by, but not addressed to, this entity. 
  [[inputs.snmp.field]]
    name = "ipForwarding"
    oid = "RFC1213-MIB::ipForwarding.0"
  # The default value inserted into the Time-To-Live field of the IP header of datagrams originated at this entity.
  [[inputs.snmp.field]]
    name = "ipDefaultTTL"
    oid = "RFC1213-MIB::ipDefaultTTL.0"
  # The total number of input datagrams received from interfaces, including those received in error.
  [[inputs.snmp.field]]
    name = "ipInReceives"
    oid = "RFC1213-MIB::ipInReceives.0"
  # The number of input datagrams discarded due to errors in their IP headers.
  [[inputs.snmp.field]]
    name = "ipInHdrErrors"
    oid = "RFC1213-MIB::ipInHdrErrors.0"
  # The number of input datagrams discarded because the IP address in their IP header's destination field was not a valid address to be received at this entity.
  [[inputs.snmp.field]]
    name = "ipInAddrErrors"
    oid = "RFC1213-MIB::ipInAddrErrors.0"
  # The number of input datagrams for which this entity was not their final IP destination.
  [[inputs.snmp.field]]
    name = "ipForwDatagrams"
    oid = "RFC1213-MIB::ipForwDatagrams.0"
  # The number of locally-addressed datagrams received successfully but discarded because of an unknown or unsupported protocol.
  [[inputs.snmp.field]]
    name = "ipInUnknownProtos"
    oid = "RFC1213-MIB::ipInUnknownProtos.0"
  # The number of input IP datagrams for which no problems were encountered to prevent their continued processing, but which were discarded.
  [[inputs.snmp.field]]
    name = "ipInDiscards"
    oid = "RFC1213-MIB::ipInDiscards.0"
  # The total number of input datagrams successfully delivered to IP user-protocols (including ICMP)
  [[inputs.snmp.field]]
    name = "ipInDelivers"
    oid = "RFC1213-MIB::ipInDelivers.0"
  # The total number of IP datagrams which local IP user-protocols (including ICMP) supplied to IP in requests for transmission
  [[inputs.snmp.field]]
    name = "ipOutRequests"
    oid = "RFC1213-MIB::ipOutRequests.0"
  # The number of output IP datagrams for which no problem was encountered to prevent their transmission to their destination, but which were discarded
  [[inputs.snmp.field]]
    name = "ipOutDiscards"
    oid = "RFC1213-MIB::ipOutDiscards.0"
  # The number of IP datagrams discarded because no route could be found to transmit them to their destination
  [[inputs.snmp.field]]
    name = "ipOutNoRoutes"
    oid = "RFC1213-MIB::ipOutNoRoutes.0"
  # The maximum number of seconds which received fragments are held while they are awaiting reassembly at this entity
  [[inputs.snmp.field]]
    name = "ipReasmTimeout"
    oid = "RFC1213-MIB::ipReasmTimeout.0"
  # The number of IP fragments received which needed to be reassembled at this entity
  [[inputs.snmp.field]]
    name = "ipReasmReqds"
    oid = "RFC1213-MIB::ipReasmReqds.0"
  # The number of IP datagrams successfully reassembled
  [[inputs.snmp.field]]
    name = "ipReasmOKs"
    oid = "RFC1213-MIB::ipReasmOKs.0"
  # The number of failures detected by the IP reassembly algorithm
  [[inputs.snmp.field]]
    name = "ipReasmFails"
    oid = "RFC1213-MIB::ipReasmFails.0"
  # The number of IP datagrams that have been successfully fragmented at this entity
  [[inputs.snmp.field]]
    name = "ipFragOKs"
    oid = "RFC1213-MIB::ipFragOKs.0"
  # The number of IP datagrams that have been discarded because they needed to be fragmented at this entity
  [[inputs.snmp.field]]
    name = "ipFragFails"
    oid = "RFC1213-MIB::ipFragFails.0"
  # The number of IP datagram fragments that have been generated as a result of fragmentation at this entity
  [[inputs.snmp.field]]
    name = "ipFragCreates"
    oid = "RFC1213-MIB::ipFragCreates.0"

  ##
  ## SNMP metrics
  ##
  #  Number of SNMP messages received
  [[inputs.snmp.field]]
    name = "snmpInPkts"
    oid = "SNMPv2-MIB::snmpInPkts.0"
  #  Number of SNMP Get-Request received
  [[inputs.snmp.field]]
    name = "snmpInGetRequests"
    oid = "SNMPv2-MIB::snmpInGetRequests.0"
  #  Number of SNMP Get-Next received
  [[inputs.snmp.field]]
    name = "snmpInGetNexts"
    oid = "SNMPv2-MIB::snmpInGetNexts.0"
  #  Number of SNMP objects requested
  [[inputs.snmp.field]]
    name = "snmpInTotalReqVars"
    oid = "SNMPv2-MIB::snmpInTotalReqVars.0"
  #  Number of SNMP Get-Response received
  [[inputs.snmp.field]]
    name = "snmpInGetResponses"
    oid = "SNMPv2-MIB::snmpInGetResponses.0"
  #  Number of SNMP messages sent
  [[inputs.snmp.field]]
    name = "snmpOutPkts"
    oid = "SNMPv2-MIB::snmpOutPkts.0"
  #  Number of SNMP Get-Request sent
  [[inputs.snmp.field]]
    name = "snmpOutGetRequests"
    oid = "SNMPv2-MIB::snmpOutGetRequests.0"
  #  Number of SNMP Get-Next sent
  [[inputs.snmp.field]]
    name = "snmpOutGetNexts"
    oid = "SNMPv2-MIB::snmpOutGetNexts.0"
  #  Number of SNMP Get-Response sent
  [[inputs.snmp.field]]
    name = "snmpOutGetResponses"
    oid = "SNMPv2-MIB::snmpOutGetResponses.0"
```

## (参考) Grafana で目一杯表示してみると

![screencapture-yui-oshiire-to-3000-d-5vstvMgGk-oshiire-routers-2021-02-07-16_31_18.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/37263/2c6f3d35-9004-d2d2-abf4-de8df1e22d86.png)