Skip to content

Instantly share code, notes, and snippets.

@ahendrix
Last active December 25, 2015 20:29
Show Gist options
  • Save ahendrix/7034822 to your computer and use it in GitHub Desktop.
Save ahendrix/7034822 to your computer and use it in GitHub Desktop.
reformat vmstat
#!/usr/bin/env python
import sys
import subprocess
def main():
if len(sys.argv) > 1:
time = sys.argv[1]
else:
time = "1"
vmstat = subprocess.Popen(['/usr/bin/vmstat', time],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
line = vmstat.stdout.readline()
max_widths = [2, 2, 6, 6, 6, 6, 4, 4, 5, 5, 4, 4, 2, 2, 2, 2]
headers = ["procs", "memory", "swap", "io", "system", "cpu"]
header_columns = [ [0, 1], [2, 3, 4, 5], [6 , 7], [8, 9], [10, 11], [12, 13, 14, 15] ]
while line:
line = line.split()
if line[0] == "procs":
header_width = [sum(max_widths[i] for i in cols) + len(cols) - 1 for cols in header_columns]
fstr = " ".join(["{:-^%ds}"%w for w in header_width])
print fstr.format(*headers)
else:
max_widths = [ max(new, old) for new,old in zip(map(len, line), max_widths) ]
fstr = " ".join(["{: >%ds}"%w for w in max_widths])
print fstr.format(*line)
line = vmstat.stdout.readline()
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment