Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
clean some things from zk, kafka, mongo
#!/bin/sh
# vars
## EDITOR/VISUAL - what process to use to pick targets interactively
## ZK_WL - regex for zookeeper paths not to remove
## KAFKA_WL - regex for kafka topics not to remove
## MONGO_WL - regex for mongo item ids not to remove
# set -x
editor=${VISUAL:-${EDITOR:-'nano'}}
zk_default_wl='clients|config|brokers|zookeeper|admin|isr_change_notification|controller-epoch'
zk_whitelist=${ZK_WL:-${ZK_WHITELIST:-${zk_default_wl}}}
kafka_whitelist=${KAFKA_WL:-${KAFKA_WHITELIST:-'kingfisher.*'}}
mongo_whitelist=${MONGO_WL:-${MONGO_WHITELIST:-'.^'}}
main () {
# clean kafka
kafka_topics=$(kafka_get_topics)
select_items selected ${kafka_whitelist} ${kafka_topics}
kafka_delete_topics ${selected}
# clean zookeeper
zk_topics=$(zk_get_topics|zk_clean_topics)
select_items selected ${zk_whitelist} ${zk_topics}
zk_delete_topics ${selected}
# clean mongo
mongo_tasks=$(mongo_get_tasks)
select_items selected ${mongo_whitelist} ${mongo_tasks}
mongo_delete_tasks ${selected}
}
################# zookeeper ######################
##################################################
zk () {
echo $* | zkCli -server 127.0.0.1
}
zk_get_topics () {
zk "ls /" | grep ']$'
}
zk_clean_topics () {
sed -e 's/^\[\(.*\)]$/\1/' -e 's/, /\\n/g'
}
zk_delete_topics () {
for i in $*; do zk rmr '/'${i}; done
}
##################################################
############### kafka ############################
##################################################
kafka () {
kafka-topics --zookeeper localhost:2181 $*
}
kafka_get_topics () {
kafka -list
}
kafka_delete_topics () {
echo deleting $* from kafka
for i in $*
do kafka --delete --topic $i
done
}
##################################################
############### mongo ############################
##################################################
_mongo () {
mongo littlebird --quiet --eval "${1}"
}
mongo_get_tasks () {
command='var tasks=db.tasks.find();
tasks.forEach(function(x){print(x._id);});'
_mongo "${command}"
}
mongo_delete_tasks () {
# echo deleting $* from mongo
for i in $*
do _mongo 'db.tasks.remove('${i}');'
done
}
##################################################
select_items () {
tmpfile=$(mktemp /tmp/cleanup.XXXX)
target_var=$1
shift
whitelist=$1
shift
# echo "whitelist is ${whitelist}"
per_line $(echo $@ | comment_items ${whitelist}) > $tmpfile
$editor $tmpfile
# tricky! - this sets the val for the symbol for arg 1
eval $target_var='$(grep -v ^# $tmpfile)'
rm $tmpfile
}
comment_items () {
sed -E 's/'${1}'/#&/'
}
per_line () {
for i in $(echo $* | sed -E "s/- marked for deletion//g")
do echo $i
done
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.