评论

收藏

[python] 使用bmp的方式监控BGP状态

编程语言 编程语言 发布于:2021-08-06 14:53 | 阅读数:689 | 评论:0

【说明】
bmp(bgp monitor protocol)服务器使用基于python开源的yabmp
因为是在模拟环境下做的实验,h3c的模拟器不支持bmp,所以通过使用gobgp来做bmp的客户端
一、实验拓扑
DSC0000.jpg

二、配置
# sw-1#
[h3c01]disp cur con  bgp
#
bgp 65001
 router-id 192.168.31.103
 peer 192.168.31.104 as-number 65002
 peer 192.168.31.110 as-number 64512
 #
 address-family ipv4 unicast
  network 33.33.33.33 255.255.255.255
  network 192.168.31.0 255.255.255.0
  peer 192.168.31.104 enable
  peer 192.168.31.110 enable
#
return
# sw-2 #
<h3c02>disp cur con bgp
#
bgp 65002
 router-id 192.168.31.104
 peer 192.168.31.103 as-number 65001
 peer 192.168.31.110 as-number 64512
 #
 address-family ipv4 unicast
  network 192.168.31.0 255.255.255.0
  peer 192.168.31.103 enable
  peer 192.168.31.110 enable
#
return
# gobgp # 
[root@C7 gobgp]# cat gobgpd.conf 
[global.config]
  as = 64512
  router-id = "192.168.31.110"
[[neighbors]]
  [neighbors.config]
  neighbor-address = "192.168.31.103"
  peer-as = 65001
[[neighbors]]
  [neighbors.config]
  neighbor-address = "192.168.31.104"
  peer-as = 65002
[[bmp-servers]]
  [bmp-servers.config]
  address = "192.168.31.247"
  port=20000
yabmp 使用默认的配置选项,可以通过python bin/yabmpd -h查看具体有哪些选项
三、运行gobgpd和yabmpd
软件的具体安装过程请查看文章开头的GitHub引用链接;
1.gobgpd -f gobgpd.conf
[root@C7-slave-72 gobgp]# gobgpd -f gobgpd.conf 
{"level":"info","msg":"gobgpd started","time":"2021-08-06T21:19:46+08:00"}
{"Topic":"Config","level":"info","msg":"Finished reading the config file","time":"2021-08-06T21:19:46+08:00"}
{"level":"info","msg":"Peer 192.168.31.103 is added","time":"2021-08-06T21:19:46+08:00"}
{"Topic":"Peer","level":"info","msg":"Add a peer configuration for:192.168.31.103","time":"2021-08-06T21:19:46+08:00"}
{"level":"info","msg":"Peer 192.168.31.104 is added","time":"2021-08-06T21:19:46+08:00"}
{"Topic":"Peer","level":"info","msg":"Add a peer configuration for:192.168.31.104","time":"2021-08-06T21:19:46+08:00"}
{"Topic":"bmp","level":"info","msg":"BMP server is connected:192.168.31.247:20000","time":"2021-08-06T21:19:46+08:00"}
{"Key":"192.168.31.103","State":"BGP_FSM_OPENCONFIRM","Topic":"Peer","level":"info","msg":"Peer Up","time":"2021-08-06T21:19:47+08:00"}
{"Key":"192.168.31.104","State":"BGP_FSM_OPENCONFIRM","Topic":"Peer","level":"info","msg":"Peer Up","time":"2021-08-06T21:19:48+08:00"}
2.启动yabmpd
(yabmp)  yabmp-master % python bin/yabmpd             
2021-08-06 13:21:05,250.250 2317 INFO yabmp.service [-] Log (Re)opened.
2021-08-06 13:21:05,250.250 2317 INFO yabmp.service [-] Configuration:
2021-08-06 13:21:05,250.250 2317 INFO yabmp.service [-] ********************************************************************************
2021-08-06 13:21:05,250.250 2317 INFO yabmp.service [-] Configuration options gathered from:
2021-08-06 13:21:05,250.250 2317 INFO yabmp.service [-] command line args: []
2021-08-06 13:21:05,251.251 2317 INFO yabmp.service [-] config files: []
2021-08-06 13:21:05,251.251 2317 INFO yabmp.service [-] ================================================================================
2021-08-06 13:21:05,251.251 2317 INFO yabmp.service [-] bind_host            = 0.0.0.0
2021-08-06 13:21:05,251.251 2317 INFO yabmp.service [-] bind_port            = 20000
2021-08-06 13:21:05,251.251 2317 INFO yabmp.service [-] config_dir           = None
2021-08-06 13:21:05,251.251 2317 INFO yabmp.service [-] config_file          = []
2021-08-06 13:21:05,251.251 2317 INFO yabmp.service [-] log_backup_count         = 5
2021-08-06 13:21:05,251.251 2317 INFO yabmp.service [-] log_config_file        = None
2021-08-06 13:21:05,251.251 2317 INFO yabmp.service [-] log_dir            = None
2021-08-06 13:21:05,251.251 2317 INFO yabmp.service [-] log_file             = /Users/**data/bmp/local/log/yabmp.log
2021-08-06 13:21:05,251.251 2317 INFO yabmp.service [-] log_file_mode          = 0644
2021-08-06 13:21:05,251.251 2317 INFO yabmp.service [-] use_stderr           = True
2021-08-06 13:21:05,251.251 2317 INFO yabmp.service [-] verbose            = False
2021-08-06 13:21:05,251.251 2317 INFO yabmp.service [-] message.write_dir        = /Users/**/data/bmp/local/msg
2021-08-06 13:21:05,251.251 2317 INFO yabmp.service [-] message.write_msg_max_size   = 500
2021-08-06 13:21:05,251.251 2317 INFO yabmp.service [-] ********************************************************************************
2021-08-06 13:21:05,251.251 2317 INFO root [-] Initial BMP Factory!
2021-08-06 13:21:05,251.251 2317 INFO yabmp.service [-] Starting bmpd server listen to port = 20000 and ip = 0.0.0.0
四、查看bgp变动的消息
消息目录:    message.write_dir              = /Users/**/data/bmp/local/msg
cat msg/192.168.31.110/192.168.31.103/1628218920.608693.msg
[1628218920.609, 1, 1, {'version': 4, 'asn': 65001, 'hold_time': 180, 'bgp_id': '192.168.31.103', 'capabilities': {'afi_safi': [(1, 1)], 'route_refresh': True, 'four_bytes_as': True}}, (0, 0)]
[1628218920.611258, 2, 130, (2, {'attr': {1: 0, 2: [(2, [65001])], 3: '192.168.31.103', 4: 0}, 'nlri': ['192.168.31.0/24'], 'withdraw': []}), (1, 1)]
[1628219330.2266371, 3, 130, (2, {'attr': {1: 0, 2: [(2, [65001])], 3: '192.168.31.103', 4: 0}, 'nlri': ['33.33.33.33/32'], 'withdraw': []}), (1, 1)]
[1628223979.388705, 4, 1, {'version': 4, 'asn': 65001, 'hold_time': 180, 'bgp_id': '192.168.31.103', 'capabilities': {'afi_safi': [(1, 1)], 'route_refresh': True, 'four_bytes_as': True}}, (0, 0)]
[1628223979.38894, 5, 130, (2, {'attr': {1: 0, 2: [(2, [65001])], 3: '192.168.31.103', 4: 0}, 'nlri': ['33.33.33.33/32'], 'withdraw': []}), (1, 1)]
[1628223979.389089, 6, 130, (2, {'attr': {1: 0, 2: [(2, [65001])], 3: '192.168.31.103', 4: 0}, 'nlri': ['192.168.31.0/24'], 'withdraw': []}), (1, 1)]
[1628225080.627847, 7, 1, {'version': 4, 'asn': 65001, 'hold_time': 180, 'bgp_id': '192.168.31.103', 'capabilities': {'afi_safi': [(1, 1)], 'route_refresh': True, 'four_bytes_as': True}}, (0, 0)]
#
# 从这里开始 多个datetime格式的时间戳,是我修改了代码,后面会说
#
['2021--08--06 12:44:40', 1628225080.6284769, 8, 130, (2, {'attr': {1: 0, 2: [(2, [65001])], 3: '192.168.31.103', 4: 0}, 'nlri': ['33.33.33.33/32'], 'withdraw': []}), (1, 1)]
['2021--08--06 12:44:40', 1628225080.628723, 9, 130, (2, {'attr': {1: 0, 2: [(2, [65001])], 3: '192.168.31.103', 4: 0}, 'nlri': ['192.168.31.0/24'], 'withdraw': []}), (1, 1)]
[1628227188.515646, 10, 1, {'version': 4, 'asn': 65001, 'hold_time': 180, 'bgp_id': '192.168.31.103', 'capabilities': {'afi_safi': [(1, 1)], 'route_refresh': True, 'four_bytes_as': True}}, (0, 0)]
['2021--08--06 13:19:48', 1628227188.5204768, 11, 130, (2, {'attr': {1: 0, 2: [(2, [65001])], 3: '192.168.31.103', 4: 0}, 'nlri': ['33.33.33.33/32'], 'withdraw': []}), (1, 1)]
['2021--08--06 13:19:48', 1628227188.520828, 12, 130, (2, {'attr': {1: 0, 2: [(2, [65001])], 3: '192.168.31.103', 4: 0}, 'nlri': ['192.168.31.0/24'], 'withdraw': []}), (1, 1)]
五、二次修改代码
1.修改msg时间戳
yabmp-master/yabmp/handler/default.py -> def on_message_received中
DSC0001.jpg

2.丰富输出方式(不只是写入文件,还可以发到syslog服务器上)
可以在如下图的路径代码中丰富输出方式
DSC0002.jpg

logging支持向syslog发送消息:
# 示例,没有具体测试!!
import logging
import logging.handlers  # handlers要单独import
logger = logging.getLogger()
fh = logging.handlers.SysLogHandler(('23.106.133.164', 514), logging.handlers.SysLogHandler.LOG_AUTH)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
logger.addHandler(fh)
logger.warning("msg")
logger.error("msg")
好了,这次实验到此,大家在公司都有用哪些方式监控BGP呢?欢迎交流学习。


关注下面的标签,发现更多相似文章