-
Notifications
You must be signed in to change notification settings - Fork 0
/
rstat_start.sh
executable file
·143 lines (123 loc) · 3.47 KB
/
rstat_start.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
#!/bin/bash
DATETIME=$(date +'%Y%m%d-%H%M%S')
TARGET_LIST=$@
PIDFILE=rstat.pid
LOGFILE_ERR=rstat.err
if [ "$#" -lt 1 ]; then
echo 'Usage: ./rstat_start.sh host1 host2 ...'
exit 1
fi
if [ -f "$PIDFILE" ]; then
while read PID; do
if [ "$(ps -p $PID -o comm=)" = 'ssh' ]; then
kill $PID
fi
done < $PIDFILE
rm -f $PIDFILE
fi
for TARGET in $TARGET_LIST; do
# dstat 1
LOGFILE_D01=d_${DATETIME}_${TARGET}_01.csv
ssh $TARGET bash <<_EOF_ >$LOGFILE_D01 2>>$LOGFILE_ERR &
rm -f rstat_d01.pipe
mkfifo rstat_d01.pipe
cat rstat_d01.pipe &
dstat -tfvnrl --output rstat_d01.pipe 1 1>/dev/null 2>/dev/null
_EOF_
echo $! >>$PIDFILE
# dstat 15
LOGFILE_D15=d_${DATETIME}_${TARGET}_15.csv
ssh $TARGET bash <<_EOF_ >$LOGFILE_D15 2>>$LOGFILE_ERR &
rm -f rstat_d15.pipe
mkfifo rstat_d15.pipe
cat rstat_d15.pipe &
dstat -tfvnrl --output rstat_d15.pipe 15 1>/dev/null 2>/dev/null
_EOF_
echo $! >>$PIDFILE
# iostat 15
LOGFILE_I15=i_${DATETIME}_${TARGET}_15.csv
ssh $TARGET perl <<_EOF_ >$LOGFILE_I15 2>>$LOGFILE_ERR &
use strict;
use warnings;
\$| = 1;
my \$header_print = 1;
my \$datetime;
open(my \$iostat, 'LC_ALL=C iostat -dxk 15 |') or die \$!;
\$SIG{'TERM'} = sub {
close(\$iostat);
exit(0);
};
while (my \$line = <\$iostat>) {
chomp(\$line);
if (\$line =~ /^Linux/) {
# Title
print "Host,\${line}\\n";
} elsif (\$line =~ /^Device:/) {
# Header
my (\$sec, \$min, \$hour, \$mday, \$mon, \$year) = localtime();
\$datetime = sprintf('%04d/%02d/%02d %02d:%02d:%02d',
\$year + 1900, \$mon + 1, \$mday, \$hour, \$min, \$sec);
if (\$header_print) {
my \$header = \$line;
\$header =~ s/[: ]+/,/g;
print "Datetime,\${header}\\n";
\$header_print = 0;
}
} elsif (\$line =~ /^\\w.*\\d\$/) {
# Body
my \$body = \$line;
\$body =~ s/ +/,/g;
print "\${datetime},\${body}\\n";
}
}
_EOF_
echo $! >>$PIDFILE
# pidstat 60
LOGFILE_P60=p_${DATETIME}_${TARGET}_60.csv
ssh $TARGET perl <<_EOF_ >$LOGFILE_P60 2>>$LOGFILE_ERR &
use strict;
use warnings;
\$| = 1;
my \$header_print = 1;
my \$ncols;
open(my \$pidstat, 'LC_ALL=C pidstat -hlurdw -p ALL 60 |') or die \$!;
\$SIG{'TERM'} = sub {
close(\$pidstat);
exit(0);
};
while (my \$line = <\$pidstat>) {
chomp(\$line);
if (\$line =~ /^Linux/) {
# Title
print "Host,\${line}\\n";
} elsif (\$line =~ /^#/) {
# Header
if (\$header_print) {
my \$header = \$line;
\$header =~ s/[# ]+/,/g;
print "Datetime\${header}\\n";
\$header_print = 0;
if (\$line =~ /UID/) {
# RHEL 7
\$ncols = 17;
} else {
# RHEL 6
\$ncols = 16;
}
}
} elsif (\$line =~ /^ *\\d/) {
# Body
my \$body = \$line;
\$body =~ s/^ +//;
my @cols = split(/ +/, \$body);
my (\$sec, \$min, \$hour, \$mday, \$mon, \$year) = localtime(\$cols[0]);
my \$datetime = sprintf('%04d/%02d/%02d %02d:%02d:%02d',
\$year + 1900, \$mon + 1, \$mday, \$hour, \$min, \$sec);
my \$stats = join(',', @cols[0..\$ncols]);
my \$command = join(' ', @cols[\$ncols + 1..\$#cols]);
print "\${datetime},\${stats},\${command}\\n";
}
}
_EOF_
echo $! >>$PIDFILE
done