-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathiostat-csv.sh
executable file
·55 lines (41 loc) · 1.64 KB
/
iostat-csv.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
#!/bin/sh
## iostat csv generator ##
# This script converts iostat output every second in one line and generates a csv file.
# Settings
COMMAND_OUTPUT=`iostat -t -x`
CONCAT_LINE_OFFSET=3
TITLE_LINE_OFFSET=6
TITLE_LINE_HEADER="Date Time"
# Count command output line number
COMMAND_OUTPUT_LINE_NUMBER=`wc -l <<EOF
${COMMAND_OUTPUT}
EOF`
# Calcurate how many lines to concatenate in each second
CONCAT_LINE_NUMBER=`expr ${COMMAND_OUTPUT_LINE_NUMBER} - ${CONCAT_LINE_OFFSET}`
# Print "N;" for ${CONCAT_LINE_NUBER} times to concatenate output lines by using sed command
CONCAT_LINE_N=`seq -s'N;' ${CONCAT_LINE_NUMBER} | tr -d '[:digit:]'`
# Generate title line for csv
TITLE_AVG_CPU=`grep avg-cpu <<EOF
${COMMAND_OUTPUT}
EOF`
TITLE_EACH_DEVICE=`grep "Device" <<EOF
${COMMAND_OUTPUT}
EOF`
DEVICE_LINE_NUMBER=`expr ${COMMAND_OUTPUT_LINE_NUMBER} - ${TITLE_LINE_OFFSET}`
TITLE_DEVICES=`seq -s"${TITLE_EACH_DEVICE} " ${DEVICE_LINE_NUMBER} | tr -d '[:digit:]'`
echo "${TITLE_LINE_HEADER} ${TITLE_AVG_CPU} ${TITLE_DEVICES}" \
| awk 'BEGIN {OFS=","} {$1=$1;print $0}' | sed 's/avg-cpu//g;s/://g;s/,,/,/g'
# Main part
LANG=C; iostat -t -x 1 | grep --line-buffered -v -e avg-cpu -e Device -e Linux \
| sed --unbuffered "${CONCAT_LINE_N}s/\n/,/g;s/\s\s*/,/g;s/,,*/,/g;s/^,//g"
# grep -v -e avg-cpu -e Device -e Linux
# => Exclude title columns
# sed --unbuffered
# '${CONCAT_LINE_N}s/\n/,/g'
# => Read ${CONCAT_LINE_NUMBER} lines and replace newline characters to ","
# 's/\s\s*/,/g'
# => Replace adjacent blank symbols for ","
# 's/,,*/,/g'
# => Replace adjacent commas for single comma
# 's/^,//g'
# => Remove comma symbol placed at the beginning of the line