Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First Major Improvements, Bash Library
Provides simple access to sending data to carbon. Not that it was hard before, but the idea is to provide a simple statsd-esq ue interace for sysadmins to use.
- Loading branch information
Showing
4 changed files
with
221 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,18 @@ | ||
Some general purposes scripts for sending data to Graphite. | ||
|
||
* linux/ - Contains scripts for Linux Hosts | ||
* lib/ | ||
* carbon-lib.sh - provides simple access to sending | ||
data with to your carbon server. | ||
|
||
References /etc/sysconfig/carbon-backend | ||
|
||
* bin/ | ||
* linux_basic_performance_data.sh - makes heavy use of /proc to | ||
extract performance metrics for linux systems and send them to | ||
a carbon cache. | ||
|
||
* time_to_graphite.sh - Wrapper script to time a command, sending | ||
timings to carbon for archival and graphing. | ||
|
||
usage: time_to_graphite.sh git-status git status | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#!/bin/sh | ||
# | ||
# Wrapper around a shell script to time it to Graphite | ||
# | ||
# Brad Lhotsky <brad.lhotsky@gmail.com> | ||
# | ||
|
||
#------------------------------------------------------------------------# | ||
# 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; | ||
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"; | ||
/usr/bin/time -o $timings_file -f "user:%U\nsys:%S\nreal:%e\ncpu:%P" ${args[@]}; | ||
RC=$?; | ||
|
||
#------------------------------------------------------------------------# | ||
# Only log successful commands | ||
if [[ $RC != 0 ]]; then | ||
rm $timings_file; | ||
exit $RC; | ||
fi; | ||
|
||
#------------------------------------------------------------------------# | ||
# Read the file, add the metrics | ||
while read line; do | ||
# Skip "Command exitted with" lines | ||
if [[ "$line" =~ 'exit' ]]; then | ||
continue; | ||
fi | ||
k=`echo $line |cut -d: -f1`; | ||
vraw=`echo $line |cut -d: -f2`; | ||
# Strip the % from the CPU line | ||
v=${vraw/\%/}; | ||
# Add the Metric | ||
add_metric "commands.$command.$k $v" | ||
done < $timings_file; | ||
rm $timings_file; | ||
|
||
#------------------------------------------------------------------------# | ||
# Send this to carbon | ||
send_to_carbon; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
#!/bin/sh | ||
# | ||
# Setup Environment and Declare functions for Graphite | ||
# enabled shell scripting | ||
# | ||
# To see debugging information, export DEBUG=1 | ||
# | ||
# To use, source this file, then add metrics: | ||
# | ||
# add_metric "simple.key $value"; | ||
# # Send your metrics at the end: | ||
# send_to_carbon; | ||
# | ||
# add_metrics will prepend "${CARBON_BASE}.${HOST}" to the | ||
# metric name. | ||
# | ||
# When send_to_carbon is called, the timestamp is added to the messages. | ||
# | ||
# Brad Lhotsky <brad.lhotsky@gmail.com> | ||
|
||
#------------------------------------------------------------------------# | ||
# Carbon Server Configuration | ||
CARBON_HOST="graphite" | ||
CARBON_PORT="2003" | ||
CARBON_BASE="tmp" | ||
|
||
# Read in System Config | ||
if [ -f "/etc/sysconfig/carbon-endpoint" ]; then | ||
. /etc/sysconfig/carbon-endpoint | ||
fi | ||
|
||
# Caching | ||
CARBON_STASH="/tmp/carbon_stash.$$" | ||
CACHE_DISKS="/tmp/cache.monitor.disks" | ||
|
||
#------------------------------------------------------------------------# | ||
# Constants | ||
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 metrics | ||
declare -a disks | ||
|
||
#------------------------------------------------------------------------# | ||
# Function Declarations | ||
function add_metric() { | ||
metrics[${#metrics[*]}]="${CARBON_BASE}.${HOST}.$1"; | ||
(( $DEBUG )) && echo $1; | ||
} | ||
|
||
function send_to_carbon() { | ||
[ -f "$CARBON_STASH" ] && rm -f $CARBON_STASH; | ||
|
||
for metric in "${metrics[@]}"; do | ||
echo "$metric $RUN_TIME" >> $CARBON_STASH; | ||
done; | ||
|
||
nc $CARBON_HOST $CARBON_PORT < $CARBON_STASH; | ||
rm -f $CARBON_STASH; | ||
} | ||
|
||
function find_disks_to_check() { | ||
if [ -f "$CACHE_DISKS" ]; then | ||
. $CACHE_DISKS; | ||
fi; | ||
|
||
if [ ${#disks} -gt 0 ]; then | ||
(( $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 | ||
if [ "X$disk" == "X" ]; then | ||
continue; | ||
fi; | ||
matched=`expr match $disk $prefix`; | ||
(( $DEBUG )) && echo " => check: expr match $disk $prefix : $matched"; | ||
if [ $matched -gt 0 ]; then | ||
disks[${#disks[*]}]="$disk"; | ||
(( $DEBUG )) && echo "DISK: $disk"; | ||
break | ||
fi; | ||
done; | ||
done < /proc/partitions; | ||
# Cache | ||
echo "disks='${disks[@]}'" > $CACHE_DISKS; | ||
fi; | ||
fi; | ||
|
||
(( $DEBUG )) && echo "disk_check found: ${disks[@]}"; | ||
} |