diff options
author | Viacheslav Hletenko <v.gletenko@vyos.io> | 2022-07-28 12:37:15 +0000 |
---|---|---|
committer | Viacheslav Hletenko <v.gletenko@vyos.io> | 2022-07-28 12:37:15 +0000 |
commit | f38729bd8d448faa578ccd5ab24b024c994522e0 (patch) | |
tree | b39790f031774749bdd28a423e44f39e5af3cb0a | |
parent | b33de0bac8172f3571ffc6d46b87de0c906e1b48 (diff) | |
download | vyos-1x-f38729bd8d448faa578ccd5ab24b024c994522e0.tar.gz vyos-1x-f38729bd8d448faa578ccd5ab24b024c994522e0.zip |
nat: T4543: Fix and rewrite show nat source statistics
Rewrite "show nat source statistics"
Use new format 'vyos.opmode module'
Ability to get raw and formatted output
-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__]) |