-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add bgpmon to be started as a new daemon under BGP docker #5329
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
61b8ce4
Add bgpmon to be started as a new daemon under BGP docker
gechiang 9c8c09a
Added bgpmon to be monitored by Monit so that if it crashed, it gets …
gechiang 7af52cb
moved bgpmon under sonic-bgpcfgd to be started as a new daemon under …
gechiang 23785b5
use console_scripts entry point to package bgpmon
gechiang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
#!/usr/bin/env python2 | ||
|
||
"""" | ||
Description: bgpmon.py -- populating bgp related information in stateDB. | ||
script is started by supervisord in bgp docker when the docker is started. | ||
|
||
Initial creation of this daemon is to assist SNMP agent in obtaining the | ||
BGP related information for its MIB support. The MIB that this daemon is | ||
assiting is for the CiscoBgp4MIB (Neighbor state only). If there are other | ||
BGP related items that needs to be updated in a periodic manner in the | ||
future, then more can be added into this process. | ||
|
||
The script check if there are any bgp activities by monitoring the bgp | ||
frr.log file timestamp. If activity is detected, then it will request bgp | ||
neighbor state via vtysh cli interface. This bgp activity monitoring is | ||
done periodically (every 15 second). When triggered, it looks specifically | ||
for the neighbor state in the json output of show ip bgp neighbors json | ||
and update the state DB for each neighbor accordingly. | ||
In order to not disturb and hold on to the State DB access too long and | ||
removal of the stale neighbors (neighbors that was there previously on | ||
previous get request but no longer there in the current get request), a | ||
"previous" neighbor dictionary will be kept and used to determine if there | ||
is a need to perform update or the peer is stale to be removed from the | ||
state DB | ||
""" | ||
import commands | ||
import json | ||
import os | ||
import syslog | ||
import swsssdk | ||
import time | ||
|
||
PIPE_BATCH_MAX_COUNT = 50 | ||
|
||
class BgpStateGet(): | ||
def __init__(self): | ||
# list peer_l stores the Neighbor peer Ip address | ||
# dic peer_state stores the Neighbor peer state entries | ||
# list new_peer_l stores the new snapshot of Neighbor peer ip address | ||
# dic new_peer_state stores the new snapshot of Neighbor peer states | ||
self.peer_l = [] | ||
self.peer_state = {} | ||
self.new_peer_l = [] | ||
self.new_peer_state = {} | ||
self.cached_timestamp = 0 | ||
self.db = swsssdk.SonicV2Connector() | ||
self.db.connect(self.db.STATE_DB, False) | ||
client = self.db.get_redis_client(self.db.STATE_DB) | ||
self.pipe = client.pipeline() | ||
self.db.delete_all_by_pattern(self.db.STATE_DB, "NEIGH_STATE_TABLE|*" ) | ||
|
||
# A quick way to check if there are anything happening within BGP is to | ||
# check its log file has any activities. This is by checking its modified | ||
# timestamp against the cached timestamp that we keep and if there is a | ||
# difference, there is activity detected. In case the log file got wiped | ||
# out, it will default back to constant pulling every 15 seconds | ||
def bgp_activity_detected(self): | ||
try: | ||
timestamp = os.stat("/var/log/frr/frr.log").st_mtime | ||
if timestamp != self.cached_timestamp: | ||
self.cached_timestamp = timestamp | ||
return True | ||
else: | ||
return False | ||
except (IOError, OSError): | ||
return True | ||
|
||
def update_new_peer_states(self, peer_dict): | ||
peer_l = peer_dict["peers"].keys() | ||
self.new_peer_l.extend(peer_l) | ||
for i in range (0, len(peer_l)): | ||
self.new_peer_state[peer_l[i]] = peer_dict["peers"][peer_l[i]]["state"] | ||
|
||
# Get a new snapshot of BGP neighbors and store them in the "new" location | ||
def get_all_neigh_states(self): | ||
cmd = "vtysh -c 'show bgp summary json'" | ||
rc, output = commands.getstatusoutput(cmd) | ||
if rc: | ||
syslog.syslog(syslog.LOG_ERR, "*ERROR* Failed with rc:{} when execute: {}".format(rc, cmd)) | ||
return | ||
|
||
peer_info = json.loads(output) | ||
# cmd ran successfully, safe to Clean the "new" lists/dic for new sanpshot | ||
del self.new_peer_l[:] | ||
self.new_peer_state.clear() | ||
for key, value in peer_info.items(): | ||
if key == "ipv4Unicast" or key == "ipv6Unicast": | ||
self.update_new_peer_states(value) | ||
|
||
# This method will take the caller's dictionary which contains the peer state operation | ||
# That need to be updated in StateDB using Redis pipeline. | ||
# The data{} will be cleared at the end of this method before returning to caller. | ||
def flush_pipe(self, data): | ||
"""Dump each entry in data{} into State DB via redis pipeline. | ||
Args: | ||
data: Neighbor state in dictionary format | ||
{ | ||
'NEIGH_STATE_TABLE|ip_address_a': {'state':state}, | ||
'NEIGH_STATE_TABLE|ip_address_b': {'state':state}, | ||
'NEIGH_STATE_TABLE|ip_address_c': {'state':state}, | ||
'NEIGH_STATE_TABLE|ip_address_x': None, | ||
'NEIGH_STATE_TABLE|ip_address_z': None | ||
... | ||
} | ||
""" | ||
for key, value in data.items(): | ||
if value is None: | ||
# delete case | ||
self.pipe.delete(key) | ||
else: | ||
# Add or Modify case | ||
self.pipe.hmset(key, value) | ||
self.pipe.execute() | ||
data.clear() | ||
|
||
def update_neigh_states(self): | ||
data = {} | ||
for i in range (0, len(self.new_peer_l)): | ||
peer = self.new_peer_l[i] | ||
key = "NEIGH_STATE_TABLE|%s" % peer | ||
if peer in self.peer_l: | ||
# only update the entry if state changed | ||
if self.peer_state[peer] != self.new_peer_state[peer]: | ||
# state changed. Update state DB for this entry | ||
state = self.new_peer_state[peer] | ||
data[key] = {'state':state} | ||
self.peer_state[peer] = state | ||
# remove this neighbor from old list since it is accounted for | ||
self.peer_l.remove(peer) | ||
else: | ||
# New neighbor found case. Add to dictionary and state DB | ||
state = self.new_peer_state[peer] | ||
data[key] = {'state':state} | ||
self.peer_state[peer] = state | ||
if len(data) > PIPE_BATCH_MAX_COUNT: | ||
self.flush_pipe(data) | ||
# Check for stale state entries to be cleaned up | ||
while len(self.peer_l) > 0: | ||
# remove this from the stateDB and the current nighbor state entry | ||
peer = self.peer_l.pop(0) | ||
del_key = "NEIGH_STATE_TABLE|%s" % peer | ||
data[del_key] = None | ||
del self.peer_state[peer] | ||
if len(data) > PIPE_BATCH_MAX_COUNT: | ||
self.flush_pipe(data) | ||
# If anything in the pipeline not yet flushed, flush them now | ||
if len(data) > 0: | ||
self.flush_pipe(data) | ||
# Save the new List | ||
self.peer_l = self.new_peer_l[:] | ||
|
||
def main(): | ||
|
||
print "bgpmon service started" | ||
|
||
try: | ||
bgp_state_get = BgpStateGet() | ||
except Exception as e: | ||
syslog.syslog(syslog.LOG_ERR, "{}: error exit 1, reason {}".format(THIS_MODULE, str(e))) | ||
exit(1) | ||
|
||
# periodically obtain the new neighbor infomraton and update if necessary | ||
while True: | ||
time.sleep(15) | ||
if bgp_state_get.bgp_activity_detected(): | ||
bgp_state_get.get_all_neigh_states() | ||
bgp_state_get.update_neigh_states() | ||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you do console_script packaging? it is better to do that and we can get rid of .py suffix.
https://python-packaging.readthedocs.io/en/latest/command-line-scripts.html
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lguohan Agreed. I have modified to use console_scripts packaging and it indeed got rid of the .py suffix.
Code re-tested to be functioning as before.