forked from Pelt10/bird-snmp-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bird-snmp-agent
executable file
·105 lines (80 loc) · 2.91 KB
/
bird-snmp-agent
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
#!/usr/bin/python3
#
# Copyright (c) 2016 Travelping GmbH <[email protected]>
# by Tobias Hintze <[email protected]>
#
# This code is inspired and partially copied from
# https://r3blog.nl/index.php/archives/2011/02/24/bgp4-mib-support-for-bird/
# That original code does not clearly declare any license.
#
# This code also uses python-agentx library licensed under GPLv3
# (see agentx.py for details)
#
# So this code is licensed under the GPLv3 (see COPYING.GPLv3).
#
from __future__ import print_function
from adv_agentx import AgentX
import time
import os
import functools
from birdagent import BirdAgent
# handle get and getnext requests
def OnSnmpRead(req, ax, axd):
pass
# handle set requests
def OnSnmpWrite(req, ax, axd):
pass
# handle get, getnext and set requests
def OnSnmpRequest(req, ax, axd):
pass
# initialize any ax and axd dependant code here
def OnInit(ax, axd):
pass
# register some variables
# this function is called when a new snmp request has been received and
# if CacheInterval has expired at that time
def OnUpdate(ax, axd, state):
print('updated bird-bgp state: {0}'.format(time.time()))
# register variables
axd.RegisterVar('bgp', 0)
axd.RegisterVar('bgpVersion', "10")
axd.RegisterVar('bgpLocalAs', 0)
axd.RegisterVar('bgpLocalAs.0', state.get("bgpLocalAs"))
# reindex by bgpPeerRemoteAddr
peers = {}
for peer in list(state["bgp-peers"].values()):
peers[peer.get("bgpPeerRemoteAddr")] = peer
for snmpkey in BirdAgent.bgp_keys:
axd.RegisterVar(snmpkey, 0)
for peer in sorted(list(peers.keys()), key=functools.cmp_to_key(BirdAgent.ipCompare)):
oid = "%s.%s" % (snmpkey, peer)
if snmpkey in peers[peer]:
axd.RegisterVar(oid, peers[peer][snmpkey])
else:
axd.RegisterVar(oid, BirdAgent.bgp_defaults[snmpkey])
return
# main program
if __name__ == '__main__':
print('bird-bgp-agent AgentX starting')
bird = BirdAgent(
os.environ.get("BIRDCONF") or "/etc/bird/bird.conf",
os.environ.get("BIRDCLI") or "/usr/sbin/birdc",
os.environ.get("SSCMD") or "/bin/ss -tan -o state established '( dport = :bgp or sport = :bgp )'")
callbacks = {
"OnSnmpRead": OnSnmpRead,
"OnSnmpWrite": OnSnmpWrite,
"OnSnmpRequest": OnSnmpRequest,
"OnInit": OnInit,
"OnUpdate": lambda ax, axd: OnUpdate(ax, axd, bird.getBGPState())
}
# initialize agentx module and run main loop
try:
AgentX(
callbacks,
Name='bird-bgp',
MIBFile=os.environ.get("BGPMIBFILE") or "/var/lib/snmp/mibs/ietf/BGP4-MIB",
RootOID='BGP4-MIB::bgp', # https://tools.ietf.org/html/draft-ietf-idr-bgp4-mib-06
CacheInterval=int(os.environ.get("AGENTCACHEINTERVAL") or "30")
)
except KeyboardInterrupt:
print('bird-bgp-agent AgentX terminating')