import re
import json


def get_interfaces_counters(module):
    """
    @summary: Parse output of "show interfaces counters" command and convert it to the dictionary.
    @param module: The AnsibleModule object
    @return: Return dictionary of parsed counters
    """
    cli_cmd = "portstat -j"
    rc, stdout, stderr = module.run_command(cli_cmd)
    if rc != 0:
        module.fail_json(msg="Failed to run {}, rc={}, stdout={}, stderr={}".format(cli_cmd, rc, stdout, stderr))

    match = re.search("Last cached time was.*\n", stdout)
    if match:
        stdout = re.sub("Last cached time was.*\n", "", stdout)

    try:
        return json.loads(stdout)
    except Exception as e:
        module.fail_json(msg="Failed to parse output of '{}', err={}".format(cli_cmd, str(e)))