Make main() in pt-stalk.

This commit is contained in:
Daniel Nichter
2011-11-01 11:39:04 -06:00
parent dfad96537e
commit 255ea01fd8

View File

@@ -99,66 +99,76 @@ log() {
echo "${1}" >&2
}
# Make the collection location
mkdir -p "${DEST}" || die "Can't make the destination directory"
test -d "${DEST}" || die "${DEST} isn't a directory"
test -w "${DEST}" || die "${DEST} isn't writable"
# The main code that runs by default. Arguments are the command-line options.
main() {
# Test if we have root; warn if not, but it isn't critical.
if [ "$(id -u)" != "0" ]; then
echo 'Not running with root privileges!';
# Make the collection location
mkdir -p "${DEST}" || die "Can't make the destination directory"
test -d "${DEST}" || die "${DEST} isn't a directory"
test -w "${DEST}" || die "${DEST} isn't writable"
# Test if we have root; warn if not, but it isn't critical.
if [ "$(id -u)" != "0" ]; then
echo 'Not running with root privileges!';
fi
# We increment this variable every time that the check is true,
# and set it to 0 if it's false.
cycles_true=0;
while true; do
d=$(date +%F-%T | tr :- _);
# XXX This is where we decide whether to execute 'collect'.
# XXX Customize this if needed. The idea is to generate a number and store
# XXX it into $detected, and if $detected > $THRESHOLD, then we'll execute
# XXX the collection process.
detected=$(mysqladmin ext ${MYSQLOPTIONS} | grep ${VARIABLE} | awk '{print $4}');
if [ -z "${detected}" -a ${MAYBE_EMPTY} = "no" ]; then
# Oops, couldn't connect, maybe max_connections problem?
echo "$d The detected value is empty; something failed? Exit status is $?"
matched="yes"
cycles_true=$(($cycles_true + 1))
elif [ "${detected:-0}" -gt ${THRESHOLD} ]; then
matched="yes"
cycles_true=$(($cycles_true + 1))
else
matched="no"
cycles_true=0
fi
# XXX Stop customizing here; everything above should be what you need.
NOTE="$d check results: ${VARIABLE} = ${detected}, matched = ${matched}, cycles_true = ${cycles_true}"
# Actually execute the collection script.
if [ "${matched:-no}" = "yes" -a ${cycles_true} -ge ${CYCLES} ]; then
log "${NOTE}" "${COLLECT} triggered"
PREFIX="$(date +%F-%T | tr :- _)"
echo "${NOTE}" > "${DEST}/${PREFIX}-trigger"
${COLLECT} -d "${DEST}" -i "${DURATION}" -g "${GDB}" -o "${OPROFILE}" -p "${PREFIX}" -s "${STRACE}" -t "${TCPDUMP}" -f "${PCT_THRESHOLD}" -m "${MB_THRESHOLD}" -- ${MYSQLOPTIONS}
echo "$d sleeping ${SLEEP} seconds to avoid DOS attack"
sleep ${SLEEP}
else
echo ${NOTE}
sleep ${INTERVAL}
fi
# Delete things more than $PURGE days old
find "${DEST}" -type f -mtime +${PURGE} -exec rm -f '{}' \;
find "/var/lib/oprofile/samples" -type d -name 'pt_collect_*' \
-depth -mtime +${PURGE} -exec rm -f '{}' \;
done
}
# Execute the program if it was not included from another file.
# This makes it possible to include without executing, and thus test.
if [ "$(basename "$0")" = "pt-stalk" ] || [ "$(basename "$0")" = "bash" -a "$_" = "$0" ]; then
main "$@"
fi
# We increment this variable every time that the check is true, and set it to 0
# if it's false.
cycles_true=0;
while true; do
d=$(date +%F-%T | tr :- _);
# XXX This is where we decide whether to execute 'collect'.
# XXX Customize this if needed. The idea is to generate a number and store
# XXX it into $detected, and if $detected > $THRESHOLD, then we'll execute
# XXX the collection process.
detected=$(mysqladmin ext ${MYSQLOPTIONS} | grep ${VARIABLE} | awk '{print $4}');
if [ -z "${detected}" -a ${MAYBE_EMPTY} = "no" ]; then
# Oops, couldn't connect, maybe max_connections problem?
echo "$d The detected value is empty; something failed? Exit status is $?"
matched="yes"
cycles_true=$(($cycles_true + 1))
elif [ "${detected:-0}" -gt ${THRESHOLD} ]; then
matched="yes"
cycles_true=$(($cycles_true + 1))
else
matched="no"
cycles_true=0
fi
# XXX Stop customizing here; everything above should be what you need.
NOTE="$d check results: ${VARIABLE} = ${detected}, matched = ${matched}, cycles_true = ${cycles_true}"
# Actually execute the collection script.
if [ "${matched:-no}" = "yes" -a ${cycles_true} -ge ${CYCLES} ]; then
log "${NOTE}" "${COLLECT} triggered"
PREFIX="$(date +%F-%T | tr :- _)"
echo "${NOTE}" > "${DEST}/${PREFIX}-trigger"
${COLLECT} -d "${DEST}" -i "${DURATION}" -g "${GDB}" -o "${OPROFILE}" -p "${PREFIX}" -s "${STRACE}" -t "${TCPDUMP}" -f "${PCT_THRESHOLD}" -m "${MB_THRESHOLD}" -- ${MYSQLOPTIONS}
echo "$d sleeping ${SLEEP} seconds to avoid DOS attack"
sleep ${SLEEP}
else
echo ${NOTE}
sleep ${INTERVAL}
fi
# Delete things more than $PURGE days old
find "${DEST}" -type f -mtime +${PURGE} -exec rm -f '{}' \;
find "/var/lib/oprofile/samples" -type d -name 'pt_collect_*' \
-depth -mtime +${PURGE} -exec rm -f '{}' \;
done
# ############################################################################
# Documentation
# ############################################################################