NVR500のtechinfoをスクリプトで抽出しグラフ化してみた

# はじめに

YAMAHAのブロードバンドVoIPルーターNVR500。
SNMPに対応しますが、MIBとして取得出来ない情報について、telnet経由でtechinfoを取得し、抽出してみました。

# 実行環境

## クライアント
- OS: ubilinux@edison (debian 7.11)
- GNU bash, version 4.2.37(1)-release (i486-pc-linux-gnu)
- telnet はインストール済み

## ルータ情報
- NVR500 Rev.11.00.35 (Thu Sep 29 15:32:52 2016)
- telnet接続は事前に許可


# スクリプト

```
#!/bin/bash

function get_nvr_techinfo () {

  IP="192.168.1.1"
  ID=""
  PASS=""

  push_enter () {
    sleep 1
    for i in {0..100}; do
      sleep 0.1
      echo ""
    done
  }

  (
   echo open "${IP}"
   sleep 1
   echo "${ID}"
   sleep 1
   echo "${PASS}"
   sleep 1

   echo "console character ascii"
   sleep 0.2
   echo "console columns 200"
   sleep 0.2
   echo "console lines 100"
   sleep 0.2
   echo "console lines infinity"
   sleep 0.2
   echo "show techinfo | grep -v ' TELNET: ' "
   push_enter
   echo "quit"
   sleep 0.2

  ) | telnet 

}

function parce_nvr_techinfo () {

  TMP=$1

  #show environment
  echo "CPU1min=`cat ${TMP} | grep -A 10 "show environment" | grep "CPU:" | sed 's/^.*(5sec) //' | sed 's/%(.*//' | sed 's/ *//'`"
  echo "MemoryUsage=`cat ${TMP} | grep -A 10 "show environment" | grep "Memory:" | sed 's/^.*Memory: //' | sed 's/%.*//'`"

  #status lan2
  echo "Lan2TransmittedOctets=`cat ${TMP} | grep -A 15 "show status lan2" | grep "Transmitted:" | sed 's/^.* packets (//' | cut -f 1 -d " "`"
  echo "Lan2ReceivedOctets=`cat ${TMP} | grep -A 15 "show status lan2" | grep "Received:" | sed 's/^.* packets (//' | cut -f 1 -d " "`"

  #Packets  Transmitted
  echo "Lan2TransmittedIPv4packets=`cat ${TMP} | grep -A 15 "show status lan2" | grep -A 10 "Transmitted:" | grep "IPv4(all/fastpath):"  | sed 's/ packets.*//g'| sed 's/.* //'`" 
  echo "Lan2TransmittedIPv6packets=`cat ${TMP} | grep -A 15 "show status lan2" | grep -A 10 "Transmitted:" | grep "IPv6(all/fastpath):"  | sed 's/ packets.*//g'| sed 's/.* //'`"

  #Packets  Received
  echo "Lan2ReceivedIPv4packets=`cat ${TMP} | grep -A 15 "show status lan2" | grep -A 10 "Received:" | grep "IPv4:"  | sed 's/ packets.*//g' | sed 's/.* //'`"
  echo "Lan2ReceivedIPv6packets=`cat ${TMP} | grep -A 15 "show status lan2" | grep -A 10 "Received:" | grep "IPv6:"  | sed 's/ packets.*//g' | sed 's/.* //'`"

  #connection
  echo "Ipv4connectionCount=`cat ${TMP} | grep -A 4000 "show ip connection" | grep -B 4000 "show ip intrusion" | grep ":" | grep ">" | wc -l`"
  echo "Ipv6connectionCount=`cat ${TMP} | grep -A 4000 "show ipv6 connection" | grep -B 4000 "show ipv6 rip table" | grep ":" | grep ">" | wc -l`"

  #show nat descriptor
  echo "NatUsedCount=`cat ${TMP} | grep -A 1000 "show nat descriptor address" | grep -B 1000 "show ip connection" | grep "Port range:" | sed 's/ used.*$//' | sed 's/^.* //'`"

}

# main

TMPDIR=/tmp/$$
mkdir -p ${TMPDIR}
TMP1=${TMPDIR}/1.tmp

get_nvr_techinfo > ${TMP1}
parce_nvr_techinfo ${TMP1} 
rm -rf ${TMPDIR}
```


# 実行例
- スクリプトは「get_techinfo.sh」として保存
- JSON変換は「jo」コマンドを利用

```
$ bash get-techinfo.sh | jo -p
{
   "CPU1min": 3,
   "MemoryUsage": 17,
   "Lan2TransmittedOctets": 4411890433,
   "Lan2ReceivedOctets": 80509481979,
   "Lan2TransmittedIPv4packets": 14398973,
   "Lan2TransmittedIPv6packets": 19149580,
   "Lan2ReceivedIPv4packets": 21852663,
   "Lan2ReceivedIPv6packets": 42126603,
   "Ipv4connectionCount": 45,
   "Ipv6connectionCount": 33,
   "NatUsedCount": 90
}
```

# グラフサンプル

- 取得した値をAWSに転送し、Cloudwatchのカスタムメトリックス経由でDatadogでグラフ化する事ができました。

![nvr500_datadog.jpg](https://qiita-image-store.s3.amazonaws.com/0/39112/36e4dfa3-1782-093a-64de-29154c70819f.jpeg)