|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import sys |
| 4 | +import pandas as pd |
| 5 | +import matplotlib.pyplot as plt |
| 6 | + |
| 7 | +KILO = 1024.0 |
| 8 | +MEGA = 1048576.0 |
| 9 | +GIGA = 1073741824.0 |
| 10 | + |
| 11 | +def byte_to_mega(num): |
| 12 | + ''' |
| 13 | + Convert byte to megabyte |
| 14 | + ''' |
| 15 | + return float("{0:.2f}".format(num/MEGA)) |
| 16 | + |
| 17 | +def minute_average(data): |
| 18 | + ''' |
| 19 | + Avarage data in 10 seconds |
| 20 | + ''' |
| 21 | + tmp = [] |
| 22 | + avg = 0 |
| 23 | + counter = 0 |
| 24 | + for itm in data: |
| 25 | + avg += itm |
| 26 | + counter += 1 |
| 27 | + if counter == 10: |
| 28 | + tmp.append(byte_to_mega(avg/10)) |
| 29 | + avg = 0 |
| 30 | + counter = 0 |
| 31 | + if counter != 0: |
| 32 | + tmp.append(byte_to_mega(avg/counter)) |
| 33 | + return tmp |
| 34 | + |
| 35 | +def minute_average_noconvert(data): |
| 36 | + ''' |
| 37 | + Avarage data in 1 minute |
| 38 | + ''' |
| 39 | + tmp = [] |
| 40 | + avg = 0 |
| 41 | + counter = 0 |
| 42 | + for itm in data: |
| 43 | + avg += itm |
| 44 | + counter += 1 |
| 45 | + if counter == 10: |
| 46 | + tmp.append(avg/10) |
| 47 | + avg = 0 |
| 48 | + counter = 0 |
| 49 | + if counter != 0: |
| 50 | + tmp.append(avg/counter) |
| 51 | + return tmp |
| 52 | + |
| 53 | +''' |
| 54 | +Matplotlib color code 'k' = black |
| 55 | +''' |
| 56 | + |
| 57 | +def plot_cpu(usr, sys, idl, wai, filename): |
| 58 | + plt.figure() |
| 59 | + plt.plot(usr, label='CPU Usage', linestyle = '-', color = 'k') |
| 60 | + plt.plot(sys, label='CPU System', linestyle = '--', color = 'k') |
| 61 | + plt.plot(idl, label='CPU Idle', linestyle = '-.', color = 'k') |
| 62 | + plt.plot(wai, label='CPU Wait', linestyle = ':', color = 'k') |
| 63 | + plt.xlabel('10 Second(s)') |
| 64 | + plt.ylabel('CPU usage(%)') |
| 65 | + plt.title('CPU usage') |
| 66 | + plt.legend() |
| 67 | + plt.savefig('/data/graph/'+filename+'cpu.pdf') |
| 68 | + |
| 69 | +def plot_memory(usd, buf, cach, free, filename): |
| 70 | + plt.figure() |
| 71 | + plt.plot(usd, label='Memory Usaged', linestyle = '-', color = 'k') |
| 72 | + plt.plot(buf, label='Memory Buffer', linestyle = '--', color = 'k') |
| 73 | + plt.plot(cach, label='Memory Cache', linestyle = '-.', color = 'k') |
| 74 | + plt.plot(free, label='Memory Free', linestyle = ':', color = 'k') |
| 75 | + plt.xlabel('10 Second(s)') |
| 76 | + plt.ylabel('Memory usage(MB)') |
| 77 | + plt.title('Memory usage') |
| 78 | + plt.legend() |
| 79 | + plt.savefig('/data/graph/'+filename+'memory.pdf') |
| 80 | + |
| 81 | +def plot_disk(read, writ, filename): |
| 82 | + plt.figure() |
| 83 | + plt.plot(read, label='Disk Read', linestyle = '-', color = 'k') |
| 84 | + plt.plot(writ, label='Disk Write', linestyle = '--', color = 'k') |
| 85 | + plt.xlabel('10 Second(s)') |
| 86 | + plt.ylabel('Storage speed(MB)') |
| 87 | + plt.title('Storage usage') |
| 88 | + plt.legend() |
| 89 | + plt.savefig('/data/graph/'+filename+'disk.pdf') |
| 90 | + |
| 91 | +def plot_network(send, recv, filename): |
| 92 | + plt.figure() |
| 93 | + plt.plot(send, label='Network Send', linestyle = '-', color = 'k') |
| 94 | + plt.plot(recv, label='Network Receive', linestyle = '--', color = 'k') |
| 95 | + plt.xlabel('10 Second(s)') |
| 96 | + plt.ylabel('Network speed(MB)') |
| 97 | + plt.title('Network usage') |
| 98 | + plt.legend() |
| 99 | + plt.savefig('/data/graph/'+filename+'net.pdf') |
| 100 | + |
| 101 | +def plot_swap(usd, fre, filename): |
| 102 | + plt.figure() |
| 103 | + plt.plot(usd, label='Used', linestyle = '-', color = 'k') |
| 104 | + plt.plot(fre, label='Free', linestyle = '--', color = 'k') |
| 105 | + plt.xlabel('10 Second(s)') |
| 106 | + plt.ylabel('Size(MB)') |
| 107 | + plt.title('Swap memory usage') |
| 108 | + plt.legend() |
| 109 | + plt.savefig('/data/graph/'+filename+'swap.pdf') |
| 110 | + |
| 111 | +def plot_paging(i, o, filename): |
| 112 | + plt.figure() |
| 113 | + plt.plot(i, label='In', linestyle = '-', color = 'k') |
| 114 | + plt.plot(o, label='Out', linestyle = '--', color = 'k') |
| 115 | + plt.xlabel('10 Second(s)') |
| 116 | + plt.ylabel('Speed(MB)') |
| 117 | + plt.title('Paging usage') |
| 118 | + plt.legend() |
| 119 | + plt.savefig('/data/graph/'+filename+'page.pdf') |
| 120 | + |
| 121 | +def main(): |
| 122 | + ''' |
| 123 | + Main function |
| 124 | + Argument [1]: File path |
| 125 | + ''' |
| 126 | + if sys.argv[1] is None: |
| 127 | + print('No arguments') |
| 128 | + return 0 |
| 129 | + file = str(sys.argv[1]) |
| 130 | + filename = file.split('.')[0] |
| 131 | + data = pd.read_csv('/data/prepData/'+file, header=[0, 1]) |
| 132 | + plot_cpu(minute_average_noconvert(data[('total cpu usage','usr')]), minute_average_noconvert(data[('total cpu usage','sys')]), minute_average_noconvert(data[('total cpu usage','idl')]), minute_average_noconvert(data[('total cpu usage','wai')]), filename) |
| 133 | + plot_memory(minute_average(data[('memory usage','used')]), minute_average(data[('memory usage','buff')]), minute_average(data[('memory usage','cach')]), minute_average(data[('memory usage','free')]), filename) |
| 134 | + plot_disk(minute_average(data[('dsk/total','read')]), minute_average(data[('dsk/total','writ')]), filename) |
| 135 | + plot_network(minute_average(data[('net/total','recv')]), minute_average(data[('net/total','send')]), filename) |
| 136 | + plot_swap(minute_average(data[('swap','used')]), minute_average(data[('swap','free')]), filename) |
| 137 | + plot_paging(minute_average(data[('paging','in')]), minute_average(data[('paging','out')]), filename) |
| 138 | + #d1 = [byte_to_mega(x) for x in data[('dsk/total', 'read')]] |
| 139 | + #d1 = minute_average(data[('dsk/total','read')]) |
| 140 | + #d2 = minute_average(data[('dsk/total','writ')]) |
| 141 | + #plt.plot(d1, label='Disk Read') |
| 142 | + #plt.plot(d2, label='Disk Write') |
| 143 | + #plt.savefig('/data/graph/'+filename+'.pdf') |
| 144 | + |
| 145 | +if __name__ == '__main__': |
| 146 | + main() |
0 commit comments