-
Notifications
You must be signed in to change notification settings - Fork 0
/
sar.sh
executable file
·248 lines (220 loc) · 5.59 KB
/
sar.sh
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#!/bin/bash
# Use getopts so we can pass along a -f to the underlying sar calls.
SARFILE=""
DEBUG=0
while getopts ":f:" opt; do case $opt in
f)
SARFILE="-f $OPTARG"
;;
esac; done
# Memory usage statistics
# Nothing special here.
#
# Networking usage statistics
# Note this breaks the numbers into upper/middle/lower thirds,
# so it is able to separate out the daily ebb and flow many
# sites encounter and also partition off the 'transition'
# periods so they don't pollute the statistics.
LC_ALL="POSIX" sar -r -n DEV -u $SARFILE | egrep "^[A0-9][v0-9][e:][r0-9][a0-9][g:][e0-9][0-9:]" | grep -v "RESTART" | awk '
function is_tier_item(item) {
}
function values(item, value) {
if (item ~ /^_[lmu]_/) {
return;
}
if (value != "") {
if (item in values_storage) {
values_storage[item] = (values_storage[item] SUBSEP value);
} else {
values_storage[item] = value;
}
} else if (item != "") {
return values_storage[item];
} else {
return;
}
}
function mean(item) {
if (stddev_size[item] > 0) {
return (stddev_base[item]/stddev_size[item]);
} else {
return log(0);
}
}
# Calculates the standard-deviation values.
function stddev(item, value) {
if (value != "") {
stddev_base[item] += value;
stddev_sqrt[item] += (value ** 2);
stddev_size[item] += 1;
values(item, value);
} else {
if (stddev_size[item] > 0) {
if ((stddev_sqrt[item]/stddev_size[item]) > (mean(item) ** 2)) {
return sqrt((stddev_sqrt[item]/stddev_size[item]) - (mean(item) ** 2));
} else {
return log(0);
}
} else {
return log(0);
}
}
}
# This divides an item in values_storage into thirds,
# recalculating stddev for the upper, middle, and the
# lower third tiers using prefixes of _u_, _m_, & _l_
# which are also checked to avoid runaway memory use.
#
# The algo. is trivial and brute-force:
# 1) Scan all values once to find min/max.
# 2) Calculate breakpoints at the thirds.
# 3) Scan again to bin values appropriately.
function values_tiers_calculate(item) {
if (item ~ /^_[lmu]_/) {
return;
}
split(values(item), v, SUBSEP);
min[item] = -log(0);
max[item] = 0;
for (j in v) {
k = v[j];
if (k < min[item]) {
min[item] = k;
}
if (k > max[item]) {
max[item] = k;
}
}
upper = (max[item] + max[item] + min[item]) / 3;
lower = (max[item] + min[item] + min[item]) / 3;
for (j in v) {
k = v[j];
if (k < lower) {
stddev(("_l_" item), k);
} else if (k <=upper) {
stddev(("_m_" item), k);
} else {
stddev(("_u_" item), k);
}
}
}
function output_range(item, mean, range) {
mean = mean(item);
range = stddev(item);
if ((mean > log(0)) && (mean < -log(0))) {
printf "%8.2f ", mean;
} else {
printf " 0.00 ";
}
if ((range > log(0)) && (range < -log(0))) {
printf "%8.2f ", range;
} else {
printf " 0.00 ";
}
}
function output_network_tier(class, title, device) {
if ((stddev_size[(class "rx_" device)] > 0) ||
(stddev_size[(class "tx_" device)] > 0)) {
printf "%6s %5s ", device, title;
output_range((class "rx_" device));
output_range((class "tx_" device));
printf "\n";
}
}
function output_memory_tier(class, title, item) {
if (stddev_size[(class item)] > 0) {
printf "%20s ", title;
output_range((class item));
printf "\n";
}
}
function output_memory(title, item) {
output_memory_tier("", title, item);
output_memory_tier("_u_", ("Lower " title), item);
output_memory_tier("_m_", ("Midd. " title), item);
output_memory_tier("_l_", ("Upper " title), item);
}
function output_cpu_tier(class, title, item) {
if (stddev_size[(class item)] > 0) {
printf "%20s ", title;
output_range((class item));
printf "\n";
}
}
function output_cpu(title, item) {
output_cpu_tier("", title, item);
output_cpu_tier("_l_", ("Lower " title), item);
output_cpu_tier("_m_", ("Midd. " title), item);
output_cpu_tier("_u_", ("Upper " title), item);
}
function output_title(name) {
print substr(("[ " name " ]========================================================================"), 1, 76);
}
BEGIN {
mode = "";
}
(mode == "memory") {
if ($1 == "Average:") {
mode = "";
} else {
stddev("mem_u", ($3 - ($5 + $6)) / 1048576);
stddev("mem_b", $5 / 1048576);
stddev("mem_c", $6 / 1048576);
}
}
(mode == "network") {
if ($1 == "Average:") {
mode = "";
} else {
stddev(("rx_" $2), $5);
stddev(("tx_" $2), $6);
ifaces[$2] = 1;
}
}
(mode == "cpu") {
if ($1 == "Average:") {
mode = "";
} else {
stddev("cpu_io", $6);
stddev("cpu_steal", $7);
stddev("cpu_used", 100.0 - $8);
}
}
(mode == "") {
if ($2 == "kbmemfree") {
mode = "memory";
} else if ($2 == "IFACE") {
mode = "network";
} else if ($2 == "CPU") {
mode = "cpu";
}
}
END {
for (i in values_storage) {
values_tiers_calculate(i);
}
output_title("CPU Stats");
output_cpu("Used", "cpu_used");
output_cpu("I/O Wait", "cpu_io");
output_cpu("Stolen", "cpu_steal");
output_title("Memory Stats");
printf " Average Plus/Minus (in Gigabytes)\n";
output_memory("Used", "mem_u");
output_memory("Buffer", "mem_b");
output_memory("Cache", "mem_c");
output_title("Network Stats");
printf " Rx Tx\n";
printf " Device Average Plus/Minus Average Plus/Minus Peak Tx\n";
for (i in ifaces) {
if ((stddev_base[("rx_" i)] >= (NR / 100)) ||
(stddev_base[("tx_" i)] >= (NR / 100))) {
printf "%6s Total ", i;
output_range(("rx_" i));
output_range(("tx_" i));
printf "%7.2fMbps\n", max[("tx_" i)] / 1024;
output_network_tier("_l_", "Lower", i);
output_network_tier("_m_", "Midd.", i);
output_network_tier("_u_", "Upper", i);
}
}
}'