Merge "[doctor] remove needless `'`"
[releng.git] / utils / test / scripts / kibana_cleanup.py
1 #! /usr/bin/env python
2 import logging
3 import urlparse
4
5 import argparse
6
7 import shared_utils
8
9 logger = logging.getLogger('clear_kibana')
10 logger.setLevel(logging.DEBUG)
11 file_handler = logging.FileHandler('/var/log/{}.log'.format('clear_kibana'))
12 file_handler.setFormatter(logging.Formatter('%(asctime)s %(levelname)s: %(message)s'))
13 logger.addHandler(file_handler)
14
15
16 def delete_all(url, es_creds):
17     ids = shared_utils.get_elastic_docs(url, es_creds, body=None, field='_id')
18     for id in ids:
19         del_url = '/'.join([url, id])
20         shared_utils.delete_request(del_url, es_creds)
21
22
23 if __name__ == '__main__':
24     parser = argparse.ArgumentParser(description='Delete saved kibana searches, visualizations and dashboards')
25     parser.add_argument('-e', '--elasticsearch-url', default='http://localhost:9200',
26                         help='the url of elasticsearch, defaults to http://localhost:9200')
27
28     parser.add_argument('-u', '--elasticsearch-username', default=None,
29                         help='The username with password for elasticsearch in format username:password')
30
31     args = parser.parse_args()
32     base_elastic_url = args.elasticsearch_url
33     es_creds = args.elasticsearch_username
34
35     urls = (urlparse.urljoin(base_elastic_url, '/.kibana/visualization'),
36             urlparse.urljoin(base_elastic_url, '/.kibana/dashboard'),
37             urlparse.urljoin(base_elastic_url, '/.kibana/search'))
38
39     for url in urls:
40         delete_all(url, es_creds)
41