1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ヤマハのNW機器のL2MSが「いい感じ」なのに監視用に構造化されてないのでAnsibleで「正規化する話」

Last updated at Posted at 2022-02-21

この記事は

SNMPだとLAN外から利用できないので、L2MSを利用してヤマハ製ネットワーク機器を監視しようと思うが、テーブル形式(show arpとか)のは別に取れなくてもいいんだよって人向けの記事。

やりたかったこと

トラフィックを定期的にデータベースに入れたいので構造化(パース)された情報が欲しくて

前提

ネットワーク機器のssh、L2MS設定、Ansible、ansible-collection-rtxansible-collection-swx 及びPythonのインストールの用意ができている。

やり方

結果に含まれるセミコロンとカンマを正規表現で置換し、iniフォーマットととして保存する。

playbook.yml

---
- hosts: RTX830
  connection: network_cli

  tasks:
    - name: get lan map
      yamaha_network.rtx.rtx_command:
        commands:
        - show lan-map lan1
      register: result

    - name: show result
      debug:
        msg: "{{ result.stdout_lines[0] }}"

    - local_action:
        module: copy
        content: "{{ result.stdout[0] | regex_replace('\\n\\s+','\\n') | regex_replace(': ','= ') }}"
        dest: lan_map.ini
 
  vars:
    ansible_network_os: yamaha_network.rtx.rtx
    ansible_user: user_name
    ansible_ssh_pass: password
    ansible_become: true
    ansible_become_password: password
    ansible_port: 22
ansible-playbook -i inventory/host playbook.yml

cat lan_map.ini
[Master]
LinkUp          = lan1:2 
Terminal num    = 2

[Slave]
[ac:44:f2:be:a0:58]
Model name      = WLX212
System name     = WLX212_Z6C02576BO
Route           = lan1:2
LinkUp          = 1 
     UpLink     = 1
     DownLink   = none
State           = Idle
Appear          = 2022/02/20 20:20:05
Search Time     = 0 sec (2 msec)
Terminal num    = 2

iniファイルをパースする

import configparser
config = configparser.ConfigParser(allow_no_value=True)
config.read('lan_map.ini')
# ['lan_map.ini']

config.sections()
# ['Master', 'Slave', 'ac:44:f2:be:a0:58']

for key in config['Master']:  
     print(key)

# linkup
# terminal num

config['Master']['linkup']
# 'lan1:2'

最後までお読みいただきありがとうございます。
もし、気に入っていただける内容ありましたら、「いいね」や「シェア」していただけるととても嬉しいです。
今後の活動の大きな励みになります!

参考になった記事様

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?