-
Notifications
You must be signed in to change notification settings - Fork 0
/
iostat_reporter.sh
executable file
·97 lines (88 loc) · 2.54 KB
/
iostat_reporter.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
#!/bin/bash
usage() {
cat << EOF >&2
Usage: $0
-h Print this help message.
-m disk Monitor mode. Collects io statistics using iostat extended mode for the specified disk.
-o output The output file to log iostat metrics when in monitor mode. Default iostat.log
-i interval Specifies the amount of time in seconds between each report. Used in monior mode. Default 2 seconds
-r file Generate graphical report. If the input file is not generated by iostat_monitor, then it must has the following format: each line must contain a timestamp and the output of the iostat extended statistics. The tool creates a folder containing the report.
EOF
}
create_out_dir() {
if [ -z "$1" ]; then
echo "No out dir specified"
exit;
fi
OUT_DIR=$1
if [ -d $OUT_DIR ]; then # check if dir exists
if [ -z "$(find "$OUT_DIR" -maxdepth 0 -type d -empty 2>/dev/null)" ]; then #check if dir is empty
echo "Error! Directory $OUT_DIR already exists and is not empty."
exit;
else
#dir exists but is empty. No need to create it
return ;
fi
fi
mkdir $OUT_DIR
}
ACTION="nop"
DISK="nop"
OUTPUT="iostat.log"
IOSTAT_INTERVAL=2
DATAFILE="nop"
while getopts ":hm:o:i:r:" opt; do
case "$opt" in
h) usage
exit 0
;;
m) DISK=$OPTARG
ACTION="monitor"
;;
r) DATAFILE=$OPTARG
ACTION="plot"
;;
o) OUTPUT=$OPTARG
;;
i) IOSTAT_INTERVAL=$OPTARG
;;
*) usage
;;
esac
done
monitor() {
echo "$(date) Running iostat for disk $DISK with $IOSTAT_INTERVAL sec interval"
echo "Logging oistat output to $OUTPUT"
echo "Press ctl-c to stop iostat monitor"
iostat -xmd $IOSTAT_INTERVAL | awk -v device=$DISK '{ if ($1 == device) {print strftime("%Y-%m-%d %H:%M:%S"), $0 } }' > $OUTPUT
}
generate_graphs() {
if [ ! -f $DATAFILE ]; then
echo "File $DATAFILE not found!"
exit;
fi
FILE=$(basename $DATAFILE); # remove path
FILENAME="${FILE%.*}"
OUTPUT_DIR="${FILENAME}_report"
echo "Creating directory $OUTPUT_DIR . . ."
create_out_dir $OUTPUT_DIR
echo "Generating reports . . ."
gnuplot -e "datafile='${DATAFILE}'; outdir='${OUTPUT_DIR}/'" iostat.gnu
cp io_stat.html $OUTPUT_DIR
cp $DATAFILE $OUTPUT_DIR
}
case "$ACTION" in
monitor)
monitor
;;
plot)
generate_graphs
;;
test)
create_out_dir
;;
*)
echo $USAGE_MSG
exit
;;
esac