From b47679cf97df1f18d18cc4e5219c8c134f3e1314 Mon Sep 17 00:00:00 2001 From: Daniel Nichter Date: Tue, 24 Jan 2012 13:20:07 -0700 Subject: [PATCH] Implement --[no]collect. --- bin/pt-stalk | 72 +++++++++++++++++++++++-------------------- t/pt-stalk/pt-stalk.t | 34 +++++++++++++++++++- 2 files changed, 71 insertions(+), 35 deletions(-) diff --git a/bin/pt-stalk b/bin/pt-stalk index c2748475..241457e5 100755 --- a/bin/pt-stalk +++ b/bin/pt-stalk @@ -971,44 +971,48 @@ stalk() { # ################################################################## # Start collecting, maybe. # ################################################################## - local prefix="${OPT_PREFIX:-$(date +%F-%T | tr :- _)}" log "Collect triggered" - # Check if we'll have enough disk space to collect. Disk space - # is also checked every interval while collecting. - local margin="20" # default 20M margin, unless: - if [ -n "$last_prefix" ]; then - margin=$(du -mc "$OPT_DEST"/"$last_prefix"-* | tail -n 1 | awk '{print $1'}) - fi - disk_space "$OPT_DEST" > "$OPT_DEST/$prefix-disk-space" - check_disk_space \ - "$OPT_DEST/$prefix-disk-space" \ - "$OPT_DISK_BYTE_LIMIT" \ - "$OPT_DISK_PCT_LIMIT" \ - "$margin" # real used MB + margin MB - if [ $? -eq 0 ]; then - # There should be enough disk space, so collect. - log "$msg" >> "$OPT_DEST/$prefix-trigger" - log "pt-stalk ran with $RAN_WITH" >> "$OPT_DEST/$prefix-trigger" - last_prefix="$prefix" + # Send email to whomever that collect has been triggered. + if [ "$OPT_NOTIFY_BY_EMAIL" ]; then + echo "$msg on $(hostname)" \ + | mail -s "Collect triggered on $(hostname)" \ + "$OPT_NOTIFY_BY_EMAIL" + fi - # Send email to whomever that collect has been triggered. - if [ "$OPT_NOTIFY_BY_EMAIL" ]; then - echo "$msg on $(hostname)" \ - | mail -s "Collect triggered on $(hostname)" \ - "$OPT_NOTIFY_BY_EMAIL" + if [ "$OPT_COLLECT" ]; then + local prefix="${OPT_PREFIX:-$(date +%F-%T | tr :- _)}" + + # Check if we'll have enough disk space to collect. Disk space + # is also checked every interval while collecting. + local margin="20" # default 20M margin, unless: + if [ -n "$last_prefix" ]; then + margin=$(du -mc "$OPT_DEST"/"$last_prefix"-* | tail -n 1 | awk '{print $1'}) + fi + disk_space "$OPT_DEST" > "$OPT_DEST/$prefix-disk-space" + check_disk_space \ + "$OPT_DEST/$prefix-disk-space" \ + "$OPT_DISK_BYTE_LIMIT" \ + "$OPT_DISK_PCT_LIMIT" \ + "$margin" # real used MB + margin MB + if [ $? -eq 0 ]; then + # There should be enough disk space, so collect. + log "$msg" >> "$OPT_DEST/$prefix-trigger" + log "pt-stalk ran with $RAN_WITH" >> "$OPT_DEST/$prefix-trigger" + last_prefix="$prefix" + + + # Fork and background the collect subroutine which will + # run for --run-time seconds. We (the parent) sleep + # while its collecting (hopefully --sleep is longer than + # --run-time). + ( + collect "$OPT_DEST" "$prefix" + ) >> "$OPT_DEST/$prefix-output" 2>&1 & + else + # There will not be enough disk space, so do not collect. + warn "Collect canceled because there will not be enough disk space after collecting another $margin MB" fi - - # Fork and background the collect subroutine which will - # run for --run-time seconds. We (the parent) sleep - # while its collecting (hopefully --sleep is longer than - # --run-time). - ( - collect "$OPT_DEST" "$prefix" - ) >> "$OPT_DEST/$prefix-output" 2>&1 & - else - # There will not be enough disk space, so do not collect. - warn "Collect canceled because there will not be enough disk space after collecting another $margin MB" fi # ################################################################## diff --git a/t/pt-stalk/pt-stalk.t b/t/pt-stalk/pt-stalk.t index eb491302..6a054899 100644 --- a/t/pt-stalk/pt-stalk.t +++ b/t/pt-stalk/pt-stalk.t @@ -24,7 +24,7 @@ if ( !$dbh ) { plan skip_all => 'Cannot connect to sandbox master'; } else { - plan tests => 17; + plan tests => 20; } my $cnf = "/tmp/12345/my.sandbox.cnf"; @@ -169,6 +169,38 @@ like( "Trigger file logs how pt-stalk was ran" ); +# ########################################################################### +# Triggered but --no-collect. +# ########################################################################### +diag(`rm $pid_file 2>/dev/null`); +diag(`rm $log_file 2>/dev/null`); +diag(`rm $dest/* 2>/dev/null`); + +(undef, $uptime) = $dbh->selectrow_array("SHOW STATUS LIKE 'Uptime'"); +$threshold = $uptime + 2; + +$retval = system("$trunk/bin/pt-stalk --no-collect --iterations 1 --dest $dest --variable Uptime --threshold $threshold --cycles 1 --run-time 1 --pid $pid_file -- --defaults-file=$cnf >$log_file 2>&1"); + +sleep 2; + +$output = `cat $log_file`; +like( + $output, + qr/Collect triggered/, + "Collect triggered" +); + +ok( + ! -f "$dest/*", + "No files collected" +); + +$output = `ps x | grep -v grep | grep 'pt-stalk pt-stalk --iterations 1 --dest $dest'`; +is( + $output, + "", + "pt-stalk is not running" +); # ############################################################################# # --config