import re
import time


def nt_gcstats(machinename, sshcli):
    """
    Gets nodetool gcstats metrics for machine
        :param machinename: monitored machine name to add to result dict
        :param sshcli: SSHClient to the monitored machine
        :return: Metrics as a dict
    """
    stdin, stdout, stderr = sshcli.exec_command("nodetool gcstats")

    # Skip header
    stdout.readline()
    stdout.readline()

    # garbage collector data
    data = re.split(r'[ ]+', stdout.readline().strip())

    return dict({
        "type":             "gcstats",
        "machine":          machinename,
        "ts":               time.time(),
        "interval":         data[0],
        "max-gc-elapsed":   data[1],
        "total-gc-elapsed": data[2],
        "stdev-gc-elapsed": data[3],
        "gc-reclaimed":     data[4],
        "collections":      data[5],
        "direct-memory":    data[6]
    })