-
Notifications
You must be signed in to change notification settings - Fork 1
/
sarreport
executable file
·266 lines (258 loc) · 6.29 KB
/
sarreport
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/bin/bash
# sarreport: Currently returns relevant data prior to a reboot event.
# Written very quickly by Kyle Walker kwalker@redhat.com
getopt_outputs=$(getopt -o aB:E:dhcCm: -n "sarreport" -- "$@");
if [[ $# -eq 0 || $1 == "-h" ]]; then
echo "sarreport: Returns relevant sar data prior to a restart"
echo "Usage: sarreport [options] <sarfile>"
echo -e "\tOptions:"
echo -e "\t\tNo options - Reports CPU utilization, Memory use, and Load"
echo -e "\t\t-d\t\tDebugging information such as the times that are being used for search"
echo -e "\t\t-m\t\tSelect restart: Can be used to select a restart event that is not the first"
echo -e "\t\t-B [val]\tChange the number of hours prior to the reboot to return"
echo -e "\t\t-a\t\tReturn all metrics available not just the defaults above"
echo -e "\t\t-c\t\tShort CPU output: only displays \"all\" entries"
echo -e "\t\t-C\t\tFull CPU output: displays all CPU entries"
echo -e "\t\t-E ##\t\tManually set the end time indicated as ##"
echo -e "\t\t-h\t\tThis message"
exit
fi
if [ ! -e $BASH_ARGV ]; then
echo -e "\tError: File does not exist"
exit
fi
eval set -- "$getopt_outputs"
if [ ! `grep RESTART -i $BASH_ARGV -c` -eq 0 ]; then
time1=(`grep RESTART -i $BASH_ARGV | cut -d':' -f 1`)
else
time1=`tail -3 $BASH_ARGV | head -1 | cut -d ':' -f 1`
fi
restarts=`grep RESTART -i $BASH_ARGV -c`
selected=0
sysall=1
hoursprior=1
runmode=0
while true; do
case "$1" in
-m)
shift
if [ $1 -le $restarts ]; then
let selected=$(($1-1))
else
echo "Incorrect selection, setting to the first iteration"
fi
shift
;;
-a)
if [ $runmode==0 ]; then
runmode=1
else
echo "Incompatible option set: -a, -d cannot be used together at this time"
fi
shift
;;
-B)
shift;
if [ -n "$1" ]; then
hoursprior=$1
shift;
fi
;;
-c)
sysall=1
shift
;;
-C)
sysall=0
shift
;;
-d)
if [ $runmode==0 ]; then
runmode=2
else
echo "Incompatible option set: -a, -d cannot be used together at this time"
fi
shift;
;;
-E)
shift
if [ $1 -le 24 ]; then
time1=$1
runmode=4
else
echo "Incorrect value provided, defaulting to scraped restart event"
fi
shift;
;;
--)
break
;;
esac
done
let time2=$((10#${time1[$selected]}))-$hoursprior;
case $runmode in
4)
awk 'BEGIN {x=0;triggered=0; if("'$sysall'" == 1) sysall=1;else sysall=0; syssub=0; upperLim='"${time1[$selected]}"'; lowerLim='"$time2"'; if(length(lowerLim)<2) lowerLim="0"lowerLim}
/iowait|kbmemfree|ldavg/,/^$/ {
if(sysall==1) {
if(syssub==0 && $0~/iowait/) {
syssub++
x++
printer[x] = $0
}
if(syssub==1 && substr($1,0,2) <= upperLim && substr($1,0,2) >= lowerLim && $0~/all/ || $0~/Average|^$/ && $0~/all/) {
x++
printer[x] = $0
}
else if(syssub==1 && $0~/^$/) {
syssub=0
x++
printer[x] = $0
}
else if(syssub==0) {
if(substr($1,0,2) <= upperLim && substr($1,0,2) >= lowerLim) {
x++
printer[x] = $0
}
else if($0~/kbmemfree|ldavg|^$|Average:/) {
x++
printer[x] = $0
}
}
}
else {
if(syssub==0 && $0~/iowait/) {
syssub++
x++
printer[x] = $0
}
if(syssub==1 && substr($1,0,2) <= upperLim && substr($1,0,2) >= lowerLim || $0~/Average:|^$/) {
x++
printer[x] = $0
}
else if(syssub==1 && $0~/^$/) {
syssub=0
x++
printer[x] = $0
}
else if(syssub==0) {
if(substr($1,0,2) <= upperLim && substr($1,0,2) >= lowerLim) {
x++
printer[x] = $0
}
else if($0~/kbmemfree|ldavg|^$/ || 0~/Average:/){
x++
printer[x] = $0
}
}
}
}
END {
if(x > 0) {
for(i=0; i<=x; i++) {
if(printer[i+1] !~/Average|^$/)
print printer[i]
}
}
else
exit
}' $BASH_ARGV
exit
;;
2)
echo time = ${time1[$selected]}
echo time[@] = ${time1[@]}
echo hoursprior = $hoursprior
echo time2 = $time2
echo selected = $selected
exit
;;
1)
if [ ! $restarts -eq 0 ]; then
echo -e "There are $restarts reboot events in this sar file, now showing number $((selected+1))"
awk 'BEGIN {
triggered=0
if("'$sysall'" == 1) sysall=1; else sysall=0
selecter='"$selected"'
syssub=0
upperLim='"${time1[$selected]}"'
lowerLim='"$time2"'
if(length(lowerLim)<2) lowerLim="0"lowerLim
}
/RESTART/ {
if(triggered==0) print
triggered++
}
/00:00/,/^$/ {
if(triggered==0) {
if($1 >= lowerLim || $1~/^00:00/)
print $0
}
}
/^$/ {
if(triggered==0) print
}' $BASH_ARGV
else
echo -e "There are $restarts restarts in this sar file\n"
fi
exit
;;
*)
if [ ! $restarts -eq 0 ]; then
echo -e "There are $restarts reboot events in this sar file, now showing number $(($selected+1))\n"
awk 'BEGIN { triggered=0; headerprinted = 0; if("'$sysall'" == 1) sysall=1;else sysall=0; selecter='"$selected"'; syssub=0; upperLim='"${time1[$selected]}"'; lowerLim='"$time2"'; if(length(lowerLim)<2) lowerLim="0"lowerLim}
/RESTART/ {
if(triggered==selecter) {
print
}
triggered++
}
/iowait|kbmemfree|ldavg/,/^$/ {
if(triggered==selecter) {
if(sysall==1) {
if(syssub==0 && $0~/iowait/) {
syssub++
print
}
if(syssub==1 && substr($1,0,2) <= upperLim && substr($1,0,2) >= lowerLim && $0~/all/ || $0~/Average|^$/ && $0~/all/) {
print
headerprinted=0
}
else if(syssub==1 && $0~/^$/) {
syssub=0
print
}
else if(syssub==0) {
if(substr($1,0,2) <= upperLim && substr($1,0,2) >= lowerLim)
print
else if($0~/kbmemfree|ldavg|^$|Average:/ && headerprinted == 0)
print
}
}
else {
if(syssub==0 && $0~/iowait/) {
syssub++
print
}
if(syssub==1 && substr($1,0,2) <= upperLim && substr($1,0,2) >= lowerLim || $0~/Average:|^$/) {
print
}
else if(syssub==1 && $0~/^$/) {
syssub=0
print
}
else if(syssub==0) {
if(substr($1,0,2) <= upperLim && substr($1,0,2) >= lowerLim)
print
else if(($0~/kbmemfree|ldavg|^$/ || 0~/Average:/) && (headerprinted == 0))
print
}
}
}
}' $BASH_ARGV
else
echo -e "There are $restarts restarts in this sar file\n"
fi
exit
;;
esac