Skip to content

Instantly share code, notes, and snippets.

@toni-moreno
Last active April 25, 2017 23:57
Show Gist options
  • Save toni-moreno/9807776 to your computer and use it in GitHub Desktop.
Save toni-moreno/9807776 to your computer and use it in GitHub Desktop.
A shell script to help export/import json formatted graph and dashboards from a Graphite mysql database.
#!/bin/bash
# (c) Toni Moreno
# tool to import / export graphs and dashboards to mysql db
# NOTE: be sure you have granted FILE privileges
# if apparmor is configured you should add to
# /etc/apparmor.d/usr.sbin.mysqld
# /tmp/* rw,
LOCAL_SETINGS="/opt/graphite/webapp/graphite/local_settings.py"
MYSQL_CONNECT_OPTIONS=""
function get_db_data() {
grep -Ev "^ *#|^ *$" $LOCAL_SETINGS | sed '/^DATABASES/,/^}/!d' |grep $1 | tr -d "', " | awk -F':' '{ print $2}'
}
DBNAME=""
DBUSER=""
DBPASS=""
DBHOST=""
function get_db_credentials() {
#set -vx
DBNAME=`get_db_data "NAME"`
DBUSER=`get_db_data "USER"`
DBPASS=`get_db_data "PASSWORD"`
DBHOST=`get_db_data "HOST"`
[ -S "$DBHOST" ] && {
MYSQL_CONNECT_OPTIONS="$MYSQL_CONNECT_OPTIONS -S $DBHOST"
echo "CONNECTING TO BDNAME: $DBNAME with Options[ $MYSQL_CONNECT_OPTIONS ] with user: $DBUSER and pass: $DBPASS "
} || {
echo "CONNECTING TO BDNAME: $DBNAME on host $DBHOST with user: $DBUSER and pass: $DBPASS "
}
#set +vx
}
function mysql_query() {
#set -vx
QUERY="$1"
echo "$QUERY" | mysql $MYSQL_CONNECT_OPTIONS --user="$DBUSER" --password="$DBPASS" "$DBNAME"
#set +vx
}
function usage() {
cat <<EOF
usage
#`basename $0` [type] [action] [action_parameters]
suported types:
* graph
* dashboard
supported acctions:
* list [ no action parameters needed ]
* get [ id ]
save the json data on a json file named as : <type>-<id>.json file in the current directory
* insert [ filename id ]
inserts into the db the data contained in the json formatted file "filename" identified as "id"
NOTE: no syntax validation is done.
if no id specified a list of existing id's will be listed
NOTE:
only mysql suported
only Django > 1.1 DATABASE configuration syntax supported
only insert / updates if FILE have been granted to the graphite user.
mysql>grant file on *.* to 'graphite_user'@'localhost';
EOF
}
[ "$#" -lt 2 ] && {
usage
exit
}
get_db_credentials
TYPE=$1
ACTION=$2
case $TYPE in
"graph" )
case $ACTION in
"list")
mysql_query "select name from account_mygraph"
;;
"get")
TYPEID=$3
TMP_OUTFILE="/tmp/${TYPE}-${TYPEID}.json"
mysql_query "select url from account_mygraph where name=\"${TYPEID}\" into outfile \"$TMP_OUTFILE\" "
[ -f "$TMP_OUTFILE" ] && {
mv "$TMP_OUTFILE" .
} || {
echo "ERROR : no $TMP_OUTFILE generated"
}
;;
"insert")
echo "WARNING: insert not supported yet in graph mode"
exit 1
;;
"update")
echo "WARNING: update not supported yet in graph mode"
exit 1
;;
*)
usage
exit 1
;;
esac
;;
"dashboard" )
case $ACTION in
"list")
mysql_query "select name from dashboard_dashboard"
;;
"get")
TYPEID=$3
TMP_OUTFILE="/tmp/${TYPE}-${TYPEID}.json"
mysql_query "select state from dashboard_dashboard where name=\"${TYPEID}\" into outfile \"$TMP_OUTFILE\" "
[ -f "$TMP_OUTFILE" ] && {
mv "$TMP_OUTFILE" .
} || {
echo "ERROR : no $TMP_OUTFILE generated"
}
;;
"insert")
TYPEID=$4
FILENAME=$3
TMP_DIR="/tmp"
cp -f $FILENAME $TMP_DIR/
FNAME=`basename $FILENAME`
[ -f $FILENAME -a -n $TYPEID ] && {
mysql_query "insert into dashboard_dashboard (name,state) values (\"$TYPEID\",LOAD_FILE(\"$TMP_DIR/$FNAME\"))"
rm -f $TMP_DIR/$FNAME
} || {
echo "ERROR: no filename $FILENAME found or not ID specified "
}
;;
"update")
TYPEID=$4
FILENAME=$3
TMP_DIR="/tmp"
cp -f $FILENAME $TMP_DIR/
FNAME=`basename $FILENAME`
[ -f $FILENAME -a -n $TYPEID ] && {
mysql_query "update dashboard_dashboard set state=LOAD_FILE(\"$TMP_DIR/$FNAME\") where name=\"$TYPEID\""
rm -f $TMP_DIR/$FNAME
} || {
echo "ERROR: no filename $FILENAME found or not ID specified "
}
;;
*)
usage
exit 1
;;
esac
;;
* )
usage
exit 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment