Skip to content

Instantly share code, notes, and snippets.

@zuazo
Forked from rcrowley/whisper-clean.py
Last active October 10, 2018 00:48
Show Gist options
  • Save zuazo/5200981 to your computer and use it in GitHub Desktop.
Save zuazo/5200981 to your computer and use it in GitHub Desktop.
import os
import os.path
import sys
from carbon.hashing import ConsistentHashRing
from carbon.conf import settings
## Settings
# Absolute path to the Graphite Data Directory
DATA_DIR = '/data/graphite/whisper'
## You need not modify anything below this
instances = []
unwelcome_instances = []
for arg in sys.argv[1:]:
unwelcome = False
if arg.startswith('-'):
arg = arg[1:]
unwelcome = True
instance = tuple(arg.split(':', 2))
instances.append(instance)
if unwelcome:
unwelcome_instances.append(instance)
if 0 == len(instances):
print('Usage: python whisper-clean.py [-]<address>:<instance>[...]')
sys.exit(1)
ring = ConsistentHashRing(instances)
for dirname, dirnames, filenames in os.walk(DATA_DIR):
if dirname.startswith(os.path.join(DATA_DIR, settings.CARBON_METRIC_PREFIX)):
continue
for filename in filenames:
pathname = os.path.join(dirname, filename)
basename, ext = os.path.splitext(filename)
if '.wsp' != ext:
print('skipping %s' % os.path.relpath(pathname, DATA_DIR)))
if ring.get_node(os.path.relpath(os.path.join(dirname, basename), DATA_DIR).
replace('/', '.')) in unwelcome_instances:
print('unlinking %s' % pathname)
os.unlink(pathname)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment