Skip to content

Instantly share code, notes, and snippets.

@seanosulliv
Created January 11, 2011 10:12
Show Gist options
  • Save seanosulliv/774264 to your computer and use it in GitHub Desktop.
Save seanosulliv/774264 to your computer and use it in GitHub Desktop.
#!/bin/sh
# Graph builder script
if [ ! $1 ]; then
echo "Enter output file (blank to output to pwd): "
read OUTPUTFILE
echo "Enter graph label: "
read GRAPH_LABEL
echo "Enter start time: "
read GRAPH_START
echo "Enter end time: "
read GRAPH_END
echo "Enter host regex: "
read HOST_REGEX
echo "Enter stat regex: "
read STAT_REGEX
echo "Enter Y-Max value: "
read GRAPH_YMAX
echo "Enter Y-Min value: "
read GRAPH_YMIN
echo "Enter graph type (line/area/stack): "
read GRAPH_TYPE
elif [ $1 = "help" ]; then
echo "Runtime Usage:"
echo "If you run the script without arguements, you'll be prompted for each value"
echo "If you run it with any arguements, it will expect all values to be provided:"
echo ""
echo "graphbuilder.sh Outputfile Graph_Label \"graph_start_date\" graph_end_time(now) host_regex stats_regex graph_type"
echo ""
echo "Outputfile: This should either be a filename or full path to the output file, should end in .pspl"
echo "Graph_Label: Name of your graph, it will be displayed at the top of the graph."
echo "Graph_Start_Date: The time to show graphs from, written as 'X days ago', or 'X hours ago' etc."
echo "Graph_End_Time: The end time, as above, can be written as 'X days/hours ago', or also 'now'."
echo "Host_Regex: This can match either a full hostname, or a partial one."
echo "Stats_Regex: This can match the full stat name, or a partial one, so 'load' will match 1, 5, 15 min loads, whereas 15-min will only match the 15-min load value."
echo "Graph_type: LINE1, STACK or AREA"
echo ""
exit
else
OUTPUTFILE=$1
GRAPH_LABEL=$2
GRAPH_START=$3
GRAPH_END=$4
HOST_REGEX=$5
STAT_REGEX=$6
GRAPH_YMAX=$7
GRAPH_YMIN=$8
GRAPH_TYPE=$9
fi
if [ ! $OUTPUTFILE ]; then
OUTPUTFILE=$(dd if=/dev/urandom count=2 2>/dev/null | md5sum | cut -c1-6).pspl
fi
if [ ! $GRAPH_LABEL ]; then
GRAPH_LABEL="Unnamed Graph"
fi
if [ ! $GRAPH_START ]; then
GRAPH_START="2 days ago"
fi
if [ ! $GRAPH_END ]; then
GRAPH_END="now"
fi
if [ ! $HOST_REGEX ]; then
HOST_REGEX="pg"
fi
if [ ! $STAT_REGEX ]; then
STAT_REGEX="15-min"
fi
if [ ! $GRAPH_TYPE ]; then
GRAPH_TYPE="LINE1"
fi
if [ $GRAPH_TYPE = "line" ]; then
GRAPH_TYPE="LINE1"
fi
if [ $GRAPH_TYPE = "area" ]; then
GRAPH_TYPE="AREA"
fi
if [ $GRAPH_TYPE = "stack" ]; then
GRAPH_TYPE="STACK"
fi
TEMPFILE=$(mktemp)
RRD_PATH="/var/yaketystats/rrd"
STATLIST=($(find $RRD_PATH -type f -name \*.rrd | egrep $HOST_REGEX | egrep $STAT_REGEX))
echo "[{\"avg\": 0, \"bglastdrawn\": 0, \"canvas\": \"ffffff\", \"description\": \"\", \"end\": \"$GRAPH_END\", \"graphlabel\": \"$GRAPH_LABEL\", \"max\": \"nan\", \"min\": \"nan\", \"mo\": 0, \"nextcolor\": 22, \"ollastdrawn\": 0,\"paths\": [" > $TEMPFILE
for i in ${STATLIST[@]}; do
GCOLOR=#$(dd if=/dev/urandom count=2 2>/dev/null | md5sum | cut -c1-6)
GNAME1=$(echo $i | awk -F/ '{print $5}' | cut -c4-10)
GNAME2=$(echo $i | awk -F/ '{print $NF}' | awk -F. '{print $1}')
GNAME="$GNAME1 $GNAME2"
echo "{\"path\": \"$i\", \"color\": \"$GCOLOR\", \"isPredict\": 0, \"name\": \"$GNAME\", \"drawtype\": \"$GRAPH_TYPE\", \"display\": 1, \"opacity\": \"ff\"}," >> $TEMPFILE
done
echo "],\"ollastdrawn\": 0,\"pathlimit\": \"\", \"size\": 100, \"start\": \"$GRAPH_START\", \"total\": 0, \"predicting\": 0, \"predictTotal\": 0, \"vertlabel\": \"\", \"xsize\": \"nan\", \"ysize\": \"nan\", \"xoff\": \"nan\", \"yoff\": \"nan\", \"ymax\": \"$GRAPH_YMAX\", \"ymin\": \"$GRAPH_YMIN\", \"events\": \"all\" }]" >> $TEMPFILE
tr '\n' ' ' < $TEMPFILE > $OUTPUTFILE
rm -f $TEMPFILE
chmod 644 $OUTPUTFILE
echo "Playlist generated, $OUTPUTFILE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment