Skip to content

Instantly share code, notes, and snippets.

@chicagobuss
Last active March 18, 2016 18:35
Show Gist options
  • Save chicagobuss/292d1d91d6260ce40929 to your computer and use it in GitHub Desktop.
Save chicagobuss/292d1d91d6260ce40929 to your computer and use it in GitHub Desktop.
Simple script for deleting metrics from kairos matching a particular name
#!/usr/bin/env python
"""
Deletes metrics from KairosDB that match a certain keyword in the name in a slice between two timestamps.
Note that this script SUBTRACTS 1 from the given end timestamp to make the end time EXCLUSIVE.
Install requests dependency:
> pip install requests
"""
import argparse
import requests
import json
import sys
import time
def datapoints_query(metric_name, start_timestamp, end_timestamp):
end_timestamp = str(int(end_timestamp) - 1)
return {
"start_absolute": start_timestamp,
"end_absolute": end_timestamp,
"metrics": [{
"name": metric_name,
"aggregators": [{"name": "sum", "align_sampling": True, "sampling": {"value": "1", "unit": "milliseconds"}}]
}],
"cache_time": 0
}
def main(keyword, start_timestamp, end_timestamp, kairos_url='http://localhost:8079/api/v1', execute=False):
metric_names = requests.get(kairos_url + '/metricnames').json()['results']
for metric_name in metric_names:
if metric_name.startswith("30") and keyword in metric_name:
payload = datapoints_query(metric_name, start_timestamp, end_timestamp)
else:
continue
if execute:
print "deleting %s between %s and %s" % (metric_name, start_timestamp, end_timestamp)
requests.post(kairos_url + '/datapoints/delete', data=json.dumps(payload)).text
else:
print "would've deleted %s between %s and %s" % (metric_name, start_timestamp, end_timestamp)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="kairos_delete.py metricsprefix 1451628000000 1454306400000 kairos-host-1")
parser.add_argument("host", type=str, help="kairos host name")
parser.add_argument("keyword", type=str, help="kairos matching keyword, i.e metricsprefix")
parser.add_argument("start_time", type=str, help="start epoch timestamps")
parser.add_argument("end_time", type=str, help="end epoch timestamps")
parser.add_argument("--execute", action='store_true', help="actually delete")
args = parser.parse_args()
main(keyword=args.keyword, start_timestamp=args.start_time, end_timestamp=args.end_time, kairos_url="http://"+args.host+":8079/api/v1", execute=args.execute)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment