-
Notifications
You must be signed in to change notification settings - Fork 7
/
bgp.py
190 lines (179 loc) · 6.59 KB
/
bgp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# -*- coding: UTF-8 -*-
"""
This module pokes Mikrotik for BGP Counters
"""
import logging
import time
import pprint
from libs.time import time_convert
from libs.strings import zabbix_escape
def run(api, ts=False, log=logging.getLogger(__name__), ver=''):
"""
Returns BGP Counters
:param api: initialized librouteros' connect()
:param ts: Use timestamps
:return:
"""
if ts:
unixtime = " {time} ".format(
time=int(time.time())
)
else:
unixtime = " "
# The list of BGP values to monitor
values_to_monitor = {
"6": {
'remote-as': 'remote-as', # Remote AS for peer
'prefix-count': 'prefix-count', # Accepted Prefixes
'disabled': 'disabled', # Administrative status
'comment': 'comment', # Printing the comment
'updates-received': 'updates-received', # Updates Received
'updates-sent': 'updates-sent', # Updates Sent
'withdrawn-received': 'withdrawn-received', # Withdrawn Received
'withdrawn-sent': 'withdrawn-sent' # Withdrawn Sent
},
"7": {
'remote-as': 'remote.as', # Remote AS for peer
'prefix-count': 'prefix-count', # Accepted Prefixes
'updates-received': 'remote.messages', # Updates Received
'updates-sent': 'local.messages', # Updates Sent
}
}
if ver.startswith('7.'):
log.debug("BGP: Dealing with firmware version 7.x ({ver})".format(
ver=ver
))
bgpstats = api(cmd='/routing/bgp/session/print')
major_version = "7"
else:
log.debug("BGP: Dealing with firmware version 6.x ({ver})".format(
ver=ver
))
bgpstats = api(cmd='/routing/bgp/peer/print')
major_version = "6"
for bgpitem in bgpstats:
log.debug("BGP: Fetching item {item}".format(
item=bgpitem
))
if log.getEffectiveLevel() <= logging.DEBUG:
pprint.pp(bgpitem)
"""
BGP item for RouterOS v 7.12 looks like this:
{'.id': '*2800002',
'name': 'ucomline-1',
'remote.address': '78.x.x.x',
'remote.as': 12883,
'remote.id': '213.x.x.x',
'remote.capabilities': 'mp,rr,as4,err',
'remote.messages': 269324,
'remote.bytes': 31476311,
'remote.eor': 'ip',
'local.role': 'ebgp-customer',
'local.address': '78.x.x.x',
'local.as': 34605,
'local.id': '194.x.x.x',
'local.capabilities': 'mp,rr,gr,as4',
'local.messages': 139711,
'local.bytes': 17700041,
'local.eor': '',
'output.affinity': 'alone',
'output.procid': 23,
'output.filter-chain': 'UCOMLINE-OUT',
'output.network': 'BGP-Advertisements',
'output.keep-sent-attributes': True,
'input.procid': 22,
'input.filter': 'UCOMLINE-IN',
'ebgp': '',
'hold-time': '1m30s',
'keepalive-time': '30s',
'uptime': '18h8m41s680ms',
'last-started': '2023-11-17 08:50:29',
'last-stopped': '2023-11-17 08:50:28',
'prefix-count': 902665,
'established': True}
BGP Item for 6.49.2 looks like this:
{'.id': '*0',
'name': 'ucomline',
'instance': 'default',
'remote-address': '78.x.x.x',
'remote-as': 12883,
'tcp-md5-key': '*****',
'nexthop-choice': 'default',
'multihop': False,
'route-reflect': False,
'hold-time': '1m30s',
'keepalive-time': '30s',
'ttl': 'default',
'in-filter': 'UCOMLINE-IN',
'out-filter': 'UCOMLINE-OUT',
'address-families': 'ip',
'update-source': 'vlan202',
'default-originate': 'never',
'remove-private-as': False,
'as-override': False,
'passive': False,
'use-bfd': False,
'remote-id': '213.x.x.x',
'local-address': '78.x.x.x',
'uptime': '19w3d21h51m11s',
'prefix-count': 902668,
'updates-sent': 1223,
'updates-received': 74537047,
'withdrawn-sent': 800,
'withdrawn-received': 4966247,
'remote-hold-time': '3m',
'used-hold-time': '1m30s',
'used-keepalive-time': '30s',
'refresh-capability': True,
'as4-capability': True,
'state': 'established',
'established': True,
'disabled': False,
'comment': 'Ucomline'}
"""
for zabbix_key, ros_key in values_to_monitor[major_version].items():
print("{host} \"{key}\"{unixtime}{value}".format(
host='-',
key='mikrotik.bgp.node[{name},{val}]'.format(
name=bgpitem['name'],
val=zabbix_key
),
unixtime=unixtime,
value=zabbix_escape(bgpitem.get(ros_key, 0))
))
# Established time for peer
uptime = bgpitem.get('uptime', '0s')
print("{host} \"{key}\"{unixtime}{value}".format(
host='-',
key='mikrotik.bgp.node[{name},uptime]'.format(
name=bgpitem['name']
),
unixtime=unixtime,
# value=bgpitem['uptime']
value=zabbix_escape(time_convert(uptime))
))
if not ver.startswith('7.'):
state = bgpitem.get('state', 'disabled')
# operational status
if state == "idle":
bgp_state = 1
elif state == "connect":
bgp_state = 2
elif state == "active":
bgp_state = 3
elif state == "opensent":
bgp_state = 4
elif state == "openconfirm":
bgp_state = 5
elif state == "established":
bgp_state = 6
else:
bgp_state = 0
print("{host} \"{key}\"{unixtime}{value}".format(
host='-',
key='mikrotik.bgp.node[{name},state]'.format(
name=bgpitem['name']
),
unixtime=unixtime,
value=zabbix_escape(bgp_state)
))