Last active
December 25, 2015 20:29
-
-
Save ahendrix/7034822 to your computer and use it in GitHub Desktop.
reformat vmstat
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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