diff options
| author | Christian Poessinger <christian@poessinger.com> | 2022-07-29 18:14:26 +0200 | 
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-07-29 18:14:26 +0200 | 
| commit | bcbfc8cc0ad42bf7bb025b7a6dae6e376978fda4 (patch) | |
| tree | 30aad369714bc1ed09961a50e7e8831898adbb26 | |
| parent | 0d7ad932d2842edb8862c9f88454fa2e668871b6 (diff) | |
| parent | f38729bd8d448faa578ccd5ab24b024c994522e0 (diff) | |
| download | vyos-1x-bcbfc8cc0ad42bf7bb025b7a6dae6e376978fda4.tar.gz vyos-1x-bcbfc8cc0ad42bf7bb025b7a6dae6e376978fda4.zip | |
Merge pull request #1441 from sever-sever/T4543
nat: T4543: Fix and rewrite show nat source statistics
| -rw-r--r-- | op-mode-definitions/nat.xml.in | 2 | ||||
| -rwxr-xr-x | src/op_mode/nat.py | 26 | 
2 files changed, 27 insertions, 1 deletions
| diff --git a/op-mode-definitions/nat.xml.in b/op-mode-definitions/nat.xml.in index 84e999995..b0ec8989f 100644 --- a/op-mode-definitions/nat.xml.in +++ b/op-mode-definitions/nat.xml.in @@ -22,7 +22,7 @@                  <properties>                    <help>Show statistics for configured source NAT rules</help>                  </properties> -                <command>${vyos_op_scripts_dir}/show_nat_statistics.py --source</command> +                <command>${vyos_op_scripts_dir}/nat.py show_statistics --direction source</command>                </node>                <node name="translations">                  <properties> diff --git a/src/op_mode/nat.py b/src/op_mode/nat.py index 666c72c7c..4b54ecf31 100755 --- a/src/op_mode/nat.py +++ b/src/op_mode/nat.py @@ -147,6 +147,24 @@ port {port}'''      return output +def _get_formatted_output_statistics(data, direction): +    data_entries = [] +    for rule in data: +        if 'comment' in rule['rule']: +            comment = rule.get('rule').get('comment') +            rule_number = comment.split('-')[-1] +            rule_number = rule_number.split(' ')[0] +        if 'expr' in rule['rule']: +            interface = rule.get('rule').get('expr')[0].get('match').get('right') \ +                if jmespath.search('rule.expr[*].match.left.meta', rule) else 'any' +            packets = jmespath.search('rule.expr[*].counter.packets | [0]', rule) +            _bytes = jmespath.search('rule.expr[*].counter.bytes | [0]', rule) +        data_entries.append([rule_number, packets, _bytes, interface]) +    headers = ["Rule", "Packets", "Bytes", "Interface"] +    output = tabulate(data_entries, headers, numalign="left") +    return output + +  def show_rules(raw: bool, direction: str):      nat_rules = _get_raw_data_rules(direction)      if raw: @@ -155,6 +173,14 @@ def show_rules(raw: bool, direction: str):          return _get_formatted_output_rules(nat_rules, direction) +def show_statistics(raw: bool, direction: str): +    nat_statistics = _get_raw_data_rules(direction) +    if raw: +        return nat_statistics +    else: +        return _get_formatted_output_statistics(nat_statistics, direction) + +  if __name__ == '__main__':      try:          res = vyos.opmode.run(sys.modules[__name__]) | 
