-
Notifications
You must be signed in to change notification settings - Fork 0
/
reformat_stats.pl.bak
executable file
·164 lines (134 loc) · 3.34 KB
/
reformat_stats.pl.bak
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/perl
use strict;
use warnings;
#my @infiles = qw(
#tx-app01.csv
#tx-intprod02.csv
#tx-websvc01.csv
#);
#my $infile = $ARGV[0] or die "No file specified\n";
my @infiles = qw(
tx-app01.csv
tx-app02.csv
tx-app03.csv
tx-app05.csv
tx-app06.csv
tx-app07.csv
tx-app08.csv
tx-app09.csv
tx-app10.csv
tx-app13.csv
tx-app14.csv
tx-app21.csv
tx-app22.csv
tx-intprod01.csv
tx-intprod02.csv
tx-intprod03.csv
tx-intprod04.csv
tx-ucm01.csv
tx-ucm02.csv
tx-ucm03.csv
tx-ucm04.csv
tx-ucm05.csv
tx-ucm06.csv
tx-websvc01.csv
tx-websvc02.csv
tx-websvc03.csv
tx-websvc04.csv
tx-websvc05.csv
);
# these hashes are needed in order to ensure columns are in the right order
my %mem; # hash to hold memory values for all apps for a given time
my %cpu; # hash to hold cpu values for all apps for a given time
my %memheader; # hash to know when to print header on app reports
for my $infile (@infiles) {
my $lasttime = ''; # keep track of the last time slice seen so we know to wrap it up when we see a new time
my $firstline = ''; # put header on first row of allmem and allcpu reports
open F, $infile or die "Couldn't open $infile for read: $!\n";
$infile =~ /(.+)\.csv/;
my $host = $1;
my $all_mem_report = "$host-mem.csv";
my $all_cpu_report = "$host-cpu.csv";
open ALLMEM, ">> $all_mem_report" or die "Couldn't open $all_mem_report for write: $!\n";
open ALLCPU, ">> $all_cpu_report" or die "Couldn't open $all_cpu_report for write: $!\n";
for my $line (<F>) {
chomp $line;
my ($time, undef, $app, $mem, $cpu) = split /,/, $line;
if ( ( $lasttime eq '' ) || ( $lasttime eq $time ) ) {
# if first line of the file or
# another entry with the same timestamp as last time
my @args = ( $app, $mem, $cpu, $host, $time );
&printme( \@args );
$lasttime = $time;
}
else {
# first time we've seen time point
# add header row if this is the first line
unless ($firstline) {
my $header = 'Time';
for my $app (sort keys %mem) {
$header .= ",$app";
}
print ALLMEM "$header\n";
print ALLCPU "$header\n";
}
$firstline = 1;
my $memline = $lasttime;
for my $app (sort keys %mem) {
$memline .= ",$mem{$app}";
}
my $cpuline = $lasttime;
for my $app (sort keys %cpu) {
$cpuline .= ",$cpu{$app}";
}
print ALLMEM "$memline\n";
print ALLCPU "$cpuline\n";
# clear out variables
$lasttime = $time;
$memline = '';
$cpuline = '';
&cleanup();
my @args = ( $app, $mem, $cpu, $host, $time );
&printme( \@args );
}
}
close ALLMEM;
close ALLCPU;
# clear this header flag hash
for ( keys %memheader ) {
delete $memheader{$_};
}
&cleanup();
}
sub printme {
my $ref = shift;
my $app = ${$ref}[0];
my $mem = ${$ref}[1];
my $cpu = ${$ref}[2];
my $host = ${$ref}[3];
my $time = ${$ref}[4];
$app =~ s/\//-/;
$app = 'noname' unless $app;
# if this is the first time this app has been seen,
# write header
unless ( $memheader{$app} ) {
open F, "> $host-$app.csv" or die "Couldn't open $host-$app.csv for write: $!\n";
print F "Time,Memory,CPU\n";
close F;
}
open F, ">> $host-$app.csv" or die "Couldn't open $host-$app.csv for write: $!\n";
print F "$time,$mem,$cpu\n";
close F;
$mem{$app} = $mem;
$cpu{$app} = $cpu;
$memheader{$app} = 1;
}
sub cleanup {
# clear these two globalhashes
for ( keys %mem ) {
delete $mem{$_};
}
for ( keys %cpu ) {
delete $cpu{$_};
}
}