LoginSignup
14
12

More than 5 years have passed since last update.

Ruby SNMPを使ったJuniperルータのBGP状態の取得

Last updated at Posted at 2014-05-23

RubyでJuniperルータのBGP状態をSNMPで取得する方法

MIB

MIBはこちら。

jnxBgpM2PeerState OBJECT-TYPE
        SYNTAX     INTEGER {
            idle(1),
            connect(2),
            active(3),
            opensent(4),
            openconfirm(5),
            established(6)
        }

1.3.6.1.4.1.2636.5.1.1.2.1.1.1.2.0で一覧を取得できるようだ。

CLIから

ルータのCLIからshow snmp mibで確認することができる。

router> show snmp mib walk jnxBgpM2PeerEntry

OID

IPv4はこんな感じ。1のあとにIPv4アドレスがドット表記で続く。前が自分のアドレス、後ろがpeerのアドレス

SNMPv2-SMI::enterprises.2636.5.1.1.2.1.1.1.14.0.1.192.168.0.1.1.192.168.0.2

IPv6はこんな感じ。2のあとになにやらたくさんの数字が続く。これはIPv6アドレスを8bitごとに区切って、10進数で表記したもの。2001:db8::132.1.13.184.0.0.0.0.0.0.0.0.0.0.0.1になる。

SNMPv2-SMI::enterprises.2636.5.1.1.2.1.1.1.14.0.2.32.1.13.184.0.0.0.0.0.0.0.0.0.0.0.1.2.32.1.13.184.0.0.0.0.0.0.0.0.0.0.0.1

IPアドレスをOIDに変える

IPAddrクラスを拡張するとこんな感じに書ける

class IPAddr
  def to_oid
    if self.ipv6?
      return self.to_string.delete(":").scan(/.{2}/).map{|h| h.hex}.join(".")
    end
    self.to_s
  end
end

SNMPで取得する

Ruby SNMP http://snmplib.rubyforge.org/ を使うとこんな感じで書ける。

protocol_id = peer_address.ipv4? 1 : 2
oid = "1.3.6.1.4.1.2636.5.1.1.2.1.1.1.2.0.#{protocol_id}.#{local_address.to_oid}.#{protocol_id}.#{peer_address.to_oid}"
begin
  snmp_manager = SNMP::Manager.new(:Host => host, :Community => community, :timeout => 1, :retries => 1)
  r = snmp_manager.get_value(oid)
  if r.to_s == "noSuchInstance"
    # no peer
  else
    if r.to_i == 6
      # established
    else
      # down
    end
  end
rescue => ex
  # SNMP error
end

SNMP::NoSuchInstance の判定方法がうまくいかなかったので、to_sで文字列として判定してます。

14
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
12