문제

I have a long automation suite that runs and generates a log file with something like:

Time, API, Min, Max, Average, Current, Invocation
2013-07-22 14:52:39, api1, 167.0, 167.0, 167.0, 167.0, 1
2013-07-22 14:52:39, api1, 167.0, 169.0, 168.0, 169.0, 2
...
...

Once the test is done, I want to automatically gather up the Unix machine statistics via sar (or nmon, but trying sar first). The sar command is something like this:

sar -i 900 -f /var/log/sa/sa22 -s 10:00:00 -e 18:00:00

I already have a Java class that will SSH into any number of machines and run a static command read from an XML file. It will save the output to a file for later parsing.

My dilemma is that I want to automate the running of the sar command for the duration of the test (which is of course going to change). For example, I want to get the start and end times from the log file above and run sar with those dates. With Unix shell, I can get those dates as follows:

SECOND_LINE=`sed -n '2p' ${LOG_DIR}/${CURRENT_DATE}_results.txt`
START_DATE=`echo ${SECOND_LINE} | cut -c9-10`
START_TIME=`echo ${SECOND_LINE} | cut -c12-13,15-16`
LAST_LINE=`sed -n '$p' ${LOG_DIR}/${CURRENT_DATE}_results.txt`
END_DATE=`echo ${LAST_LINE} | cut -c9-10`
END_TIME=`echo ${LAST_LINE} | cut -c12-13,15-16`

I could pass those into my Java class and form the sar command each time or I could do some find and replaces on the text file that my Java class reads from with the commands. However, I am not sure how to handle if the automation suite runs for multiple days or crosses into another month.

Is there a better way to accomplish what I want or should I continue down my line of thinking? My end goal is that I want the sar data (CPU utilization, disk IO, memory usage) for the entire duration of my test, which could be minutes or days.

도움이 되었습니까?

해결책

I come with a basic script handling some options. It is not an answer at all, just an approach that is too broad to post in comments:

start_day=22
end_day=$1
sar_path="/var/log/sa"

start_time=14
end_time=17

echo "start_day = $start_day - end_day = $end_day"
echo "start_time = $start_time - end_time = $end_time"

if [ $start_day -eq $end_day ]; then
        echo "sar -i 900 -f $sar_path/sa$start_day -s $start_time:00:00 -e $end_time:00:00"
elif [ "$(($start_day + 1))" -eq $end_day ]; then
        echo "sar -i 900 -f $sar_path/sa$start_day -s $start_time:00:00 -e 23:59"
        echo "sar -i 900 -f $sar_path/sa$end_day -s 00:00:00 -e $end_time:00:00"
elif [ $start_day -gt $end_day ]; then
        last_day=$(ls -1 $sar_path/sa[0-9]* | tail -1)
        last_day=$(basename $last_day | grep -o [0-9]*)
        echo "sar -i 900 -f $sar_path/sa$start_day -s $start_time:00:00 -e 23:59"
        for i in $(seq $(($start_day +1)) $(($last_day)))
        do
                echo "sar -i 900 -f $sar_path/sa$i -s 00:00:00 -e 23:59:00"
        done
        for i in $(seq 1 $(($end_day -1)))
        do
                echo "sar -i 900 -f $sar_path/sa$i -s 00:00:00 -e 23:59:00"
        done
        echo "sar -i 900 -f $sar_path/sa$end_day -s 00:00:00 -e $end_time:00:00"
else
        echo "sar -i 900 -f $sar_path/sa$start_day -s $start_time:00:00 -e 23:59"
        for i in $(seq $(($start_day +1)) $(($end_day -1)))
        do
                echo "sar -i 900 -f $sar_path/sa$i -s 00:00:00 -e 23:59:00"
        done
        echo "sar -i 900 -f $sar_path/sa$end_day -s 00:00:00 -e $end_time:00:00"
fi

To execute with ./file <last_day>.

Note that the case when last_day is in the next month is not considered. Should check also the last day of the month (best way, with ls -l /var/log/sa), etc.

That said, I think that it is way better to handle this with Java if it provides you such fancy date parsing.

Examples

#same day
$ ./a 22
start_day = 22 - end_day = 22
start_time = 14 - end_time = 17
sar -i 900 -f /var/log/sa/sa22 -s 14:00:00 -e 17:00:00

#one day difference
$ ./a 23
start_day = 22 - end_day = 23
start_time = 14 - end_time = 17
sar -i 900 -f /var/log/sa/sa22 -s 14:00:00 -e 23:59
sar -i 900 -f /var/log/sa/sa23 -s 00:00:00 -e 17:00:00

#>1 day difference
$ ./a 27
start_day = 22 - end_day = 27
start_time = 14 - end_time = 17
sar -i 900 -f /var/log/sa/sa22 -s 14:00:00 -e 23:59
sar -i 900 -f /var/log/sa/sa23 -s 00:00:00 -e 23:59:00
sar -i 900 -f /var/log/sa/sa24 -s 00:00:00 -e 23:59:00
sar -i 900 -f /var/log/sa/sa25 -s 00:00:00 -e 23:59:00
sar -i 900 -f /var/log/sa/sa26 -s 00:00:00 -e 23:59:00
sar -i 900 -f /var/log/sa/sa27 -s 00:00:00 -e 17:00:00

# change of month
$ ./a 5
start_day = 22 - end_day = 5
start_time = 14 - end_time = 17
sar -i 900 -f /var/log/sa/sa22 -s 14:00:00 -e 23:59
sar -i 900 -f /var/log/sa/sa23 -s 00:00:00 -e 23:59:00
sar -i 900 -f /var/log/sa/sa24 -s 00:00:00 -e 23:59:00
sar -i 900 -f /var/log/sa/sa25 -s 00:00:00 -e 23:59:00
sar -i 900 -f /var/log/sa/sa1 -s 00:00:00 -e 23:59:00
sar -i 900 -f /var/log/sa/sa2 -s 00:00:00 -e 23:59:00
sar -i 900 -f /var/log/sa/sa3 -s 00:00:00 -e 23:59:00
sar -i 900 -f /var/log/sa/sa4 -s 00:00:00 -e 23:59:00
sar -i 900 -f /var/log/sa/sa5 -s 00:00:00 -e 17:00:00
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top