Skip to content

Instantly share code, notes, and snippets.

@alexott
Last active January 7, 2019 13:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexott/afd2d4764f7136b705ededcd7c65f40d to your computer and use it in GitHub Desktop.
Save alexott/afd2d4764f7136b705ededcd7c65f40d to your computer and use it in GitHub Desktop.
Scripts for generation DSE service discovery JSON files for Prometheus
from dse.cluster import Cluster
import sys
import json
if (len(sys.argv) < 3):
print("Usage: generate.py contact_point file_name")
exit(1)
cluster = Cluster([sys.argv[1]])
session = cluster.connect()
metadata = cluster.metadata
host_list = [host.broadcast_address for host in metadata.all_hosts()]
data = {'labels': {'cluster': metadata.cluster_name}, 'targets': host_list}
print('data: ' + json.dumps(data))
with open(sys.argv[2], 'w') as f:
json.dump(data, f)
#!/bin/bash
#
# File: generate.sh
#
# Created: Thursday, January 3 2019
#
if [ $# -lt 2 ]; then
echo "Usage: generate-dse-topology.sh filename port"
fi
FILE=$1
PORT=$2
TMPFILE=generate-$$.tmp
echo '[{"targets": [' > $TMPFILE
cnt=0
for node in `nodetool status|grep -e '^[UD][NLJM] '|sed -e 's|^[UD][NLJM] *\([^ ]*\) .*$|\1|'`; do
if [ $cnt -gt 0 ]; then
echo -n ", " >> $TMPFILE
fi
echo -n "\"$node:$PORT\"" >> $TMPFILE
cnt=$((1 + cnt))
done
CLUSTER_NAME=`nodetool describecluster|grep 'Name:'|sed -e 's|^.*Name: \(.*\)$|\1|'`
echo "" >> $TMPFILE
echo "],\"labels\": {\"cluster\": \"$CLUSTER_NAME\"}}]" >> $TMPFILE
mv $TMPFILE $FILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment