mirror of
https://github.com/netbirdio/plugins.git
synced 2026-05-22 18:44:07 -07:00
Merge pull request #133 from fabianfrz/quagga
integrate bgp.rb into quagga.rb and fix a bug inside
This commit is contained in:
@@ -1,98 +0,0 @@
|
||||
#!/usr/local/bin/ruby
|
||||
=begin
|
||||
Copyright 2017 Fabian Franz
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
=end
|
||||
require 'json'
|
||||
|
||||
VTYSH = '/usr/local/bin/vtysh'
|
||||
|
||||
def show_ip_bgp
|
||||
output = `#{VTYSH} -d bgpd -c "show ip bgp"`
|
||||
return {} if output.include? "No BGP process is configured"
|
||||
return {} unless output.include? 'version' # we get an empty output if quagga is not running
|
||||
output = output.split("\n")
|
||||
bgp = {}
|
||||
|
||||
# global BGP information (version and ID)
|
||||
x,y = output.shift.scan(/.*?version is (\d+).*?ID is ([0-9\.]+).*/).first
|
||||
bgp['table_version'] = x
|
||||
bgp['local_router_id'] = y
|
||||
|
||||
# find out, what the status abbreviations mean
|
||||
status_codes = {}
|
||||
line = output.shift
|
||||
line.split(":").last.strip.split(",").each do |x|
|
||||
k,v = x.strip.split(" ")
|
||||
status_codes[k] = v
|
||||
end
|
||||
while line.end_with? ","
|
||||
line = output.shift
|
||||
line.strip.split(",").each do |x|
|
||||
k,v = x.strip.split(" ")
|
||||
status_codes[k] = v
|
||||
end
|
||||
end
|
||||
|
||||
# same like before but for the origin codes
|
||||
origin_codes = {}
|
||||
output.shift.split(":").last.strip.split(",").each do |x|
|
||||
k,v = x.strip.split(" - ")
|
||||
origin_codes[k] = v
|
||||
end
|
||||
|
||||
# drop empty line
|
||||
output.shift
|
||||
|
||||
# get begin of header (number of the first char of the string)
|
||||
header = output.shift
|
||||
header_offset = {}
|
||||
header_offset[0] = 'status'
|
||||
["Network", "Next Hop", "Metric", "LocPrf", "Weight", "Path"].map do |x|
|
||||
header_offset[header.index(x)] = x
|
||||
end
|
||||
|
||||
|
||||
# make ranges: this will make a range of the first char of the sting until
|
||||
# the the char befor the next heading begins
|
||||
ranges = []
|
||||
0.upto (header_offset.keys.length - 3) do |i|
|
||||
ranges << ((header_offset.keys[i])...(header_offset.keys[i + 1]))
|
||||
end
|
||||
# the last one has no next heading - this will go to the end of the line
|
||||
ranges.push ((header_offset.keys.last)..-1) # path
|
||||
|
||||
|
||||
# collect the real data
|
||||
bgp['output'] = []
|
||||
while line = output.shift
|
||||
tmp = {}
|
||||
# kill empty lines
|
||||
unless line && (line.include? 'Total' || line.strip.length > 10 || line.include?(' out of'))
|
||||
ranges.each do |r|
|
||||
# the string starts here
|
||||
b = r.begin
|
||||
# get the heading starting where the string starts
|
||||
n = header_offset[b]
|
||||
# get the data or return an empty string
|
||||
tmp[n] = line[r]&.strip || ""
|
||||
end
|
||||
# replace characters by the meaning
|
||||
tmp['status'] = tmp['status'].split("").map {|x| {dn: status_codes[x], abb: x} }
|
||||
tmp['Path'] = tmp['Path'].split("").map {|x| {dn: origin_codes[x], abb: x} }
|
||||
# add the line - except if it was empty
|
||||
bgp['output'] << tmp unless tmp['Network'] == ""
|
||||
end
|
||||
end
|
||||
# final return
|
||||
bgp
|
||||
end
|
||||
|
||||
puts show_ip_bgp.to_json
|
||||
@@ -355,6 +355,61 @@ class OSPF
|
||||
end
|
||||
end
|
||||
|
||||
class BGP
|
||||
def initialize(sh)
|
||||
@vtysh = sh
|
||||
end
|
||||
|
||||
def overview
|
||||
output = @vtysh.execute('show ip bgp')
|
||||
return {} if output.include? "No BGP process is configured"
|
||||
return {} unless output.include? 'version' # we get an empty output if quagga is not running
|
||||
output = output.split("\n")
|
||||
bgp = {}
|
||||
|
||||
x,y = output.shift.scan(/.*?version is (\d+).*?ID is ([0-9\.]+).*/).first
|
||||
bgp['table_version'] = x
|
||||
bgp['local_router_id'] = y
|
||||
|
||||
# find out, what the status abbreviations mean
|
||||
status_codes = {}
|
||||
line = output.shift
|
||||
line.split(":").last.strip.split(",").each do |x|
|
||||
k,v = x.strip.split(" ")
|
||||
status_codes[k] = v
|
||||
end
|
||||
while line.end_with? ","
|
||||
line = output.shift
|
||||
line.strip.split(",").each do |x|
|
||||
k,v = x.strip.split(" ")
|
||||
status_codes[k] = v
|
||||
end
|
||||
end
|
||||
|
||||
# same like before but for the origin codes
|
||||
origin_codes = {}
|
||||
output.shift.split(":").last.strip.split(",").each do |x|
|
||||
k,v = x.strip.split(" - ")
|
||||
origin_codes[k] = v
|
||||
end
|
||||
|
||||
# drop empty line
|
||||
output.shift
|
||||
# read entries
|
||||
bgp['output'] = []
|
||||
qta = QuaggaTableReader.new(["Network", "Next Hop", "Metric", "LocPrf", "Weight", "Path"])
|
||||
qta.read_headline(output.shift,true)
|
||||
while line = output.shift&.strip
|
||||
break if line == ''
|
||||
data = qta.read_entry(line)
|
||||
data['status'] = data['status'].split("").map {|x| {dn: status_codes[x], abb: x} }
|
||||
data['Path'] = data['Path'].split("").map {|x| {dn: origin_codes[x], abb: x} }
|
||||
bgp['output'] << data
|
||||
end
|
||||
bgp
|
||||
end
|
||||
end
|
||||
|
||||
require 'optparse'
|
||||
options = {}
|
||||
supported_sections = %w{general ospf}
|
||||
@@ -378,6 +433,9 @@ OptionParser.new do |opts|
|
||||
opts.on("-R", "--general-routes", "Print Routing Table") do |od|
|
||||
options[:general_routes] = od
|
||||
end
|
||||
opts.on("-B", "--bgp-overview", "Print an overview of BGP") do |od|
|
||||
options[:bgp_overview] = od
|
||||
end
|
||||
opts.on("-H", "--human-readable", "Print the output human readable (not json)") do |od|
|
||||
options[:human_readable] = od
|
||||
end
|
||||
@@ -389,6 +447,7 @@ end.parse!
|
||||
# use the lib
|
||||
sh = VTYSH.new
|
||||
ospf = OSPF.new sh
|
||||
bgp = BGP.new sh
|
||||
general = General.new sh
|
||||
|
||||
result = {}
|
||||
@@ -402,13 +461,17 @@ options.keys.each do |k|
|
||||
elsif k.to_s.include? 'general'
|
||||
cmd = k.to_s.split('_').last
|
||||
result[k] = general.send(cmd)
|
||||
elsif k.to_s.include? 'bgp'
|
||||
cmd = k.to_s.split('_').last
|
||||
result[k] = bgp.send(cmd)
|
||||
end
|
||||
rescue # do nothing on an error
|
||||
result[k] = "error"
|
||||
#puts $!
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
# ospf.database, general.routes, ospf.interface, ospf.neighbors, ospf.route, ospf.overview
|
||||
if options[:human_readable]
|
||||
pp result
|
||||
|
||||
Reference in New Issue
Block a user