Skip to content

Commit

Permalink
Code relocation and clean up of some race conditions
Browse files Browse the repository at this point in the history
Moved disk logic into linux_basic_performance_data.sh as it's better
suited to that environment and not needed for all applications.

Surrounded variables in "" to prevent weird behavior.  Used "" around
the $@ expansions to prevent word splitting.
  • Loading branch information
reyjrar committed Mar 8, 2012
1 parent b7f2187 commit 049a9ff
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 59 deletions.
34 changes: 32 additions & 2 deletions bin/linux_basic_performance_data.sh
Expand Up @@ -24,8 +24,38 @@ if [ -z $CARBON_NO_SPLAY ]; then
fi;

#------------------------------------------------------------------------#
# Pre Check Routines
find_disks_to_check;
# Determine the Disks to Monitor
disk_prefixes=( 'sd' 'hd' 'c0d' 'c1d' )
declare -r disks_prefixes
declare -a disks
CACHE_DISKS="$CARBON_CACHE/disks";
if [ -f "$CACHE_DISKS" ]; then
. $CACHE_DISKS;
fi;

if [ ${#disks} -gt 0 ]; then
(( $CARBON_DEBUG )) && echo "disk_check: retrieved from cache";
else
if [ -f /proc/partitions ]; then
while read line
do
disk=`echo $line |awk '{print $4}'`;
for prefix in "${disk_prefixes[@]}"; do
[ -z "$disk" ] && continue;

(( $CARBON_DEBUG )) && echo " => check: '$disk' =~ '$prefix' : $matched";
if [[ "$disk" =~ "$prefix" ]]; then
disks[${#disks[*]}]="$disk";
(( $CARBON_DEBUG )) && echo "DISK: $disk";
break
fi;
done;
done < /proc/partitions;
# Cache
echo "disks='${disks[@]}'" > "$CACHE_DISKS";
fi;
fi;
(( $CARBON_DEBUG )) && echo "disk_check found: ${disks[@]}";

#------------------------------------------------------------------------#
# Load Average
Expand Down
23 changes: 12 additions & 11 deletions bin/time_to_graphite.sh
Expand Up @@ -5,26 +5,27 @@
# Brad Lhotsky <brad.lhotsky@gmail.com>
#

#------------------------------------------------------------------------#
# Arguement Processing
args=("$@");
command=$1;
unset args[0];
timings_file="/tmp/timinigs.$$";


#------------------------------------------------------------------------#
# Load Caron Library
if [ -e /usr/local/lib/carbon-lib.sh ]; then
. /usr/local/lib/carbon-lib.sh
else
echo "unable to load /usr/local/lib/carbon-lib.sh";
exit 1;
(( $CARBON_DEBUG )) && echo "failed to load carbon-lib.sh, running command";
"${args[@]}"
exit $?
fi;

#------------------------------------------------------------------------#
# Arguement Processing
args=($@);
command=$1;
unset args[0];

timings_file="/tmp/timinigs.$$";

#------------------------------------------------------------------------#
# Execute the Command, capturing the RC
(( $DEBUG )) && echo "($command) ${args[@]} to $timings_file";
(( $CARBON_DEBUG )) && echo "($command) ${args[@]} to $timings_file";
/usr/bin/time -o $timings_file -f "user:%U\nsys:%S\nreal:%e\ncpu:%P" ${args[@]};
RC=$?;

Expand Down
52 changes: 6 additions & 46 deletions lib/carbon-lib.sh
Expand Up @@ -42,15 +42,15 @@ CARBON_STASH="$CARBON_CACHE/stash.$$"
declare -r CARBON_STASH

if [ ! -d "$CARBON_CACHE" ]; then
mkdir $CARBON_CACHE;
mkdir "$CARBON_CACHE";
fi;

#------------------------------------------------------------------------#
# Cleanup
[ -f "$CARBON_STASH" ] && rm -f $CARBON_STASH;
if [ -O "$CARBON_CACHE" ]; then
chmod 0777 "$CARBON_CACHE";
find $CARBON_CACHE -type f -mtime +1 -exec rm {} \; 2> /dev/null
find "$CARBON_CACHE" -type f -mtime +1 -exec rm -f {} \; 2> /dev/null
fi;

#------------------------------------------------------------------------#
Expand All @@ -59,57 +59,17 @@ HOST=`hostname -s`
declare -r HOST
RUN_TIME=`date +%s`
declare -r RUN_TIME
# Hard Disk Monitoring
disk_prefixes=( 'sd' 'hd' 'c0d' 'c1d' )
declare -r disks_prefixes

#------------------------------------------------------------------------#
# Globals
declare -a disks

#------------------------------------------------------------------------#
# Function Declarations
function add_metric() {
echo "${CARBON_BASE}.${HOST}.$1 $RUN_TIME" >> $CARBON_STASH;
echo "${CARBON_BASE}.${HOST}.$1 $RUN_TIME" >> "$CARBON_STASH";
(( $CARBON_DEBUG )) && echo $1;
}

function send_to_carbon() {
if [ $CARBON_SEND != "disabled" ]; then
nc $CARBON_HOST $CARBON_PORT < $CARBON_STASH;
[[ $CARBON_DEBUG -gt 1 ]] && cat $CARBON_STASH;
fi;
rm -f $CARBON_STASH;
}

function find_disks_to_check() {
CACHE_DISKS="$CARBON_CACHE/disks";
if [ -f "$CACHE_DISKS" ]; then
. $CACHE_DISKS;
nc $CARBON_HOST $CARBON_PORT < "$CARBON_STASH";
[[ $CARBON_DEBUG -gt 1 ]] && cat "$CARBON_STASH";
fi;

if [ ${#disks} -gt 0 ]; then
(( $CARBON_DEBUG )) && echo "disk_check: retrieved from cache";
else
if [ -f /proc/partitions ]; then
while read line
do
disk=`echo $line |awk '{print $4}'`;
for prefix in "${disk_prefixes[@]}"; do
[ -z "$disk" ] && continue;

(( $CARBON_DEBUG )) && echo " => check: '$disk' =~ '$prefix' : $matched";
if [[ "$disk" =~ "$prefix" ]]; then
disks[${#disks[*]}]="$disk";
(( $CARBON_DEBUG )) && echo "DISK: $disk";
break
fi;
done;
done < /proc/partitions;
# Cache
echo "disks='${disks[@]}'" > $CACHE_DISKS;
fi;
fi;

(( $CARBON_DEBUG )) && echo "disk_check found: ${disks[@]}";
rm -f "$CARBON_STASH";
}

0 comments on commit 049a9ff

Please sign in to comment.