thatmattlove / hyperglass

hyperglass is the network looking glass that tries to make the internet better.
https://hyperglass.dev
BSD 3-Clause Clear License
617 stars 92 forks source link

Error in bgp_router query on Huawei #187

Open arraisfilho opened 2 years ago

arraisfilho commented 2 years ago

In Huawei it is necessary to have a space after the slash "1.1.1.0 24", by the logs, this treatment is not happening.

[DEBUG] 20220128 01:01:14 | hyperglass.execution.drivers._construct:27 | __init__ → Constructing bgp_route query for '1.1.1.0/24'
[DEBUG] 20220128 01:01:14 | hyperglass.execution.drivers._construct:111 | queries → Constructed query: ['display bgp routing-table 1.1.1.0/24']
[DEBUG] 20220128 01:01:14 | hyperglass.execution.drivers.ssh_netmiko:57 | collect → Connecting directly to São Paulo, SP, Brasil
[DEBUG] 20220128 01:01:18 | hyperglass.execution.drivers.ssh_netmiko:103 | collect → Raw response for command "display bgp routing-table 1.1.1.0/24":
                                        ^
Error: Wrong parameter found at '^' position.
[DEBUG] 20220128 01:01:18 | hyperglass.execution.drivers._common:34 | parsed_response → Pre-parsed responses:
("                                        ^\nError: Wrong parameter found at '^' position.",)
[DEBUG] 20220128 01:01:18 | hyperglass.execution.drivers._common:80 | parsed_response → Post-parsed responses:
^
Error: Wrong parameter found at '^' position.

Up to version 1.0.0, this treatment is performed, according to the logs below.

[DEBUG] 20220128 00:44:52 | hyperglass.execution.drivers._construct:36 | __init__ → Constructing bgp_route query for '1.1.1.0/24'
[DEBUG] 20220128 00:44:52 | hyperglass.execution.drivers._construct:160 | queries → Constructed query: ['display bgp routing-table 1.1.1.0 24']
[DEBUG] 20220128 00:44:52 | hyperglass.execution.drivers.ssh_netmiko:57 | collect → Connecting directly to São Paulo, SP, Brasil

Best Regards

dd1245 commented 2 years ago

I have this same issue. Ping commands and traceroute work fine, but the command issued for the routing table is incorrect as already mentioned.

As per the example, display bgp routing-table 1.1.1.0/24 needs to be display bgp routing-table 1.1.1.0 24

What is the best way to address this?

jakeb91 commented 2 years ago

I suspect this is being caused by the addition of the Formatter class in execution/drivers/_construct.py - this is a new addition in 1.0.4 that wasn't present in 1.0.0

Specifically - you can see that the formatting override for huawei and huawei_vrpv8 are ignored because the Formatter class references the original query target, rather than the corrected query target.

Line 39-40 in execution/drivers/_construct.py - where the query target is corrected.

        if self.device.nos in TARGET_FORMAT_SPACE:
            self.target = re.sub(r"\/", r" ", str(self.query_data.query_target))

Line 67-68 in execution/drivers/_construct.py - where the corrected query target is ignored and the original data is referenced and replaces the corrected query target with the uncorrected version.

        with Formatter(self.device.nos, self.query_data.query_type) as formatter:
            self.target = formatter(self.query_data.query_target)

This particular if statement will probably need to be moved into the Formatter class to fix this properly.

jakeb91 commented 2 years ago

Further to the above - I don't have environment to test this in (so I don't want to create a pull request), but adding the following code to _construct.py may work - or at-least this is a starting point.

Delete or comment out the following lines Line 39-40:

if self.device.nos in TARGET_FORMAT_SPACE:
   self.target = re.sub(r"\/", r" ", str(self.query_data.query_target))

Add the following to def _get_formatter(self) in the Formatter class:

if self.nos in TARGET_FORMAT_SPACE:
   if self.query_type == "bgp_route":
      return self._huawei_bgp_route

Add the following to the Formatter class

def _huawei_bgp_route(self, target: str) -> str:
    """ Remove slashes from target for required platforms."""
    self.target = re.sub(r"\/", r" ", str(self.query_data.query_target))
    log.debug("Modified target '{}' to '{}'", self.query_data.query_target, self.target)
    return self.target

If someone running Huawei gear is confident enough to try this in their installation and see if it resolves the issue, that would be good :) .

Pre-made file below: _construct.py.txt

arraisfilho commented 2 years ago

Hi @jakeb91, did not work, the error below is the result when starting the Hyperglass service.

Traceback (most recent call last): File "/usr/local/bin/hyperglass", line 11, in sys.exit(CLI()) File "/usr/local/lib/python3.6/dist-packages/click/core.py", line 829, in call return self.main(args, kwargs) File "/usr/local/lib/python3.6/dist-packages/click/core.py", line 782, in main rv = self.invoke(ctx) File "/usr/local/lib/python3.6/dist-packages/click/core.py", line 1259, in invoke return _process_result(sub_ctx.command.invoke(sub_ctx)) File "/usr/local/lib/python3.6/dist-packages/click/core.py", line 1066, in invoke return ctx.invoke(self.callback, ctx.params) File "/usr/local/lib/python3.6/dist-packages/click/core.py", line 610, in invoke return callback(args, **kwargs) File "/usr/local/lib/python3.6/dist-packages/hyperglass/cli/commands.py", line 98, in start from hyperglass.api import start as uvicorn_start File "/usr/local/lib/python3.6/dist-packages/hyperglass/api/init.py", line 22, in from hyperglass.api.routes import ( File "/usr/local/lib/python3.6/dist-packages/hyperglass/api/routes.py", line 24, in from hyperglass.execution.main import execute File "/usr/local/lib/python3.6/dist-packages/hyperglass/execution/main.py", line 20, in from .drivers import Connection, AgentConnection, NetmikoConnection, ScrapliConnection File "/usr/local/lib/python3.6/dist-packages/hyperglass/execution/drivers/init.py", line 4, in from .agent import AgentConnection File "/usr/local/lib/python3.6/dist-packages/hyperglass/execution/drivers/agent.py", line 24, in from ._common import Connection File "/usr/local/lib/python3.6/dist-packages/hyperglass/execution/drivers/_common.py", line 14, in from ._construct import Construct File "/usr/local/lib/python3.6/dist-packages/hyperglass/execution/drivers/_construct.py", line 129 if self.nos in TARGET_FORMAT_SPACE: ^ TabError: inconsistent use of tabs and spaces in indentation

Regards

rafamira2007 commented 1 year ago

For me it worked just editing the code below:

with Formatter(self.device.nos, self.query_data.query_type) as formatter: self.target = re.sub(r"\/", r" ", str(self.query_data.query_target))

arraisfilho commented 1 year ago

For me it worked just editing the code below:

with Formatter(self.device.nos, self.query_data.query_type) as formatter: self.target = re.sub(r"\/", r" ", str(self.query_data.query_target))

What version are you on? 1.0.4?

I will alter it in /hyperglass/execution/drivers/__construct.py

The error below is returned. Has anything been tweaked or just what was mentioned?

Traceback (most recent call last): File "/usr/local/bin/hyperglass", line 11, in sys.exit(CLI()) File "/usr/local/lib/python3.6/dist-packages/click/core.py", line 829, in call return self.main(args, kwargs) File "/usr/local/lib/python3.6/dist-packages/click/core.py", line 782, in main rv = self.invoke(ctx) File "/usr/local/lib/python3.6/dist-packages/click/core.py", line 1259, in invoke return _process_result(sub_ctx.command.invoke(sub_ctx)) File "/usr/local/lib/python3.6/dist-packages/click/core.py", line 1066, in invoke return ctx.invoke(self.callback, ctx.params) File "/usr/local/lib/python3.6/dist-packages/click/core.py", line 610, in invoke return callback(args, **kwargs) File "/usr/local/lib/python3.6/dist-packages/hyperglass/cli/commands.py", line 98, in start from hyperglass.api import start as uvicorn_start File "/usr/local/lib/python3.6/dist-packages/hyperglass/api/init.py", line 22, in from hyperglass.api.routes import ( File "/usr/local/lib/python3.6/dist-packages/hyperglass/api/routes.py", line 24, in from hyperglass.execution.main import execute File "/usr/local/lib/python3.6/dist-packages/hyperglass/execution/main.py", line 20, in from .drivers import Connection, AgentConnection, NetmikoConnection, ScrapliConnection File "/usr/local/lib/python3.6/dist-packages/hyperglass/execution/drivers/init.py", line 4, in from .agent import AgentConnection File "/usr/local/lib/python3.6/dist-packages/hyperglass/execution/drivers/agent.py", line 24, in from ._common import Connection File "/usr/local/lib/python3.6/dist-packages/hyperglass/execution/drivers/_common.py", line 14, in from ._construct import Construct File "/usr/local/lib/python3.6/dist-packages/hyperglass/execution/drivers/_construct.py", line 66 with Formatter(self.device.nos, self.query_data.query_type) as formatter: ^ IndentationError: unindent does not match any outer indentation level

rafamira2007 commented 1 year ago

Edit on /hyperglass/execution/drivers/_construct.py and not /hyperglass/execution/drivers/__construct.py

only just make these changes

with Formatter(self.device.nos, self.query_data.query_type) as formatter: self.target = re.sub(r"\/", r" ", str(self.query_data.query_target))

Distro: Linux Debian 11 64Bits NetInstall HyperGlass: Version 1.0.4

allanfrizzo commented 1 year ago

That way I was successful

Delete or comment out the following lines Line 39-40:

if self.device.nos in TARGET_FORMAT_SPACE:
   self.target = re.sub(r"\/", r" ", str(self.query_data.query_target))

Add the following to def _get_formatter(self) in the Formatter class:

    if self.nos in TARGET_FORMAT_SPACE:
        if self.query_type == "bgp_route":
            return self._huawei_bgp_route

Add the following to the Formatter class

def _huawei_bgp_route(self, target: str) -> str:
    """ Remove slashes from target for required platforms."""
    self.target = re.sub(r"\/", r" ", str(target))
    log.debug("Modified target '{}' to '{}'", target, self.target)
    return self.target
remontti commented 1 year ago

Above solutions did not work for me in real environment, the solution I found was:

         with Formatter(self.device.nos, self.query_data.query_type) as formatter:
            log.debug("==*HW*== '{}' to '{}'", self.device.nos, self.query_data.query_type)
            if self.device.nos in TARGET_FORMAT_SPACE:
                if self.query_data.query_type == "bgp_route":
                    self.target = re.sub(r"/", r" ", str(self.query_data.query_target))
                else:
                    self.target = formatter(self.query_data.query_target)
            else:
                self.target = formatter(self.query_data.query_target)
jakeb91 commented 1 year ago

Above solutions did not work for me in real environment, the solution I found was:

         with Formatter(self.device.nos, self.query_data.query_type) as formatter:
            log.debug("==*HW*== '{}' to '{}'", self.device.nos, self.query_data.query_type)
            if self.device.nos in TARGET_FORMAT_SPACE:
                if self.query_data.query_type == "bgp_route":
                    self.target = re.sub(r"/", r" ", str(self.query_data.query_target))
                else:
                    self.target = formatter(self.query_data.query_target)
            else:
                self.target = formatter(self.query_data.query_target)

The related pull request has the working / fixed code - I don't have access to Huawei gear anymore though so I was unable to test it.

emersonfelipesp commented 1 year ago

I have the same issue here using huawei_vrpv8, the code just needs handle to remove the / from network prefix when the device is Huawei.

thatmattlove commented 3 months ago

Can anyone re-test this in v2 and list any changes that may be needed?

gondimcodes commented 3 days ago

Same problem with 2.0.4