import re
import time


def vmstat(machinename, sshcli):
    """
    Gets vmstat 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("vmstat")

    # Discard headers
    stdout.readline()
    stdout.readline()

    # Get data
    data = re.split(r'[ ]+', stdout.readline().strip())
    return dict({
       "type":      "vmstat",
       "machine":   machinename,
       "ts":        time.time(),
       "prs-r":     data[0],
       "prs-b":     data[1],
       "mem-swpd":  data[2],
       "mem-free":  data[3],
       "mem-buff":  data[4],
       "mem-cache": data[5],
       "swp-si":    data[6],
       "swp-so":    data[7],
       "io-bi":     data[8],
       "io-bo":     data[9],
       "sys-in":    data[10],
       "sys-cs":    data[11],
       "cpu-us":    data[12],
       "cpu-sy":    data[13],
       "cpu-id":    data[14],
       "cpu-wa":    data[15],
       "cpu-st":    data[16]
    })