public
Created — forked from seanosulliv/graphbuilder.sh

  • Download Gist
graphbuilder.sh
Shell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
#!/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"

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.