Example #1
0
File: bgp.py Project: qoke/exabgp
import syslog
import string

from exabgp.logger import Logger

from exabgp.version import version
# import before the fork to improve copy on write memory savings
from exabgp.reactor.loop import Reactor

from exabgp.vendoring import docopt
from exabgp.vendoring import lsprofcalltree

from exabgp.configuration.usage import usage

from exabgp.debug import setup_report
setup_report()


def is_bgp(s):
    return all(c in string.hexdigits or c == ':' for c in s)


def __exit(memory, code):
    if memory:
        from exabgp.vendoring import objgraph
        print("memory utilisation")
        print()
        print(objgraph.show_most_common_types(limit=20))
        print()
        print()
        print("generating memory utilisation graph")
Example #2
0
import syslog
import string

from exabgp.logger import Logger

from exabgp.version import version
# import before the fork to improve copy on write memory savings
from exabgp.reactor.loop import Reactor

from exabgp.dep import docopt
from exabgp.dep import lsprofcalltree

from exabgp.configuration.usage import usage

from exabgp.debug import setup_report
setup_report()


def is_bgp (s):
	return all(c in string.hexdigits or c == ':' for c in s)


def __exit (memory, code):
	if memory:
		from exabgp.dep import objgraph
		print "memory utilisation"
		print
		print objgraph.show_most_common_types(limit=20)
		print
		print
		print "generating memory utilisation graph"
Example #3
0
def exabgp(localip, routerid, asn, peerip, peerport, peeras):
    from exabgp.reactor.api.command.command import Command
    from exabgp.reactor.api.command.limit import match_neighbors
    from exabgp.reactor.api.command.limit import extract_neighbors
    from exabgp.reactor.loop import Reactor

    # Extend ExaBGP to handle a "send-raw-data [[neighbor IP] ...] filename" command to cram raw
    # data down the peers throat as fast as possible (limited by kernel syscall).
    @Command.register('text', 'send-raw-data')
    def send_raw_data(self, reactor, service, line):
        def callback():
            try:
                descriptions, command = extract_neighbors(line)
                peers = match_neighbors(reactor.peers, descriptions)
                if not peers:
                    self.log_failure(
                        'no neighbor matching the command : %s' % command)
                    reactor.processes.answer(service, 'error')
                    yield True
                    return

                with open(command, "rb") as datafile:
                    rawdata = datafile.read()

                assert rawdata
                for peer in peers:
                    log.info("send-raw-data: %d bytes to %s", len(rawdata),
                             str(peer))
                    peer.proto.connection.writer(rawdata)

                reactor.processes.answer_done(service)
            except Exception as ex:
                self.log_failure('issue with send-raw-data: ' + str(ex))
                reactor.processes.answer(service, 'error')
                yield True

        reactor.asynchronous.schedule(service, line, callback())

    # from exabgp.pplication.bgp import main
    from exabgp.application.bgp import __exit
    from exabgp.debug import setup_report
    setup_report()
    from exabgp.configuration.setup import environment
    env = environment.setup("/nofile")

    with tempfile.NamedTemporaryFile(mode='w') as scriptfile:
        scriptfile.write("""#!/bin/bash
while read nchange; do
    echo $nchange >> /tmp/bgp-script-out.txt
    if [[ $(echo $nchange | sed '/neighbor {} up/!d') ]]; then
        echo send-raw-data foobar.raw
    fi
done
""".format(peerip))
        scriptfile.flush()
        os.system("chmod 755 " + scriptfile.name)

        with tempfile.NamedTemporaryFile(mode='w') as conffile:
            conffile.write("""
    process senddata {{
        run bash {};
        encoder text;
    }}

    neighbor {} {{                 # Remote neighbor to peer with
        router-id {};              # Our local router-id
        local-address {};          # Our local update-source
        local-as {};               # Our local AS
        peer-as {};                # Peer's AS

        api {{
            processes [senddata];
            neighbor-changes;
        }}
    }}
        """.format(scriptfile.name, peerip, routerid, localip, asn, peeras))
            conffile.flush()
            root = os.getcwd()
            exit_code = Reactor([conffile.name]).run(False, root)
            __exit(env.debug.memory, exit_code)