Skip to content

Commit

Permalink
First Major Improvements, Bash Library
Browse files Browse the repository at this point in the history
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
reyjrar committed Feb 29, 2012
1 parent ee438b3 commit 6977229
Show file tree
Hide file tree
Showing 4 changed files with 221 additions and 79 deletions.
17 changes: 16 additions & 1 deletion README
@@ -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

Expand Up @@ -5,85 +5,15 @@
# as often as you'd like.
#
# 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";
}

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;
}

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[@]}";
}
# Load Carbon Lib
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;

#------------------------------------------------------------------------#
# Pre Check Routines
Expand Down Expand Up @@ -211,6 +141,47 @@ while read line; do
done < /tmp/cache.monitors.df;
rm -f /tmp/cache.monitors.df;
#------------------------------------------------------------------------#
# Network Statistics
for nic in `route -n |grep -v Kernel|grep -v Gateway|awk '{print $8}'|sort -u`; do
(( $DEBUG )) && echo "Fetching interface statistics for $nic";
/sbin/ifconfig $nic |grep packets| while read line; do
set -- $line;
direction=`echo $1|tr '[A-Z]' '[a-z]'`;
tmp=($@); fields=(${tmp[@]:1});
for statistic in "${fields[@]}"; do
k=`echo $statistic|cut -d: -f 1`;
v=`echo $statistic|cut -d: -f 2`;
add_metric "nic.$nic.$direction.$k $v";
done;
done;
/sbin/ifconfig $nic |grep bytes| while read line; do
set -- $line;
rx_bytes=`echo $2 | cut -d: -f 2`;
tx_bytes=`echo $6 | cut -d: -f 2`;
add_metric "nic.$nic.rx.bytes $rx_bytes";
add_metric "nic.$nic.tx.bytes $tx_bytes";
done;
collisions=`/sbin/ifconfig $nic |grep collisions|awk '{print $1}'|cut -d: -f2`;
add_metric "nic.$nic.collisions $collisions";
done;
# Grab TCP Connection Data
/bin/netstat -s --tcp |grep 'connections* opening' | while read line; do
set -- $line;
add_metric "tcp.connections.$2 $1";
done;
tcp_failed=`/bin/netstat -s --tcp |grep 'failed connection attempts'|awk '{print $1}'`;
add_metric "tcp.connections.failed $tcp_failed";
# Grab TCP Reset Data
/bin/netstat -s --tcp |grep reset|grep -v due |awk '{print $1 " " $NF}' | while read line; do
set -- $line;
add_metric "tcp.resets.$2 $1";
done;
# Grab UDP Packet Data
/bin/netstat -s --udp|grep packets|grep -v unknown | while read line; do
set -- $line;
add_metric "udp.packets.$3 $1";
done;
#------------------------------------------------------------------------#
# SEND THE UPDATES TO CARBON
send_to_carbon;
exit 0;
56 changes: 56 additions & 0 deletions bin/time_to_graphite.sh
@@ -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;
100 changes: 100 additions & 0 deletions lib/carbon-lib.sh
@@ -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[@]}";
}

0 comments on commit 6977229

Please sign in to comment.